Skip to content

Commit 863b170

Browse files
committed
LibraryConfiguration: defaultedVersionSCM(): avoid WorkflowJob.getSCMs(), it does not give the answers we are looking for
1 parent ba01128 commit 863b170

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryConfiguration.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
import hudson.model.TaskListener;
3838
import hudson.scm.SCM;
3939
import hudson.util.FormValidation;
40+
import jenkins.branch.Branch;
4041
import jenkins.model.Jenkins;
42+
import jenkins.scm.api.SCMSourceOwner;
4143
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
4244
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
4345
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
@@ -274,34 +276,41 @@ private String defaultedVersionSCM(@NonNull Run<?, ?> run, @NonNull TaskListener
274276
}
275277

276278
if (scm0 == null) {
277-
Collection<SCM> wjscms = (Collection<SCM>) ((WorkflowJob)runParent).getSCMs();;
278-
if (wjscms.isEmpty()) {
279+
// WARNING: the WorkflowJob.getSCMs() does not return SCMs
280+
// associated with the current build configuration, but
281+
// rather those that were associated with previous runs
282+
// for different branches (with lastSuccessfulBuild on top)!
283+
BranchJobProperty property = ((WorkflowJob)runParent).getProperty(BranchJobProperty.class);
284+
if (property == null) { // && !(runParent.getParent() instanceof SCMSourceOwner)) {
279285
if (logger != null) {
280286
logger.println("defaultedVersion(): " +
281287
"WorkflowJob '" +
282288
runParent.getClass().getName() +
283289
"' is not associated with any SCMs");
284290
}
285291
} else {
292+
Branch pipelineBranch = property.getBranch();
286293
if (logger != null) {
287294
logger.println("defaultedVersion(): " +
288295
"inspecting WorkflowJob '" +
289296
runParent.getClass().getName() +
290297
"' for SCMs it might use");
291298
}
292-
for (SCM scmN : wjscms) {
299+
if (pipelineBranch != null) {
293300
if (logger != null) {
294-
logger.println("defaultedVersion(): inspecting SCM '" +
295-
scmN.getClass().getName() +
296-
"': " + scmN.toString());
301+
logger.println("defaultedVersion(): inspecting pipelineBranch '" +
302+
pipelineBranch.getClass().getName() +
303+
"': " + pipelineBranch.toString());
297304
}
298-
if ("hudson.plugins.git.GitSCM".equals(scmN.getClass().getName())) {
305+
/*
306+
if ("hudson.plugins.git.GitSCM".equals(scmN.getClass().getName())) {
299307
// The best we can do here is accept
300308
// the first seen SCM (with branch
301309
// support which we know how to query).
302310
scm0 = scmN;
303311
break;
304312
}
313+
*/
305314
}
306315
}
307316
}

0 commit comments

Comments
 (0)