|
1 | 1 | package com.browserstack.automate.ci.common; |
2 | 2 |
|
3 | | -import static com.browserstack.automate.ci.common.logger.PluginLogger.log; |
4 | | -import java.io.File; |
5 | | -import java.io.IOException; |
6 | | -import java.io.PrintStream; |
7 | | -import java.util.ArrayList; |
8 | | -import java.util.Map; |
9 | | -import org.apache.commons.lang.StringUtils; |
10 | | -import org.apache.tools.ant.FileScanner; |
11 | | -import org.apache.tools.ant.types.FileSet; |
12 | 3 | import com.browserstack.automate.ci.jenkins.BrowserStackCredentials; |
13 | 4 | import com.browserstack.automate.ci.jenkins.local.JenkinsBrowserStackLocal; |
14 | 5 | import com.browserstack.automate.ci.jenkins.local.LocalConfig; |
|
23 | 14 | import hudson.security.ACL; |
24 | 15 | import hudson.util.FormValidation; |
25 | 16 | import hudson.util.ListBoxModel; |
| 17 | +import org.apache.commons.lang.StringUtils; |
| 18 | +import org.apache.tools.ant.FileScanner; |
| 19 | +import org.apache.tools.ant.types.FileSet; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.PrintStream; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import static com.browserstack.automate.ci.common.logger.PluginLogger.log; |
26 | 28 |
|
27 | 29 | public class BrowserStackBuildWrapperOperations { |
28 | | - private BrowserStackCredentials credentials; |
29 | | - private boolean isTearDownPhase; |
30 | | - private PrintStream logger; |
31 | | - private static final String ENV_JENKINS_BUILD_TAG = "BUILD_TAG"; |
32 | | - private LocalConfig localConfig; |
33 | | - private JenkinsBrowserStackLocal browserstackLocal; |
34 | | - |
35 | | - public BrowserStackBuildWrapperOperations(BrowserStackCredentials credentials, |
36 | | - boolean isTearDownPhase, PrintStream logger, LocalConfig localConfig, |
37 | | - JenkinsBrowserStackLocal browserStackLocal) { |
38 | | - super(); |
39 | | - this.credentials = credentials; |
40 | | - this.isTearDownPhase = isTearDownPhase; |
41 | | - this.logger = logger; |
42 | | - this.localConfig = localConfig; |
43 | | - this.browserstackLocal = browserStackLocal; |
44 | | - } |
45 | | - |
46 | | - public void buildEnvVars(Map<String, String> env) { |
47 | | - if (credentials != null) { |
48 | | - if (credentials.hasUsername()) { |
49 | | - String username = credentials.getUsername(); |
50 | | - |
51 | | - env.put(BrowserStackEnvVars.BROWSERSTACK_USER, username + "-jenkins"); |
52 | | - env.put(BrowserStackEnvVars.BROWSERSTACK_USERNAME, username + "-jenkins"); |
53 | | - logEnvVar(BrowserStackEnvVars.BROWSERSTACK_USERNAME, username); |
54 | | - } |
55 | | - |
56 | | - if (credentials.hasAccesskey()) { |
57 | | - String accesskey = credentials.getDecryptedAccesskey(); |
58 | | - env.put(BrowserStackEnvVars.BROWSERSTACK_ACCESSKEY, accesskey); |
59 | | - env.put(BrowserStackEnvVars.BROWSERSTACK_ACCESS_KEY, accesskey); |
60 | | - logEnvVar(BrowserStackEnvVars.BROWSERSTACK_ACCESS_KEY, Tools.maskString(accesskey)); |
61 | | - } |
| 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; |
62 | 46 | } |
63 | 47 |
|
64 | | - String buildTag = env.get(ENV_JENKINS_BUILD_TAG); |
65 | | - if (buildTag != null) { |
66 | | - env.put(BrowserStackEnvVars.BROWSERSTACK_BUILD, buildTag); |
67 | | - logEnvVar(BrowserStackEnvVars.BROWSERSTACK_BUILD, 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>())); |
68 | 57 | } |
69 | 58 |
|
70 | | - String isLocalEnabled = localConfig != null ? "true" : "false"; |
71 | | - env.put(BrowserStackEnvVars.BROWSERSTACK_LOCAL, "" + isLocalEnabled); |
72 | | - 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 | + } |
73 | 65 |
|
74 | | - String localIdentifier = |
75 | | - (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 | + } |
76 | 74 |
|
77 | | - if (StringUtils.isNotBlank(localIdentifier)) { |
78 | | - env.put(BrowserStackEnvVars.BROWSERSTACK_LOCAL_IDENTIFIER, localIdentifier); |
79 | | - logEnvVar(BrowserStackEnvVars.BROWSERSTACK_LOCAL_IDENTIFIER, localIdentifier); |
| 75 | + return FormValidation.error("Invalid path."); |
80 | 76 | } |
81 | | - } |
82 | 77 |
|
83 | | - public void logEnvVar(String key, String value) { |
84 | | - if (!isTearDownPhase) { |
85 | | - log(logger, key + "=" + value); |
86 | | - } |
87 | | - } |
| 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 | + } |
88 | 84 |
|
89 | | - public static ListBoxModel doFillCredentialsIdItems(Item context) { |
90 | | - if (context != null && !context.hasPermission(Item.CONFIGURE)) { |
91 | | - return new StandardListBoxModel(); |
92 | | - } |
| 85 | + // For absolute paths |
| 86 | + FormValidation validateExec = FormValidation.validateExecutable(path); |
| 87 | + if (validateExec.kind == FormValidation.Kind.OK) { |
| 88 | + return f; |
| 89 | + } |
93 | 90 |
|
94 | | - return new StandardListBoxModel().withMatching( |
95 | | - CredentialsMatchers.anyOf(CredentialsMatchers.instanceOf(BrowserStackCredentials.class)), |
96 | | - CredentialsProvider.lookupCredentials(BrowserStackCredentials.class, context, ACL.SYSTEM, |
97 | | - new ArrayList<DomainRequirement>())); |
98 | | - } |
99 | | - |
100 | | - public static FormValidation doCheckLocalPath(final AbstractProject project, |
101 | | - final String localPath) { |
102 | | - final String path = Util.fixEmptyAndTrim(localPath); |
103 | | - if (StringUtils.isBlank(path)) { |
104 | | - 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; |
105 | 109 | } |
106 | 110 |
|
107 | | - try { |
108 | | - File f = resolvePath(project, localPath); |
109 | | - if (f != null) { |
110 | | - return FormValidation.ok(); |
111 | | - } |
112 | | - } catch (Exception e) { |
113 | | - return FormValidation.error(e.getMessage()); |
114 | | - } |
| 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 | + } |
115 | 128 |
|
116 | | - return FormValidation.error("Invalid path."); |
117 | | - } |
| 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); |
118 | 133 |
|
119 | | - private static File resolvePath(final AbstractProject project, final String path) |
120 | | - throws IOException, InterruptedException { |
121 | | - File f = new File(path); |
122 | | - if (f.isAbsolute() && (!f.isFile() || !f.canExecute())) { |
123 | | - return null; |
124 | | - } |
| 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); |
125 | 143 |
|
126 | | - // For absolute paths |
127 | | - FormValidation validateExec = FormValidation.validateExecutable(path); |
128 | | - if (validateExec.kind == FormValidation.Kind.OK) { |
129 | | - 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 | + } |
130 | 151 | } |
131 | 152 |
|
132 | | - // Ant style path definitions |
133 | | - FilePath workspace = project.getSomeWorkspace(); |
134 | | - if (workspace != null) { |
135 | | - File workspaceRoot = new File(workspace.toURI()); |
136 | | - FileSet fileSet = Util.createFileSet(workspaceRoot, path); |
137 | | - FileScanner fs = fileSet.getDirectoryScanner(); |
138 | | - fs.setIncludes(new String[] {path}); |
139 | | - fs.scan(); |
140 | | - |
141 | | - String[] includedFiles = fs.getIncludedFiles(); |
142 | | - if (includedFiles.length > 0) { |
143 | | - File includedFile = new File(workspaceRoot, includedFiles[0]); |
144 | | - if (includedFile.exists() && includedFile.isFile() && includedFile.canExecute()) { |
145 | | - return includedFile; |
| 153 | + public void logEnvVar(String key, String value) { |
| 154 | + if (!isTearDownPhase) { |
| 155 | + log(logger, key + "=" + value); |
146 | 156 | } |
147 | | - } |
148 | 157 | } |
149 | | - return null; |
150 | | - } |
151 | 158 | } |
0 commit comments