Skip to content

Commit 92112af

Browse files
authored
Merge branch 'main' into patch-1
2 parents ea025b5 + e0edb9d commit 92112af

File tree

142 files changed

+6770
-30
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+6770
-30
lines changed

.github/workflows/maven-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
run: mvn -B clean install -D enable-ci --file pom.xml "-Dsurefire.argLine=--add-opens java.base/java.net=ALL-UNNAMED"
8787
- name: Codecov Report
8888
if: matrix.os == 'ubuntu' && matrix.java == '17'
89-
uses: codecov/codecov-action@v4.1.1
89+
uses: codecov/codecov-action@v4.4.1
9090

9191
test-java-8:
9292
name: test Java 8 (no-build)

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<jacoco.coverage.target.class.method>0.50</jacoco.coverage.target.class.method>
4545
<!-- For non-ci builds we'd like the build to still complete if jacoco metrics aren't met. -->
4646
<jacoco.haltOnFailure>false</jacoco.haltOnFailure>
47-
<jjwt.suite.version>0.12.3</jjwt.suite.version>
47+
<jjwt.suite.version>0.12.5</jjwt.suite.version>
4848

4949
<jacoco.surefire.argLine />
5050
<surefire.argLine />
@@ -350,7 +350,7 @@
350350
<plugin>
351351
<groupId>org.apache.maven.plugins</groupId>
352352
<artifactId>maven-jar-plugin</artifactId>
353-
<version>3.3.0</version>
353+
<version>3.4.1</version>
354354
<configuration>
355355
<archive>
356356
<manifestEntries>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.kohsuke.github;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
5+
import java.io.PrintWriter;
6+
import java.io.StringWriter;
7+
import java.util.Optional;
8+
import java.util.logging.Logger;
9+
10+
/**
11+
* Utility class for helping with operations for enterprise managed resources.
12+
*
13+
* @author Miguel Esteban Gutiérrez
14+
*/
15+
class EnterpriseManagedSupport {
16+
17+
static final String COULD_NOT_RETRIEVE_ORGANIZATION_EXTERNAL_GROUPS = "Could not retrieve organization external groups";
18+
static final String NOT_PART_OF_EXTERNALLY_MANAGED_ENTERPRISE_ERROR = "This organization is not part of externally managed enterprise.";
19+
static final String TEAM_CANNOT_BE_EXTERNALLY_MANAGED_ERROR = "This team cannot be externally managed since it has explicit members.";
20+
21+
private static final Logger LOGGER = Logger.getLogger(EnterpriseManagedSupport.class.getName());
22+
23+
private final GHOrganization organization;
24+
25+
private EnterpriseManagedSupport(GHOrganization organization) {
26+
this.organization = organization;
27+
}
28+
29+
Optional<GHIOException> filterException(final HttpException he, final String scenario) {
30+
if (he.getResponseCode() == 400) {
31+
final String responseMessage = he.getMessage();
32+
try {
33+
final GHError error = GitHubClient.getMappingObjectReader(this.organization.root())
34+
.forType(GHError.class)
35+
.readValue(responseMessage);
36+
if (NOT_PART_OF_EXTERNALLY_MANAGED_ENTERPRISE_ERROR.equals(error.getMessage())) {
37+
return Optional.of(new GHNotExternallyManagedEnterpriseException(scenario, error, he));
38+
} else if (TEAM_CANNOT_BE_EXTERNALLY_MANAGED_ERROR.equals(error.getMessage())) {
39+
return Optional.of(new GHTeamCannotBeExternallyManagedException(scenario, error, he));
40+
}
41+
} catch (final JsonProcessingException e) {
42+
// We can ignore it
43+
LOGGER.warning(() -> logUnexpectedFailure(e, responseMessage));
44+
}
45+
}
46+
return Optional.empty();
47+
}
48+
49+
Optional<GHException> filterException(final GHException e) {
50+
if (e.getCause() instanceof HttpException) {
51+
final HttpException he = (HttpException) e.getCause();
52+
return filterException(he, COULD_NOT_RETRIEVE_ORGANIZATION_EXTERNAL_GROUPS)
53+
.map(translated -> new GHException(COULD_NOT_RETRIEVE_ORGANIZATION_EXTERNAL_GROUPS, translated));
54+
}
55+
return Optional.empty();
56+
}
57+
58+
static EnterpriseManagedSupport forOrganization(final GHOrganization org) {
59+
return new EnterpriseManagedSupport(org);
60+
}
61+
62+
private static String logUnexpectedFailure(final JsonProcessingException exception, final String payload) {
63+
final StringWriter sw = new StringWriter();
64+
final PrintWriter pw = new PrintWriter(sw);
65+
exception.printStackTrace(pw);
66+
return String.format("Could not parse GitHub error response: '%s'. Full stacktrace follows:%n%s", payload, sw);
67+
}
68+
69+
}

