1
1
package com .saucelabs .saucebindings ;
2
2
3
- import com .google .common .collect .ImmutableList ;
4
3
import com .google .common .collect .ImmutableMap ;
4
+ import com .saucelabs .saucebindings .citools .Bamboo ;
5
+ import com .saucelabs .saucebindings .citools .CITool ;
6
+ import com .saucelabs .saucebindings .citools .CircleCI ;
7
+ import com .saucelabs .saucebindings .citools .DefaultTool ;
8
+ import com .saucelabs .saucebindings .citools .GitHub ;
9
+ import com .saucelabs .saucebindings .citools .GitLab ;
10
+ import com .saucelabs .saucebindings .citools .Jenkins ;
11
+ import com .saucelabs .saucebindings .citools .TeamCity ;
12
+ import com .saucelabs .saucebindings .citools .TravisCI ;
5
13
import java .util .Map ;
14
+ import java .util .function .Supplier ;
6
15
7
16
/**
8
17
* Determines the information a test needs based on what CI Tool is being used. It does not need to
9
18
* be initialized.
10
19
*/
11
20
public abstract class CITools {
12
- private static String ciToolName ;
21
+ private static CITool ciTool ;
13
22
14
23
/** Map of CI Tools and what Environment variable, if present, indicates its usage. */
15
- public static final Map <String , String > KNOWN_TOOLS =
24
+ public static final Map <String , Supplier < CITool > > KNOWN_TOOLS =
16
25
ImmutableMap .of (
17
- "Bamboo" , "bamboo_agentId" ,
18
- "Circle" , "CIRCLE_JOB" ,
19
- "Github" , "GITHUB_SHA" ,
20
- "GitLab" , "CI" ,
21
- "Jenkins" , "JENKINS_HOME" ,
22
- "TeamCity" , "TEAMCITY_PROJECT_NAME" ,
23
- "Travis" , "TRAVIS_JOB_ID" );
24
-
25
- /** The Build Name and Build Number values to properly differentiate test runs. */
26
- public static final Map <String , ImmutableList <String >> BUILD_VALUES =
27
- ImmutableMap .of (
28
- "Bamboo" , ImmutableList .of ("bamboo_shortJobName" , "bamboo_buildNumber" ),
29
- "Circle" , ImmutableList .of ("CIRCLE_JOB" , "CIRCLE_BUILD_NUM" ),
30
- "Github" , ImmutableList .of ("GITHUB_JOB" , "GITHUB_RUN_NUMBER" ),
31
- "GitLab" , ImmutableList .of ("CI_JOB_NAME" , "CI_JOB_ID" ),
32
- "Jenkins" , ImmutableList .of ("BUILD_NAME" , "BUILD_NUMBER" ),
33
- "TeamCity" , ImmutableList .of ("TEAMCITY_PROJECT_NAME" , "BUILD_NUMBER" ),
34
- "Travis" , ImmutableList .of ("TRAVIS_JOB_NAME" , "TRAVIS_JOB_NUMBER" ));
26
+ "bamboo_agentId" , Bamboo ::new ,
27
+ "CIRCLE_JOB" , CircleCI ::new ,
28
+ "GITHUB_SHA" , GitHub ::new ,
29
+ "CI" , GitLab ::new ,
30
+ "JENKINS_HOME" , Jenkins ::new ,
31
+ "TEAMCITY_PROJECT_NAME" , TeamCity ::new ,
32
+ "TRAVIS_JOB_ID" , TravisCI ::new );
35
33
36
34
public static String getCiToolName () {
37
- if (ciToolName == null ) {
38
- for (Map .Entry <String , String > tool : KNOWN_TOOLS .entrySet ()) {
39
- if (SystemManager .get (tool .getValue ()) != null ) {
40
- ciToolName = tool .getKey ();
41
- return ciToolName ;
42
- }
43
- }
44
- }
45
- return ciToolName == null ? "unknown" : ciToolName ;
35
+ return getCiTool ().getClientPlatform ();
46
36
}
47
37
48
38
/**
@@ -51,11 +41,7 @@ public static String getCiToolName() {
51
41
* @return the constant name of the build
52
42
*/
53
43
public static String getBuildName () {
54
- if (!getCiToolName ().equalsIgnoreCase ("unknown" )) {
55
- String buildNameKey = BUILD_VALUES .get (getCiToolName ()).get (0 );
56
- return SystemManager .get (buildNameKey );
57
- }
58
- return "Default Build Name" ;
44
+ return getCiTool ().getBuildName ();
59
45
}
60
46
61
47
/**
@@ -64,10 +50,24 @@ public static String getBuildName() {
64
50
* @return the variable number of the current build
65
51
*/
66
52
public static String getBuildNumber () {
67
- if (!getCiToolName ().equalsIgnoreCase ("unknown" )) {
68
- String buildNumberKey = BUILD_VALUES .get (getCiToolName ()).get (1 );
69
- return SystemManager .get (buildNumberKey );
53
+ return getCiTool ().getBuildNumber ();
54
+ }
55
+
56
+ private static CITool getCiTool () {
57
+ if (ciTool != null ) {
58
+ return ciTool ;
59
+ } else if (SystemManager .get ("SAUCE_CLIENT_PLATFORM" ) != null ) {
60
+ // Override the CI Tool lookup when this environment variable is set.
61
+ return new DefaultTool ();
62
+ } else {
63
+ for (Map .Entry <String , Supplier <CITool >> tool : KNOWN_TOOLS .entrySet ()) {
64
+ if (System .getenv (tool .getKey ()) != null ) {
65
+ ciTool = tool .getValue ().get ();
66
+ return ciTool ;
67
+ }
68
+ }
69
+ ciTool = new DefaultTool ();
70
+ return ciTool ;
70
71
}
71
- return String .valueOf (System .currentTimeMillis ());
72
72
}
73
73
}
0 commit comments