Hi I have same problem as Dayton Gomez It is ridiculous how this bug still exists. I've checked Jenkins version 2.205 and Git plugin version 4.0.0. Here is why:
package org.jenkinsci.plugins.workflow.job;
...
public final class WorkflowJob extends Job<WorkflowJob,WorkflowRun> implements LazyBuildMixIn.LazyLoadingJob<WorkflowJob,WorkflowRun>, ParameterizedJobMixIn.ParameterizedJob<WorkflowJob, WorkflowRun>, TopLevelItem, Queue.FlyweightTask, SCMTriggerItem, BlockableResume {
...
@Override public Collection<? extends SCM> getSCMs() {
WorkflowRun b = getLastSuccessfulBuild();
if (b == null) {
b = getLastCompletedBuild();
}
if (b == null) {
return Collections.emptySet();
}
Map<String,SCM> scms = new LinkedHashMap<>();
for (WorkflowRun.SCMCheckout co : b.checkouts(null)) {
scms.put(co.scm.getKey(), co.scm);
}
return scms.values();
}
Above is method getSCM from WorkflowJob class and as you can see that it will return empty collection when there is no successful or completed build for this workflow. And here is code from GitStatus class that is responsible for processing notifications:
package hudson.plugins.git;
...
public class GitStatus implements UnprotectedRootAction {
...
public static class JenkinsAbstractProjectListener extends Listener {
...
@Override
public List<ResponseContributor> onNotifyCommit(String origin, URIish uri, String sha1, List<ParameterValue> buildParameters, String... branches) {
...
for (final Item project : jenkins.getAllItems()) {
SCMTriggerItem scmTriggerItem = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(project);
if (scmTriggerItem == null) {
continue;
}
SCMS: for (SCM scm : scmTriggerItem.getSCMs()) {
// do stuff
...
I.e. if getSCMs returns empty collection (and it will if no builds had happened yet) nothing will be checked. Maybe better way would be to first check if definition of the scmTriggerItem has scm attached with something like this:
...
for (final Item project : jenkins.getAllItems()) {
SCMTriggerItem scmTriggerItem = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(project);
if (scmTriggerItem == null) {
continue;
}
Collection<? extends SCM> scms = scmTriggerItem.getSCMs();
if (scms.isEmpty()){
if (scmTriggerItem.getDefinition() instanceof org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition) {
CpsScmFlowDefinition fd = (CpsScmFlowDefinition) scmTriggerItem.getDefinition();
scms = Arrays.asList(fd.getScm());
}
}
SCMS: for (SCM scm : scms) {
// do stuff
...
It is hard to tell if `org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition` will be available so maybe only real way to do that will be to do some reflection. Though main idea it NOT to rely on build happened at least once for receiving scms. |