use of org.gitlab4j.api.GitLabApiException in project choerodon-starters by open-hand.
the class WebHookManager method handleEvent.
/**
* Parses and verifies an Event instance from the HTTP request and
* fires it off to the registered listeners.
*
* @param request the HttpServletRequest to read the Event instance from
* @throws GitLabApiException if the parsed event is not supported
*/
public void handleEvent(HttpServletRequest request) throws GitLabApiException {
if (!isValidSecretToken(request)) {
String message = "X-Gitlab-Token mismatch!";
LOG.warning(message);
throw new GitLabApiException(message);
}
String eventName = request.getHeader("X-Gitlab-Event");
LOG.info("handleEvent: X-Gitlab-Event=" + eventName);
switch(eventName) {
case BuildEvent.X_GITLAB_EVENT:
case IssueEvent.X_GITLAB_EVENT:
case MergeRequestEvent.X_GITLAB_EVENT:
case NoteEvent.X_GITLAB_EVENT:
case PipelineEvent.X_GITLAB_EVENT:
case PushEvent.X_GITLAB_EVENT:
case TagPushEvent.X_GITLAB_EVENT:
case WikiPageEvent.X_GITLAB_EVENT:
break;
default:
String message = "Unsupported X-Gitlab-Event, event Name=" + eventName;
LOG.warning(message);
throw new GitLabApiException(message);
}
String errorMessage = null;
try {
Event event;
if (LOG.isLoggable(Level.FINE)) {
LOG.fine(HttpRequestUtils.getShortRequestDump(eventName + " webhook", true, request));
String postData = HttpRequestUtils.getPostDataAsString(request);
LOG.fine("Raw POST data:\n" + postData);
event = jacksonJson.unmarshal(Event.class, postData);
LOG.fine(event.getObjectKind() + " event:\n" + jacksonJson.marshal(event) + "\n");
} else {
InputStreamReader reader = new InputStreamReader(request.getInputStream());
event = jacksonJson.unmarshal(Event.class, reader);
}
fireEvent(event);
} catch (JsonParseException jpe) {
errorMessage = jpe.getMessage();
LOG.warning("Error parsing JSON data, error=" + errorMessage);
} catch (JsonMappingException jme) {
errorMessage = jme.getMessage();
LOG.warning("Error mapping JSON data, error=" + errorMessage);
} catch (IOException ioe) {
errorMessage = ioe.getMessage();
LOG.warning("Error reading JSON data, error=" + errorMessage);
} catch (Exception e) {
errorMessage = e.getMessage();
LOG.warning("Unexpected error reading JSON data, error=" + errorMessage);
}
if (errorMessage != null)
throw new GitLabApiException(errorMessage);
}
use of org.gitlab4j.api.GitLabApiException in project dash-licenses by eclipse.
the class GitLabSupport method createReviews.
public void createReviews(List<LicenseData> needsReview, PrintWriter output) {
execute(connection -> {
var count = 0;
for (LicenseData licenseData : needsReview) {
if (count >= MAXIMUM_REVIEWS)
break;
count++;
output.println(String.format("Setting up a review for %s.", licenseData.getId().toString()));
if (!licenseData.getId().isValid()) {
output.println(" - Don't know what to do with this.");
continue;
}
Stream<ExtendedContentData> extendedData = dataService.findFor(licenseData.getId());
/*
* Ideally, we need a way to "create if does not already exist" feature in the
* GitLab API. But since we don't have that, we'll leverage the expectation that
* concurrent requests to review the same content will be relatively rare (there
* is some risk that between asking if we have an existing issue for a review
* for a particular bit of content and creating a new one, that somebody else
* might be doing the same). Our expectation is that the potential additional
* churn on the backend should require significantly less effort than that
* required to prevent rare duplication.
*/
try {
GitLabReview review = new GitLabReview(settings.getProjectId(), licenseData, extendedData);
Issue existing = connection.findIssue(review);
if (existing != null) {
output.println(String.format(" - Existing: %s", existing.getWebUrl()));
continue;
}
Issue created = connection.createIssue(review);
if (created == null) {
output.println(" - An error occurred while attempting to create a review request");
// TODO If we break creating a review, then don't try to create any more.
break;
}
output.println(String.format(" - Created: %s", created.getWebUrl()));
output.flush();
} catch (GitLabApiException e) {
throw new RuntimeException(e);
}
}
if (count < needsReview.size()) {
output.println();
output.println("More content needs to be reviewed.");
output.printf("For now, however, this experimental feature only submits the first %d.\n", count);
output.println();
}
});
}
Aggregations