Skip to content
Snippets Groups Projects
Verified Commit 4c347135 authored by Jordi Gómez's avatar Jordi Gómez
Browse files

feat: adding option to disable revalidation alert

parent de863da2
No related branches found
No related tags found
1 merge request!221feat: adding email notification when certian revalidation threshold is met
Pipeline #70190 failed
/** /**
* Copyright (c) 2023 Eclipse Foundation * Copyright (c) 2023 Eclipse Foundation
* *
* This program and the accompanying materials are made * This program and the accompanying materials are made available under the terms of the Eclipse
* available under the terms of the Eclipse Public License 2.0 * Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0/
* which is available at https://www.eclipse.org/legal/epl-2.0/
* *
* Author: Martin Lowe <martin.lowe@eclipse-foundation.org> * Author: Martin Lowe <martin.lowe@eclipse-foundation.org>
* *
...@@ -42,15 +41,12 @@ import jakarta.ws.rs.core.MultivaluedHashMap; ...@@ -42,15 +41,12 @@ import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.core.MultivaluedMap;
/** /**
* Scheduled regular task that will interact with the backend persistence to * Scheduled regular task that will interact with the backend persistence to look for requests that
* look for requests that are in a * are in a failed/unvalidated state after an error while processing that could not be recovered.
* failed/unvalidated state after an error while processing that could not be * These requests will be reprocessed using the same logic as the standard validation, updating the
* recovered. These requests will be * timestamp on completion and either setting the revalidation flag to false or incrementing the
* reprocessed using the same logic as the standard validation, updating the * number of repeated revalidations needed for the request for tracking, depending on the succcess
* timestamp on completion and either setting * of the revalidation.
* the revalidation flag to false or incrementing the number of repeated
* revalidations needed for the request for
* tracking, depending on the succcess of the revalidation.
*/ */
@ApplicationScoped @ApplicationScoped
public class GithubRevalidationQueue { public class GithubRevalidationQueue {
...@@ -62,7 +58,12 @@ public class GithubRevalidationQueue { ...@@ -62,7 +58,12 @@ public class GithubRevalidationQueue {
@ConfigProperty(name = "eclipse.git-eca.tasks.gh-revalidation.enabled", defaultValue = "true") @ConfigProperty(name = "eclipse.git-eca.tasks.gh-revalidation.enabled", defaultValue = "true")
Instance<Boolean> isEnabled; Instance<Boolean> isEnabled;
@ConfigProperty(name = "eclipse.git-eca.tasks.gh-revalidation.notification-threshold", defaultValue = "3") @ConfigProperty(name = "eclipse.git-eca.tasks.gh-revalidation.notification-enabled",
defaultValue = "true")
Instance<Boolean> isRevalidationAlertEnabled;
@ConfigProperty(name = "eclipse.git-eca.tasks.gh-revalidation.notification-threshold",
defaultValue = "100")
Instance<Integer> revalidationThreshold; Instance<Integer> revalidationThreshold;
@Inject @Inject
...@@ -77,14 +78,14 @@ public class GithubRevalidationQueue { ...@@ -77,14 +78,14 @@ public class GithubRevalidationQueue {
@PostConstruct @PostConstruct
void init() { void init() {
// indicate to log whether enabled to reduce log spam // indicate to log whether enabled to reduce log spam
LOGGER.info("Github revalidation queue task is{} enabled.", Boolean.TRUE.equals(isEnabled.get()) ? "" : " not"); LOGGER.info("Github revalidation queue task is{} enabled.",
Boolean.TRUE.equals(isEnabled.get()) ? "" : " not");
} }
/** /**
* Every 5s, this method will attempt to load a Github webhook validation * Every 5s, this method will attempt to load a Github webhook validation request that has the
* request that has the needs revalidation flag * needs revalidation flag set to true. This will retrieve the oldest request in queue and will
* set to true. This will retrieve the oldest request in queue and will attempt * attempt to revalidate it.
* to revalidate it.
*/ */
@Scheduled(every = "${eclipse.git-eca.tasks.gh-revalidation.frequency:60s}") @Scheduled(every = "${eclipse.git-eca.tasks.gh-revalidation.frequency:60s}")
@ActivateRequestContext @ActivateRequestContext
...@@ -102,9 +103,10 @@ public class GithubRevalidationQueue { ...@@ -102,9 +103,10 @@ public class GithubRevalidationQueue {
// build the request and query to lookup the longest standing request that needs // build the request and query to lookup the longest standing request that needs
// revalidation // revalidation
RequestWrapper wrap = new FlatRequestWrapper(URI.create("https://api.eclipse.org/git/eca/revalidation-queue")); RequestWrapper wrap =
RDBMSQuery<GithubWebhookTracking> trackingQuery = new RDBMSQuery<>(wrap, filters.get(GithubWebhookTracking.class), new FlatRequestWrapper(URI.create("https://api.eclipse.org/git/eca/revalidation-queue"));
params); RDBMSQuery<GithubWebhookTracking> trackingQuery =
new RDBMSQuery<>(wrap, filters.get(GithubWebhookTracking.class), params);
List<GithubWebhookTracking> oldestRevalidation = dao.get(trackingQuery); List<GithubWebhookTracking> oldestRevalidation = dao.get(trackingQuery);
if (oldestRevalidation.isEmpty()) { if (oldestRevalidation.isEmpty()) {
...@@ -115,22 +117,18 @@ public class GithubRevalidationQueue { ...@@ -115,22 +117,18 @@ public class GithubRevalidationQueue {
} }
/** /**
* Reprocess the given record, attempting to run the ECA validation logic again. * Reprocess the given record, attempting to run the ECA validation logic again. If it passes, the
* If it passes, the revalidation flag is * revalidation flag is set to false and the time code is updated. If the processing fails again,
* set to false and the time code is updated. If the processing fails again, the * the failure count gets incremented and the updated time is set so that another entry can be
* failure count gets incremented and the * updated, as to not block on potentially broken records.
* updated time is set so that another entry can be updated, as to not block on
* potentially broken records.
* *
* @param requestToRevalidate the webhook tracking request to attempt to * @param requestToRevalidate the webhook tracking request to attempt to revalidate
* revalidate * @param wrap the current stubbed request wrapper used for queries.
* @param wrap the current stubbed request wrapper used for
* queries.
*/ */
private void reprocessRequest(GithubWebhookTracking requestToRevalidate, RequestWrapper wrap) { private void reprocessRequest(GithubWebhookTracking requestToRevalidate, RequestWrapper wrap) {
LOGGER LOGGER.debug("Attempting revalidation of request w/ ID {}, in repo {}#{}",
.debug("Attempting revalidation of request w/ ID {}, in repo {}#{}", requestToRevalidate.getId(), requestToRevalidate.getId(), requestToRevalidate.getRepositoryFullName(),
requestToRevalidate.getRepositoryFullName(), requestToRevalidate.getPullRequestNumber()); requestToRevalidate.getPullRequestNumber());
// update the number of times this status has revalidated (tracking) // update the number of times this status has revalidated (tracking)
requestToRevalidate requestToRevalidate
...@@ -145,17 +143,16 @@ public class GithubRevalidationQueue { ...@@ -145,17 +143,16 @@ public class GithubRevalidationQueue {
// split the full repo name into the org and repo name // split the full repo name into the org and repo name
String[] repoFullNameParts = requestToRevalidate.getRepositoryFullName().split("/"); String[] repoFullNameParts = requestToRevalidate.getRepositoryFullName().split("/");
if (repoFullNameParts.length != FULL_REPO_NAME_PARTS) { if (repoFullNameParts.length != FULL_REPO_NAME_PARTS) {
throw new IllegalStateException("Record with ID '" + Long.toString(requestToRevalidate.getId()) throw new IllegalStateException(
+ "' is in an invalid state (repository full name is not valid)"); "Record with ID '" + Long.toString(requestToRevalidate.getId())
+ "' is in an invalid state (repository full name is not valid)");
} }
// run the validation and then check if it succeeded. Use the forced flag since // run the validation and then check if it succeeded. Use the forced flag since
// we want to try even if there are no // we want to try even if there are no
// changes // changes
validationHelper validationHelper.validateIncomingRequest(wrap, repoFullNameParts[0], repoFullNameParts[1],
.validateIncomingRequest(wrap, repoFullNameParts[0], repoFullNameParts[1], requestToRevalidate.getPullRequestNumber(), true);
requestToRevalidate.getPullRequestNumber(),
true);
// if we have gotten here, then the validation has completed and can be removed // if we have gotten here, then the validation has completed and can be removed
// from queue // from queue
requestToRevalidate.setNeedsRevalidation(false); requestToRevalidate.setNeedsRevalidation(false);
...@@ -171,26 +168,32 @@ public class GithubRevalidationQueue { ...@@ -171,26 +168,32 @@ public class GithubRevalidationQueue {
// be valid // be valid
// if a PR is reopened, it should create a new event and validate anyways // if a PR is reopened, it should create a new event and validate anyways
if ("closed".equalsIgnoreCase(requestToRevalidate.getLastKnownState())) { if ("closed".equalsIgnoreCase(requestToRevalidate.getLastKnownState())) {
LOGGER.debug("Tracking request {} set to not revalidate as it was closed", requestToRevalidate.getId()); LOGGER.debug("Tracking request {} set to not revalidate as it was closed",
requestToRevalidate.getId());
requestToRevalidate.setNeedsRevalidation(false); requestToRevalidate.setNeedsRevalidation(false);
} }
// push the update with the potentially updated validation flag or error count // push the update with the potentially updated validation flag or error count
dao.add(new RDBMSQuery<>(wrap, filters.get(GithubWebhookTracking.class)), Arrays.asList(requestToRevalidate)); dao.add(new RDBMSQuery<>(wrap, filters.get(GithubWebhookTracking.class)),
Arrays.asList(requestToRevalidate));
} }
} }
/** /**
* Checks if the manual revalidation count has reached the configured threshold * Checks if the manual revalidation count has reached the configured threshold and sends an email
* and sends an email notification if it has. * notification if it has and the notification alert is enabled.
* *
* @param tracking the webhook tracking object * @param tracking the webhook tracking object
*/ */
private void checkRevalidationThreshold(GithubWebhookTracking tracking) { private void checkRevalidationThreshold(GithubWebhookTracking tracking) {
// If revalidation alert is not enabled, do nothing
if (!Boolean.TRUE.equals(isRevalidationAlertEnabled.get()))
return;
Integer threshold = revalidationThreshold.get(); Integer threshold = revalidationThreshold.get();
// If manual revalidation count is null or less than threshold, do nothing // If manual revalidation count is null or less than threshold, do nothing
if (Objects.isNull(tracking.getManualRevalidationCount()) || if (Objects.isNull(tracking.getManualRevalidationCount())
tracking.getManualRevalidationCount() < threshold) { || tracking.getManualRevalidationCount() < threshold) {
return; return;
} }
...@@ -201,8 +204,7 @@ public class GithubRevalidationQueue { ...@@ -201,8 +204,7 @@ public class GithubRevalidationQueue {
} }
/** /**
* Sends an email alert about a webhook that has reached the revalidation * Sends an email alert about a webhook that has reached the revalidation threshold.
* threshold.
* *
* @param tracking the webhook tracking object * @param tracking the webhook tracking object
*/ */
...@@ -211,7 +213,8 @@ public class GithubRevalidationQueue { ...@@ -211,7 +213,8 @@ public class GithubRevalidationQueue {
mailerService.sendRevalidationAlert(tracking, revalidationThreshold.get()); mailerService.sendRevalidationAlert(tracking, revalidationThreshold.get());
LOGGER.info("Sent revalidation alert email for request ID {}", tracking.getId()); LOGGER.info("Sent revalidation alert email for request ID {}", tracking.getId());
} catch (RuntimeException e) { } catch (RuntimeException e) {
LOGGER.error("Failed to send revalidation alert email for request ID {}", tracking.getId(), e); LOGGER.error("Failed to send revalidation alert email for request ID {}", tracking.getId(),
e);
} }
} }
} }
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