From 24aa92b4dc47a4b027ec59929a058525c2cb3c11 Mon Sep 17 00:00:00 2001 From: Ricardo Zanini <1538000+ricardozanini@users.noreply.github.com> Date: Thu, 10 Dec 2020 11:56:39 -0300 Subject: [PATCH] KOGITO-3659 - Remove duplicate services for SW GH example (#482) Signed-off-by: Ricardo Zanini --- .../workflow/AbstractGitHubService.java | 110 ------------------ .../sw/github/workflow/GitHubService.java | 79 ++++++++++++- .../github/workflow/GitHubServiceBackend.java | 29 ----- .../workflow/GitHubServiceFrontend.java | 29 ----- .../src/main/resources/handle-backend.sw.json | 4 +- .../main/resources/handle-frontend.sw.json | 4 +- .../sw/github/workflow/GitHubServiceTest.java | 4 +- .../workflow/PRCheckerWorkflowTest.java | 8 +- 8 files changed, 86 insertions(+), 181 deletions(-) delete mode 100644 serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/AbstractGitHubService.java delete mode 100644 serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/GitHubServiceBackend.java delete mode 100644 serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/GitHubServiceFrontend.java diff --git a/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/AbstractGitHubService.java b/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/AbstractGitHubService.java deleted file mode 100644 index e00bee6050..0000000000 --- a/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/AbstractGitHubService.java +++ /dev/null @@ -1,110 +0,0 @@ -/** - * Copyright 2020 Red Hat, Inc. and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.kogito.examples.sw.github.workflow; - -import java.util.Collections; -import java.util.Objects; - -import javax.inject.Inject; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import org.eclipse.microprofile.rest.client.inject.RestClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -// remove after https://issues.redhat.com/browse/KOGITO-3659 -public abstract class AbstractGitHubService { - private static final Logger LOGGER = LoggerFactory.getLogger(GitHubService.class); - - @Inject - @RestClient - GitHubClient gitHubClient; - - public JsonNode addLabels(JsonNode pullRequest) { - LOGGER.info("Adding labels to PR"); - final String repoName = getRepoFullName(pullRequest); - if (repoName == null) { - return pullRequest; - } - if (pullRequest.get("labels") == null) { - LOGGER.error("Skipping adding labels. 'labels' attribute not found in the PR object: {}", pullRequest); - return pullRequest; - } - LOGGER.info("Calling GitHub Client to perform addLabels action"); - gitHubClient.addLabels( - repoName.split("/")[0], - repoName.split("/")[1], - Objects.requireNonNull(pullRequest.get("number")).asInt(), - Collections.singletonList(pullRequest.get("labels").asText())); - return pullRequest; - } - - public JsonNode addReviewers(JsonNode pullRequest) { - LOGGER.info("Adding reviewers to PR"); - final String repoName = getRepoFullName(pullRequest); - if (repoName == null) { - return pullRequest; - } - if (pullRequest.get("reviewers") == null) { - LOGGER.error("Skipping adding reviewers. 'reviewers' attribute not found in the PR object: {}", pullRequest); - return pullRequest; - } - LOGGER.info("Calling GitHub Client to perform addReviewers action"); - gitHubClient.addReviewers( - repoName.split("/")[0], - repoName.split("/")[1], - Objects.requireNonNull(pullRequest.get("number")).asInt(), - Collections.singletonList(pullRequest.get("reviewers").asText())); - return pullRequest; - } - - public JsonNode fetchPRFiles(JsonNode pullRequest) { - LOGGER.info("Fetching files for PR"); - final String repoName = getRepoFullName(pullRequest); - if (repoName == null) { - return pullRequest; - } - final JsonNode jsonNode = gitHubClient.fetchFiles( - repoName.split("/")[0], - repoName.split("/")[1], - Objects.requireNonNull(pullRequest.get("number")).asInt()); - if (pullRequest.isObject()) { - ((ObjectNode)pullRequest).replace("files", jsonNode); - } else { - LOGGER.error("Pull Request JsonNode is not an object: {}", pullRequest); - } - return pullRequest; - } - - private String getRepoFullName(JsonNode pullRequest) { - if (pullRequest.get("repository") == null) { - LOGGER.error("Impossible to resolve the repository name for {}, no 'repository' tag found.", pullRequest); - return null; - } - final String repoName = pullRequest.get("repository").get("full_name").asText(); - if ("".equals(repoName)) { - LOGGER.error("Impossible to resolve the repository name for {}", pullRequest); - return null; - } else if (!repoName.contains("/")) { - LOGGER.error("Wrong format for repository name {}", repoName); - return null; - } - LOGGER.info("Extracted repository name from PR: {}", repoName); - return repoName; - } -} diff --git a/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/GitHubService.java b/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/GitHubService.java index cc0ffd14b2..afd357dc08 100644 --- a/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/GitHubService.java +++ b/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/GitHubService.java @@ -32,7 +32,84 @@ * Simple wrapper class to call the github-service. */ @ApplicationScoped -public class GitHubService extends AbstractGitHubService { +public class GitHubService { + private static final Logger LOGGER = LoggerFactory.getLogger(GitHubService.class); + @Inject + @RestClient + GitHubClient gitHubClient; + + public JsonNode addLabels(JsonNode pullRequest) { + LOGGER.info("Adding labels to PR"); + final String repoName = getRepoFullName(pullRequest); + if (repoName == null) { + return pullRequest; + } + if (pullRequest.get("labels") == null) { + LOGGER.error("Skipping adding labels. 'labels' attribute not found in the PR object: {}", pullRequest); + return pullRequest; + } + LOGGER.info("Calling GitHub Client to perform addLabels action"); + gitHubClient.addLabels( + repoName.split("/")[0], + repoName.split("/")[1], + Objects.requireNonNull(pullRequest.get("number")).asInt(), + Collections.singletonList(pullRequest.get("labels").asText())); + return pullRequest; + } + + public JsonNode addReviewers(JsonNode pullRequest) { + LOGGER.info("Adding reviewers to PR"); + final String repoName = getRepoFullName(pullRequest); + if (repoName == null) { + return pullRequest; + } + if (pullRequest.get("reviewers") == null) { + LOGGER.error("Skipping adding reviewers. 'reviewers' attribute not found in the PR object: {}", pullRequest); + return pullRequest; + } + LOGGER.info("Calling GitHub Client to perform addReviewers action"); + gitHubClient.addReviewers( + repoName.split("/")[0], + repoName.split("/")[1], + Objects.requireNonNull(pullRequest.get("number")).asInt(), + Collections.singletonList(pullRequest.get("reviewers").asText())); + return pullRequest; + } + + public JsonNode fetchPRFiles(JsonNode pullRequest) { + LOGGER.info("Fetching files for PR"); + final String repoName = getRepoFullName(pullRequest); + if (repoName == null) { + return pullRequest; + } + final JsonNode jsonNode = gitHubClient.fetchFiles( + repoName.split("/")[0], + repoName.split("/")[1], + Objects.requireNonNull(pullRequest.get("number")).asInt()); + if (pullRequest.isObject()) { + ((ObjectNode)pullRequest).replace("files", jsonNode); + } else { + LOGGER.error("Pull Request JsonNode is not an object: {}", pullRequest); + } + return pullRequest; + } + + private String getRepoFullName(JsonNode pullRequest) { + if (pullRequest.get("repository") == null) { + LOGGER.error("Impossible to resolve the repository name for {}, no 'repository' tag found.", pullRequest); + return null; + } + final String repoName = pullRequest.get("repository").get("full_name").asText(); + if ("".equals(repoName)) { + LOGGER.error("Impossible to resolve the repository name for {}", pullRequest); + return null; + } else if (!repoName.contains("/")) { + LOGGER.error("Wrong format for repository name {}", repoName); + return null; + } + LOGGER.info("Extracted repository name from PR: {}", repoName); + return repoName; + } } diff --git a/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/GitHubServiceBackend.java b/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/GitHubServiceBackend.java deleted file mode 100644 index 8a71053cb3..0000000000 --- a/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/GitHubServiceBackend.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Copyright 2020 Red Hat, Inc. and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.kogito.examples.sw.github.workflow; - -import javax.enterprise.context.ApplicationScoped; - -/** - * Placeholder for the SW file duo to codegen bug. - * - * JIRA: https://issues.redhat.com/browse/KOGITO-3659 - */ -@ApplicationScoped -public class GitHubServiceBackend extends AbstractGitHubService { - -} \ No newline at end of file diff --git a/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/GitHubServiceFrontend.java b/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/GitHubServiceFrontend.java deleted file mode 100644 index fe7983e2de..0000000000 --- a/serverless-workflow-github-showcase/pr-checker-workflow/src/main/java/org/kogito/examples/sw/github/workflow/GitHubServiceFrontend.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Copyright 2020 Red Hat, Inc. and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.kogito.examples.sw.github.workflow; - -import javax.enterprise.context.ApplicationScoped; - -/** - * Placeholder for the SW file duo to codegen bug. - * - * JIRA: https://issues.redhat.com/browse/KOGITO-3659 - */ -@ApplicationScoped -public class GitHubServiceFrontend extends AbstractGitHubService { - -} \ No newline at end of file diff --git a/serverless-workflow-github-showcase/pr-checker-workflow/src/main/resources/handle-backend.sw.json b/serverless-workflow-github-showcase/pr-checker-workflow/src/main/resources/handle-backend.sw.json index b65715deea..6e3662936e 100644 --- a/serverless-workflow-github-showcase/pr-checker-workflow/src/main/resources/handle-backend.sw.json +++ b/serverless-workflow-github-showcase/pr-checker-workflow/src/main/resources/handle-backend.sw.json @@ -7,7 +7,7 @@ "name": "AddLabels", "type": "service", "metadata": { - "interface": "org.kogito.examples.sw.github.workflow.GitHubServiceBackend", + "interface": "org.kogito.examples.sw.github.workflow.GitHubService", "operation": "addLabels" } }, @@ -15,7 +15,7 @@ "name": "AddReviewers", "type": "service", "metadata": { - "interface": "org.kogito.examples.sw.github.workflow.GitHubServiceBackend", + "interface": "org.kogito.examples.sw.github.workflow.GitHubService", "operation": "addReviewers" } } diff --git a/serverless-workflow-github-showcase/pr-checker-workflow/src/main/resources/handle-frontend.sw.json b/serverless-workflow-github-showcase/pr-checker-workflow/src/main/resources/handle-frontend.sw.json index e24d46ffa2..15465b779b 100644 --- a/serverless-workflow-github-showcase/pr-checker-workflow/src/main/resources/handle-frontend.sw.json +++ b/serverless-workflow-github-showcase/pr-checker-workflow/src/main/resources/handle-frontend.sw.json @@ -7,7 +7,7 @@ "name": "AddLabels", "type": "service", "metadata": { - "interface": "org.kogito.examples.sw.github.workflow.GitHubServiceFrontend", + "interface": "org.kogito.examples.sw.github.workflow.GitHubService", "operation": "addLabels" } }, @@ -15,7 +15,7 @@ "name": "AddReviewers", "type": "service", "metadata": { - "interface": "org.kogito.examples.sw.github.workflow.GitHubServiceFrontend", + "interface": "org.kogito.examples.sw.github.workflow.GitHubService", "operation": "addReviewers" } } diff --git a/serverless-workflow-github-showcase/pr-checker-workflow/src/test/java/org/kogito/examples/sw/github/workflow/GitHubServiceTest.java b/serverless-workflow-github-showcase/pr-checker-workflow/src/test/java/org/kogito/examples/sw/github/workflow/GitHubServiceTest.java index cf6ae72c69..3471facda0 100644 --- a/serverless-workflow-github-showcase/pr-checker-workflow/src/test/java/org/kogito/examples/sw/github/workflow/GitHubServiceTest.java +++ b/serverless-workflow-github-showcase/pr-checker-workflow/src/test/java/org/kogito/examples/sw/github/workflow/GitHubServiceTest.java @@ -26,7 +26,7 @@ import io.quarkus.test.junit.QuarkusTest; import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; @QuarkusTest @QuarkusTestResource(GitHubServiceMockServer.class) @@ -35,7 +35,7 @@ class GitHubServiceTest { final ObjectMapper objectMapper = new ObjectMapper(); @Inject - GitHubServiceBackend gitHubServiceBackend; + GitHubService gitHubServiceBackend; @Test void addLabels() throws IOException { diff --git a/serverless-workflow-github-showcase/pr-checker-workflow/src/test/java/org/kogito/examples/sw/github/workflow/PRCheckerWorkflowTest.java b/serverless-workflow-github-showcase/pr-checker-workflow/src/test/java/org/kogito/examples/sw/github/workflow/PRCheckerWorkflowTest.java index 10198ed3de..330c7194c7 100644 --- a/serverless-workflow-github-showcase/pr-checker-workflow/src/test/java/org/kogito/examples/sw/github/workflow/PRCheckerWorkflowTest.java +++ b/serverless-workflow-github-showcase/pr-checker-workflow/src/test/java/org/kogito/examples/sw/github/workflow/PRCheckerWorkflowTest.java @@ -19,20 +19,17 @@ import java.io.IOException; import java.nio.charset.Charset; -import javax.ws.rs.core.MediaType; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import com.github.jknack.handlebars.internal.Files; import io.cloudevents.jackson.JsonFormat; import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static io.restassured.RestAssured.given; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.*; @QuarkusTest @QuarkusTestResource(GitHubServiceMockServer.class) // mock the GitHub API @@ -51,5 +48,4 @@ void onPREdited() throws IOException { .contentType(JsonFormat.CONTENT_TYPE) .body(pullRequestEvent).post("/").then().statusCode(200); } - }