Skip to content

Commit 234f8e3

Browse files
committed
chore: optimize imports and re-arrange code
1 parent 7eaceb6 commit 234f8e3

20 files changed

+895
-894
lines changed

src/main/java/com/browserstack/automate/ci/common/BrowserStackBuildWrapperOperations.java

Lines changed: 109 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -27,132 +27,132 @@
2727
import static com.browserstack.automate.ci.common.logger.PluginLogger.log;
2828

2929
public class BrowserStackBuildWrapperOperations {
30-
private BrowserStackCredentials credentials;
31-
private boolean isTearDownPhase;
32-
private PrintStream logger;
33-
private static final String ENV_JENKINS_BUILD_TAG = "BUILD_TAG";
34-
private LocalConfig localConfig;
35-
private JenkinsBrowserStackLocal browserstackLocal;
36-
37-
public BrowserStackBuildWrapperOperations(BrowserStackCredentials credentials,
38-
boolean isTearDownPhase, PrintStream logger, LocalConfig localConfig,
39-
JenkinsBrowserStackLocal browserStackLocal) {
40-
super();
41-
this.credentials = credentials;
42-
this.isTearDownPhase = isTearDownPhase;
43-
this.logger = logger;
44-
this.localConfig = localConfig;
45-
this.browserstackLocal = browserStackLocal;
46-
}
47-
48-
public void buildEnvVars(Map<String, String> env) {
49-
if (credentials != null) {
50-
if (credentials.hasUsername()) {
51-
String username = credentials.getUsername();
52-
53-
env.put(BrowserStackEnvVars.BROWSERSTACK_USER, username + "-jenkins");
54-
env.put(BrowserStackEnvVars.BROWSERSTACK_USERNAME, username + "-jenkins");
55-
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_USERNAME, username);
56-
}
57-
58-
if (credentials.hasAccesskey()) {
59-
String accesskey = credentials.getDecryptedAccesskey();
60-
env.put(BrowserStackEnvVars.BROWSERSTACK_ACCESSKEY, accesskey);
61-
env.put(BrowserStackEnvVars.BROWSERSTACK_ACCESS_KEY, accesskey);
62-
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_ACCESS_KEY, Tools.maskString(accesskey));
63-
}
30+
private static final String ENV_JENKINS_BUILD_TAG = "BUILD_TAG";
31+
private BrowserStackCredentials credentials;
32+
private boolean isTearDownPhase;
33+
private PrintStream logger;
34+
private LocalConfig localConfig;
35+
private JenkinsBrowserStackLocal browserstackLocal;
36+
37+
public BrowserStackBuildWrapperOperations(BrowserStackCredentials credentials,
38+
boolean isTearDownPhase, PrintStream logger, LocalConfig localConfig,
39+
JenkinsBrowserStackLocal browserStackLocal) {
40+
super();
41+
this.credentials = credentials;
42+
this.isTearDownPhase = isTearDownPhase;
43+
this.logger = logger;
44+
this.localConfig = localConfig;
45+
this.browserstackLocal = browserStackLocal;
6446
}
6547

66-
String buildTag = env.get(ENV_JENKINS_BUILD_TAG);
67-
if (buildTag != null) {
68-
env.put(BrowserStackEnvVars.BROWSERSTACK_BUILD, buildTag);
69-
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_BUILD, buildTag);
70-
71-
// Maintaining build name separately to have more control over it.
72-
// To keep it consistent with other CI/CD plugins.
73-
env.put(BrowserStackEnvVars.BROWSERSTACK_BUILD_NAME, buildTag);
74-
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_BUILD_NAME, buildTag);
48+
public static ListBoxModel doFillCredentialsIdItems(Item context) {
49+
if (context != null && !context.hasPermission(Item.CONFIGURE)) {
50+
return new StandardListBoxModel();
51+
}
52+
53+
return new StandardListBoxModel().withMatching(
54+
CredentialsMatchers.anyOf(CredentialsMatchers.instanceOf(BrowserStackCredentials.class)),
55+
CredentialsProvider.lookupCredentials(BrowserStackCredentials.class, context, ACL.SYSTEM,
56+
new ArrayList<DomainRequirement>()));
7557
}
7658