src/main/java/org/kohsuke/github/GHBranchProtection.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.kohsuke.github;
22

3+
import com.fasterxml.jackson.annotation.JsonInclude;
34
import com.fasterxml.jackson.annotation.JsonProperty;
45
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
56

@@ -221,6 +222,55 @@ public boolean isEnabled() {
221222
}
222223
}
223224

225+
/**
226+
* The type Check.
227+
*/
228+
public static class Check {
229+
private String context;
230+
231+
@JsonInclude(JsonInclude.Include.NON_NULL)
232+
private Integer appId;
233+
234+
/**
235+
* no-arg constructor for the serializer
236+
*/
237+
public Check() {
238+
}
239+
240+
/**
241+
* Regular constructor for use in user business logic
242+
*
243+
* @param context
244+
* the context string of the check
245+
* @param appId
246+
* the application ID the check is supposed to come from. Pass "-1" to explicitly allow any app to
247+
* set the status. Pass "null" to automatically select the GitHub App that has recently provided this
248+
* check.
249+
*/
250+
public Check(String context, Integer appId) {
251+
this.context = context;
252+
this.appId = appId;
253+
}
254+
255+
/**
256+
* The context string of the check
257+
*
258+
* @return the string
259+
*/
260+
public String getContext() {
261+
return context;
262+
}
263+
264+
/**
265+
* The application ID the check is supposed to come from. The value "-1" indicates "any source".
266+
*
267+
* @return the integer
268+
*/
269+
public Integer getAppId() {
270+
return appId;
271+
}
272+
}
273+
224274
/**
225275
* The type AllowForcePushes.
226276
*/
@@ -462,6 +512,9 @@ public static class RequiredStatusChecks {
462512
@JsonProperty
463513
private Collection<String> contexts;
464514

515+
@JsonProperty
516+
private Collection<Check> checks;
517+
465518
@JsonProperty
466519
private boolean strict;
467520

@@ -477,6 +530,15 @@ public Collection<String> getContexts() {
477530
return Collections.unmodifiableCollection(contexts);
478531
}
479532

533+
/**
534+
* Gets checks.
535+
*
536+
* @return the checks
537+
*/
538+
public Collection<Check> getChecks() {
539+
return Collections.unmodifiableCollection(checks);
540+
}
541+
480542
/**
481543
* Gets url.
482544
*

src/main/java/org/kohsuke/github/GHBranchProtectionBuilder.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.Map;
1313
import java.util.Set;
14+
import java.util.stream.Collectors;
1415

1516
import static org.kohsuke.github.internal.Previews.LUKE_CAGE;
1617

@@ -50,8 +51,23 @@ public class GHBranchProtectionBuilder {
5051
* the checks
5152
* @return the gh branch protection builder
5253
*/
54+
public GHBranchProtectionBuilder addRequiredStatusChecks(Collection<GHBranchProtection.Check> checks) {
55+
getStatusChecks().checks.addAll(checks);
56+
return this;
57+
}
58+
59+
/**
60+
* Add required checks gh branch protection builder.
61+
*
62+
* @param checks
63+
* the checks
64+
* @return the gh branch protection builder
65+
*/
66+
@Deprecated
5367
public GHBranchProtectionBuilder addRequiredChecks(Collection<String> checks) {
54-
getStatusChecks().contexts.addAll(checks);
68+
getStatusChecks().checks.addAll(checks.stream()
69+
.map(context -> new GHBranchProtection.Check(context, null))
70+
.collect(Collectors.toList()));
5571
return this;
5672
}
5773

@@ -62,11 +78,24 @@ public GHBranchProtectionBuilder addRequiredChecks(Collection<String> checks) {
6278
* the checks
6379
* @return the gh branch protection builder
6480
*/
81+
@Deprecated
6582
public GHBranchProtectionBuilder addRequiredChecks(String... checks) {
6683
addRequiredChecks(Arrays.asList(checks));
6784
return this;
6885
}
6986

87+
/**
88+
* Add required checks gh branch protection builder.
89+
*
90+
* @param checks
91+
* the checks
92+
* @return the gh branch protection builder
93+
*/
94+
public GHBranchProtectionBuilder addRequiredChecks(GHBranchProtection.Check... checks) {
95+
addRequiredStatusChecks(Arrays.asList(checks));
96+
return this;
97+
}
98+
7099
/**
71100
* Allow deletion of the protected branch.
72101
*
@@ -547,7 +576,7 @@ private static class Restrictions {
547576
}
548577

549578
private static class StatusChecks {
550-
final List<String> contexts = new ArrayList<String>();
579+
final List<GHBranchProtection.Check> checks = new ArrayList<>();
551580
boolean strict;
552581
}
553582
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.kohsuke.github;
2+
3+
/**
4+
* Failure related to Enterprise Managed Users operations.
5+
*
6+
* @author Miguel Esteban Gutiérrez
7+
*/
8+
public class GHEnterpriseManagedUsersException extends GHIOException {
9+
10+
/**
11+
* The serial version UID of the exception.
12+
*/
13+
private static final long serialVersionUID = 1980051901L;
14+
15+
/**
16+
* The error that caused the exception.
17+
*/
18+
private final GHError error;
19+
20+
/**
21+
* Instantiates a new exception.
22+
*
23+
* @param message
24+
* the message
25+
* @param error
26+
* the error that caused the exception
27+
* @param cause
28+
* the cause
29+
*/
30+
public GHEnterpriseManagedUsersException(final String message, final GHError error, final Throwable cause) {
31+
super(message, cause);
32+
this.error = error;
33+
}
34+
35+
/**
36+
* Get the error that caused the exception.
37+
*
38+
* @return the error
39+
*/
40+
public GHError getError() {
41+
return error;
42+
}
43+
44+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.kohsuke.github;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
5+
6+
import java.io.Serializable;
7+
import java.net.URL;
8+
9+
/**
10+
* Represents an error from GitHub.
11+
*
12+
* @author Miguel Esteban Gutiérrez
13+
*/
14+
public class GHError implements Serializable {
15+
16+
/**
17+
* The serial version UID of the error
18+
*/
19+
private static final long serialVersionUID = 2008071901;
20+
21+
/**
22+
* The error message.
23+
*/
24+
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Field comes from JSON deserialization")
25+
private String message;
26+
27+
/**
28+
* The URL to the documentation for the error.
29+
*/
30+
@JsonProperty("documentation_url")
31+
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Field comes from JSON deserialization")
32+
private String documentation;
33+
34+
/**
35+
* Get the error message.
36+
*
37+
* @return the message
38+
*/
39+
public String getMessage() {
40+
return message;
41+
}
42+
43+
/**
44+
* Get the URL to the documentation for the error.
45+
*
46+
* @return the url
47+
*/
48+
public URL getDocumentationUrl() {
49+
return GitHubClient.parseURL(documentation);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)