-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1835 from mestebangutierrez/support-external-groups
Provide support for External Group operations
- Loading branch information
Showing
112 changed files
with
5,443 additions
and
11 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/java/org/kohsuke/github/EnterpriseManagedSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.kohsuke.github; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.Optional; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Utility class for helping with operations for enterprise managed resources. | ||
* | ||
* @author Miguel Esteban Gutiérrez | ||
*/ | ||
class EnterpriseManagedSupport { | ||
|
||
static final String COULD_NOT_RETRIEVE_ORGANIZATION_EXTERNAL_GROUPS = "Could not retrieve organization external groups"; | ||
static final String NOT_PART_OF_EXTERNALLY_MANAGED_ENTERPRISE_ERROR = "This organization is not part of externally managed enterprise."; | ||
static final String TEAM_CANNOT_BE_EXTERNALLY_MANAGED_ERROR = "This team cannot be externally managed since it has explicit members."; | ||
|
||
private static final Logger LOGGER = Logger.getLogger(EnterpriseManagedSupport.class.getName()); | ||
|
||
private final GHOrganization organization; | ||
|
||
private EnterpriseManagedSupport(GHOrganization organization) { | ||
this.organization = organization; | ||
} | ||
|
||
Optional<GHIOException> filterException(final HttpException he, final String scenario) { | ||
if (he.getResponseCode() == 400) { | ||
final String responseMessage = he.getMessage(); | ||
try { | ||
final GHError error = GitHubClient.getMappingObjectReader(this.organization.root()) | ||
.forType(GHError.class) | ||
.readValue(responseMessage); | ||
if (NOT_PART_OF_EXTERNALLY_MANAGED_ENTERPRISE_ERROR.equals(error.getMessage())) { | ||
return Optional.of(new GHNotExternallyManagedEnterpriseException(scenario, error, he)); | ||
} else if (TEAM_CANNOT_BE_EXTERNALLY_MANAGED_ERROR.equals(error.getMessage())) { | ||
return Optional.of(new GHTeamCannotBeExternallyManagedException(scenario, error, he)); | ||
} | ||
} catch (final JsonProcessingException e) { | ||
// We can ignore it | ||
LOGGER.warning(() -> logUnexpectedFailure(e, responseMessage)); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
Optional<GHException> filterException(final GHException e) { | ||
if (e.getCause() instanceof HttpException) { | ||
final HttpException he = (HttpException) e.getCause(); | ||
return filterException(he, COULD_NOT_RETRIEVE_ORGANIZATION_EXTERNAL_GROUPS) | ||
.map(translated -> new GHException(COULD_NOT_RETRIEVE_ORGANIZATION_EXTERNAL_GROUPS, translated)); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
static EnterpriseManagedSupport forOrganization(final GHOrganization org) { | ||
return new EnterpriseManagedSupport(org); | ||
} | ||
|
||
private static String logUnexpectedFailure(final JsonProcessingException exception, final String payload) { | ||
final StringWriter sw = new StringWriter(); | ||
final PrintWriter pw = new PrintWriter(sw); | ||
exception.printStackTrace(pw); | ||
return String.format("Could not parse GitHub error response: '%s'. Full stacktrace follows:%n%s", payload, sw); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/org/kohsuke/github/GHEnterpriseManagedUsersException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.kohsuke.github; | ||
|
||
/** | ||
* Failure related to Enterprise Managed Users operations. | ||
* | ||
* @author Miguel Esteban Gutiérrez | ||
*/ | ||
public class GHEnterpriseManagedUsersException extends GHIOException { | ||
|
||
/** | ||
* The serial version UID of the exception. | ||
*/ | ||
private static final long serialVersionUID = 1980051901L; | ||
|
||
/** | ||
* The error that caused the exception. | ||
*/ | ||
private final GHError error; | ||
|
||
/** | ||
* Instantiates a new exception. | ||
* | ||
* @param message | ||
* the message | ||
* @param error | ||
* the error that caused the exception | ||
* @param cause | ||
* the cause | ||
*/ | ||
public GHEnterpriseManagedUsersException(final String message, final GHError error, final Throwable cause) { | ||
super(message, cause); | ||
this.error = error; | ||
} | ||
|
||
/** | ||
* Get the error that caused the exception. | ||
* | ||
* @return the error | ||
*/ | ||
public GHError getError() { | ||
return error; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.kohsuke.github; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
||
import java.io.Serializable; | ||
import java.net.URL; | ||
|
||
/** | ||
* Represents an error from GitHub. | ||
* | ||
* @author Miguel Esteban Gutiérrez | ||
*/ | ||
public class GHError implements Serializable { | ||
|
||
/** | ||
* The serial version UID of the error | ||
*/ | ||
private static final long serialVersionUID = 2008071901; | ||
|
||
/** | ||
* The error message. | ||
*/ | ||
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Field comes from JSON deserialization") | ||
private String message; | ||
|
||
/** | ||
* The URL to the documentation for the error. | ||
*/ | ||
@JsonProperty("documentation_url") | ||
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Field comes from JSON deserialization") | ||
private String documentation; | ||
|
||
/** | ||
* Get the error message. | ||
* | ||
* @return the message | ||
*/ | ||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
/** | ||
* Get the URL to the documentation for the error. | ||
* | ||
* @return the url | ||
*/ | ||
public URL getDocumentationUrl() { | ||
return GitHubClient.parseURL(documentation); | ||
} | ||
|
||
} |
Oops, something went wrong.