Skip to content

Commit

Permalink
JENKINS-64946 : Branch indexing occurring for every multibranch job
Browse files Browse the repository at this point in the history
execution

WHAT

1. If multibranch project trigger a build for a branch then it should
build for head which is in the Observer and not for all heads that
matches the multibranch.
  • Loading branch information
skumar7322 committed Apr 21, 2022
1 parent 41c4090 commit b3d23fa
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

import static org.jenkinsci.plugins.p4.review.ReviewProp.P4_CHANGE;
Expand Down Expand Up @@ -171,10 +172,7 @@ public String getScriptPathOrDefault() {
@Override
protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHeadObserver observer, @CheckForNull SCMHeadEvent<?> event, @NonNull TaskListener listener) throws IOException, InterruptedException {
try (TempClientHelper p4 = new TempClientHelper(getOwner(), credential, listener, null)) {
List<P4SCMHead> heads = getHeads(listener);
List<P4SCMHead> tags = getTags(listener);
heads.addAll(tags);

List<P4SCMHead> heads = getP4SCMHeads(observer, listener);
for (P4SCMHead head : heads) {
logger.fine("SCM: retrieve Head: " + head);
retrieveHead(p4, head, criteria, observer, event);
Expand All @@ -184,6 +182,20 @@ protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHe
}
}

private List<P4SCMHead> getP4SCMHeads(SCMHeadObserver observer, TaskListener listener) throws Exception {
List<P4SCMHead> heads = new ArrayList<>();
Set<SCMHead> includedHead = observer.getIncludes();
if (null != includedHead) {
for (SCMHead head : includedHead) {
heads.add((P4SCMHead) head);
}
} else {
heads.addAll(getHeads(listener));
heads.addAll(getTags(listener));
}
return heads;
}

private void retrieveHead(TempClientHelper p4, P4SCMHead head, SCMSourceCriteria criteria, SCMHeadObserver observer, SCMHeadEvent<?> event) throws Exception {
P4Path p4Path = head.getPath();
Workspace workspace = getWorkspace(p4Path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,23 @@
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -170,6 +176,36 @@ public void testExcludesWithClassic() throws Exception {
assertEquals(1, multi.getItems().size());
}

@Test
public void testAJobShouldNotCreateClientForOtherJobsInAMultibranchProject() throws Exception {

String project = "multi";
String base = "//depot/" + project;
String[] branches = new String[]{"br1", "br2"};

sampleProject(base, branches, "Jenkinsfile");

String format = "jenkins-${NODE_NAME}-${JOB_NAME}";
String includes = base + "/...";
BranchesScmSource source = new BranchesScmSource(CREDENTIAL, includes, null, format);

WorkflowMultiBranchProject multi = jenkins.jenkins.createProject(WorkflowMultiBranchProject.class, project);
multi.getSourcesList().add(new BranchSource(source));
multi.scheduleBuild2(0);
jenkins.waitUntilNoActivity();

WorkflowJob job = multi.getItem("br1");
File logFile = Objects.requireNonNull(job).getLastBuild().getLogFile();
BufferedReader br = new BufferedReader(new FileReader(logFile));
StringBuilder builder = new StringBuilder();
String line;
while((line = br.readLine()) !=null){
builder.append(line);
}
assertTrue("A Job should include its branch in console logs",builder.toString().contains("//depot/multi/br1"));
assertFalse("A job should not include other job branch in console logs",builder.toString().contains("//depot/multi/br2"));
}

@Test
public void testExcludesWithSwarm() throws Exception {

Expand Down

0 comments on commit b3d23fa

Please sign in to comment.