Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.browserstack.automate.ci.common;

import com.browserstack.automate.ci.common.logger.PluginLogger;
import org.apache.commons.lang.StringUtils;

import hudson.Util;
import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -50,7 +50,7 @@ public AutomateTestCase(String sessionId, String packageName, String className,
}

public static String stripTestParams(String testCaseName) {
if (StringUtils.isEmpty(testCaseName)) {
if (Util.fixEmpty(testCaseName) == null) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.ant.FileScanner;
import org.apache.tools.ant.types.FileSet;

Expand Down Expand Up @@ -63,7 +62,7 @@ public static ListBoxModel doFillCredentialsIdItems(Item context) {
public static FormValidation doCheckLocalPath(final AbstractProject project,
final String localPath) {
final String path = Util.fixEmptyAndTrim(localPath);
if (StringUtils.isBlank(path)) {
if (Util.fixEmptyAndTrim(path) == null) {
return FormValidation.ok();
}

Expand Down Expand Up @@ -166,23 +165,23 @@ public void buildEnvVars(Map<String, String> env) {
String localIdentifier =
(browserstackLocal != null) ? browserstackLocal.getLocalIdentifier() : "";

if (StringUtils.isNotBlank(localIdentifier)) {
if (Util.fixEmptyAndTrim(localIdentifier) != null) {
env.put(BrowserStackEnvVars.BROWSERSTACK_LOCAL_IDENTIFIER, localIdentifier);
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_LOCAL_IDENTIFIER, localIdentifier);
}

String tests =
(observabilityConfig != null) ? observabilityConfig.getTests() : "";

if (StringUtils.isNotBlank(tests)) {
if (Util.fixEmptyAndTrim(tests) != null) {
env.put(BrowserStackEnvVars.BROWSERSTACK_RERUN_TESTS, tests);
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_RERUN_TESTS, tests);
}

String reRun =
(observabilityConfig != null) ? observabilityConfig.getReRun() : "";

if (StringUtils.isNotBlank(reRun)) {
if (Util.fixEmptyAndTrim(reRun) != null) {
env.put(BrowserStackEnvVars.BROWSERSTACK_RERUN, reRun);
logEnvVar(BrowserStackEnvVars.BROWSERSTACK_RERUN, reRun);
}
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/browserstack/automate/ci/common/Tools.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.browserstack.automate.ci.common;

import org.apache.commons.lang.RandomStringUtils;
import java.security.SecureRandom;

import hudson.FilePath;
import hudson.model.Run;
Expand Down Expand Up @@ -63,8 +63,23 @@ public static String durationToHumanReadable(long duration) {
return result;
}

private static final String LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String NUMBERS = "0123456789";
private static final SecureRandom RANDOM = new SecureRandom();

public static String getUniqueString(boolean letters, boolean numbers) {
return RandomStringUtils.random(48, letters, numbers);
StringBuilder pool = new StringBuilder();
if (letters) {
pool.append(LETTERS);
}
if (numbers) {
pool.append(NUMBERS);
}
StringBuilder result = new StringBuilder(48);
for (int i = 0; i < 48; i++) {
result.append(pool.charAt(RANDOM.nextInt(pool.length())));
}
return result.toString();
}

/** Gets the directory to store report files */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import hudson.PluginWrapper;
import jenkins.model.Jenkins;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import hudson.Util;

/**
* @author Shirish Kamath
Expand Down Expand Up @@ -63,7 +63,7 @@ private static GoogleAnalytics buildGoogleAnalyticsClient() {

String trackingId = pluginProps.getProperty(GOOGLE_PROPERTIES_KEY);
LOGGER.fine("Using Google Analytics Tracking ID :: " + trackingId);
if (StringUtils.isNotEmpty(trackingId)) {
if (Util.fixEmpty(trackingId) != null) {
return new GoogleAnalytics(trackingId);
}
} catch (IOException ioe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.util.FormValidation;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

Expand All @@ -20,6 +19,7 @@
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import hudson.Util;

public class AppUploaderBuilder extends Builder {

Expand All @@ -41,7 +41,7 @@ public boolean perform(@Nonnull AbstractBuild<?, ?> build, @Nonnull Launcher lau

String appId = AppUploaderHelper.uploadApp(build, logger, this.buildFilePath, null);

if (StringUtils.isEmpty(appId)) {
if (Util.fixEmpty(appId) == null) {
return false;
} else {
addAppIdToEnvironment(build, appId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import hudson.model.Job;
import hudson.tasks.BuildWrapper;
import hudson.util.DescribableList;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

Expand All @@ -27,6 +26,7 @@
import java.util.Map;

import static com.browserstack.automate.ci.common.logger.PluginLogger.log;
import hudson.Util;


public class BrowserStackBuildWrapper extends BuildWrapper {
Expand Down Expand Up @@ -148,8 +148,8 @@ public void setCredentialsId(String credentialsId) {

private void recordBuildStats() {
boolean localEnabled = (localConfig != null);
boolean localPathSet = localEnabled && StringUtils.isNotBlank(localConfig.getLocalPath());
boolean localOptionsSet = localEnabled && StringUtils.isNotBlank(localConfig.getLocalOptions());
boolean localPathSet = localEnabled && Util.fixEmptyAndTrim(localConfig.getLocalPath()) != null;
boolean localOptionsSet = localEnabled && Util.fixEmptyAndTrim(localConfig.getLocalOptions()) != null;
Analytics.trackBuildRun(localEnabled, localPathSet, localOptionsSet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import hudson.util.FormValidation;
import hudson.util.Secret;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -68,7 +67,7 @@ public BrowserStackCredentials(String id, String description, String username, S
}

public static FormValidation testAuthentication(final String username, final String accesskey) {
if (StringUtils.isBlank(username) || StringUtils.isBlank(accesskey)) {
if (Util.fixEmptyAndTrim(username) == null || Util.fixEmptyAndTrim(accesskey) == null) {
return FormValidation.ok();
}

Expand Down Expand Up @@ -124,7 +123,7 @@ public String getUsername() {
}

public boolean hasUsername() {
return StringUtils.isNotBlank(username);
return Util.fixEmptyAndTrim(username) != null;
}

@Exported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import hudson.EnvVars;
import hudson.Launcher;
import jenkins.security.MasterToSlaveCallable;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.PrintStream;
Expand All @@ -15,6 +14,7 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import hudson.Util;

public class JenkinsBrowserStackLocal extends Local implements Serializable {
private static final long serialVersionUID = 1830651088511115761L;
Expand Down Expand Up @@ -62,7 +62,7 @@ private String[] processLocalArguments(final String argString, String buildTag)
localIdPos = i;
if (i < args.length - 1 && args[i + 1] != null && !args[i + 1].startsWith("-")) {
localIdentifier = args[i + 1];
if (StringUtils.isNotBlank(localIdentifier)) {
if (Util.fixEmptyAndTrim(localIdentifier) != null) {
localIdentifierOverriden = true;
}

Expand Down Expand Up @@ -133,7 +133,7 @@ protected LocalProcess runCommand(List<String> command) throws IOException {
DaemonAction daemonAction = detectDaemonAction(command);
if (daemonAction != null) {
for (String arg : arguments) {
if (StringUtils.isNotBlank(arg)) {
if (Util.fixEmptyAndTrim(arg) != null) {
command.add(arg.trim());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import hudson.EnvVars;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.steps.BodyExecution;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
Expand All @@ -16,6 +15,7 @@
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Optional;
import hudson.Util;

public class AppUploadStepExecution extends SynchronousNonBlockingStepExecution<Void> {

Expand All @@ -42,7 +42,7 @@ protected Void run() throws Exception {

String appId = AppUploaderHelper.uploadApp(run, logger, this.appPath, customProxy);

if (StringUtils.isEmpty(appId)) {
if (Util.fixEmpty(appId) == null) {
PluginLogger.log(logger, "ERROR : App Id empty. ABORTING!!!");
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import hudson.Extension;
import hudson.model.*;
import hudson.model.listeners.RunListener;
import io.jenkins.cli.shaded.org.apache.commons.lang.StringUtils;
import jenkins.model.Jenkins;
import okhttp3.*;
import org.apache.commons.io.FileUtils;
Expand All @@ -24,6 +23,7 @@
import java.sql.Timestamp;
import java.time.Instant;
import java.util.logging.Logger;
import hudson.Util;

@Extension
public class QualityDashboardPipelineTracker extends RunListener<Run<?, ?>> {
Expand Down Expand Up @@ -69,7 +69,7 @@ private void processArtifactsAndSendData(Run<?, ?> run, Result overallResult, Br

apiUtil.logToQD(browserStackCredentials, "Final Computed Zip Path for jobName: " + jobName + " and buildNumber: " + buildNumber + " is: " + finalPathToZip);

if(StringUtils.isNotEmpty(finalPathToZip)) {
if(Util.fixEmpty(finalPathToZip) != null) {
apiUtil.logToQD(browserStackCredentials, "Found artifacts in configured path for jobName: " + jobName + " and buildNumber: " + buildNumber);
copyDirectoryToParentIfRequired(run, finalPathToZip, browserStackCredentials);
qdS3Url = zipArtifactsAndUploadToQD(finalPathToZip, browserStackCredentials, jobName, buildNumber);
Expand All @@ -87,7 +87,7 @@ private void processArtifactsAndSendData(Run<?, ?> run, Result overallResult, Br
finalPathToZip = null;
}
}
if (StringUtils.isNotEmpty(finalPathToZip) && Files.exists(Paths.get(finalPathToZip))) {
if (Util.fixEmpty(finalPathToZip) != null && Files.exists(Paths.get(finalPathToZip))) {
apiUtil.logToQD(browserStackCredentials, "Got artifact path for jobName: " + jobName + " and buildNumber: " + buildNumber + " as: " + finalPathToZip);
qdS3Url = zipArtifactsAndUploadToQD(finalPathToZip, browserStackCredentials, jobName, buildNumber);
} else {
Expand All @@ -104,7 +104,7 @@ private String zipArtifactsAndUploadToQD (String finalPathToZip, BrowserStackCre
String finalZipFilePath = packZip(finalPathToZip, jobName, browserStackCredentials);
apiUtil.logToQD(browserStackCredentials, "Final zip file's path for jobName: " + jobName + " and buildNumber: " + buildNumber + " is:" + finalZipFilePath);
String qdS3Url = uploadZipToQd(finalZipFilePath, browserStackCredentials, jobName, buildNumber);
if(StringUtils.isNotEmpty(finalZipFilePath)) {
if(Util.fixEmpty(finalZipFilePath) != null) {
Files.deleteIfExists(Paths.get(finalZipFilePath));
apiUtil.logToQD(browserStackCredentials, "Deleted file from server after upload for jobName: " + jobName + " and buildNumber: " + buildNumber);
} else {
Expand Down Expand Up @@ -164,11 +164,11 @@ private boolean checkIfPathIsFound(String filePath) {
private String getFinalZipPath(Run<?, ?> run, BrowserStackCredentials browserStackCredentials) throws JsonProcessingException {
String finalZipPath = null;
String currentResultDir = getResultDirForPipeline(getUrlForPipeline(run), browserStackCredentials, run.getNumber());
if(StringUtils.isNotEmpty(currentResultDir) && checkIfPathIsFound(currentResultDir)) {
if(Util.fixEmpty(currentResultDir) != null && checkIfPathIsFound(currentResultDir)) {
finalZipPath = currentResultDir;
} else {
String defaultWorkspaceDir = getDefaultWorkspaceDirectory(run);
if(StringUtils.isNotEmpty(defaultWorkspaceDir)) {
if(Util.fixEmpty(defaultWorkspaceDir) != null) {
String jobName = run.getParent().getName();
defaultWorkspaceDir = defaultWorkspaceDir + "/workspace/" + jobName + "/browserstack-artifacts";
finalZipPath = checkIfPathIsFound(defaultWorkspaceDir) ? defaultWorkspaceDir : null;
Expand All @@ -180,7 +180,7 @@ private String getFinalZipPath(Run<?, ?> run, BrowserStackCredentials browserSta
private String getDefaultWorkspaceDirectory(Run<?, ?> run) {
Jenkins jenkins = Jenkins.getInstanceOrNull();
String workspacePath = jenkins != null && jenkins.getRootDir() != null ? jenkins.getRootDir().getAbsolutePath() : null;
return StringUtils.isNotEmpty(workspacePath) ? workspacePath : null;
return Util.fixEmpty(workspacePath) != null ? workspacePath : null;
}

private String getUrlForPipeline(Run<?, ?> build) {
Expand Down Expand Up @@ -294,13 +294,13 @@ private String uploadZipToQd(String pathToZip, BrowserStackCredentials browserSt
private void copyDirectoryToParentIfRequired(Run<?, ?> run, String finalParentPathFrom, BrowserStackCredentials browserStackCredentials) throws IOException {
String finalParentPathTo = null;
String upStreamProj = UpstreamPipelineResolver.resolveImmediateUpstreamProject(run, browserStackCredentials);
if(StringUtils.isNotEmpty(upStreamProj)) {
if(Util.fixEmpty(upStreamProj) != null) {
String parentResultDir = getResultDirForPipeline(upStreamProj, browserStackCredentials, run.getNumber());
if(StringUtils.isNotEmpty(parentResultDir) && checkIfPathIsFound(parentResultDir)) {
if(Util.fixEmpty(parentResultDir) != null && checkIfPathIsFound(parentResultDir)) {
finalParentPathTo = parentResultDir;
} else {
String defaultWorkspaceDir = getDefaultWorkspaceDirectory(run);
if(StringUtils.isNotEmpty(defaultWorkspaceDir) && checkIfPathIsFound(defaultWorkspaceDir)) {
if(Util.fixEmpty(defaultWorkspaceDir) != null && checkIfPathIsFound(defaultWorkspaceDir)) {
defaultWorkspaceDir = defaultWorkspaceDir + "/workspace/" + upStreamProj + "/browserstack-artifacts";
boolean pathAlreadyExists = checkIfPathIsFound(defaultWorkspaceDir);
if(!pathAlreadyExists) {
Expand All @@ -309,7 +309,7 @@ private void copyDirectoryToParentIfRequired(Run<?, ?> run, String finalParentPa
finalParentPathTo = defaultWorkspaceDir;
}
}
if(StringUtils.isNotEmpty(finalParentPathTo)) {
if(Util.fixEmpty(finalParentPathTo) != null) {
FileUtils.copyDirectoryToDirectory(new File(finalParentPathFrom), new File(finalParentPathTo));
int buildNum = run.getNumber();
File finalParentFromFile = new File(finalParentPathFrom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -29,6 +28,7 @@
import org.jvnet.hudson.test.TouchBuilder;

import java.io.IOException;
import hudson.Util;

/**
* Unit test for {@link AutomateTestAction} class.
Expand Down Expand Up @@ -89,7 +89,7 @@ public void testAutomateExceptionIsHandled() throws Exception {
Assert.assertTrue("Exception should be of Type AutomateException",
automateTestAction.getLastException() instanceof AutomateException);
Assert.assertTrue("Exception message MUST not be empty",
StringUtils.isNotEmpty(automateTestAction.getLastError()));
Util.fixEmpty(automateTestAction.getLastError()) != null);
}

@Test
Expand All @@ -114,7 +114,7 @@ public void testSessionNotFoundExceptionIsHandled() throws Exception {
Assert.assertTrue("Exception should be of Type SessionNotFound",
automateTestAction.getLastException() instanceof SessionNotFound);
Assert.assertTrue("Exception message MUST not be empty",
StringUtils.isNotEmpty(automateTestAction.getLastError()));
Util.fixEmpty(automateTestAction.getLastError()) != null);
}

public void addBuildStep() throws IOException {
Expand Down
Loading
Loading