14
14
import java .util .Map ;
15
15
import java .util .Optional ;
16
16
import java .util .Properties ;
17
- import java .util .function .Supplier ;
18
17
import java .util .regex .Matcher ;
19
18
import java .util .regex .Pattern ;
20
19
import java .util .stream .Stream ;
21
20
21
+ import static com .gradle .GitMetadata .given ;
22
22
import static com .gradle .Utils .appendIfMissing ;
23
23
import static com .gradle .Utils .execAndCheckSuccess ;
24
- import static com .gradle .Utils .execAndGetStdOut ;
25
- import static com .gradle .Utils .isNotEmpty ;
26
24
import static com .gradle .Utils .redactUserInfo ;
27
25
import static com .gradle .Utils .urlEncode ;
28
-
29
26
/**
30
27
* Adds a standard set of useful tags, links and custom values to all build scans published.
31
28
*/
@@ -281,44 +278,35 @@ private CaptureGitMetadataAction(ProviderFactory providers, CustomValueSearchLin
281
278
282
279
@ Override
283
280
public void execute (BuildScanExtension buildScan ) {
284
- if (!isGitInstalled ()) {
285
- return ;
286
- }
287
-
288
- String gitRepo = execAndGetStdOut ("git" , "config" , "--get" , "remote.origin.url" );
289
- String gitCommitId = execAndGetStdOut ("git" , "rev-parse" , "--verify" , "HEAD" );
290
- String gitCommitShortId = execAndGetStdOut ("git" , "rev-parse" , "--short=8" , "--verify" , "HEAD" );
291
- String gitBranchName = getGitBranchName (() -> execAndGetStdOut ("git" , "rev-parse" , "--abbrev-ref" , "HEAD" ));
292
- String gitStatus = execAndGetStdOut ("git" , "status" , "--porcelain" );
293
-
294
- if (isNotEmpty (gitRepo )) {
295
- buildScan .value ("Git repository" , redactUserInfo (gitRepo ));
296
- }
297
- if (isNotEmpty (gitCommitId )) {
298
- buildScan .value ("Git commit id" , gitCommitId );
299
- }
300
- if (isNotEmpty (gitCommitShortId )) {
301
- customValueSearchLinker .addCustomValueAndSearchLink ("Git commit id" , "Git commit id short" , gitCommitShortId );
302
- }
303
- if (isNotEmpty (gitBranchName )) {
304
- buildScan .tag (gitBranchName );
305
- buildScan .value ("Git branch" , gitBranchName );
306
- }
307
- if (isNotEmpty (gitStatus )) {
281
+ boolean isGitInstalled = isGitInstalled ();
282
+ Optional <String > gitRepo = given (isGitInstalled ).fromCmd ("git" , "config" , "--get" , "remote.origin.url" ).resolve ();
283
+ Optional <String > gitCommitId = given (isGitInstalled ).fromCmd ("git" , "rev-parse" , "--verify" , "HEAD" ).resolve ();
284
+ Optional <String > gitCommitShortId = given (isGitInstalled ).fromCmd ("git" , "rev-parse" , "--short=8" , "--verify" , "HEAD" ).resolve ();
285
+ Optional <String > gitBranchName = given (isGitInstalled ).fromCmd ("git" , "rev-parse" , "--abbrev-ref" , "HEAD" ).fromEnv (this ::getGitBranchNameFromEnv ).resolve ();
286
+ Optional <String > gitStatus = given (isGitInstalled ).fromCmd ( "git" , "status" , "--porcelain" ).resolve ();
287
+
288
+ gitRepo .ifPresent (s -> buildScan .value ("Git repository" , redactUserInfo (s )));
289
+ gitCommitId .ifPresent (s -> buildScan .value ("Git commit id" , s ));
290
+ gitCommitShortId .ifPresent (s -> customValueSearchLinker .addCustomValueAndSearchLink ("Git commit id" , "Git commit id short" , s ));
291
+ gitBranchName .ifPresent (s -> {
292
+ buildScan .tag (s );
293
+ buildScan .value ("Git branch" , s );
294
+ });
295
+ gitStatus .ifPresent (s -> {
308
296
buildScan .tag ("Dirty" );
309
- buildScan .value ("Git status" , gitStatus );
310
- }
297
+ buildScan .value ("Git status" , s );
298
+ });
311
299
312
- if (isNotEmpty ( gitRepo ) && isNotEmpty ( gitCommitId )) {
313
- if (gitRepo .contains ("github.com/" ) || gitRepo .contains ("github.com:" )) {
314
- Matcher matcher = Pattern .compile ("(.*)github\\ .com[/|:](.*)" ).matcher (gitRepo );
300
+ if (gitRepo . isPresent ( ) && gitCommitId . isPresent ( )) {
301
+ if (gitRepo .get (). contains ("github.com/" ) || gitRepo . get () .contains ("github.com:" )) {
302
+ Matcher matcher = Pattern .compile ("(.*)github\\ .com[/|:](.*)" ).matcher (gitRepo . get () );
315
303
if (matcher .matches ()) {
316
304
String rawRepoPath = matcher .group (2 );
317
305
String repoPath = rawRepoPath .endsWith (".git" ) ? rawRepoPath .substring (0 , rawRepoPath .length () - 4 ) : rawRepoPath ;
318
306
buildScan .link ("Github source" , "https://github.com/" + repoPath + "/tree/" + gitCommitId );
319
307
}
320
- } else if (gitRepo .contains ("gitlab.com/" ) || gitRepo .contains ("gitlab.com:" )) {
321
- Matcher matcher = Pattern .compile ("(.*)gitlab\\ .com[/|:](.*)" ).matcher (gitRepo );
308
+ } else if (gitRepo .get (). contains ("gitlab.com/" ) || gitRepo . get () .contains ("gitlab.com:" )) {
309
+ Matcher matcher = Pattern .compile ("(.*)gitlab\\ .com[/|:](.*)" ).matcher (gitRepo . get () );
322
310
if (matcher .matches ()) {
323
311
String rawRepoPath = matcher .group (2 );
324
312
String repoPath = rawRepoPath .endsWith (".git" ) ? rawRepoPath .substring (0 , rawRepoPath .length () - 4 ) : rawRepoPath ;
@@ -332,14 +320,11 @@ private boolean isGitInstalled() {
332
320
return execAndCheckSuccess ("git" , "--version" );
333
321
}
334
322
335
- private String getGitBranchName ( Supplier <String > gitCommand ) {
323
+ private Optional <String > getGitBranchNameFromEnv ( ) {
336
324
if (isJenkins () || isHudson ()) {
337
- Optional <String > branch = Utils .envVariable ("BRANCH_NAME" , providers );
338
- if (branch .isPresent ()) {
339
- return branch .get ();
340
- }
325
+ return Utils .envVariable ("BRANCH_NAME" , providers );
341
326
}
342
- return gitCommand . get ();
327
+ return Optional . empty ();
343
328
}
344
329
345
330
private boolean isJenkins () {
0 commit comments