Skip to content

Commit

Permalink
feat: add ability to add checks with app ids
Browse files Browse the repository at this point in the history
  • Loading branch information
tginiotis-at-work committed May 17, 2024
1 parent 2e6dcc1 commit c8c7b30
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 4 deletions.
60 changes: 60 additions & 0 deletions src/main/java/org/kohsuke/github/GHBranchProtection.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,54 @@ public boolean isEnabled() {
}
}

/**
* The type Check.
*/
public static class Check {
@JsonProperty
private String context;

@JsonProperty
private Integer app_id;

/**
* no-arg constructor for the serializer
*/
public Check() {
}

/**
* Regular constructor for use in user business logic
*
* @param context
* the context string of the check
* @param appId
* the application ID the check is supposed to come from
*/
public Check(String context, Integer appId) {
this.context = context;
this.app_id = appId;
}

/**
* The context string of the check
*
* @return the string
*/
public String getContext() {
return context;
}

/**
* The application ID the check is supposed to come from. The value "-1" indicates "any source".
*
* @return the integer
*/
public Integer getAppId() {
return app_id;
}
}

/**
* The type AllowForcePushes.
*/
Expand Down Expand Up @@ -462,6 +510,9 @@ public static class RequiredStatusChecks {
@JsonProperty
private Collection<String> contexts;

@JsonProperty
private Collection<Check> checks;

@JsonProperty
private boolean strict;

Expand All @@ -477,6 +528,15 @@ public Collection<String> getContexts() {
return Collections.unmodifiableCollection(contexts);
}

/**
* Gets checks.
*
* @return the checks
*/
public Collection<Check> getChecks() {
return Collections.unmodifiableCollection(checks);
}

/**
* Gets url.
*
Expand Down
47 changes: 43 additions & 4 deletions src/main/java/org/kohsuke/github/GHBranchProtectionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,27 @@ public class GHBranchProtectionBuilder {
* the checks
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder addRequiredChecksWithAppIds(Collection<GHBranchProtection.Check> checks) {
if (!(getStatusChecks() instanceof StatusChecksWithAppId)) {
statusChecks = new StatusChecksWithAppId();
}
((StatusChecksWithAppId) getStatusChecks()).checks.addAll(checks);
return this;
}

/**
* Add required checks gh branch protection builder.
*
* @param checks
* the checks
* @return the gh branch protection builder
*/
@Deprecated
public GHBranchProtectionBuilder addRequiredChecks(Collection<String> checks) {
getStatusChecks().contexts.addAll(checks);
if (!(getStatusChecks() instanceof StatusChecksDeprecated)) {
statusChecks = new StatusChecksDeprecated();
}
((StatusChecksDeprecated) getStatusChecks()).contexts.addAll(checks);
return this;
}

Expand All @@ -62,11 +81,24 @@ public GHBranchProtectionBuilder addRequiredChecks(Collection<String> checks) {
* the checks
* @return the gh branch protection builder
*/
@Deprecated
public GHBranchProtectionBuilder addRequiredChecks(String... checks) {
addRequiredChecks(Arrays.asList(checks));
return this;
}

/**
* Add required checks gh branch protection builder.
*
* @param checks
* the checks
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder addRequiredChecksWithAppIds(GHBranchProtection.Check... checks) {
addRequiredChecksWithAppIds(Arrays.asList(checks));
return this;
}

/**
* Allow deletion of the protected branch.
*
Expand Down Expand Up @@ -532,7 +564,7 @@ private Restrictions getRestrictions() {

private StatusChecks getStatusChecks() {
if (statusChecks == null) {
statusChecks = new StatusChecks();
statusChecks = new StatusChecksWithAppId();
}
return statusChecks;
}
Expand All @@ -546,8 +578,15 @@ private static class Restrictions {
private Set<String> users = new HashSet<String>();
}

private static class StatusChecks {
final List<String> contexts = new ArrayList<String>();
private static abstract class StatusChecks {
boolean strict;
}

private static class StatusChecksWithAppId extends StatusChecks {
final List<GHBranchProtection.Check> checks = new ArrayList<>();
}

private static class StatusChecksDeprecated extends StatusChecks {
final List<String> contexts = new ArrayList<>();
}
}

0 comments on commit c8c7b30

Please sign in to comment.