Skip to content
Snippets Groups Projects

Add error page for GH status validation page for bad requests

Merged Martin Lowe requested to merge (removed):malowe/main/141 into main
2 files
+ 110
37
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -36,6 +36,7 @@ import org.eclipsefoundation.git.eca.namespace.ProviderType;
@@ -36,6 +36,7 @@ import org.eclipsefoundation.git.eca.namespace.ProviderType;
import org.eclipsefoundation.git.eca.service.GithubApplicationService;
import org.eclipsefoundation.git.eca.service.GithubApplicationService;
import org.eclipsefoundation.git.eca.service.ValidationService;
import org.eclipsefoundation.git.eca.service.ValidationService;
import org.eclipsefoundation.git.eca.service.ValidationStatusService;
import org.eclipsefoundation.git.eca.service.ValidationStatusService;
 
import org.jboss.resteasy.client.exception.ResteasyWebApplicationException;
import io.quarkus.qute.Location;
import io.quarkus.qute.Location;
import io.quarkus.qute.Template;
import io.quarkus.qute.Template;
@@ -49,6 +50,12 @@ import io.quarkus.qute.Template;
@@ -49,6 +50,12 @@ import io.quarkus.qute.Template;
@Path("eca/status")
@Path("eca/status")
public class StatusResource extends GithubAdjacentResource {
public class StatusResource extends GithubAdjacentResource {
 
// parameter names for the status error page
 
private static final String INCLUDE_INSTALL_LINK_PARAMETER = "includeInstallLink";
 
private static final String MESSAGE_PARAMETER = "message";
 
private static final String PULL_REQUEST_NUMBER_PARAMETER = "pullRequestNumber";
 
private static final String REPO_URL_PARAMETER = "repoUrl";
 
@Inject
@Inject
GithubApplicationService ghAppService;
GithubApplicationService ghAppService;
@Inject
@Inject
@@ -64,6 +71,8 @@ public class StatusResource extends GithubAdjacentResource {
@@ -64,6 +71,8 @@ public class StatusResource extends GithubAdjacentResource {
// Qute templates, generates UI status page
// Qute templates, generates UI status page
@Location("simple_fingerprint_ui")
@Location("simple_fingerprint_ui")
Template statusUiTemplate;
Template statusUiTemplate;
 
@Location("error")
 
Template errorTemplate;
/**
/**
* Standard endpoint for retrieving raw validation information on a historic request, using the fingerprint for lookups.
* Standard endpoint for retrieving raw validation information on a historic request, using the fingerprint for lookups.
@@ -97,10 +106,10 @@ public class StatusResource extends GithubAdjacentResource {
@@ -97,10 +106,10 @@ public class StatusResource extends GithubAdjacentResource {
.ok()
.ok()
.entity(statusUiTemplate
.entity(statusUiTemplate
.data("statuses", statuses)
.data("statuses", statuses)
.data("pullRequestNumber", null)
.data(PULL_REQUEST_NUMBER_PARAMETER, null)
.data("fullRepoName", null)
.data("fullRepoName", null)
.data("project", ps.isEmpty() ? null : ps.get(0))
.data("project", ps.isEmpty() ? null : ps.get(0))
.data("repoUrl", statuses.get(0).getRepoUrl())
.data(REPO_URL_PARAMETER, statuses.get(0).getRepoUrl())
.data("installationId", null)
.data("installationId", null)
.render())
.render())
.build();
.build();
@@ -120,47 +129,89 @@ public class StatusResource extends GithubAdjacentResource {
@@ -120,47 +129,89 @@ public class StatusResource extends GithubAdjacentResource {
@Path("gh/{org}/{repoName}/{prNo}")
@Path("gh/{org}/{repoName}/{prNo}")
public Response getCommitValidationForGithub(@PathParam("org") String org, @PathParam("repoName") String repoName,
public Response getCommitValidationForGithub(@PathParam("org") String org, @PathParam("repoName") String repoName,
@PathParam("prNo") Integer prNo) {
@PathParam("prNo") Integer prNo) {
 
// get the repo full name, used in a few lookups
String repoFullName = org + '/' + repoName;
String repoFullName = org + '/' + repoName;
// check that the passed repo has a valid installation
String installationId = ghAppService.getInstallationForRepo(org, repoName);
if (StringUtils.isBlank(installationId)) {
throw new BadRequestException("Repo " + repoFullName + " requested, but does not have visible installation, returning");
}
// generate the URL used to retrieve valid projects
// generate the URL used to retrieve valid projects
String repoUrl = GithubValidationHelper.getRepoUrl(org, repoName);
String repoUrl = GithubValidationHelper.getRepoUrl(org, repoName);
// get the data about the current request, and check that we have some validation statuses
try {
ValidationRequest req = validationHelper.generateRequest(installationId, repoFullName, prNo, repoUrl);
// check that the passed repo has a valid installation
List<CommitValidationStatus> statuses = validationStatus
String installationId = ghAppService.getInstallationForRepo(org, repoName);
.getHistoricValidationStatusByShas(wrapper, req.getCommits().stream().map(Commit::getHash).collect(Collectors.toList()));
if (StringUtils.isBlank(installationId)) {
// check if we have any data, and if there is none, attempt to validate the request information
return Response
if (statuses.isEmpty()) {
.ok()
// run the validation for the current request
.entity(errorTemplate
ValidationResponse r = validationHelper.validateIncomingRequest(wrapper, org, repoName, prNo);
.data(MESSAGE_PARAMETER, "No Github ECA app installation could be found for the current pull request.")
if (r == null) {
.data(REPO_URL_PARAMETER, repoUrl)
throw new BadRequestException("Cannot validate request for " + repoFullName + "#" + prNo + " as it is already closed");
.data(PULL_REQUEST_NUMBER_PARAMETER, prNo)
 
.data(INCLUDE_INSTALL_LINK_PARAMETER, true)
 
.render())
 
.build();
}
}
// retrieve the status of the commits to display on the status page
// get the data about the current request, and check that we have some validation statuses
statuses = validationStatus
ValidationRequest req = validationHelper.generateRequest(installationId, repoFullName, prNo, repoUrl);
.getHistoricValidationStatusByShas(wrapper, r.getCommits().keySet().stream().collect(Collectors.toList()));
List<CommitValidationStatus> statuses = validationStatus
}
.getHistoricValidationStatusByShas(wrapper,
 
req.getCommits().stream().map(Commit::getHash).collect(Collectors.toList()));
 
// check if we have any data, and if there is none, attempt to validate the request information
 
if (statuses.isEmpty()) {
 
// run the validation for the current request adhoc
 
ValidationResponse r = validationHelper.validateIncomingRequest(wrapper, org, repoName, prNo);
 
if (r == null) {
 
return Response
 
.ok()
 
.entity(errorTemplate
 
.data(MESSAGE_PARAMETER,
 
"The currently selected PR is in a non-opened state, and will not be validated.")
 
.data(REPO_URL_PARAMETER, repoUrl)
 
.data(PULL_REQUEST_NUMBER_PARAMETER, prNo)
 
.data(INCLUDE_INSTALL_LINK_PARAMETER, false)
 
.render())
 
.build();
 
}
 
 
// retrieve the status of the commits to display on the status page
 
statuses = validationStatus
 
.getHistoricValidationStatusByShas(wrapper, r.getCommits().keySet().stream().collect(Collectors.toList()));
 
}
// get projects for use in queries + UI
// get projects for use in queries + UI
List<Project> ps = projects.retrieveProjectsForRepoURL(repoUrl, ProviderType.GITHUB);
List<Project> ps = projects.retrieveProjectsForRepoURL(repoUrl, ProviderType.GITHUB);
// render and return the status UI
// render and return the status UI
return Response
return Response
.ok()
.ok()
.entity(statusUiTemplate
.entity(statusUiTemplate
.data("statuses", statuses)
.data("statuses", statuses)
.data("pullRequestNumber", prNo)
.data(PULL_REQUEST_NUMBER_PARAMETER, prNo)
.data("fullRepoName", repoFullName)
.data("fullRepoName", repoFullName)
.data("project", ps.isEmpty() ? null : ps.get(0))
.data("project", ps.isEmpty() ? null : ps.get(0))
.data("repoUrl", repoUrl)
.data(REPO_URL_PARAMETER, repoUrl)
.data("installationId", installationId)
.data("installationId", installationId)
.render())
.render())
.build();
.build();
 
} catch (BadRequestException e) {
 
return Response
 
.ok()
 
.entity(errorTemplate
 
.data(MESSAGE_PARAMETER,
 
"Request made to validate content that is not eligible for validation (either closed, or with no identifiable changes to validate).")
 
.data(REPO_URL_PARAMETER, repoUrl)
 
.data(PULL_REQUEST_NUMBER_PARAMETER, prNo)
 
.data(INCLUDE_INSTALL_LINK_PARAMETER, false)
 
.render())
 
.build();
 
} catch (NotFoundException | ResteasyWebApplicationException e) {
 
return Response
 
.ok()
 
.entity(errorTemplate
 
.data(MESSAGE_PARAMETER,
 
"Could not find a pull request given the passed parameters, please check the URL and try again.")
 
.data(REPO_URL_PARAMETER, repoUrl)
 
.data(PULL_REQUEST_NUMBER_PARAMETER, prNo)
 
.data(INCLUDE_INSTALL_LINK_PARAMETER, false)
 
.render())
 
.build();
 
}
}
}
}
}
Loading