9
9
import java .io .InputStreamReader ;
10
10
import java .util .Arrays ;
11
11
import java .util .List ;
12
+ import java .util .ArrayList ;
12
13
import java .util .Map ;
13
14
import io .github .lambdatest .utils .LoggerUtil ;
14
15
import java .util .logging .Logger ;
@@ -20,15 +21,11 @@ public class GitUtils {
20
21
private static Logger log = LoggerUtil .createLogger ("lambdatest-java-sdk" );
21
22
22
23
public static GitInfo getGitInfo (Map <String , String > envVars ) {
23
-
24
24
String gitInfoFilePath = envVars .get ("SMARTUI_GIT_INFO_FILEPATH" );
25
- // If Git info file exists, read from it
26
25
if (gitInfoFilePath != null ) {
27
26
return readGitInfoFromFile (gitInfoFilePath , envVars );
28
- }
29
- // Otherwise, fetch Git info from Git commands
30
- else {
31
- return fetchGitInfoFromCommands (envVars );
27
+ } else {
28
+ return fetchGitInfoFromCommands (envVars );
32
29
}
33
30
}
34
31
@@ -44,22 +41,20 @@ private static GitInfo readGitInfoFromFile(String filePath, Map<String, String>
44
41
(String ) gitInfo .get ("commit_body" ),
45
42
(String ) gitInfo .get ("commit_author" ),
46
43
getGitHubURL (envVars , (String ) gitInfo .get ("commit_id" )),
47
- envVars .getOrDefault ("BASELINE_BRANCH" , "" )
48
- );
44
+ envVars .getOrDefault ("BASELINE_BRANCH" , "" ));
49
45
} catch (IOException e ) {
50
- log .info ("Error reading Git info file: " + e .getMessage ());
46
+ log .info ("Error reading Git info file: " + e .getMessage ());
51
47
return null ;
52
48
}
53
49
}
54
50
55
51
private static GitInfo fetchGitInfoFromCommands (Map <String , String > envVars ) {
56
52
String splitCharacter = "<##>" ;
57
- String [] prettyFormat = {"%h" , "%H" , "%s" , "%f" , "%b" , "%at" , "%ct" , "%an" , "%ae" , "%cn" , "%ce" , "%N" , "" };
53
+ String [] prettyFormat = { "%h" , "%H" , "%s" , "%f" , "%b" , "%at" , "%ct" , "%an" , "%ae" , "%cn" , "%ce" , "%N" , "" };
58
54
59
55
String command = String .format (
60
56
"git log -1 --pretty=format:\" %s\" && git rev-parse --abbrev-ref HEAD && git tag --contains HEAD" ,
61
- String .join (splitCharacter , prettyFormat )
62
- );
57
+ String .join (splitCharacter , prettyFormat ));
63
58
64
59
List <String > outputLines = executeCommand (command );
65
60
@@ -74,15 +69,13 @@ private static GitInfo fetchGitInfoFromCommands(Map<String, String> envVars) {
74
69
List <String > branchAndTagsList = Arrays .asList (branchAndTags );
75
70
String branch = envVars .getOrDefault ("CURRENT_BRANCH" , branchAndTagsList .get (0 ));
76
71
77
-
78
72
return new GitInfo (
79
73
branch ,
80
74
res [0 ], // commitId
81
75
res [2 ], // commitMessage
82
76
res [7 ], // commitAuthor
83
77
getGitHubURL (envVars , res [1 ]), // githubURL
84
- envVars .getOrDefault ("BASELINE_BRANCH" , "" )
85
- );
78
+ envVars .getOrDefault ("BASELINE_BRANCH" , "" ));
86
79
}
87
80
88
81
private static String shortenCommitId (String commitId ) {
@@ -99,12 +92,13 @@ private static String getGitHubURL(Map<String, String> envVars, String commitId)
99
92
100
93
private static List <String > executeCommand (String command ) {
101
94
try {
102
- Process process = Runtime .getRuntime ().exec (new String []{ "/bin/sh" , "-c" , command });
95
+ Process process = Runtime .getRuntime ().exec (new String [] { "/bin/sh" , "-c" , command });
103
96
return new BufferedReader (new InputStreamReader (process .getInputStream ()))
104
97
.lines ()
105
98
.collect (Collectors .toList ());
106
99
} catch (IOException e ) {
107
- throw new RuntimeException ("Error executing Git command: " + e .getMessage (), e );
100
+ log .severe ("Error executing command: " + e .getMessage ());
101
+ return new ArrayList <String >();
108
102
}
109
103
}
110
104
}
0 commit comments