Skip to content

Commit

Permalink
Merge pull request #343 from hannes-ucsc/JENKINS-29796
Browse files Browse the repository at this point in the history
[JENKINS-29796] Multiple refspecs should be matched using OR, not AND
  • Loading branch information
MarkEWaite committed Aug 6, 2015
2 parents af01132 + e67c39c commit 9abd397
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/main/java/hudson/plugins/git/GitSCM.java
Expand Up @@ -591,16 +591,19 @@ private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher
Iterator<Entry<String, ObjectId>> it = heads.entrySet().iterator();
while (it.hasNext()) {
String head = it.next().getKey();
boolean match = false;
for (RefSpec spec : refSpecs) {
if (!spec.matchSource(head)) {
listener.getLogger().println("Ignoring " + head + " as it doesn't match configured refspecs");
it.remove();
if (spec.matchSource(head)) {
match = true;
break;
}
}
if (!match) {
listener.getLogger().println("Ignoring " + head + " as it doesn't match any of the configured refspecs");
it.remove();
}
}


for (BranchSpec branchSpec : getBranches()) {
for (Entry<String, ObjectId> entry : heads.entrySet()) {
final String head = entry.getKey();
Expand Down
45 changes: 38 additions & 7 deletions src/test/java/hudson/plugins/git/SCMTriggerTest.java
Expand Up @@ -33,6 +33,7 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.TemporaryDirectoryAllocator;

public abstract class SCMTriggerTest extends AbstractGitProject
Expand Down Expand Up @@ -224,13 +225,37 @@ public void testCommitAsBranchSpec() throws Exception {
"detached");
}


public void check(ZipFile repoZip, Properties commits, String branchSpec,
@Issue("JENKINS-29796")
@Test
public void testMultipleRefspecs() throws Exception {
final String remote = prepareRepo(namespaceRepoZip);
final UserRemoteConfig remoteConfig = new UserRemoteConfig(remote, "origin",
"+refs/pull/*:refs/remotes/origin/pr/* +refs/heads/*:refs/remotes/origin/*", null);
// First, build the master branch
String branchSpec = "refs/heads/master";
FreeStyleProject project = setupProject(asList(remoteConfig),
asList(new BranchSpec(branchSpec)),
//empty scmTriggerSpec, SCMTrigger triggered manually
"", isDisableRemotePoll(), getGitClient());
triggerSCMTrigger(project.getTrigger(SCMTrigger.class));
FreeStyleBuild build1 = waitForBuildFinished(project, 1, 60000);
assertNotNull("Job has not been triggered", build1);

// Now switch request a different branch
GitSCM scm = (GitSCM) project.getScm();
scm.getBranches().set(0,new BranchSpec("b_namespace3/master"));
TaskListener listener = StreamTaskListener.fromStderr();

// Since the new branch has an additional commit, polling should report changes. Without the fix for
// JENKINS-29796, this assertion fails.
PollingResult poll = project.poll(listener);
assertEquals("Expected and actual polling results disagree", true, poll.hasChanges());
}

public void check(ZipFile repoZip, Properties commits, String branchSpec,
String expected_GIT_COMMIT, String expected_GIT_BRANCH) throws Exception {
File tempRemoteDir = tempAllocator.allocate();
extract(repoZip, tempRemoteDir);
final String remote = tempRemoteDir.getAbsolutePath();

String remote = prepareRepo(repoZip);

FreeStyleProject project = setupProject(asList(new UserRemoteConfig(remote, null, null, null)),
asList(new BranchSpec(branchSpec)),
//empty scmTriggerSpec, SCMTrigger triggered manually
Expand All @@ -257,7 +282,13 @@ public void check(ZipFile repoZip, Properties commits, String branchSpec,
assertEquals("Unexpected GIT_BRANCH",
expected_GIT_BRANCH, build1.getEnvironment(null).get("GIT_BRANCH"));
}


private String prepareRepo(ZipFile repoZip) throws IOException {
File tempRemoteDir = tempAllocator.allocate();
extract(repoZip, tempRemoteDir);
return tempRemoteDir.getAbsolutePath();
}

private Future<Void> triggerSCMTrigger(final SCMTrigger trigger)
{
if(trigger == null) return null;
Expand Down

0 comments on commit 9abd397

Please sign in to comment.