Skip to content

Commit e0edb9d

Browse files
authored
Merge pull request hub4j#1835 from mestebangutierrez/support-external-groups
Provide support for External Group operations
2 parents 58c5976 + c2ac4b0 commit e0edb9d

112 files changed

Lines changed: 5443 additions & 11 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: 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+
}
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)