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
25
@@ -216,7 +213,7 @@ private void captureCiMetadata() {
216
213
customValueSearchLinker .addCustomValueAndSearchLink ("CI stage" , value ));
217
214
}
218
215
219
- if (isAzurePipelines ()) {
216
+ if (isAzurePipelines ()) {
220
217
Optional <String > azureServerUrl = envVariable ("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI" );
221
218
Optional <String > azureProject = envVariable ("SYSTEM_TEAMPROJECT" );
222
219
Optional <String > buildId = envVariable ("BUILD_BUILDID" );
@@ -302,44 +299,35 @@ private CaptureGitMetadataAction(ProviderFactory providers, CustomValueSearchLin
302
299
303
300
@ Override
304
301
public void execute (BuildScanExtension buildScan ) {
305
- if (!isGitInstalled ()) {
306
- return ;
307
- }
308
-
309
- String gitRepo = execAndGetStdOut ("git" , "config" , "--get" , "remote.origin.url" );
310
- String gitCommitId = execAndGetStdOut ("git" , "rev-parse" , "--verify" , "HEAD" );
311
- String gitCommitShortId = execAndGetStdOut ("git" , "rev-parse" , "--short=8" , "--verify" , "HEAD" );
312
- String gitBranchName = getGitBranchName (() -> execAndGetStdOut ("git" , "rev-parse" , "--abbrev-ref" , "HEAD" ));
313
- String gitStatus = execAndGetStdOut ("git" , "status" , "--porcelain" );
314
-
315
- if (isNotEmpty (gitRepo )) {
316
- buildScan .value ("Git repository" , redactUserInfo (gitRepo ));
317
- }
318
- if (isNotEmpty (gitCommitId )) {
319
- buildScan .value ("Git commit id" , gitCommitId );
320
- }
321
- if (isNotEmpty (gitCommitShortId )) {
322
- customValueSearchLinker .addCustomValueAndSearchLink ("Git commit id" , "Git commit id short" , gitCommitShortId );
323
- }
324
- if (isNotEmpty (gitBranchName )) {
325
- buildScan .tag (gitBranchName );
326
- buildScan .value ("Git branch" , gitBranchName );
327
- }
328
- if (isNotEmpty (gitStatus )) {
302
+ GitMetadataResolver gitMetadataResolver = new GitMetadataResolver (isGitInstalled ());
303
+ Optional <String > gitRepo = gitMetadataResolver .resolve (() -> Utils .execAndGetStdOut ("git" , "config" , "--get" , "remote.origin.url" ));
304
+ Optional <String > gitCommitId = gitMetadataResolver .resolve (() -> Utils .execAndGetStdOut ("git" , "rev-parse" , "--verify" , "HEAD" ));
305
+ Optional <String > gitCommitShortId = gitMetadataResolver .resolve (() -> Utils .execAndGetStdOut ("git" , "rev-parse" , "--short=8" , "--verify" , "HEAD" ));
306
+ Optional <String > gitStatus = gitMetadataResolver .resolve (() -> Utils .execAndGetStdOut ("git" , "status" , "--porcelain" ));
307
+ Optional <String > gitBranchName = gitMetadataResolver .resolve (this ::getGitBranchNameFromEnv , () -> Utils .execAndGetStdOut ("git" , "rev-parse" , "--abbrev-ref" , "HEAD" ));
308
+
309
+ gitRepo .ifPresent (s -> buildScan .value ("Git repository" , redactUserInfo (s )));
310
+ gitCommitId .ifPresent (s -> buildScan .value ("Git commit id" , s ));
311
+ gitCommitShortId .ifPresent (s -> customValueSearchLinker .addCustomValueAndSearchLink ("Git commit id" , "Git commit id short" , s ));
312
+ gitBranchName .ifPresent (s -> {
313
+ buildScan .tag (s );
314
+ buildScan .value ("Git branch" , s );
315
+ });
316
+ gitStatus .ifPresent (s -> {
329
317
buildScan .tag ("Dirty" );
330
- buildScan .value ("Git status" , gitStatus );
331
- }
318
+ buildScan .value ("Git status" , s );
319
+ });
332
320
333
- if (isNotEmpty ( gitRepo ) && isNotEmpty ( gitCommitId )) {
334
- if (gitRepo .contains ("github.com/" ) || gitRepo .contains ("github.com:" )) {
335
- Matcher matcher = Pattern .compile ("(.*)github\\ .com[/|:](.*)" ).matcher (gitRepo );
321
+ if (gitRepo . isPresent ( ) && gitCommitId . isPresent ( )) {
322
+ if (gitRepo .get (). contains ("github.com/" ) || gitRepo . get () .contains ("github.com:" )) {
323
+ Matcher matcher = Pattern .compile ("(.*)github\\ .com[/|:](.*)" ).matcher (gitRepo . get () );
336
324
if (matcher .matches ()) {
337
325
String rawRepoPath = matcher .group (2 );
338
326
String repoPath = rawRepoPath .endsWith (".git" ) ? rawRepoPath .substring (0 , rawRepoPath .length () - 4 ) : rawRepoPath ;
339
327
buildScan .link ("Github source" , "https://github.com/" + repoPath + "/tree/" + gitCommitId );
340
328
}
341
- } else if (gitRepo .contains ("gitlab.com/" ) || gitRepo .contains ("gitlab.com:" )) {
342
- Matcher matcher = Pattern .compile ("(.*)gitlab\\ .com[/|:](.*)" ).matcher (gitRepo );
329
+ } else if (gitRepo .get (). contains ("gitlab.com/" ) || gitRepo . get () .contains ("gitlab.com:" )) {
330
+ Matcher matcher = Pattern .compile ("(.*)gitlab\\ .com[/|:](.*)" ).matcher (gitRepo . get () );
343
331
if (matcher .matches ()) {
344
332
String rawRepoPath = matcher .group (2 );
345
333
String repoPath = rawRepoPath .endsWith (".git" ) ? rawRepoPath .substring (0 , rawRepoPath .length () - 4 ) : rawRepoPath ;
@@ -353,24 +341,15 @@ private boolean isGitInstalled() {
353
341
return execAndCheckSuccess ("git" , "--version" );
354
342
}
355
343
356
- private String getGitBranchName ( Supplier <String > gitCommand ) {
344
+ private Optional <String > getGitBranchNameFromEnv ( ) {
357
345
if (isJenkins () || isHudson ()) {
358
- Optional <String > branch = Utils .envVariable ("BRANCH_NAME" , providers );
359
- if (branch .isPresent ()) {
360
- return branch .get ();
361
- }
346
+ return Utils .envVariable ("BRANCH_NAME" , providers );
362
347
} else if (isGitLab ()) {
363
- Optional <String > branch = Utils .envVariable ("CI_COMMIT_REF_NAME" , providers );
364
- if (branch .isPresent ()) {
365
- return branch .get ();
366
- }
348
+ return Utils .envVariable ("CI_COMMIT_REF_NAME" , providers );
367
349
} else if (isAzurePipelines ()) {
368
- Optional <String > branch = Utils .envVariable ("BUILD_SOURCEBRANCH" , providers );
369
- if (branch .isPresent ()) {
370
- return branch .get ();
371
- }
350
+ return Utils .envVariable ("BUILD_SOURCEBRANCH" , providers );
372
351
}
373
- return gitCommand . get ();
352
+ return Optional . empty ();
374
353
}
375
354
376
355
private boolean isJenkins () {
0 commit comments