Skip to content
Snippets Groups Projects
Commit a64cdb06 authored by Martin Lowe's avatar Martin Lowe :flag_ca:
Browse files

Iss #143 - Handle Status event by making PR in hook optional but checked

Resolves #143
parent f1452db3
No related branches found
No related tags found
1 merge request!160Iss #143 - Handle Status event by making PR in hook optional but checked
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
*/ */
package org.eclipsefoundation.git.eca.api.models; package org.eclipsefoundation.git.eca.api.models;
import javax.annotation.Nullable;
import org.eclipsefoundation.git.eca.dto.GithubWebhookTracking; import org.eclipsefoundation.git.eca.dto.GithubWebhookTracking;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
...@@ -31,6 +33,7 @@ public abstract class GithubWebhookRequest { ...@@ -31,6 +33,7 @@ public abstract class GithubWebhookRequest {
public abstract Repository getRepository(); public abstract Repository getRepository();
@Nullable
public abstract PullRequest getPullRequest(); public abstract PullRequest getPullRequest();
/** /**
...@@ -73,7 +76,7 @@ public abstract class GithubWebhookRequest { ...@@ -73,7 +76,7 @@ public abstract class GithubWebhookRequest {
public abstract Builder setRepository(Repository repository); public abstract Builder setRepository(Repository repository);
public abstract Builder setPullRequest(PullRequest pullRequest); public abstract Builder setPullRequest(@Nullable PullRequest pullRequest);
public abstract GithubWebhookRequest build(); public abstract GithubWebhookRequest build();
} }
......
...@@ -27,6 +27,7 @@ import javax.ws.rs.QueryParam; ...@@ -27,6 +27,7 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.ServerErrorException; import javax.ws.rs.ServerErrorException;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.eclipsefoundation.core.helper.LoggingHelper; import org.eclipsefoundation.core.helper.LoggingHelper;
...@@ -83,16 +84,22 @@ public class GithubWebhooksResource extends GithubAdjacentResource { ...@@ -83,16 +84,22 @@ public class GithubWebhooksResource extends GithubAdjacentResource {
return Response.ok().build(); return Response.ok().build();
} }
// check that the pull request isn't somehow missing
if (request.getPullRequest() == null) {
throw new BadRequestException("Pull request event submitted, but pull request information was missing");
}
PullRequest pr = request.getPullRequest();
// track the request before we start processing // track the request before we start processing
GithubWebhookTracking tracking = validationHelper GithubWebhookTracking tracking = validationHelper
.retrieveAndUpdateTrackingInformation(wrapper, request.getInstallation().getId(), request.getRepository().getFullName(), .retrieveAndUpdateTrackingInformation(wrapper, request.getInstallation().getId(), request.getRepository().getFullName(),
request.getPullRequest()); pr);
// start processing the request // start processing the request
LOGGER.trace("Processing PR event for install {} with delivery ID of {}", request.getInstallation().getId(), deliveryId); LOGGER.trace("Processing PR event for install {} with delivery ID of {}", request.getInstallation().getId(), deliveryId);
try { try {
// prepare for validation process by pre-processing into standard format // prepare for validation process by pre-processing into standard format
ValidationRequest vr = generateRequest(request); ValidationRequest vr = generateRequest(request, pr);
// process the request // process the request
validationHelper.handleGithubWebhookValidation(request, vr, wrapper); validationHelper.handleGithubWebhookValidation(request, vr, wrapper);
} catch (Exception e) { } catch (Exception e) {
...@@ -160,7 +167,13 @@ public class GithubWebhooksResource extends GithubAdjacentResource { ...@@ -160,7 +167,13 @@ public class GithubWebhooksResource extends GithubAdjacentResource {
try { try {
// get the tracking class, convert back to a GH webhook request, and validate the request // get the tracking class, convert back to a GH webhook request, and validate the request
GithubWebhookRequest request = GithubWebhookRequest.buildFromTracking(tracking); GithubWebhookRequest request = GithubWebhookRequest.buildFromTracking(tracking);
boolean isSuccessful = validationHelper.handleGithubWebhookValidation(request, generateRequest(request), wrapper).getPassed(); if (request.getPullRequest() == null) {
throw new ServerErrorException("Current validation is in a bad state, pull request information missing",
Status.INTERNAL_SERVER_ERROR);
}
boolean isSuccessful = validationHelper
.handleGithubWebhookValidation(request, generateRequest(request, request.getPullRequest()), wrapper)
.getPassed();
LOGGER.debug("Revalidation for request for '{}#{}' was {}successful.", sanitizedRepoName, prNo, isSuccessful ? "" : " not"); LOGGER.debug("Revalidation for request for '{}#{}' was {}successful.", sanitizedRepoName, prNo, isSuccessful ? "" : " not");
// build the url for pull request page // build the url for pull request page
...@@ -181,12 +194,13 @@ public class GithubWebhooksResource extends GithubAdjacentResource { ...@@ -181,12 +194,13 @@ public class GithubWebhooksResource extends GithubAdjacentResource {
* Generate the validation request for the current GH Webhook request. * Generate the validation request for the current GH Webhook request.
* *
* @param request the current webhook request for validation * @param request the current webhook request for validation
* @param pr null checked pull request from Github
* @return the Validation Request body to be used in validating data * @return the Validation Request body to be used in validating data
*/ */
private ValidationRequest generateRequest(GithubWebhookRequest request) { private ValidationRequest generateRequest(GithubWebhookRequest request, PullRequest pr) {
return validationHelper return validationHelper
.generateRequest(request.getInstallation().getId(), request.getRepository().getFullName(), .generateRequest(request.getInstallation().getId(), request.getRepository().getFullName(), pr.getNumber(),
request.getPullRequest().getNumber(), request.getRepository().getHtmlUrl()); request.getRepository().getHtmlUrl());
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment