Skip to content

Commit

Permalink
Merge 6dc74f5 into d1c340a
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandella committed Aug 10, 2015
2 parents d1c340a + 6dc74f5 commit 7506740
Show file tree
Hide file tree
Showing 28 changed files with 637 additions and 217 deletions.
9 changes: 5 additions & 4 deletions phabricator-plugin.iml
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
<orderEntry type="library" scope="TEST" name="Maven: org.apache.maven.wagon:wagon-http-shared:2.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apache.jackrabbit:jackrabbit-webdav:2.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jenkins-ci:SECURITY-144-compat:1.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.3" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.3" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.3" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.6" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.4.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.1" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.9" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.plugins:cobertura:1.9.6" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.main:maven-plugin:1.424" level="project" />
<orderEntry type="library" name="Maven: org.jenkins-ci.main.maven:maven-agent:1.2" level="project" />
Expand Down Expand Up @@ -130,6 +130,7 @@
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apache.httpcomponents:httpclient:tests:4.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jenkins-ci.main:jenkins-war:war:1.609" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jenkins-ci.main:jenkins-core:1.609" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jenkins-ci.plugins.icon-shim:icon-set:1.0.5" level="project" />
Expand Down
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3</version>
<version>4.4.1</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -96,6 +96,14 @@
<scope>test</scope>
<version>4.12</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import hudson.plugins.cobertura.targets.CoverageMetric;
import hudson.plugins.cobertura.targets.CoverageResult;