77-
String isLocalEnabled = localConfig != null ? "true" : "false";
78-
env.put(BrowserStackEnvVars.BROWSERSTACK_LOCAL, "" + isLocalEnabled);
79-
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_LOCAL, isLocalEnabled);
59+
public static FormValidation doCheckLocalPath(final AbstractProject project,
60+
final String localPath) {
61+
final String path = Util.fixEmptyAndTrim(localPath);
62+
if (StringUtils.isBlank(path)) {
63+
return FormValidation.ok();
64+
}
8065

81-
String localIdentifier =
82-
(browserstackLocal != null) ? browserstackLocal.getLocalIdentifier() : "";
66+
try {
67+
File f = resolvePath(project, localPath);
68+
if (f != null) {
69+
return FormValidation.ok();
70+
}
71+
} catch (Exception e) {
72+
return FormValidation.error(e.getMessage());
73+
}
8374

84-
if (StringUtils.isNotBlank(localIdentifier)) {
85-
env.put(BrowserStackEnvVars.BROWSERSTACK_LOCAL_IDENTIFIER, localIdentifier);
86-
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_LOCAL_IDENTIFIER, localIdentifier);
75+
return FormValidation.error("Invalid path.");
8776
}
88-
}
8977

90-
public void logEnvVar(String key, String value) {
91-
if (!isTearDownPhase) {
92-
log(logger, key + "=" + value);
93-
}
94-
}
78+
private static File resolvePath(final AbstractProject project, final String path)
79+
throws IOException, InterruptedException {
80+
File f = new File(path);
81+
if (f.isAbsolute() && (!f.isFile() || !f.canExecute())) {
82+
return null;
83+
}
9584

96-
public static ListBoxModel doFillCredentialsIdItems(Item context) {
97-
if (context != null && !context.hasPermission(Item.CONFIGURE)) {
98-
return new StandardListBoxModel();
99-
}
85+
// For absolute paths
86+
FormValidation validateExec = FormValidation.validateExecutable(path);
87+
if (validateExec.kind == FormValidation.Kind.OK) {
88+
return f;
89+
}
10090

101-
return new StandardListBoxModel().withMatching(
102-
CredentialsMatchers.anyOf(CredentialsMatchers.instanceOf(BrowserStackCredentials.class)),
103-
CredentialsProvider.lookupCredentials(BrowserStackCredentials.class, context, ACL.SYSTEM,
104-
new ArrayList<DomainRequirement>()));
105-
}
106-
107-
public static FormValidation doCheckLocalPath(final AbstractProject project,
108-
final String localPath) {
109-
final String path = Util.fixEmptyAndTrim(localPath);
110-
if (StringUtils.isBlank(path)) {
111-
return FormValidation.ok();
91+
// Ant style path definitions
92+
FilePath workspace = project.getSomeWorkspace();
93+
if (workspace != null) {
94+
File workspaceRoot = new File(workspace.toURI());
95+
FileSet fileSet = Util.createFileSet(workspaceRoot, path);
96+
FileScanner fs = fileSet.getDirectoryScanner();
97+
fs.setIncludes(new String[]{path});
98+
fs.scan();
99+
100+
String[] includedFiles = fs.getIncludedFiles();
101+
if (includedFiles.length > 0) {
102+
File includedFile = new File(workspaceRoot, includedFiles[0]);
103+
if (includedFile.exists() && includedFile.isFile() && includedFile.canExecute()) {
104+
return includedFile;
105+
}
106+
}
107+
}
108+
return null;
112109
}
113110

114-
try {
115-
File f = resolvePath(project, localPath);
116-
if (f != null) {
117-
return FormValidation.ok();
118-
}
119-
} catch (Exception e) {
120-
return FormValidation.error(e.getMessage());
121-
}
111+
public void buildEnvVars(Map<String, String> env) {
112+
if (credentials != null) {
113+
if (credentials.hasUsername()) {
114+
String username = credentials.getUsername();
115+
116+
env.put(BrowserStackEnvVars.BROWSERSTACK_USER, username + "-jenkins");
117+
env.put(BrowserStackEnvVars.BROWSERSTACK_USERNAME, username + "-jenkins");
118+
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_USERNAME, username);
119+
}
120+
121+
if (credentials.hasAccesskey()) {
122+
String accesskey = credentials.getDecryptedAccesskey();
123+
env.put(BrowserStackEnvVars.BROWSERSTACK_ACCESSKEY, accesskey);
124+
env.put(BrowserStackEnvVars.BROWSERSTACK_ACCESS_KEY, accesskey);
125+
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_ACCESS_KEY, Tools.maskString(accesskey));
126+
}
127+
}
122128

123-
return FormValidation.error("Invalid path.");
124-
}
129+
String buildTag = env.get(ENV_JENKINS_BUILD_TAG);
130+
if (buildTag != null) {
131+
env.put(BrowserStackEnvVars.BROWSERSTACK_BUILD, buildTag);
132+
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_BUILD, buildTag);
125133

