Skip to content

Commit

Permalink
Merge pull request #147 from AngryGami/master
Browse files Browse the repository at this point in the history
[JENKINS-38669] (3/3) Call `FlowDefinition.getSCMs`
  • Loading branch information
jglick authored Dec 4, 2023
2 parents c17d180 + 58e04b7 commit b58b86e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
<scope>import</scope>
<type>pom</type>
</dependency>
<!-- TODO until in BOM -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>3817.vd20b_7e2b_692b_</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,17 +540,23 @@ public void replaceAction(@NonNull Action a) {

@NonNull
@Override public Collection<? extends SCM> getSCMs() {
Collection<? extends SCM> definedSCMs = definition != null
? definition.getSCMs()
: Collections.emptySet();
WorkflowRun b = getLastSuccessfulBuild();
if (b == null) {
b = getLastCompletedBuild();
}
if (b == null) {
return Collections.emptySet();
return definedSCMs;
}
Map<String,SCM> scms = new LinkedHashMap<>();
for (WorkflowRun.SCMCheckout co : b.checkouts(null)) {
scms.put(co.scm.getKey(), co.scm);
}
for (SCM scm : definedSCMs) {
scms.put(scm.getKey(), scm);
}
return scms.values();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,32 @@ public class WorkflowJobTest {
j.assertLogContains("second version", b2);
}

@Issue("JENKINS-38669")
@Test public void nonEmptySCMListForGitSCMJobBeforeBuild() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
CpsScmFlowDefinition def = new CpsScmFlowDefinition(new GitSCM("I don't care"), "Jenkinsfile");
assertEquals("Expecting one SCM for definition", 1, def.getSCMs().size());
p.setDefinition(def);
assertEquals("Expecting one SCM", 1, p.getSCMs().size());
}

@Issue("JENKINS-38669")
@Test public void neverBuiltSCMBasedJobMustBeTriggerableByHook() throws Exception {
sampleRepo.init();
sampleRepo.write("Jenkinsfile", "echo 'first version'");
sampleRepo.git("add", "Jenkinsfile");
sampleRepo.git("commit", "-m", "init");
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.addTrigger(new SCMTrigger(""));
p.setDefinition(new CpsScmFlowDefinition(new GitSCM(sampleRepo.toString()), "Jenkinsfile"));
j.jenkins.setQuietPeriod(0);
j.createWebClient().getPage(new WebRequest(j.createWebClient().createCrumbedUrl(p.getUrl() + "polling"), HttpMethod.POST));
j.waitUntilNoActivity();
WorkflowRun b1 = p.getLastBuild();
assertEquals(1, b1.getNumber());
j.assertLogContains("first version", b1);
}

@Test
public void addAction() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
Expand Down

0 comments on commit b58b86e

Please sign in to comment.