From fbc4b3c31d68bb4406a9f287ce22ef9ed0dbef1b Mon Sep 17 00:00:00 2001 From: Agaba Derrick Date: Mon, 28 Jul 2025 00:16:40 +0300 Subject: [PATCH] add event processor and logger implementations Signed-off-by: Agaba Derrick --- .../NotificationBotApplication.java | 3 +- .../Notificationbot/rest/HelloController.java | 2 +- src/main/java/derrick/logging/Logger | 6 +++ src/main/java/derrick/logging/LoggerImpl | 14 +++++ .../processor/BuildFailureEventProcessor.java | 5 ++ .../derrick/processor/EventProcessor.java | 23 ++++++++ .../processor/PullRequestEventProcessor | 53 +++++++++++++++++++ .../derrick/pullrequest/PullRequestService | 8 +++ .../pullrequest/PullRequestServiceImpl | 34 ++++++++++++ 9 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 src/main/java/derrick/logging/Logger create mode 100644 src/main/java/derrick/logging/LoggerImpl create mode 100644 src/main/java/derrick/processor/BuildFailureEventProcessor.java create mode 100644 src/main/java/derrick/processor/EventProcessor.java create mode 100644 src/main/java/derrick/processor/PullRequestEventProcessor create mode 100644 src/main/java/derrick/pullrequest/PullRequestService create mode 100644 src/main/java/derrick/pullrequest/PullRequestServiceImpl diff --git a/src/main/java/derrick/Notificationbot/NotificationBotApplication.java b/src/main/java/derrick/Notificationbot/NotificationBotApplication.java index 4db9941..596bc2f 100644 --- a/src/main/java/derrick/Notificationbot/NotificationBotApplication.java +++ b/src/main/java/derrick/Notificationbot/NotificationBotApplication.java @@ -1,4 +1,5 @@ -package com.derick.notificationbot; +package derrick.Notificationbot; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/src/main/java/derrick/Notificationbot/rest/HelloController.java b/src/main/java/derrick/Notificationbot/rest/HelloController.java index d560201..c756fdd 100644 --- a/src/main/java/derrick/Notificationbot/rest/HelloController.java +++ b/src/main/java/derrick/Notificationbot/rest/HelloController.java @@ -1,4 +1,4 @@ -package com.derick.notificationbot; +package derrick.Notificationbot.rest; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; diff --git a/src/main/java/derrick/logging/Logger b/src/main/java/derrick/logging/Logger new file mode 100644 index 0000000..0450f6a --- /dev/null +++ b/src/main/java/derrick/logging/Logger @@ -0,0 +1,6 @@ +package derrick.logging; + +public interface Logger { + void info(String message); + void error(String message, Throwable t); +} diff --git a/src/main/java/derrick/logging/LoggerImpl b/src/main/java/derrick/logging/LoggerImpl new file mode 100644 index 0000000..8cc5c5b --- /dev/null +++ b/src/main/java/derrick/logging/LoggerImpl @@ -0,0 +1,14 @@ +package derrick.logging.impl; + +import derrick.logging.Logger; + +public class ConsoleLogger implements Logger { + public void info(String message) { + System.out.println("[INFO] " + message); // Can replace later with structured logging + } + + public void error(String message, Throwable t) { + System.err.println("[ERROR] " + message); + t.printStackTrace(System.err); + } +} diff --git a/src/main/java/derrick/processor/BuildFailureEventProcessor.java b/src/main/java/derrick/processor/BuildFailureEventProcessor.java new file mode 100644 index 0000000..451e9d6 --- /dev/null +++ b/src/main/java/derrick/processor/BuildFailureEventProcessor.java @@ -0,0 +1,5 @@ +package derrick.processor; + +public class BuildFailureEventProcessor { + +} diff --git a/src/main/java/derrick/processor/EventProcessor.java b/src/main/java/derrick/processor/EventProcessor.java new file mode 100644 index 0000000..a79d55b --- /dev/null +++ b/src/main/java/derrick/processor/EventProcessor.java @@ -0,0 +1,23 @@ +package derrick.processor; + +/** + * EventProcessor defines the contract for processing different types of CI/CD + * events. + */ +public interface EventProcessor { + + /** + * Checks if this processor supports the given event type. + * + * @param eventType The event type (e.g. pull_request, build_failure) + * @return true if supported, false otherwise + */ + boolean supports(String eventType); + + /** + * Processes the given event payload. + * + * @param payload The event payload as a String (typically JSON) + */ + void processEvent(String payload); +} \ No newline at end of file diff --git a/src/main/java/derrick/processor/PullRequestEventProcessor b/src/main/java/derrick/processor/PullRequestEventProcessor new file mode 100644 index 0000000..b94aad3 --- /dev/null +++ b/src/main/java/derrick/processor/PullRequestEventProcessor @@ -0,0 +1,53 @@ +package derrick.processor; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import derrick.service.PullRequestService; +import derrick.util.Logger; + +public class PullRequestEventProcessor implements EventProcessor { + + private static final String SUPPORTED_EVENT = "pull_request"; + + private final ObjectMapper objectMapper; + private final PullRequestService pullRequestService; + private final Logger logger; + + public PullRequestEventProcessor(PullRequestService pullRequestService, Logger logger) { + this.objectMapper = new ObjectMapper(); + this.pullRequestService = pullRequestService; + this.logger = logger; + } + + @Override + public boolean supports(String eventType) { + return SUPPORTED_EVENT.equalsIgnoreCase(eventType); + } + + @Override + public void processEvent(String payload) { + try { + JsonNode root = objectMapper.readTree(payload); + JsonNode actionNode = root.path("action"); + JsonNode pullRequestNode = root.path("pull_request"); + + if (actionNode.isMissingNode() || pullRequestNode.isMissingNode()) { + logger.warn("Missing pull request action or data in payload."); + return; + } + + String action = actionNode.asText(); + String prTitle = pullRequestNode.path("title").asText(); + String prUrl = pullRequestNode.path("html_url").asText(); + String author = pullRequestNode.path("user").path("login").asText(); + + pullRequestService.handlePullRequest(action, prTitle, prUrl, author); + + logger.info(String.format("Processed pull request event: action=%s, title=%s, author=%s", + action, prTitle, author)); + + } catch (Exception e) { + logger.error("Failed to process pull request event", e); + } + } +} diff --git a/src/main/java/derrick/pullrequest/PullRequestService b/src/main/java/derrick/pullrequest/PullRequestService new file mode 100644 index 0000000..ba8ebae --- /dev/null +++ b/src/main/java/derrick/pullrequest/PullRequestService @@ -0,0 +1,8 @@ +package derrick.service; + +public interface PullRequestService { + void commentOnPullRequest(String payload); + void validatePullRequest(String payload); + void mergePullRequest(String payload); + String fetchPullRequestInfo(String pullRequestId); +} \ No newline at end of file diff --git a/src/main/java/derrick/pullrequest/PullRequestServiceImpl b/src/main/java/derrick/pullrequest/PullRequestServiceImpl new file mode 100644 index 0000000..bbf7287 --- /dev/null +++ b/src/main/java/derrick/pullrequest/PullRequestServiceImpl @@ -0,0 +1,34 @@ +package derrick.service.impl; + +import derrick.service.PullRequestService; + +public class GitHubPullRequestServiceImpl implements PullRequestService { + @Override + public void commentOnPullRequest(String payload) { + // Implementation for commenting on a pull request + // This could involve using GitHub's API to post a comment + // based on the provided payload. + } + + @Override + public void validatePullRequest(String payload) { + // Implementation for validating a pull request + // This could involve checking the status of the pull request + // or ensuring it meets certain criteria. + } + + @Override + public void mergePullRequest(String payload) { + // Implementation for merging a pull request + // This could involve using GitHub's API to merge the pull request + // based on the provided payload. + } + + @Override + public String fetchPullRequestInfo(String pullRequestId) { + // Implementation for fetching pull request information + // This could involve using GitHub's API to retrieve details + // about the specified pull request. + return null; + } +}