From cdbd136f3b25b963c346a0dced11263bb2e04176 Mon Sep 17 00:00:00 2001 From: Michael Tughan Date: Fri, 8 Dec 2023 15:29:41 -0500 Subject: [PATCH] [JENKINS-60874] Configuring ForkPullRequestDiscoveryTrait using Job DSL (#752) JENKINS-26535 describes an issue where wildcards in generics aren't handled well and result in the inability to configure classes that use wildcards in Job DSL, such as the SCMHeadAuthority class used for the forked PR trust policy. To work around this issue, create a new abstract class named GitHubForkTrustPolicy which extends the necessary SCMHeadAuthority superclass with its proper generics and use it in the constructor instead. Update all the trust policies to extend from this abstract class so they can be used. Fixes JENKINS-60874. --- .../ForkPullRequestDiscoveryTrait.java | 30 ++++++++++++------- .../ForkPullRequestDiscoveryTrait2Test.java | 6 +--- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/ForkPullRequestDiscoveryTrait.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/ForkPullRequestDiscoveryTrait.java index f7bf3e7d0..495380b21 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/ForkPullRequestDiscoveryTrait.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/ForkPullRequestDiscoveryTrait.java @@ -39,7 +39,6 @@ import jenkins.scm.api.trait.SCMHeadAuthority; import jenkins.scm.api.trait.SCMHeadAuthorityDescriptor; import jenkins.scm.api.trait.SCMSourceContext; -import jenkins.scm.api.trait.SCMSourceRequest; import jenkins.scm.api.trait.SCMSourceTrait; import jenkins.scm.api.trait.SCMSourceTraitDescriptor; import jenkins.scm.impl.ChangeRequestSCMHeadCategory; @@ -83,6 +82,12 @@ public class ForkPullRequestDiscoveryTrait extends SCMSourceTrait { * @param trust the authority to use. */ @DataBoundConstructor + public ForkPullRequestDiscoveryTrait(int strategyId, @NonNull GitHubForkTrustPolicy trust) { + this.strategyId = strategyId; + this.trust = trust; + } + + @Deprecated public ForkPullRequestDiscoveryTrait( int strategyId, @NonNull @@ -236,9 +241,15 @@ public List getTrustDescriptors() { } } + /** Trust policy for forked pull requests. + *

+ * This reduces generics in the DataBoundConstructor signature as a workaround for JENKINS-26535. + */ + public abstract static class GitHubForkTrustPolicy + extends SCMHeadAuthority {} + /** An {@link SCMHeadAuthority} that trusts nothing. */ - public static class TrustNobody - extends SCMHeadAuthority { + public static class TrustNobody extends GitHubForkTrustPolicy { /** Constructor. */ @DataBoundConstructor @@ -246,7 +257,7 @@ public TrustNobody() {} /** {@inheritDoc} */ @Override - public boolean checkTrusted(@NonNull SCMSourceRequest request, @NonNull PullRequestSCMHead head) { + public boolean checkTrusted(@NonNull GitHubSCMSourceRequest request, @NonNull PullRequestSCMHead head) { return false; } @@ -270,8 +281,7 @@ public boolean isApplicableToOrigin(@NonNull Class orig } /** An {@link SCMHeadAuthority} that trusts contributors to the repository. */ - public static class TrustContributors - extends SCMHeadAuthority { + public static class TrustContributors extends GitHubForkTrustPolicy { /** Constructor. */ @DataBoundConstructor public TrustContributors() {} @@ -303,8 +313,7 @@ public boolean isApplicableToOrigin(@NonNull Class orig } /** An {@link SCMHeadAuthority} that trusts those with write permission to the repository. */ - public static class TrustPermission - extends SCMHeadAuthority { + public static class TrustPermission extends GitHubForkTrustPolicy { /** Constructor. */ @DataBoundConstructor @@ -347,15 +356,14 @@ public boolean isApplicableToOrigin(@NonNull Class orig } /** An {@link SCMHeadAuthority} that trusts everyone. */ - public static class TrustEveryone - extends SCMHeadAuthority { + public static class TrustEveryone extends GitHubForkTrustPolicy { /** Constructor. */ @DataBoundConstructor public TrustEveryone() {} /** {@inheritDoc} */ @Override - protected boolean checkTrusted(@NonNull SCMSourceRequest request, @NonNull PullRequestSCMHead head) { + protected boolean checkTrusted(@NonNull GitHubSCMSourceRequest request, @NonNull PullRequestSCMHead head) { return true; } diff --git a/src/test/java/org/jenkinsci/plugins/github_branch_source/ForkPullRequestDiscoveryTrait2Test.java b/src/test/java/org/jenkinsci/plugins/github_branch_source/ForkPullRequestDiscoveryTrait2Test.java index 91ef0c4d5..cb61162c6 100644 --- a/src/test/java/org/jenkinsci/plugins/github_branch_source/ForkPullRequestDiscoveryTrait2Test.java +++ b/src/test/java/org/jenkinsci/plugins/github_branch_source/ForkPullRequestDiscoveryTrait2Test.java @@ -28,9 +28,6 @@ import java.util.Collections; import java.util.List; import jenkins.branch.BranchSource; -import jenkins.scm.api.SCMRevision; -import jenkins.scm.api.mixin.ChangeRequestSCMHead2; -import jenkins.scm.api.trait.SCMHeadAuthority; import jenkins.scm.api.trait.SCMSourceTrait; import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject; import org.junit.Ignore; @@ -69,8 +66,7 @@ public void configRoundtripWithRawUrl() throws Exception { private void assertRoundTrip( WorkflowMultiBranchProject p, - SCMHeadAuthority - trust, + ForkPullRequestDiscoveryTrait.GitHubForkTrustPolicy trust, boolean configuredByUrl) throws Exception {