Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webhook): WebHook Listener Microservice & Receiver Setup #16

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,6 @@ install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
CMakeUserPresets.json
CMakeUserPresets.json

.DS_STORE
6 changes: 5 additions & 1 deletion server/application-server/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
DATASOURCE_URL=jdbc:postgresql://127.0.0.1/helios
DATASOURCE_USERNAME=helios
DATASOURCE_PASSWORD=helios
DATASOURCE_PASSWORD=helios
NATS_SERVER=localhost:4222
NATS_AUTH_TOKEN='5760e8ae09adfb2756f9f8cd5cb2caa704cd3f549eaa9298be843ceb165185d815b81f90c680fa7f626b7cd63abf6ac9'
REPOSITORY_NAME=<repository_name e.g. ls1intum/Helios>

2 changes: 2 additions & 0 deletions server/application-server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ out/
### VS Code ###
.vscode/
.sdkmanrc

.DS_STORE
2 changes: 1 addition & 1 deletion server/application-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ $ ./gradlew build

**3. Setup configuration and environment**

Copy the file `.env.example` to `.env` and adjust the values to your needs. It is automatically set up to work with the Docker Compose setup.
Copy the file `.env.example` to `.env` and adjust the values to your needs. It is set up to work with the Docker Compose setup for database. You need to adjust some fields for NATS server.