126-
private static File resolvePath(final AbstractProject project, final String path)
127-
throws IOException, InterruptedException {
128-
File f = new File(path);
129-
if (f.isAbsolute() && (!f.isFile() || !f.canExecute())) {
130-
return null;
131-
}
134+
// Maintaining build name separately to have more control over it.
135+
// To keep it consistent with other CI/CD plugins.
136+
env.put(BrowserStackEnvVars.BROWSERSTACK_BUILD_NAME, buildTag);
137+
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_BUILD_NAME, buildTag);
138+
}
139+
140+
String isLocalEnabled = localConfig != null ? "true" : "false";
141+
env.put(BrowserStackEnvVars.BROWSERSTACK_LOCAL, "" + isLocalEnabled);
142+
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_LOCAL, isLocalEnabled);
132143

133-
// For absolute paths
134-
FormValidation validateExec = FormValidation.validateExecutable(path);
135-
if (validateExec.kind == FormValidation.Kind.OK) {
136-
return f;
144+
String localIdentifier =
145+
(browserstackLocal != null) ? browserstackLocal.getLocalIdentifier() : "";
146+
147+
if (StringUtils.isNotBlank(localIdentifier)) {
148+
env.put(BrowserStackEnvVars.BROWSERSTACK_LOCAL_IDENTIFIER, localIdentifier);
149+
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_LOCAL_IDENTIFIER, localIdentifier);
150+
}
137151
}
138152

139-
// Ant style path definitions
140-
FilePath workspace = project.getSomeWorkspace();
141-
if (workspace != null) {
142-
File workspaceRoot = new File(workspace.toURI());
143-
FileSet fileSet = Util.createFileSet(workspaceRoot, path);
144-
FileScanner fs = fileSet.getDirectoryScanner();
145-
fs.setIncludes(new String[] {path});
146-
fs.scan();
147-
148-
String[] includedFiles = fs.getIncludedFiles();
149-
if (includedFiles.length > 0) {
150-
File includedFile = new File(workspaceRoot, includedFiles[0]);
151-
if (includedFile.exists() && includedFile.isFile() && includedFile.canExecute()) {
152-
return includedFile;
153+
public void logEnvVar(String key, String value) {
154+
if (!isTearDownPhase) {
155+
log(logger, key + "=" + value);
153156
}
154-
}
155157
}
156-
return null;
157-
}
158158
}

