Search in sources :

Example 76 with GitLabApiException

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);
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) IOException(java.io.IOException) GitLabApiException(org.gitlab4j.api.GitLabApiException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 77 with GitLabApiException

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();
        }
    });
}
Also used : Issue(org.gitlab4j.api.models.Issue) LicenseData(org.eclipse.dash.licenses.LicenseData) GitLabApiException(org.gitlab4j.api.GitLabApiException) ExtendedContentData(org.eclipse.dash.licenses.extended.ExtendedContentData)

Aggregations

GitLabApiException (org.gitlab4j.api.GitLabApiException)77 IOException (java.io.IOException)35 GitLabApi (org.gitlab4j.api.GitLabApi)18 Project (org.gitlab4j.api.models.Project)18 Group (org.gitlab4j.api.models.Group)14 List (java.util.List)12 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)9 MergeRequest (org.gitlab4j.api.models.MergeRequest)9 GitMember (de.catma.repository.git.GitMember)8 ArrayList (java.util.ArrayList)8 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)8 MergeRequestApi (org.gitlab4j.api.MergeRequestApi)8 UserApi (org.gitlab4j.api.UserApi)8 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)7 Workflow (com.tremolosecurity.provisioning.core.Workflow)7 GroupApi (org.gitlab4j.api.GroupApi)7 IssuesApi (org.gitlab4j.api.IssuesApi)7 AccessLevel (org.gitlab4j.api.models.AccessLevel)7 Issue (org.gitlab4j.api.models.Issue)7 Comment (de.catma.document.comment.Comment)6