Skip to content

Commit

Permalink
[JENKINS-60874] Configuring ForkPullRequestDiscoveryTrait using Job D…
Browse files Browse the repository at this point in the history
…SL (#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.
  • Loading branch information
mtughan authored Dec 8, 2023
1 parent 46825fd commit cdbd136
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -236,17 +241,23 @@ public List<SCMHeadAuthorityDescriptor> getTrustDescriptors() {
}
}

/** Trust policy for forked pull requests.
* <p>
* This reduces generics in the DataBoundConstructor signature as a workaround for JENKINS-26535.
*/
public abstract static class GitHubForkTrustPolicy
extends SCMHeadAuthority<GitHubSCMSourceRequest, PullRequestSCMHead, PullRequestSCMRevision> {}

/** An {@link SCMHeadAuthority} that trusts nothing. */
public static class TrustNobody
extends SCMHeadAuthority<SCMSourceRequest, PullRequestSCMHead, PullRequestSCMRevision> {
public static class TrustNobody extends GitHubForkTrustPolicy {

/** Constructor. */
@DataBoundConstructor
public TrustNobody() {}

/** {@inheritDoc} */
@Override
public boolean checkTrusted(@NonNull SCMSourceRequest request, @NonNull PullRequestSCMHead head) {
public boolean checkTrusted(@NonNull GitHubSCMSourceRequest request, @NonNull PullRequestSCMHead head) {
return false;
}

Expand All @@ -270,8 +281,7 @@ public boolean isApplicableToOrigin(@NonNull Class<? extends SCMHeadOrigin> orig
}

/** An {@link SCMHeadAuthority} that trusts contributors to the repository. */
public static class TrustContributors
extends SCMHeadAuthority<GitHubSCMSourceRequest, PullRequestSCMHead, PullRequestSCMRevision> {
public static class TrustContributors extends GitHubForkTrustPolicy {
/** Constructor. */
@DataBoundConstructor
public TrustContributors() {}
Expand Down Expand Up @@ -303,8 +313,7 @@ public boolean isApplicableToOrigin(@NonNull Class<? extends SCMHeadOrigin> orig
}

/** An {@link SCMHeadAuthority} that trusts those with write permission to the repository. */
public static class TrustPermission
extends SCMHeadAuthority<GitHubSCMSourceRequest, PullRequestSCMHead, PullRequestSCMRevision> {
public static class TrustPermission extends GitHubForkTrustPolicy {

/** Constructor. */
@DataBoundConstructor
Expand Down Expand Up @@ -347,15 +356,14 @@ public boolean isApplicableToOrigin(@NonNull Class<? extends SCMHeadOrigin> orig
}

/** An {@link SCMHeadAuthority} that trusts everyone. */
public static class TrustEveryone
extends SCMHeadAuthority<SCMSourceRequest, PullRequestSCMHead, PullRequestSCMRevision> {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,8 +66,7 @@ public void configRoundtripWithRawUrl() throws Exception {

private void assertRoundTrip(
WorkflowMultiBranchProject p,
SCMHeadAuthority<? super GitHubSCMSourceRequest, ? extends ChangeRequestSCMHead2, ? extends SCMRevision>
trust,
ForkPullRequestDiscoveryTrait.GitHubForkTrustPolicy trust,
boolean configuredByUrl)
throws Exception {

Expand Down

0 comments on commit cdbd136

Please sign in to comment.