public class CommentBuilder {
class CommentBuilder {
private static final String UBERALLS_TAG = "uberalls";
private final Logger logger;
private final CoverageResult currentCoverage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import java.util.List;

public class ConduitCredentialsDescriptor {
public static List<ConduitCredentials> availableCredentials(Job owner) {
private static List<ConduitCredentials> availableCredentials(Job owner) {
return CredentialsProvider.lookupCredentials(
ConduitCredentials.class,
owner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

package com.uber.jenkins.phabricator;

import com.uber.jenkins.phabricator.conduit.ArcanistClient;
import com.uber.jenkins.phabricator.conduit.ArcanistUsageException;
import com.uber.jenkins.phabricator.conduit.ConduitAPIClient;
import com.uber.jenkins.phabricator.conduit.ConduitAPIException;
import com.uber.jenkins.phabricator.conduit.Differential;
import com.uber.jenkins.phabricator.conduit.DifferentialClient;
import com.uber.jenkins.phabricator.credentials.ConduitCredentials;
import com.uber.jenkins.phabricator.tasks.ApplyPatchTask;
import com.uber.jenkins.phabricator.tasks.Task;
import com.uber.jenkins.phabricator.utils.CommonUtils;
import com.uber.jenkins.phabricator.utils.Logger;
import hudson.EnvVars;
Expand All @@ -36,9 +38,12 @@
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import java.util.*;
import java.util.Arrays;

public class PhabricatorBuildWrapper extends BuildWrapper {
private static final String CONDUIT_TAG = "conduit";
private static final String DEFAULT_GIT_PATH = "git";

private final boolean createCommit;
private final boolean applyToMaster;
private final boolean uberDotArcanist;
Expand All @@ -52,6 +57,7 @@ public PhabricatorBuildWrapper(boolean createCommit, boolean applyToMaster, bool
this.showBuildStartedMessage = showBuildStartedMessage;
}

/** {@inheritDoc} */
@Override
public Environment setUp(AbstractBuild build,
Launcher launcher,
Expand All @@ -62,105 +68,71 @@ public Environment setUp(AbstractBuild build,
return this.ignoreBuild(logger, "No environment variables found?!");
}

final String arcPath = this.getArcPath();

final Map<String, String> envAdditions = new HashMap<String, String>();
envAdditions.put(PhabricatorPlugin.ARCANIST_PATH, arcPath);

final String conduitToken = this.getConduitToken(build.getParent(), logger);

String diffID = environment.get(PhabricatorPlugin.DIFFERENTIAL_ID_FIELD);
if (CommonUtils.isBlank(diffID)) {
this.addShortText(build);
this.ignoreBuild(logger, "No differential ID found.");
} else {
LauncherFactory starter = new LauncherFactory(launcher, environment, listener.getLogger(), build.getWorkspace());

if (uberDotArcanist) {
int npmCode = starter.launch()
.cmds(Arrays.asList("npm", "install", "uber-dot-arcanist"))
.stdout(logger.getStream())
.join();

if (npmCode != 0) {
logger.warn("uber-dot-arcanist", "Got non-zero exit code installing uber-dot-arcanist from npm: " + npmCode);
}
}

DifferentialClient diffClient = new DifferentialClient(diffID, starter, conduitToken, arcPath);
Differential diff;
try {
diff = new Differential(diffClient.fetchDiff());
diff.decorate(build, this.getPhabricatorURL(build.getParent()));

logger.info("arcanist", "Applying patch for differential");

// Post a silent notification if option is enabled
if (showBuildStartedMessage) {
diffClient.postComment(diff.getRevisionID(false), diff.getBuildStartedMessage(environment));
}
} catch (ArcanistUsageException e) {
logger.warn("arcanist", "Unable to apply patch");
logger.warn("arcanist", e.getMessage());
return null;
}
return new Environment(){};
}

String baseCommit = "origin/master";
if (!applyToMaster) {
baseCommit = diff.getBaseCommit();
}
LauncherFactory starter = new LauncherFactory(launcher, environment, listener.getLogger(), build.getWorkspace());

int resetCode = starter.launch()
.cmds(Arrays.asList("git", "reset", "--hard", baseCommit))
if (uberDotArcanist) {
int npmCode = starter.launch()
.cmds(Arrays.asList("npm", "install", "uber-dot-arcanist"))
.stdout(logger.getStream())
.join();

if (resetCode != 0) {
logger.warn("arcanist", "Got non-zero exit code resetting to base commit " + baseCommit + ": " + resetCode);
if (npmCode != 0) {
logger.warn("uber-dot-arcanist", "Got non-zero exit code installing uber-dot-arcanist from npm: " + npmCode);
}
}

// Clean workspace, otherwise `arc patch` may fail
starter.launch()
.stdout(logger.getStream())
.cmds(Arrays.asList("git", "clean", "-fd", "-f"))
.join();
ConduitAPIClient conduitClient;
try {
conduitClient = getConduitClient(build.getParent(), logger);
} catch (ConduitAPIException e) {
e.printStackTrace(logger.getStream());
logger.warn(CONDUIT_TAG, e.getMessage());
return null;
}

// Update submodules recursively.
starter.launch()
.stdout(logger.getStream())
.cmds(Arrays.asList("git", "submodule", "update", "--init", "--recursive"))
.join();
DifferentialClient diffClient = new DifferentialClient(diffID, conduitClient);
Differential diff;
try {
diff = new Differential(diffClient.fetchDiff());
diff.decorate(build, this.getPhabricatorURL(build.getParent()));

List<String> params = new ArrayList<String>(Arrays.asList("--nobranch", "--diff", diffID));
if (!createCommit) {
params.add("--nocommit");
}
logger.info(CONDUIT_TAG, "Fetching differential from Conduit API");

ArcanistClient arc = new ArcanistClient(
arcPath,
"patch",
null,
conduitToken,
params.toArray(new String[params.size()]));
// Post a silent notification if option is enabled
if (showBuildStartedMessage) {
diffClient.postComment(diff.getRevisionID(false), diff.getBuildStartedMessage(environment));
}
} catch (ConduitAPIException e) {
e.printStackTrace(logger.getStream());
logger.warn(CONDUIT_TAG, "Unable to apply patch");
logger.warn(CONDUIT_TAG, e.getMessage());
return null;
}

int result = arc.callConduit(starter.launch(), logger.getStream());
String baseCommit = "origin/master";
if (!applyToMaster) {
baseCommit = diff.getBaseCommit();
}

if (result != 0) {
logger.warn("arcanist", "Error applying arc patch; got non-zero exit code " + result);
return null;
}
final String conduitToken = this.getConduitToken(build.getParent(), logger);
Task.Result result = new ApplyPatchTask(
logger, starter, baseCommit, diffID, conduitToken, getArcPath(),
DEFAULT_GIT_PATH, createCommit
).run();

if (result != Task.Result.SUCCESS) {
logger.warn("arcanist", "Error applying arc patch; got non-zero exit code " + result);
return null;
}

return new Environment() {
@Override
public void buildEnvVars(Map<String, String> env) {
// A little roundabout, but allows us to do overrides per
// how EnvVars#override works (PATH+unique=/foo/bar)
EnvVars envVars = new EnvVars(env);
envVars.putAll(envAdditions);
env.putAll(envVars);
}
};
return new Environment(){};
}

private void addShortText(final AbstractBuild build) {
Expand All @@ -172,6 +144,18 @@ private Environment ignoreBuild(Logger logger, String message) {
return new Environment(){};
}

private ConduitAPIClient getConduitClient(Job owner, Logger logger) throws ConduitAPIException {
ConduitCredentials credentials = getConduitCredentials(owner);
if (credentials == null) {
throw new ConduitAPIException("No credentials configured for conduit");
}
return new ConduitAPIClient(credentials.getUrl(), getConduitToken(owner, logger));
}

private ConduitCredentials getConduitCredentials(Job owner) {
return getDescriptor().getCredentials(owner);
}

/**
* This is used in config.jelly to populate the state of the checkbox
*/
Expand All @@ -195,16 +179,16 @@ public boolean isShowBuildStartedMessage() {
return showBuildStartedMessage;
}

public String getPhabricatorURL(Job owner) {
ConduitCredentials credentials = this.getDescriptor().getCredentials(owner);
private String getPhabricatorURL(Job owner) {
ConduitCredentials credentials = getConduitCredentials(owner);
if (credentials != null) {
return credentials.getUrl();
}
return this.getDescriptor().getConduitURL();
}

public String getConduitToken(Job owner, Logger logger) {
ConduitCredentials credentials = this.getDescriptor().getCredentials(owner);
private String getConduitToken(Job owner, Logger logger) {
ConduitCredentials credentials = getConduitCredentials(owner);
if (credentials != null) {
return credentials.getToken().getPlainText();
}
Expand All @@ -216,7 +200,7 @@ public String getConduitToken(Job owner, Logger logger) {
* Return the path to the arcanist executable
* @return a string, fully-qualified or not, could just be "arc"
*/
public String getArcPath() {
private String getArcPath() {
final String providedPath = this.getDescriptor().getArcPath();
if (CommonUtils.isBlank(providedPath)) {
return "arc";
Expand Down
Loading

0 comments on commit 7506740

Please sign in to comment.