-
Notifications
You must be signed in to change notification settings - Fork 31
Add notification to pipeline #2325
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...equest-processor/src/main/java/com/redhat/hacbs/container/notification/NotifyCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package com.redhat.hacbs.container.notification; | ||
|
|
||
| import static org.apache.commons.lang3.StringUtils.isEmpty; | ||
|
|
||
| import java.net.http.HttpClient; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.time.Duration; | ||
| import java.util.Optional; | ||
|
|
||
| import jakarta.inject.Inject; | ||
|
|
||
| import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
| import org.jboss.pnc.api.dto.Request; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| import io.quarkus.logging.Log; | ||
| import picocli.CommandLine; | ||
|
|
||
| @CommandLine.Command(name = "notify") | ||
| public class NotifyCommand implements Runnable { | ||
|
|
||
| @CommandLine.Option(names = "--build-id", required = true) | ||
| String buildId; | ||
|
|
||
| @CommandLine.Option(names = "--status", required = true) | ||
| String status; | ||
|
|
||
| @CommandLine.Option(names = "--context", required = true) | ||
| String context; | ||
|
|
||
| @CommandLine.Option(names = "--request-timeout") | ||
| int requestTimeout = 15; | ||
|
|
||
| // TODO: do we need this... | ||
| @ConfigProperty(name = "access.token") | ||
| Optional<String> accessToken; | ||
|
|
||
| @Inject | ||
| ObjectMapper objectMapper; | ||
|
|
||
| public void run() { | ||
| try { | ||
|
|
||
| if (isEmpty(context)) { | ||
| Log.infof("No callback configured ; unable to notify."); | ||
| return; | ||
| } | ||
|
|
||
| Request callback = objectMapper.readValue(context, Request.class ); | ||
|
|
||
| Log.infof("Notification for build %s with status %s and callback %s", buildId, status, callback); | ||
| PipelineNotification notification = PipelineNotification.builder().buildId(buildId).status(status).completionCallback((Request) callback.getAttachment()).build(); | ||
| String body = objectMapper.writeValueAsString(notification); | ||
|
|
||
| HttpRequest.Builder builder = HttpRequest.newBuilder() | ||
| .uri(callback.getUri()) | ||
| .method(callback.getMethod().name(), HttpRequest.BodyPublishers.ofString(body)) | ||
| .timeout(Duration.ofSeconds(requestTimeout)); | ||
| callback.getHeaders().forEach(h -> builder.header(h.getName(), h.getValue())); | ||
|
|
||
|
|
||
| HttpRequest request = builder.build(); | ||
| // TODO: Retry? Send async? Some useful mutiny examples from quarkus in https://gist.github.com/cescoffier/e9abce907a1c3d05d70bea3dae6dc3d5 | ||
| HttpResponse<String> response; | ||
| try (HttpClient httpClient = HttpClient.newHttpClient()) { | ||
| response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); | ||
| } | ||
| Log.infof("Response %s", response); | ||
|
|
||
| } catch (Exception e) { | ||
| Log.error("Notification failed", e); | ||
| throw new RuntimeException(e); | ||
|
Check warning on line 74 in java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/notification/NotifyCommand.java
|
||
| } | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
...processor/src/main/java/com/redhat/hacbs/container/notification/PipelineNotification.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.redhat.hacbs.container.notification; | ||
|
|
||
| import org.jboss.pnc.api.dto.Request; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
|
||
| import lombok.Builder; | ||
|
|
||
| // TODO: This is a direct copy of the same class in konflux-build-driver. Both need moved to pnc-api to | ||
| // avoid clashes and duplication. For instance, we can't depend upon konflux-build-driver as that | ||
| // then leads to oidc client issues. | ||
| @Builder(builderClassName = "Builder") | ||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| public record PipelineNotification( | ||
| String status, | ||
| String buildId, | ||
| Request completionCallback) { | ||
|
|
||
| } |
73 changes: 73 additions & 0 deletions
73
...est-processor/src/test/java/com/redhat/hacbs/container/notification/NotificationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package com.redhat.hacbs.container.notification; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.util.List; | ||
| import java.util.logging.LogRecord; | ||
|
|
||
| import javax.ws.rs.core.MediaType; | ||
|
|
||
| import org.jboss.pnc.api.constants.HttpHeaders; | ||
| import org.jboss.pnc.api.dto.Request; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.github.tomakehurst.wiremock.WireMockServer; | ||
|
|
||
| import io.quarkus.test.LogCollectingTestResource; | ||
| import io.quarkus.test.common.QuarkusTestResource; | ||
| import io.quarkus.test.common.ResourceArg; | ||
| import io.quarkus.test.junit.QuarkusTest; | ||
|
|
||
| @QuarkusTest | ||
| @QuarkusTestResource(value = LogCollectingTestResource.class, restrictToAnnotatedClass = true, initArgs = @ResourceArg(name = LogCollectingTestResource.LEVEL, value = "FINE")) | ||
| @QuarkusTestResource(WireMockExtensions.class) | ||
| public class NotificationTest { | ||
|
|
||
| private WireMockServer wireMockServer; | ||
|
|
||
| @BeforeEach | ||
| public void clearLogs() { | ||
| LogCollectingTestResource.current().clear(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNoNotify() { | ||
| NotifyCommand notifyCommand = new NotifyCommand(); | ||
| notifyCommand.status = "Succeeded"; | ||
| notifyCommand.buildId = "1234"; | ||
| notifyCommand.run(); | ||
| List<LogRecord> logRecords = LogCollectingTestResource.current().getRecords(); | ||
| assertTrue(logRecords.stream() | ||
| .anyMatch(r -> LogCollectingTestResource.format(r).contains("No callback configured ; unable to notify"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNotify() throws IOException, URISyntaxException { | ||
|
|
||
| Request request = Request.builder() | ||
| .method(Request.Method.PUT) | ||
| .header(new Request.Header(HttpHeaders.CONTENT_TYPE_STRING, MediaType.APPLICATION_JSON)) | ||
| .attachment(null) | ||
| .uri(new URI(wireMockServer.baseUrl() + "/internal/completed")) | ||
| .build(); | ||
|
|
||
| System.err.println("### wiremock uri: " + wireMockServer.baseUrl()); | ||
| // {"method":"PUT","uri":"http://localhost:8081/internal/completed","headers":[{"name":"Content-Type","value":"application/json"}],"attachment":null} | ||
|
|
||
| NotifyCommand notifyCommand = new NotifyCommand(); | ||
| notifyCommand.status = "Succeeded"; | ||
| notifyCommand.buildId = "1234"; | ||
| notifyCommand.objectMapper = new ObjectMapper(); | ||
| notifyCommand.context = notifyCommand.objectMapper.writeValueAsString(request); | ||
| notifyCommand.run(); | ||
|
|
||
| List<LogRecord> logRecords = LogCollectingTestResource.current().getRecords(); | ||
| assertTrue(logRecords.stream() | ||
| .anyMatch(r -> LogCollectingTestResource.format(r).contains("Response (PUT http://localhost:8080/internal/completed) 200"))); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
...t-processor/src/test/java/com/redhat/hacbs/container/notification/WireMockExtensions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.redhat.hacbs.container.notification; | ||
|
|
||
| import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.put; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; | ||
| import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import com.github.tomakehurst.wiremock.WireMockServer; | ||
| import com.github.tomakehurst.wiremock.common.ConsoleNotifier; | ||
|
|
||
| import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; | ||
|
|
||
| public class WireMockExtensions implements QuarkusTestResourceLifecycleManager { | ||
| private WireMockServer wireMockServer; | ||
|
|
||
| @Override | ||
| public Map<String, String> start() { | ||
| wireMockServer = new WireMockServer(wireMockConfig().notifier(new ConsoleNotifier(true))); | ||
| wireMockServer.start(); | ||
|
|
||
| wireMockServer.stubFor( | ||
| put(urlEqualTo("/internal/completed")) | ||
| .withRequestBody( | ||
| equalToJson("{\"status\":\"Succeeded\",\"buildId\":\"1234\",\"completionCallback\":null}")) | ||
| .willReturn(aResponse() | ||
| .withStatus(200))); | ||
|
|
||
| return Map.of("quarkus.rest-client.wiremockextensions.url", wireMockServer.baseUrl()); | ||
| } | ||
|
|
||
| @Override | ||
| public void stop() { | ||
| if (wireMockServer != null) { | ||
| wireMockServer.stop(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void inject(TestInjector testInjector) { | ||
| testInjector.injectIntoFields(wireMockServer, new TestInjector.MatchesType(WireMockServer.class)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you miss param BULID_ID?