|
27 | 27 | import static com.browserstack.automate.ci.common.logger.PluginLogger.log; |
28 | 28 |
|
29 | 29 | 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; |
64 | 46 | } |
65 | 47 |
|
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>())); |
75 | 57 | } |
76 | 58 |
|
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 | + } |
80 | 65 |
|
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 | + } |
83 | 74 |
|
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."); |
87 | 76 | } |
88 | | - } |
89 | 77 |
|
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 | + } |
95 | 84 |
|
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 | + } |
100 | 90 |
|
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; |
112 | 109 | } |
113 | 110 |
|
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 | + } |
122 | 128 |
|
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); |
125 | 133 |
|
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); |
132 | 143 |
|
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 | + } |
137 | 151 | } |
138 | 152 |
|
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); |
153 | 156 | } |
154 | | - } |
155 | 157 | } |
156 | | - return null; |
157 | | - } |
158 | 158 | } |
0 commit comments