Ok, sorry for a delay. Now I have my fix working and it involves changes to 3 plugins:
workflow-api
diff --git a/src/main/java/org/jenkinsci/plugins/workflow/flow/FlowDefinition.java b/src/main/java/org/jenkinsci/plugins/workflow/flow/FlowDefinition.java
index 55c29c7..ef71822 100644
--- a/src/main/java/org/jenkinsci/plugins/workflow/flow/FlowDefinition.java
+++ b/src/main/java/org/jenkinsci/plugins/workflow/flow/FlowDefinition.java
@@ -29,12 +29,15 @@ import hudson.Util;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Action;
import hudson.model.TaskListener;
+import hudson.scm.SCM;
import hudson.util.LogTaskListener;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.List;
+import java.util.Collection;
+import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -80,4 +83,7 @@ public abstract class FlowDefinition extends AbstractDescribableImpl<FlowDefinit
return (FlowDefinitionDescriptor) super.getDescriptor();
}
+ public Collection<? extends SCM> getSCMs() {
+ return Collections.emptyList();
+ }
}
Here I've added new method to FlowDefinition class that could be overridden by subclasses and by default it returns empty collection. This is meant to help subclasses of FlowDefinition to provide list of SCMs that it might be aware of.
workflow-cps
diff --git a/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsScmFlowDefinition.java b/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsScmFlowDefinition.java
index 6e665a8..6fcd729 100644
--- a/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsScmFlowDefinition.java
+++ b/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsScmFlowDefinition.java
@@ -46,6 +46,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMFileSystem;
@@ -82,6 +83,11 @@ public class CpsScmFlowDefinition extends FlowDefinition {
return scm;
}
+ @Override
+ public Collection<? extends SCM> getSCMs() {
+ return Collections.singletonList(scm);
+ }
+
public String getScriptPath() {
return scriptPath;
}
Here I actually override getSCMs method from FlowDefinition for case when we are dealing with CpsScmFlowDefinition - i.e. definition of the flow that actually known to be based on SCM.
For definitions that are based of inline script we can't tell if there are any SCM used until first build and this behavior is preserved.
workflow-job
diff --git a/src/main/java/org/jenkinsci/plugins/workflow/job/WorkflowJob.java b/src/main/java/org/jenkinsci/plugins/workflow/job/WorkflowJob.java
index 57ea992..3d0c84d 100644
--- a/src/main/java/org/jenkinsci/plugins/workflow/job/WorkflowJob.java
+++ b/src/main/java/org/jenkinsci/plugins/workflow/job/WorkflowJob.java
@@ -526,17 +526,23 @@ public final class WorkflowJob extends Job<WorkflowJob,WorkflowRun> implements L
}
@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();
}
And finally here I use getSCMs method from FlowDefinition to return known SCMs when there were no build happened yet.
I can try to make pull requests from all of the above, though I don't know how to do that for three projects simultaneously and consistently. Maybe markewaite can help me with that somehow?
I also don't understand how to write a test that cover three plugins at same time.
Not really knowing anything at all about the inner workings of jenkins, I started digging around in the hopes that I'd find a workaround. Came across this line:
https://github.com/jenkinsci/git-plugin/blob/master/src/main/java/hudson/plugins/git/GitStatus.java#L300
With some console work, I came to find that
is empty until you the pipeline job is run at least once. After it runs, getSCMs() returns the same object references as
... at least in my scenario. The object doesn't seem to change after the first run, at least as far as my little script sees. The data is for sure there.