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