Skip to content

Commit 7c204fe

Browse files
feat: List external groups in an organization
1 parent 3043691 commit 7c204fe

50 files changed

Lines changed: 2537 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 operation 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+
20+
private static final Logger LOGGER = Logger.getLogger(EnterpriseManagedSupport.class.getName());
21+
22+
private final GHOrganization organization;
23+
24+
private EnterpriseManagedSupport(GHOrganization organization) {
25+
this.organization = organization;
26+
}
27+
28+
Optional<GHIOException> handleException(final HttpException he, final String scenario) {
29+
if (he.getResponseCode() == 400) {
30+
final String responseMessage = he.getMessage();
31+
try {
32+
final GHError error = GitHubClient.getMappingObjectReader(this.organization.root())
33+
.forType(GHError.class)
34+
.readValue(responseMessage);
35+
if (NOT_PART_OF_EXTERNALLY_MANAGED_ENTERPRISE_ERROR.equals(error.getMessage())) {
36+
return Optional.of(new GHNotExternallyManagedEnterpriseException(scenario, error, he));
37+
}
38+
} catch (final JsonProcessingException e) {
39+
// We can ignore it
40+
LOGGER.warning(() -> logUnexpectedFailure(e, responseMessage));
41+
}
42+
}
43+
return Optional.empty();
44+
}
45+
46+
Optional<GHException> handleException(final GHException e) {
47+
if (e.getCause() instanceof HttpException) {
48+
final HttpException he = (HttpException) e.getCause();
49+
return handleException(he, COULD_NOT_RETRIEVE_ORGANIZATION_EXTERNAL_GROUPS)
50+
.map(translated -> new GHException(COULD_NOT_RETRIEVE_ORGANIZATION_EXTERNAL_GROUPS, translated));
51+
}
52+
return Optional.empty();
53+
}
54+
55+
static EnterpriseManagedSupport forOrganization(final GHOrganization org) {
56+
return new EnterpriseManagedSupport(org);
57+
}
58+
59+
private static String logUnexpectedFailure(final JsonProcessingException exception, final String payload) {
60+
final StringWriter sw = new StringWriter();
61+
final PrintWriter pw = new PrintWriter(sw);
62+
exception.printStackTrace(pw);
63+
return String.format("Could not parse GitHub error response: '%s'. Full stacktrace follows:%n%s", payload, sw);
64+
}
65+
66+
}
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)