Skip to content

Commit a78c302

Browse files
committed
Fix SCM config radio index mismatch when isApplicable filters descriptors
When an SCM plugin uses isApplicable() to restrict itself to specific job types, the radio button indices in the UI no longer match the unfiltered descriptor list used during form submission. The fix modifies SCMS.parseSCM() to use SCM._for(target), which returns the same filtered list used by the UI, ensuring the selected index correctly maps to the intended SCM descriptor.
1 parent 665e565 commit a78c302

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

core/src/main/java/hudson/scm/SCMS.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
import hudson.Extension;
2828
import hudson.model.AbstractProject;
2929
import hudson.model.Descriptor.FormException;
30+
import hudson.model.Job;
3031
import hudson.util.DescriptorList;
3132
import io.jenkins.servlet.ServletExceptionWrapper;
3233
import jakarta.servlet.ServletException;
3334
import java.util.List;
35+
import net.sf.json.JSONObject;
36+
import org.kohsuke.stapler.Stapler;
3437
import org.kohsuke.stapler.StaplerRequest;
3538
import org.kohsuke.stapler.StaplerRequest2;
3639

@@ -56,14 +59,25 @@ public class SCMS {
5659
*/
5760
@SuppressWarnings("deprecation")
5861
public static SCM parseSCM(StaplerRequest2 req, AbstractProject target) throws FormException, ServletException {
59-
SCM scm = SCM.all().newInstanceFromRadioList(req.getSubmittedForm().getJSONObject("scm"));
62+
List<SCMDescriptor<?>> applicableDescriptors = SCM._for((Job) target);
63+
SCM scm = newInstanceFromRadioList(applicableDescriptors, req.getSubmittedForm().getJSONObject("scm"));
6064
if (scm == null) {
6165
scm = new NullSCM(); // JENKINS-36043 workaround for AbstractMultiBranchProject.submit
6266
}
6367
scm.getDescriptor().incrementGeneration();
6468
return scm;
6569
}
6670

71+
private static SCM newInstanceFromRadioList(List<SCMDescriptor<?>> descriptors, JSONObject config) throws FormException {
72+
if (config.isNullObject())
73+
return null;
74+
int idx = config.getInt("value");
75+
if (idx < 0 || idx >= descriptors.size()) {
76+
throw new FormException("Invalid SCM index: " + idx, "scm");
77+
}
78+
return descriptors.get(idx).newInstance(Stapler.getCurrentRequest2(), config);
79+
}
80+
6781
/**
6882
* @deprecated use {@link #parseSCM(StaplerRequest2, AbstractProject)}
6983
*/

0 commit comments

Comments
 (0)