Skip to content

Commit

Permalink
[JENKINS-33273] Make SCMBinder use SCMFileSystem where available.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Jan 13, 2017
1 parent 973fc79 commit 683cce9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Expand Up @@ -80,12 +80,12 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>2.0.1-beta-1</version>
<version>2.0.1-beta-3</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>branch-api</artifactId>
<version>2.0.0-beta-1</version>
<version>2.0.0-beta-3</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -162,7 +162,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>3.0.0</version>
<version>3.0.2-beta-2</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand All @@ -180,7 +180,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>3.0.0</version>
<version>3.0.2-beta-2</version>
<classifier>tests</classifier>
<scope>test</scope>
<exclusions>
Expand Down
Expand Up @@ -83,6 +83,7 @@ public static class Execution extends AbstractSynchronousNonBlockingStepExecutio

@Override protected String run() throws Exception {
Job<?, ?> job = build.getParent();
// TODO try using SCMFileSystem
// Adapted from CpsScmFlowDefinition:
Node node = Jenkins.getActiveInstance();
FilePath dir;
Expand Down
Expand Up @@ -25,19 +25,23 @@
package org.jenkinsci.plugins.workflow.multibranch;

import hudson.Extension;
import hudson.Functions;
import hudson.model.Action;
import hudson.model.Descriptor;
import hudson.model.DescriptorVisibilityFilter;
import hudson.model.ItemGroup;
import hudson.model.Queue;
import hudson.model.TaskListener;
import hudson.scm.SCM;
import java.io.IOException;
import java.util.List;
import jenkins.branch.Branch;
import jenkins.scm.api.SCMFileSystem;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMRevisionAction;
import jenkins.scm.api.SCMSource;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
import org.jenkinsci.plugins.workflow.flow.FlowDefinitionDescriptor;
Expand Down Expand Up @@ -75,8 +79,22 @@ class SCMBinder extends FlowDefinition {
SCMRevision tip = scmSource.fetch(head, listener);
SCM scm;
if (tip != null) {
scm = scmSource.build(head, scmSource.getTrustedRevision(tip, listener));
build.addAction(new SCMRevisionAction(tip));
SCMRevision rev = scmSource.getTrustedRevision(tip, listener);
SCMFileSystem fs = SCMFileSystem.of(scmSource, head, rev);
if (fs != null) { // JENKINS-33273; TODO perhaps this should be pushed down into CpsScmFlowDefinition?
listener.getLogger().println("Performing lightweight checkout");
String script = null;
try {
script = fs.child(WorkflowBranchProjectFactory.SCRIPT).contentAsString();
} catch (IOException | InterruptedException x) {
listener.error("Could not do lightweight checkout, falling back to heavyweight").println(Functions.printThrowable(x).trim());
}
if (script != null) {
return new CpsFlowDefinition(script, true).create(handle, listener, actions);
}
}
scm = scmSource.build(head, rev);
} else {
listener.error("Could not determine exact tip revision of " + branch.getName() + "; falling back to nondeterministic checkout");
// Build might fail later anyway, but reason should become clear: for example, branch was deleted before indexing could run.
Expand All @@ -96,6 +114,7 @@ class SCMBinder extends FlowDefinition {
/** Want to display this in the r/o configuration for a branch project, but not offer it on standalone jobs or in any other context. */
@Extension public static class HideMeElsewhere extends DescriptorVisibilityFilter {

@SuppressWarnings("rawtypes")
@Override public boolean filter(Object context, Descriptor descriptor) {
if (descriptor instanceof DescriptorImpl) {
return context instanceof WorkflowJob && ((WorkflowJob) context).getParent() instanceof WorkflowMultiBranchProject;
Expand Down
Expand Up @@ -36,9 +36,7 @@
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import jenkins.branch.BranchProperty;
import jenkins.branch.BranchSource;
import jenkins.branch.DefaultBranchPropertyStrategy;
import jenkins.model.Jenkins;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.plugins.git.GitSCMSource;
Expand Down Expand Up @@ -79,13 +77,14 @@ public class SCMBinderTest {
sampleGitRepo.git("add", "Jenkinsfile");
sampleGitRepo.git("commit", "--all", "--message=flow");
WorkflowMultiBranchProject mp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false)));
WorkflowJob p = WorkflowMultiBranchProjectTest.scheduleAndFindBranchProject(mp, "master");
SemaphoreStep.waitForStart("wait/1", null);
WorkflowRun b1 = p.getLastBuild();
assertNotNull(b1);
assertEquals(1, b1.getNumber());
assertRevisionAction(b1);
r.assertLogContains("Performing lightweight checkout", b1);
sampleGitRepo.write("Jenkinsfile", "node {checkout scm; echo readFile('file').toUpperCase()}");
sampleGitRepo.write("file", "subsequent content");
sampleGitRepo.git("commit", "--all", "--message=tweaked");
Expand Down Expand Up @@ -115,11 +114,14 @@ public static void assertRevisionAction(WorkflowRun build) {
assertNotNull(revisionAction);
SCMRevision revision = revisionAction.getRevision();
assertEquals(AbstractGitSCMSource.SCMRevisionImpl.class, revision.getClass());
Set<String> expected = new HashSet<String>();
for (BuildData data : build.getActions(BuildData.class)) {
expected.add(data.lastBuild.marked.getSha1().getName());
Set<String> expected = new HashSet<>();
List<BuildData> buildDataActions = build.getActions(BuildData.class);
if (!buildDataActions.isEmpty()) { // i.e., we have run at least one checkout step, or done a heavyweight checkout to get a single file
for (BuildData data : buildDataActions) {
expected.add(data.lastBuild.marked.getSha1().getName());
}
assertThat(expected, hasItem(((AbstractGitSCMSource.SCMRevisionImpl) revision).getHash()));
}
assertThat(expected, hasItem(((AbstractGitSCMSource.SCMRevisionImpl) revision).getHash()));
}

@Test public void exactRevisionSubversion() throws Exception {
Expand All @@ -132,7 +134,7 @@ public static void assertRevisionAction(WorkflowRun build) {
sampleSvnRepo.svnkit("add", sampleSvnRepo.wc() + "/Jenkinsfile");
sampleSvnRepo.svnkit("commit", "--message=flow", sampleSvnRepo.wc());
WorkflowMultiBranchProject mp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
mp.getSourcesList().add(new BranchSource(new SubversionSCMSource(null, sampleSvnRepo.prjUrl(), null, null, null), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
mp.getSourcesList().add(new BranchSource(new SubversionSCMSource(null, sampleSvnRepo.prjUrl())));
WorkflowJob p = WorkflowMultiBranchProjectTest.scheduleAndFindBranchProject(mp, "trunk");
SemaphoreStep.waitForStart("wait/1", null);
WorkflowRun b1 = p.getLastBuild();
Expand Down Expand Up @@ -167,7 +169,7 @@ public static void assertRevisionAction(WorkflowRun build) {
sampleGitRepo.git("add", "Jenkinsfile");
sampleGitRepo.git("commit", "--all", "--message=flow");
WorkflowMultiBranchProject mp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false)));
WorkflowJob p = WorkflowMultiBranchProjectTest.scheduleAndFindBranchProject(mp, "master");
assertEquals(1, mp.getItems().size());
r.waitUntilNoActivity();
Expand All @@ -191,7 +193,7 @@ public static void assertRevisionAction(WorkflowRun build) {
sampleGitRepo.git("add", "somefile");
sampleGitRepo.git("commit", "--all", "--message=tweaked");
WorkflowMultiBranchProject mp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false)));
WorkflowJob p = WorkflowMultiBranchProjectTest.scheduleAndFindBranchProject(mp, "feature");
assertEquals(2, mp.getItems().size());
r.waitUntilNoActivity();
Expand All @@ -217,7 +219,7 @@ public static void assertRevisionAction(WorkflowRun build) {
sampleGitRepo.git("add", "Jenkinsfile");
sampleGitRepo.git("commit", "--all", "--message=flow");
WorkflowMultiBranchProject mp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
mp.getSourcesList().add(new BranchSource(new WarySource(null, sampleGitRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
mp.getSourcesList().add(new BranchSource(new WarySource(null, sampleGitRepo.toString(), "", "*", "", false)));
WorkflowJob p = WorkflowMultiBranchProjectTest.scheduleAndFindBranchProject(mp, "master");
r.waitUntilNoActivity();
WorkflowRun b = p.getLastBuild();
Expand Down

0 comments on commit 683cce9

Please sign in to comment.