Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide support for External Group operations #1835

Merged
merged 11 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/main/java/org/kohsuke/github/EnterpriseManagedSupport.java
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);
}

}
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;
}

}
52 changes: 52 additions & 0 deletions src/main/java/org/kohsuke/github/GHError.java
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);
}

}
Loading