Skip to content

Commit

Permalink
[FIXED JENKINS-40521] Mark orphaned branch projects as unbuildable.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Feb 6, 2017
1 parent 3043437 commit 1ba8c22
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Expand Up @@ -28,7 +28,7 @@ THE SOFTWARE.
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.19</version>
<version>2.21</version>
<relativePath />
</parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down Expand Up @@ -87,7 +87,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.7</version>
<version>2.10-20170206.194854-4</version> <!-- https://github.com/jenkinsci/workflow-job-plugin/pull/37 -->
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down Expand Up @@ -230,7 +230,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.7</version>
<version>2.10-20170206.194854-4</version> <!-- https://github.com/jenkinsci/workflow-job-plugin/pull/37 -->
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
Expand Down
Expand Up @@ -27,7 +27,6 @@
import hudson.Extension;
import hudson.model.AbstractItem;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.JobPropertyDescriptor;
import hudson.security.ACL;
import hudson.security.Permission;
Expand Down Expand Up @@ -67,12 +66,7 @@ synchronized void setBranch(@Nonnull Branch branch) {
@Override public boolean hasPermission(Authentication a, Permission permission) {
// This project is managed by its parent and may not be directly configured or deleted by users.
// Note that Item.EXTENDED_READ may still be granted, so you can still see Snippet Generator, etc.
if (branch instanceof Branch.Dead && (permission == Job.BUILD)) {
// if dead, nobody, not even SYSTEM may build this job
return false;
} else if (ACL.SYSTEM.equals(a)) {
return true; // e.g., DefaultDeadBranchStrategy.runDeadBranchCleanup
} else if (permission == Item.CONFIGURE) {
if (permission == Item.CONFIGURE) {
return false;
} else if (permission == Item.DELETE && !(branch instanceof Branch.Dead)) {
// allow early manual clean-up of dead branches
Expand All @@ -84,6 +78,13 @@ synchronized void setBranch(@Nonnull Branch branch) {
};
}

@Override public Boolean isBuildable() {
if (branch instanceof Branch.Dead) {
return false;
}
return null;
}

@Extension public static class DescriptorImpl extends JobPropertyDescriptor {

@Override public String getDisplayName() {
Expand Down
Expand Up @@ -24,7 +24,9 @@

package org.jenkinsci.plugins.workflow.multibranch;

import com.cloudbees.hudson.plugins.folder.computed.DefaultOrphanedItemStrategy;
import hudson.Util;
import hudson.model.Item;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.plugins.git.util.BuildData;
Expand Down Expand Up @@ -58,6 +60,7 @@
import org.junit.ClassRule;
import org.junit.Rule;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

public class SCMBinderTest {
Expand Down Expand Up @@ -181,6 +184,7 @@ public static void assertRevisionAction(WorkflowRun build) {
r.assertLogContains("Jenkinsfile not found", b2);
}

@Issue("JENKINS-40521")
@Test public void deletedBranch() throws Exception {
sampleGitRepo.init();
// TODO GitSCMSource offers no way to set a GitSCMExtension such as CleanBeforeCheckout; work around with deleteDir
Expand All @@ -194,11 +198,14 @@ public static void assertRevisionAction(WorkflowRun build) {
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)));
mp.setOrphanedItemStrategy(new DefaultOrphanedItemStrategy(false, "", ""));
WorkflowJob p = WorkflowMultiBranchProjectTest.scheduleAndFindBranchProject(mp, "feature");
assertEquals(2, mp.getItems().size());
r.waitUntilNoActivity();
WorkflowRun b1 = p.getLastBuild();
assertEquals(1, b1.getNumber());
assertFalse(p.hasPermission(Item.DELETE));
assertTrue(p.isBuildable());
sampleGitRepo.git("checkout", "master");
sampleGitRepo.git("branch", "-D", "feature");
{ // TODO AbstractGitSCMSource.retrieve(SCMHead, TaskListener) is incorrect: after fetching remote refs into the cache,
Expand All @@ -209,6 +216,13 @@ public static void assertRevisionAction(WorkflowRun build) {
r.assertLogContains("nondeterministic checkout", b2); // SCMBinder
r.assertLogContains("Could not determine exact tip revision of feature", b2); // SCMVar
mp.scheduleBuild2(0).getFuture().get();
WorkflowMultiBranchProjectTest.showIndexing(mp);
assertEquals(2, mp.getItems().size());
assertTrue(p.hasPermission(Item.DELETE));
assertFalse(p.isBuildable());
mp.setOrphanedItemStrategy(new DefaultOrphanedItemStrategy(true, "", "0"));
mp.scheduleBuild2(0).getFuture().get();
WorkflowMultiBranchProjectTest.showIndexing(mp);
assertEquals(1, mp.getItems().size());
}

Expand Down

0 comments on commit 1ba8c22

Please sign in to comment.