Skip to content

Commit

Permalink
Discover MR from forked projects & Add build status name overwrite op…
Browse files Browse the repository at this point in the history
…tion (#257)

Co-authored-by: Egor Abramov <[email protected]>
Co-authored-by: Irrandon <[email protected]>
Resolves #167
  • Loading branch information
mifitous authored Dec 30, 2022
1 parent dc12a4a commit aa66886
Show file tree
Hide file tree
Showing 17 changed files with 484 additions and 377 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class BuildStatusNameCustomPartTrait extends SCMSourceTrait {
@NonNull
private String buildStatusNameCustomPart = "";

private boolean buildStatusNameOverwrite;

/**
* Constructor for stapler.
*/
Expand All @@ -31,11 +33,17 @@ public void setBuildStatusNameCustomPart(@NonNull String buildStatusNameCustomPa
this.buildStatusNameCustomPart = buildStatusNameCustomPart;
}

@DataBoundSetter
public void setBuildStatusNameOverwrite(@NonNull Boolean buildStatusNameOverwrite) {
this.buildStatusNameOverwrite = buildStatusNameOverwrite;
}

@Override
protected void decorateContext(SCMSourceContext<?, ?> context) {
if (context instanceof GitLabSCMSourceContext) {
GitLabSCMSourceContext ctx = (GitLabSCMSourceContext) context;
ctx.withBuildStatusNameCustomPart(getBuildStatusNameCustomPart());
ctx.withBuildStatusNameOverwrite(getBuildStatusNameOverwrite());
}
}

Expand All @@ -49,6 +57,13 @@ public String getBuildStatusNameCustomPart() {
return buildStatusNameCustomPart;
}

/**
* Getter method for the build status name overwrite
*
* @return build status name overwrite option
*/
public boolean getBuildStatusNameOverwrite() { return buildStatusNameOverwrite; }

/**
* Our descriptor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

/**
* A {@link Discovery} trait for GitLab that will discover merge requests from forks of the
* A {@link Discovery} trait for GitLab that will discover merge requests from
* forks of the
* project.
*/
public class ForkMergeRequestDiscoveryTrait extends SCMSourceTrait {
Expand All @@ -40,35 +42,41 @@ public class ForkMergeRequestDiscoveryTrait extends SCMSourceTrait {
* The authority.
*/
@NonNull
private final SCMHeadAuthority<? super GitLabSCMSourceRequest, ? extends ChangeRequestSCMHead2, ? extends
SCMRevision>
trust;
private final SCMHeadAuthority<? super GitLabSCMSourceRequest, ? extends ChangeRequestSCMHead2, ? extends SCMRevision> trust;
/**
* Build MR for Forks that are not Mirrors
*/
private boolean buildMRForksNotMirror = false;

/**
* Constructor for stapler.
*
* @param strategyId the strategy id.
* @param trust the authority to use.
* @param strategyId the strategy id.
* @param trust the authority to use.
* @param buildMRForksNotMirror the buildMRForksNotMirror flag
*/
@DataBoundConstructor
public ForkMergeRequestDiscoveryTrait(int strategyId,
@NonNull SCMHeadAuthority<? super GitLabSCMSourceRequest, ? extends
ChangeRequestSCMHead2, ? extends SCMRevision> trust) {
@NonNull SCMHeadAuthority<? super GitLabSCMSourceRequest, ? extends ChangeRequestSCMHead2, ? extends SCMRevision> trust,
boolean buildMRForksNotMirror) {
this.strategyId = strategyId;
this.trust = trust;
this.buildMRForksNotMirror = buildMRForksNotMirror;
}

/**
* Constructor for programmatic instantiation.
*
* @param strategies the {@link ChangeRequestCheckoutStrategy} instances.
* @param trust the authority.
* @param strategies the {@link ChangeRequestCheckoutStrategy}
* instances.
* @param trust the authority.
* @param buildMRForksNotMirror the buildMRForksNotMirror flag
*/
public ForkMergeRequestDiscoveryTrait(@NonNull Set<ChangeRequestCheckoutStrategy> strategies,
@NonNull SCMHeadAuthority<? super GitLabSCMSourceRequest, ? extends
ChangeRequestSCMHead2, ? extends SCMRevision> trust) {
@NonNull SCMHeadAuthority<? super GitLabSCMSourceRequest, ? extends ChangeRequestSCMHead2, ? extends SCMRevision> trust,
boolean buildMRForksNotMirror) {
this((strategies.contains(ChangeRequestCheckoutStrategy.MERGE) ? 1 : 0)
+ (strategies.contains(ChangeRequestCheckoutStrategy.HEAD) ? 2 : 0), trust);
+ (strategies.contains(ChangeRequestCheckoutStrategy.HEAD) ? 2 : 0), trust, buildMRForksNotMirror);
}

/**
Expand All @@ -94,7 +102,7 @@ public Set<ChangeRequestCheckoutStrategy> getStrategies() {
return EnumSet.of(ChangeRequestCheckoutStrategy.HEAD);
case 3:
return EnumSet
.of(ChangeRequestCheckoutStrategy.HEAD, ChangeRequestCheckoutStrategy.MERGE);
.of(ChangeRequestCheckoutStrategy.HEAD, ChangeRequestCheckoutStrategy.MERGE);
default:
return EnumSet.noneOf(ChangeRequestCheckoutStrategy.class);
}
Expand All @@ -106,11 +114,27 @@ public Set<ChangeRequestCheckoutStrategy> getStrategies() {
* @return the authority.
*/
@NonNull
public SCMHeadAuthority<? super GitLabSCMSourceRequest, ? extends ChangeRequestSCMHead2, ? extends SCMRevision>
getTrust() {
public SCMHeadAuthority<? super GitLabSCMSourceRequest, ? extends ChangeRequestSCMHead2, ? extends SCMRevision> getTrust() {
return trust;
}

/**
* Gets the buildMRForksNotMirror
*
* @return true to build MR for Forks that are not Mirror
*/
public boolean getBuildMRForksNotMirror() {
return buildMRForksNotMirror;
}

/**
* Setter for stapler to set the buildMRForksNotMirror
*/
@DataBoundSetter
public void setBuildMRForksNotMirror(boolean buildMRForksNotMirror) {
this.buildMRForksNotMirror = buildMRForksNotMirror;
}

/**
* {@inheritDoc}
*/
Expand All @@ -120,6 +144,7 @@ protected void decorateContext(SCMSourceContext<?, ?> context) {
ctx.wantForkMRs(true);
ctx.withAuthority(trust);
ctx.withForkMRStrategies(getStrategies());
ctx.withBuildMRForksNotMirror(getBuildMRForksNotMirror());
}

/**
Expand Down Expand Up @@ -188,17 +213,18 @@ public ListBoxModel doFillStrategyIdItems() {
@SuppressWarnings("unused") // stapler
public List<SCMHeadAuthorityDescriptor> getTrustDescriptors() {
return SCMHeadAuthority._for(
GitLabSCMSourceRequest.class,
MergeRequestSCMHead.class,
MergeRequestSCMRevision.class,
SCMHeadOrigin.Fork.class
);
GitLabSCMSourceRequest.class,
MergeRequestSCMHead.class,
MergeRequestSCMRevision.class,
SCMHeadOrigin.Fork.class);
}

/**
* Returns the default trust for new instances of {@link ForkMergeRequestDiscoveryTrait}.
* Returns the default trust for new instances of
* {@link ForkMergeRequestDiscoveryTrait}.
*
* @return the default trust for new instances of {@link ForkMergeRequestDiscoveryTrait}.
* @return the default trust for new instances of
* {@link ForkMergeRequestDiscoveryTrait}.
*/
@NonNull
@SuppressWarnings("unused") // stapler
Expand All @@ -207,12 +233,11 @@ public List<SCMHeadAuthorityDescriptor> getTrustDescriptors() {
}
}


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

/**
* Constructor.
Expand All @@ -226,7 +251,7 @@ public TrustNobody() {
*/
@Override
public boolean checkTrusted(@NonNull SCMSourceRequest request,
@NonNull ChangeRequestSCMHead2 head) {
@NonNull ChangeRequestSCMHead2 head) {
return false;
}

Expand All @@ -242,7 +267,7 @@ public static class DescriptorImpl extends SCMHeadAuthorityDescriptor {
*/
@Override
public boolean isApplicableToOrigin(
@NonNull Class<? extends SCMHeadOrigin> originClass) {
@NonNull Class<? extends SCMHeadOrigin> originClass) {
return SCMHeadOrigin.Fork.class.isAssignableFrom(originClass);
}

Expand All @@ -255,16 +280,15 @@ public String getDisplayName() {
return Messages.ForkMergeRequestDiscoveryTrait_nobodyDisplayName();
}


}
}

/**
* An {@link SCMHeadAuthority} that trusts Members to the project.
*/
public static class TrustMembers
extends
SCMHeadAuthority<GitLabSCMSourceRequest, MergeRequestSCMHead, MergeRequestSCMRevision> {
extends
SCMHeadAuthority<GitLabSCMSourceRequest, MergeRequestSCMHead, MergeRequestSCMRevision> {

/**
* Constructor.
Expand All @@ -278,7 +302,7 @@ public TrustMembers() {
*/
@Override
protected boolean checkTrusted(@NonNull GitLabSCMSourceRequest request,
@NonNull MergeRequestSCMHead head) {
@NonNull MergeRequestSCMHead head) {
if (head.getOrigin().equals(SCMHeadOrigin.DEFAULT)) {
return false;
}
Expand Down Expand Up @@ -307,19 +331,20 @@ public String getDisplayName() {
*/
@Override
public boolean isApplicableToOrigin(
@NonNull Class<? extends SCMHeadOrigin> originClass) {
@NonNull Class<? extends SCMHeadOrigin> originClass) {
return SCMHeadOrigin.Fork.class.isAssignableFrom(originClass);
}

}
}

/**
* An {@link SCMHeadAuthority} that trusts those with required permission to the project.
* An {@link SCMHeadAuthority} that trusts those with required permission to the
* project.
*/
public static class TrustPermission
extends
SCMHeadAuthority<GitLabSCMSourceRequest, MergeRequestSCMHead, MergeRequestSCMRevision> {
extends
SCMHeadAuthority<GitLabSCMSourceRequest, MergeRequestSCMHead, MergeRequestSCMRevision> {

/**
* Constructor.
Expand All @@ -333,7 +358,7 @@ public TrustPermission() {
*/
@Override
protected boolean checkTrusted(@NonNull GitLabSCMSourceRequest request,
@NonNull MergeRequestSCMHead head) {
@NonNull MergeRequestSCMHead head) {
if (!head.getOrigin().equals(SCMHeadOrigin.DEFAULT)) {
AccessLevel permission = request.getPermission(head.getOriginOwner());
if (permission != null) {
Expand Down Expand Up @@ -371,7 +396,7 @@ public String getDisplayName() {
*/
@Override
public boolean isApplicableToOrigin(
@NonNull Class<? extends SCMHeadOrigin> originClass) {
@NonNull Class<? extends SCMHeadOrigin> originClass) {
return SCMHeadOrigin.Fork.class.isAssignableFrom(originClass);
}
}
Expand All @@ -381,7 +406,7 @@ public boolean isApplicableToOrigin(
* An {@link SCMHeadAuthority} that trusts everyone.
*/
public static class TrustEveryone extends
SCMHeadAuthority<SCMSourceRequest, ChangeRequestSCMHead2, SCMRevision> {
SCMHeadAuthority<SCMSourceRequest, ChangeRequestSCMHead2, SCMRevision> {

/**
* Constructor.
Expand All @@ -395,7 +420,7 @@ public TrustEveryone() {
*/
@Override
protected boolean checkTrusted(@NonNull SCMSourceRequest request,
@NonNull ChangeRequestSCMHead2 head) {
@NonNull ChangeRequestSCMHead2 head) {
return true;
}

Expand All @@ -420,7 +445,7 @@ public String getDisplayName() {
*/
@Override
public boolean isApplicableToOrigin(
@NonNull Class<? extends SCMHeadOrigin> originClass) {
@NonNull Class<? extends SCMHeadOrigin> originClass) {
return SCMHeadOrigin.Fork.class.isAssignableFrom(originClass);
}
}
Expand Down
Loading

0 comments on commit aa66886

Please sign in to comment.