```bash
$ cp .env.example .env
Expand Down
50 changes: 26 additions & 24 deletions server/application-server/build.gradle
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
plugins {
id 'java'
id 'war'
id 'org.springframework.boot' version '3.3.4'
id 'io.spring.dependency-management' version '1.1.6'
id 'io.freefair.lombok' version '8.10.2'
id 'org.springdoc.openapi-gradle-plugin' version '1.9.0'
id 'org.openapi.generator' version '6.6.0'
id 'java'
id 'war'
id 'org.springframework.boot' version '3.3.4'
id 'io.spring.dependency-management' version '1.1.6'
id 'io.freefair.lombok' version '8.10.2'
id 'org.springdoc.openapi-gradle-plugin' version '1.9.0'
id 'org.openapi.generator' version '6.6.0'
}

group = 'de.tum.cit.aet'
version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(22)
}
toolchain {
languageVersion = JavaLanguageVersion.of(22)
}
}

repositories {
mavenCentral()
mavenCentral()
}

def loadEnvFile() {
Expand Down Expand Up @@ -48,27 +48,29 @@ def loadEnvFile() {


dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0'
implementation 'org.openapitools:jackson-databind-nullable:0.2.6'
runtimeOnly 'org.postgresql:postgresql'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0'
implementation 'org.openapitools:jackson-databind-nullable:0.2.6'
implementation 'io.nats:jnats:2.20.4'
implementation 'org.kohsuke:github-api:1.326'
runtimeOnly 'org.postgresql:postgresql'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
useJUnitPlatform()
useJUnitPlatform()
}

openApi {
apiDocsUrl = 'http://localhost:8080/v3/api-docs.yaml'
outputDir = file('.')
outputFileName = 'openapi.yaml'
def envVars = loadEnvFile()
customBootRun {
args.set(["--spring.profiles.active=dev", "--DATASOURCE_URL=${envVars['DATASOURCE_URL'] ?: ''}", "--DATASOURCE_USERNAME=${envVars['DATASOURCE_USERNAME'] ?: ''}", "--DATASOURCE_PASSWORD=${envVars['DATASOURCE_PASSWORD'] ?: ''}"])
}
def envVars = loadEnvFile()
customBootRun {
args.set(["--spring.profiles.active=dev", "--DATASOURCE_URL=${envVars['DATASOURCE_URL'] ?: ''}", "--DATASOURCE_USERNAME=${envVars['DATASOURCE_USERNAME'] ?: ''}", "--DATASOURCE_PASSWORD=${envVars['DATASOURCE_PASSWORD'] ?: ''}"])
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.tum.cit.aet.helios.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import io.nats.client.Connection;
import io.nats.client.Nats;
import io.nats.client.Options;

@Configuration
public class NatsConfig {

@Value("${nats.server}")
private String natsServer;

@Value("${nats.auth.token}")
private String natsAuthToken;

@Bean
public Connection natsConnection() throws Exception {

Options options = Options.builder().server(natsServer).token(natsAuthToken).build();
return Nats.connect(options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package de.tum.cit.aet.helios.gitprovider.common.github;

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import org.kohsuke.github.GHEvent;
import org.kohsuke.github.GHEventPayload;
import org.kohsuke.github.GitHub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.nats.client.Message;
import io.nats.client.MessageHandler;
import org.springframework.stereotype.Component;

@Component
public abstract class GitHubMessageHandler<T extends GHEventPayload> implements MessageHandler {

private static final Logger logger = LoggerFactory.getLogger(GitHubMessageHandler.class);

private final Class<T> payloadType;

protected GitHubMessageHandler(Class<T> payloadType) {
this.payloadType = payloadType;
}

@Override
public void onMessage(Message msg) {
String eventType = getHandlerEvent().name().toLowerCase();
String subject = msg.getSubject();
if (!subject.endsWith(eventType)) {
logger.error("Received message on unexpected subject: {}, expected to end with {}", subject, eventType);
return;
}

String payload = new String(msg.getData(), StandardCharsets.UTF_8);

try (StringReader reader = new StringReader(payload)) {
T eventPayload = GitHub.offline().parseEventPayload(reader, payloadType);
handleEvent(eventPayload);
} catch (IOException e) {
logger.error("Failed to parse payload for subject {}: {}", subject, e.getMessage(), e);
} catch (Exception e) {
logger.error("Unexpected error while handling message for subject {}: {}", subject, e.getMessage(), e);
}
}

/**
* Handles the parsed event payload.
*
* @param eventPayload The parsed GHEventPayload.
*/
protected abstract void handleEvent(T eventPayload);

/**
* Returns the GHEvent that this handler is responsible for.
*
* @return The GHEvent.
*/
protected abstract GHEvent getHandlerEvent();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.tum.cit.aet.helios.gitprovider.common.github;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kohsuke.github.GHEvent;
import org.springframework.stereotype.Component;

@Component
public class GitHubMessageHandlerRegistry {

private final Map<GHEvent, GitHubMessageHandler<?>> handlerMap = new HashMap<>();

public GitHubMessageHandlerRegistry(GitHubMessageHandler<?>[] handlers) {
for (GitHubMessageHandler<?> handler : handlers) {
handlerMap.put(handler.getHandlerEvent(), handler);
}
}

public GitHubMessageHandler<?> getHandler(GHEvent eventType) {
return handlerMap.get(eventType);
}

public List<GHEvent> getSupportedEvents() {
return new ArrayList<>(handlerMap.keySet());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.tum.cit.aet.helios.gitprovider.issue.github;

import org.kohsuke.github.GHEvent;
import org.kohsuke.github.GHEventPayload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import de.tum.cit.aet.helios.gitprovider.common.github.GitHubMessageHandler;

@Component
public class GitHubIssueMessageHandler extends GitHubMessageHandler<GHEventPayload.Issue> {

private static final Logger logger = LoggerFactory.getLogger(GitHubIssueMessageHandler.class);

private GitHubIssueMessageHandler() {
super(GHEventPayload.Issue.class);
}

@Override
protected void handleEvent(GHEventPayload.Issue eventPayload) {
var action = eventPayload.getAction();
var repository = eventPayload.getRepository();
var issue = eventPayload.getIssue();
logger.info("Received issue event for repository: {}, issue: {}, action: {}",
repository.getFullName(),
issue.getNumber(),
action);
}

@Override
protected GHEvent getHandlerEvent() {
return GHEvent.ISSUES;
}
}
Loading