Skip to content

Commit

Permalink
JENKINS-14492 Update Sauce REST API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
rossrowe committed Jul 20, 2012
1 parent 1eee111 commit f3f6f09
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 46 deletions.
13 changes: 12 additions & 1 deletion pom.xml
Expand Up @@ -100,10 +100,21 @@
<version>7.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>saucerest</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>sauce-rest-api</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
Expand All @@ -115,7 +126,7 @@
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>ci-sauce</artifactId>
<version>1.13</version>
<version>1.14</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/hudson/plugins/sauce_ondemand/PluginImpl.java
Expand Up @@ -24,10 +24,10 @@
package hudson.plugins.sauce_ondemand;

import com.saucelabs.ci.SauceLibraryManager;
import com.saucelabs.common.SauceOnDemandAuthentication;
import com.saucelabs.hudson.HudsonSauceLibraryManager;
import com.saucelabs.hudson.HudsonSauceManagerFactory;
import com.saucelabs.rest.Credential;
import com.saucelabs.rest.SauceTunnelFactory;
import com.saucelabs.saucerest.SauceREST;
import hudson.Extension;
import hudson.Plugin;
import hudson.model.Describable;
Expand Down Expand Up @@ -133,8 +133,9 @@ public String getDisplayName() {

public FormValidation doValidate(@QueryParameter String username, @QueryParameter String apiKey, @QueryParameter boolean reuseSauceAuth ) {
try {
Credential credential = reuseSauceAuth ? new Credential() : new Credential(username, Secret.toString(Secret.fromString(apiKey)));
new SauceTunnelFactory(credential).list();
SauceOnDemandAuthentication credential = reuseSauceAuth ? new SauceOnDemandAuthentication() : new SauceOnDemandAuthentication(username, Secret.toString(Secret.fromString(apiKey)));
//we aren't interested in the results of the REST API call - just the fact that we executed without an error is enough to verify the connection
new SauceREST(credential.getUsername(), credential.getAccessKey()).retrieveResults("tunnels");
return FormValidation.ok("Success");
} catch (IOException e) {
return FormValidation.error(e, "Failed to connect to Sauce OnDemand");
Expand Down
Expand Up @@ -28,10 +28,9 @@
import com.saucelabs.ci.BrowserFactory;
import com.saucelabs.ci.sauceconnect.SauceConnectUtils;
import com.saucelabs.ci.sauceconnect.SauceTunnelManager;
import com.saucelabs.common.SauceOnDemandAuthentication;
import com.saucelabs.hudson.HudsonSauceManagerFactory;
import com.saucelabs.rest.Credential;
import com.saucelabs.rest.JobFactory;
import com.saucelabs.rest.UpdateJob;
import com.saucelabs.saucerest.SauceREST;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
Expand All @@ -58,10 +57,7 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -229,7 +225,7 @@ public boolean tearDown(AbstractBuild build, BuildListener listener) throws IOEx
}

private void processBuildOutput(AbstractBuild build) {
JobFactory factory = new JobFactory(new Credential(getUserName(), getApiKey()));
SauceREST sauceREST = new SauceREST(getUserName(), getApiKey());

String[] array = logParser.getLines().toArray(new String[logParser.getLines().size()]);
List<String[]> sessionIDs = SauceOnDemandReportFactory.findSessionIDs(null, array);
Expand All @@ -240,14 +236,11 @@ private void processBuildOutput(AbstractBuild build) {

String jobName = sessionId[1];
if (StringUtils.isNotBlank(jobName)) {
factory.update(id,
new UpdateJob(
jobName,
false,
Collections.<String>emptyList(),
Integer.toString(build.getNumber()),
build.getResult().equals(Result.SUCCESS),
Collections.<String, Object>emptyMap()));
Map<String, Object> updates = new HashMap<String, Object>();
updates.put("public", false);
updates.put("build", build.getNumber());
updates.put("passed", build.getResult().equals(Result.SUCCESS));
sauceREST.updateJobInfo(jobName, updates);
}
} catch (IOException e) {
logger.log(Level.WARNING, "Error while updating job " + id, e);
Expand Down Expand Up @@ -314,20 +307,14 @@ public String getUserName() {
} else {
PluginImpl p = PluginImpl.get();
if (p.isReuseSauceAuth()) {
com.saucelabs.rest.Credential storedCredentials = null;
try {
storedCredentials = new com.saucelabs.rest.Credential();
return storedCredentials.getUsername();
} catch (IOException e) {
logger.log(Level.WARNING, "Error retrieving credentials", e);
}

SauceOnDemandAuthentication storedCredentials = null;
storedCredentials = new SauceOnDemandAuthentication();
return storedCredentials.getUsername();
} else {
return p.getUsername();

}
}
return "";
}

public String getApiKey() {
Expand All @@ -336,18 +323,13 @@ public String getApiKey() {
} else {
PluginImpl p = PluginImpl.get();
if (p.isReuseSauceAuth()) {
com.saucelabs.rest.Credential storedCredentials;
try {
storedCredentials = new com.saucelabs.rest.Credential();
return storedCredentials.getKey();
} catch (IOException e) {
logger.log(Level.WARNING, "Error retrieving credentials", e);
}
SauceOnDemandAuthentication storedCredentials;
storedCredentials = new SauceOnDemandAuthentication();
return storedCredentials.getAccessKey();
} else {
return Secret.toString(p.getApiKey());
}
}
return "";
}

public String getSeleniumHost() {
Expand Down Expand Up @@ -510,7 +492,7 @@ public List<Browser> getBrowsers() {
/**
* @author Ross Rowe
*/
public class SauceOnDemandLogParser extends LineTransformationOutputStream implements Serializable {
public class SauceOnDemandLogParser extends LineTransformationOutputStream implements Serializable {

private transient OutputStream outputStream;
private transient Charset charset;
Expand Down
Expand Up @@ -23,9 +23,7 @@
*/
package hudson.plugins.sauce_ondemand;

import com.saucelabs.rest.Credential;
import com.saucelabs.rest.JobFactory;
import com.saucelabs.rest.UpdateJob;
import com.saucelabs.saucerest.SauceREST;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
Expand All @@ -40,8 +38,9 @@
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Associates Sauce OnDemand session ID to unit tests.
Expand All @@ -54,8 +53,8 @@ public SauceOnDemandReportPublisher() {
}

@Override
public SauceOnDemandReportFactory getTestData(AbstractBuild<?,?> build, Launcher launcher, BuildListener buildListener, TestResult testResult) throws IOException, InterruptedException {
JobFactory factory = new JobFactory(new Credential(PluginImpl.get().getUsername(), Secret.toString(PluginImpl.get().getApiKey())));
public SauceOnDemandReportFactory getTestData(AbstractBuild<?, ?> build, Launcher launcher, BuildListener buildListener, TestResult testResult) throws IOException, InterruptedException {
SauceREST sauceREST = new SauceREST(PluginImpl.get().getUsername(), Secret.toString(PluginImpl.get().getApiKey()));

buildListener.getLogger().println("Scanning for Sauce OnDemand test data...");
boolean hasResult = false;
Expand All @@ -71,7 +70,12 @@ public SauceOnDemandReportFactory getTestData(AbstractBuild<?,?> build, Launcher
for (String[] id : sessionIDs) {
hasResult = true;
try {
factory.update(id[0], new UpdateJob(cr.getFullName(), false, Collections.<String>emptyList(), Integer.toString(build.getNumber()), cr.isPassed(), Collections.<String, Object>emptyMap()));
Map<String, Object> updates = new HashMap<String, Object>();
updates.put("name", cr.getFullName());
updates.put("public", false);
updates.put("build", build.getNumber());
updates.put("passed", cr.isPassed());
sauceREST.updateJobInfo(id[0], updates);
} catch (IOException e) {
e.printStackTrace(buildListener.error("Error while updating job " + id));
}
Expand Down

0 comments on commit f3f6f09

Please sign in to comment.