Skip to content

Commit

Permalink
Add Subversion support (#214)
Browse files Browse the repository at this point in the history
* Let arc can execute in customized path

* Select arguments for arc based on SCM

* Chase constructor change

* Set default values for workDir and scmType to fix unit tests

* Add a simple test for subversion
  • Loading branch information
lwhsu authored and Aiden Scandella committed Apr 13, 2017
1 parent 0f711f3 commit 7ab3436
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 29 deletions.
Expand Up @@ -33,6 +33,7 @@
import com.uber.jenkins.phabricator.utils.CommonUtils;
import com.uber.jenkins.phabricator.utils.Logger;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
Expand All @@ -48,6 +49,7 @@
import hudson.util.RunList;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.IOException;
import java.util.HashMap;
Expand All @@ -66,6 +68,8 @@ public class PhabricatorBuildWrapper extends BuildWrapper {
private final boolean skipForcedClean;
private final boolean createBranch;
private final boolean patchWithForceFlag;
private String workDir;
private String scmType;

@DataBoundConstructor
public PhabricatorBuildWrapper(boolean createCommit, boolean applyToMaster,
Expand All @@ -76,6 +80,18 @@ public PhabricatorBuildWrapper(boolean createCommit, boolean applyToMaster,
this.skipForcedClean = skipForcedClean;
this.createBranch = createBranch;
this.patchWithForceFlag = patchWithForceFlag;
this.workDir = null;
this.scmType = "git";
}

@DataBoundSetter
public void setWorkDir(final String workDir) {
this.workDir = workDir;
}

@DataBoundSetter
public void setScmType(final String scmType) {
this.scmType = scmType;
}

/** {@inheritDoc} */
Expand All @@ -99,7 +115,13 @@ public Environment setUp(AbstractBuild build,
return new Environment(){};
}

LauncherFactory starter = new LauncherFactory(launcher, environment, listener.getLogger(), build.getWorkspace());
FilePath arcWorkPath;
if (this.workDir != null && this.workDir.length() > 0) {
arcWorkPath = build.getWorkspace().child(workDir);
} else {
arcWorkPath = build.getWorkspace();
}
LauncherFactory starter = new LauncherFactory(launcher, environment, listener.getLogger(), arcWorkPath);

ConduitAPIClient conduitClient;
try {
Expand Down Expand Up @@ -150,7 +172,7 @@ public Environment setUp(AbstractBuild build,
Task.Result result = new ApplyPatchTask(
logger, starter, baseCommit, diffID, conduitToken, getArcPath(),
DEFAULT_GIT_PATH, createCommit, skipForcedClean, createBranch,
patchWithForceFlag
patchWithForceFlag, scmType
).run();

if (result != Task.Result.SUCCESS) {
Expand Down Expand Up @@ -251,6 +273,16 @@ public boolean isPatchWithForceFlag() {
return patchWithForceFlag;
}

@SuppressWarnings("unused")
public String getWorkDir() {
return workDir;
}

@SuppressWarnings("unused")
public String getScmType() {
return scmType;
}

private String getPhabricatorURL(Job owner) {
ConduitCredentials credentials = getConduitCredentials(owner);
if (credentials != null) {
Expand Down
Expand Up @@ -42,11 +42,13 @@ public class ApplyPatchTask extends Task {
private final boolean skipForcedClean;
private final boolean createBranch;
private final boolean patchWithForceFlag;
private final String scmType;

public ApplyPatchTask(Logger logger, LauncherFactory starter, String baseCommit,
String diffID, String conduitToken, String arcPath,
String gitPath, boolean createCommit, boolean skipForcedClean,
boolean createBranch, boolean patchWithForceFlag) {
boolean createBranch, boolean patchWithForceFlag,
String scmType) {
super(logger);
this.starter = starter;
this.baseCommit = baseCommit;
Expand All @@ -58,6 +60,7 @@ public ApplyPatchTask(Logger logger, LauncherFactory starter, String baseCommit,
this.skipForcedClean = skipForcedClean;
this.createBranch = createBranch;
this.patchWithForceFlag = patchWithForceFlag;
this.scmType = scmType;

this.logStream = logger.getStream();
}
Expand All @@ -84,36 +87,42 @@ protected void setup() {
@Override
protected void execute() {
try {
int exitCode = starter.launch()
.cmds(Arrays.asList(gitPath, "reset", "--hard", baseCommit))
.stdout(logStream)
.join();

if (exitCode != 0) {
info("Got non-zero exit code resetting to base commit " + baseCommit + ": " + exitCode);
}

if (!skipForcedClean) {
// Clean workspace, otherwise `arc patch` may fail
int exitCode;
if (this.scmType.equals("git")) {
exitCode = starter.launch()
.cmds(Arrays.asList(gitPath, "reset", "--hard", baseCommit))
.stdout(logStream)
.join();

if (exitCode != 0) {
info("Got non-zero exit code resetting to base commit " + baseCommit + ": " + exitCode);
}

if (!skipForcedClean) {
// Clean workspace, otherwise `arc patch` may fail
starter.launch()
.stdout(logStream)
.cmds(Arrays.asList(gitPath, "clean", "-fd", "-f"))
.join();
}

// Update submodules recursively.
starter.launch()
.stdout(logStream)
.cmds(Arrays.asList(gitPath, "clean", "-fd", "-f"))
.join();
.stdout(logStream)
.cmds(Arrays.asList(gitPath, "submodule", "update", "--init", "--recursive"))
.join();
}

// Update submodules recursively.
starter.launch()
.stdout(logStream)
.cmds(Arrays.asList(gitPath, "submodule", "update", "--init", "--recursive"))
.join();

List<String> arcPatchParams = new ArrayList<String>(Arrays.asList("--diff", diffID));
if (!createCommit) {
arcPatchParams.add("--nocommit");
}

if (!createBranch) {
arcPatchParams.add("--nobranch");
if (this.scmType.equals("git")) {
if (!createCommit) {
arcPatchParams.add("--nocommit");
}

if (!createBranch) {
arcPatchParams.add("--nobranch");
}
}

if (patchWithForceFlag) {
Expand Down
@@ -1,5 +1,9 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry title="SCM type" field="scmType" description="SCM used in this job">
<f:radio name="scmType" title="git" value="git" checked="${instance.scmType == 'git' || instance.scmType == null}" />
<f:radio name="scmType" title="svn" value="svn" checked="${instance.scmType == 'svn'}" />
</f:entry>
<f:entry title="Create git commit" field="createCommit"
description="Create a git commit with the patch">
<f:checkbox />
Expand All @@ -20,4 +24,8 @@
description="Skips running sanity checks (like base commit) when applying patch">
<f:checkbox default="false" />
</f:entry>
<f:entry title="Work directory" field="workDir"
description="Directory arc works at">
<f:textbox default="" />
</f:entry>
</j:jelly>
Expand Up @@ -38,6 +38,19 @@ public void testApplyPatchWithValidArc() throws Exception {
assertEquals(Task.Result.SUCCESS, result);
}

@Test
public void testApplyPatchForSvnWithValidArc() throws Exception {
ApplyPatchTask task = new ApplyPatchTask(
TestUtils.getDefaultLogger(),
TestUtils.createLauncherFactory(j),
TestUtils.TEST_SHA,
TestUtils.TEST_DIFFERENTIAL_ID,
TestUtils.TEST_CONDUIT_TOKEN,
"echo", "true", false, false, false, false, "svn");
Task.Result result = task.run();
assertEquals(Task.Result.SUCCESS, result);
}

@Test
public void testApplyPatchWithInvalidArc() throws Exception {
ApplyPatchTask task = getTask("false", "echo");
Expand All @@ -63,7 +76,8 @@ private ApplyPatchTask getTask(String arcPath, String gitPath) throws Exception
false, // createCommit
false, // skipForcedClean
false, // createBranch
false // patchWithForceFlag
false, // patchWithForceFlag
"git" // scmType
);
}
}

0 comments on commit 7ab3436

Please sign in to comment.