Skip to content

Commit d75fd24

Browse files
committed
add logging
1 parent 5ee1542 commit d75fd24

File tree

7 files changed

+140
-112
lines changed

7 files changed

+140
-112
lines changed

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = 'io.github.lambdatest'
9-
version = '1.0.12-beta.1'
9+
version = '1.0.12-beta.2'
1010
description = 'lambdatest-java-sdk'
1111

1212
repositories {
@@ -83,7 +83,7 @@ afterEvaluate {
8383
mavenJava(MavenPublication) {
8484
groupId = 'io.github.lambdatest'
8585
artifactId = 'lambdatest-java-sdk'
86-
version = '1.0.12-beta.1'
86+
version = '1.0.12-beta.2'
8787

8888
pom {
8989
name.set('LambdaTest Java SDK')

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>io.github.lambdatest</groupId>
88
<artifactId>lambdatest-java-sdk</artifactId>
9-
<version>1.0.12-beta.1</version>
9+
<version>1.0.12-beta.2</version>
1010
<name>lambdatest-java-sdk</name>
1111
<description>LambdaTest SDK in Java</description>
1212
<url>https://www.lambdatest.com</url>

src/main/java/io/github/lambdatest/SmartUIAppSnapshot.java

+7-23
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,22 @@ public void start(Map<String, String> options) throws Exception{
4040
log.severe(Constants.Errors.PROJECT_TOKEN_UNSET);
4141
throw new Exception("Project token is a mandatory field", e);
4242
}
43-
Map<String, String> envVars = new HashMap<>(System.getenv());
44-
GitInfo git = GitUtils.getGitInfo(envVars);
45-
// Authenticate user and create a build
43+
4644
try {
45+
Map<String, String> envVars = new HashMap<>(System.getenv());
46+
GitInfo git = GitUtils.getGitInfo(envVars);
4747
BuildResponse buildRes = util.build(git, this.projectToken, options);
4848
this.buildData = buildRes.getData();
4949
log.info("Build ID set : " + this.buildData.getBuildId() + "for Build name : "+ this.buildData.getName());
5050
options.put("buildName", this.buildData.getName());
5151
} catch(Exception e) {
52-
log.severe("Couldn't create build: " + e.getMessage());
53-
throw new Exception("Couldn't create build: " + e.getMessage());
52+
log.severe("Couldn't create smartui build: " + e.getMessage());
53+
throw new Exception("Couldn't create smartui build: " + e.getMessage());
5454
}
5555
}
56+
5657
public void start() throws Exception{
57-
String envToken = System.getenv("PROJECT_TOKEN");
58-
this.projectToken = envToken;
59-
log.info("Project token set as: " + this.projectToken);
60-
if(Objects.isNull(this.projectToken)){
61-
log.severe(Constants.Errors.PROJECT_TOKEN_UNSET);
62-
throw new Exception("Project token is a mandatory field");
63-
}
64-
Map<String, String> envVars = System.getenv();
65-
GitInfo git = GitUtils.getGitInfo(envVars);
66-
// Authenticate user and create a build
67-
try {
68-
BuildResponse buildRes = util.build(git, this.projectToken, null);
69-
this.buildData = buildRes.getData();
70-
log.info("Build ID set : " + this.buildData.getBuildId() + "for Build name : "+ this.buildData.getName());
71-
} catch(Exception e) {
72-
log.severe("Couldn't create build: " + e.getMessage());
73-
throw new Exception("Couldn't create build due to: ", e);
74-
}
58+
this.start(new HashMap<>());
7559
}
7660

7761
private String getProjectToken(Map<String, String> options) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
package io.github.lambdatest.models;
3+
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
public class ProjectTokenResponse {
8+
@JsonProperty("status")
9+
private String status;
10+
11+
@JsonProperty("message")
12+
private String message;
13+
14+
@JsonProperty("projectToken")
15+
private String projectToken;
16+
17+
// Getters and setters
18+
public String getStatus() { return status; }
19+
public void setStatus(String status) { this.status = status; }
20+
21+
public String getMessage() { return message; }
22+
public void setMessage(String message) { this.message = message; }
23+
24+
public String getProjectToken() { return projectToken; }
25+
public void setProjectToken(String projectToken) { this.projectToken = projectToken; }
26+
27+
// Helper method to check if verification was successful
28+
public boolean isSuccessful() {
29+
return "Success".equalsIgnoreCase(status);
30+
}
31+
}

src/main/java/io/github/lambdatest/utils/GitUtils.java

+11-17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.InputStreamReader;
1010
import java.util.Arrays;
1111
import java.util.List;
12+
import java.util.ArrayList;
1213
import java.util.Map;
1314
import io.github.lambdatest.utils.LoggerUtil;
1415
import java.util.logging.Logger;
@@ -20,15 +21,11 @@ public class GitUtils {
2021
private static Logger log = LoggerUtil.createLogger("lambdatest-java-sdk");
2122

2223
public static GitInfo getGitInfo(Map<String, String> envVars) {
23-
2424
String gitInfoFilePath = envVars.get("SMARTUI_GIT_INFO_FILEPATH");
25-
// If Git info file exists, read from it
2625
if (gitInfoFilePath != null) {
2726
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);
3229
}
3330
}
3431

@@ -44,22 +41,20 @@ private static GitInfo readGitInfoFromFile(String filePath, Map<String, String>
4441
(String) gitInfo.get("commit_body"),
4542
(String) gitInfo.get("commit_author"),
4643
getGitHubURL(envVars, (String) gitInfo.get("commit_id")),
47-
envVars.getOrDefault("BASELINE_BRANCH", "")
48-
);
44+
envVars.getOrDefault("BASELINE_BRANCH", ""));
4945
} catch (IOException e) {
50-
log.info("Error reading Git info file: " + e.getMessage());
46+
log.info("Error reading Git info file: " + e.getMessage());
5147
return null;
5248
}
5349
}
5450

5551
private static GitInfo fetchGitInfoFromCommands(Map<String, String> envVars) {
5652
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", "" };
5854

5955
String command = String.format(
6056
"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));
6358

6459
List<String> outputLines = executeCommand(command);
6560

@@ -74,15 +69,13 @@ private static GitInfo fetchGitInfoFromCommands(Map<String, String> envVars) {
7469
List<String> branchAndTagsList = Arrays.asList(branchAndTags);
7570
String branch = envVars.getOrDefault("CURRENT_BRANCH", branchAndTagsList.get(0));
7671

77-
7872
return new GitInfo(
7973
branch,
8074
res[0], // commitId
8175
res[2], // commitMessage
8276
res[7], // commitAuthor
8377
getGitHubURL(envVars, res[1]), // githubURL
84-
envVars.getOrDefault("BASELINE_BRANCH", "")
85-
);
78+
envVars.getOrDefault("BASELINE_BRANCH", ""));
8679
}
8780

8881
private static String shortenCommitId(String commitId) {
@@ -99,12 +92,13 @@ private static String getGitHubURL(Map<String, String> envVars, String commitId)
9992

10093
private static List<String> executeCommand(String command) {
10194
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 });
10396
return new BufferedReader(new InputStreamReader(process.getInputStream()))
10497
.lines()
10598
.collect(Collectors.toList());
10699
} 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>();
108102
}
109103
}
110104
}

0 commit comments

Comments
 (0)