src/main/java/com/browserstack/automate/ci/common/analytics/Analytics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private static void postAsync(GoogleAnalyticsRequest request) {
136136
if (isEnabled && googleAnalyticsClient != null) {
137137
if (LOGGER.getLevel() == Level.FINE && request.hitType().equals("event")) {
138138
LOGGER.fine("Posting Event :: " + ((EventHit) request).eventCategory()
139-
+ "." + ((EventHit) request).eventAction());
139+
+ "." + ((EventHit) request).eventAction());
140140
}
141141
googleAnalyticsClient.postAsync(request);
142142
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.browserstack.automate.ci.common.enums;
22

33
public enum ProjectType {
4-
AUTOMATE, APP_AUTOMATE
4+
AUTOMATE, APP_AUTOMATE
55
}

src/main/java/com/browserstack/automate/ci/common/logger/PluginLogger.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
import java.io.PrintStream;
44

55
public class PluginLogger {
6-
private static final String PROPERTY_DEBUG = "browserstack.automate.debug";
7-
private static String TAG = "[BrowserStack]";
6+
private static final String PROPERTY_DEBUG = "browserstack.automate.debug";
7+
private static String TAG = "[BrowserStack]";
88

9-
public static boolean isDebugEnabled() {
10-
return System.getProperty(PROPERTY_DEBUG, "false").equals("true");
11-
}
9+
public static boolean isDebugEnabled() {
10+
return System.getProperty(PROPERTY_DEBUG, "false").equals("true");
11+
}
1212

13-
public static void log(PrintStream printStream, String message) {
14-
printStream.println(TAG + " " + message);
15-
}
13+
public static void log(PrintStream printStream, String message) {
14+
printStream.println(TAG + " " + message);
15+
}
1616

17-
public static void logDebug(PrintStream printStream, String message) {
18-
if (isDebugEnabled())
19-
printStream.println(TAG + ": " + message);
20-
}
17+
public static void logDebug(PrintStream printStream, String message) {
18+
if (isDebugEnabled())
19+
printStream.println(TAG + ": " + message);
20+
}
2121

22-
public static void setTag(String tag) {
23-
TAG = tag;
24-
}
22+
public static void setTag(String tag) {
23+
TAG = tag;
24+
}
2525

26-
public static void logError(PrintStream printStream, String message) {
27-
log(printStream, "[ERROR] " + message);
28-
}
26+
public static void logError(PrintStream printStream, String message) {
27+
log(printStream, "[ERROR] " + message);
28+
}
2929
}

src/main/java/com/browserstack/automate/ci/common/model/BrowserStackSession.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,34 @@
44

55
/**
66
* Description : For storing info of a browserstack session.
7-
*
87
*/
98

109
public class BrowserStackSession {
1110

12-
private static final String KEY_SESSION_ID = "sessionId";
13-
private static final String KEY_PROJECT_TYPE = "projectType";
11+
private static final String KEY_SESSION_ID = "sessionId";
12+
private static final String KEY_PROJECT_TYPE = "projectType";
1413

15-
private String sessionId;
16-
private ProjectType projectType;
14+
private String sessionId;
15+
private ProjectType projectType;
1716

18-
public BrowserStackSession(String sessionId, String projectType) {
19-
this.sessionId = sessionId;
20-
this.projectType = getProjectTypeFromString(projectType);
21-
}
17+
public BrowserStackSession(String sessionId, String projectType) {
18+
this.sessionId = sessionId;
19+
this.projectType = getProjectTypeFromString(projectType);
20+
}
2221

23-
private ProjectType getProjectTypeFromString(String projectType) {
24-
try {
25-
return ProjectType.valueOf(projectType);
26-
} catch (Exception e) {
27-
return ProjectType.AUTOMATE; // default project type for backward compatibility.
22+
private ProjectType getProjectTypeFromString(String projectType) {
23+
try {
24+
return ProjectType.valueOf(projectType);
25+
} catch (Exception e) {
26+
return ProjectType.AUTOMATE; // default project type for backward compatibility.
27+
}
2828
}
29-
}
3029

31-
public String getSessionId() {
32-
return sessionId;
33-
}
30+
public String getSessionId() {
31+
return sessionId;
32+
}
3433

35-
public ProjectType getProjectType() {
36-
return projectType;
37-
}
34+
public ProjectType getProjectType() {
35+
return projectType;
36+
}
3837
}

0 commit comments

Comments
 (0)