Skip to content

Commit

Permalink
KOGITO-3659 - Remove duplicate services for SW GH example (apache#482)
Browse files Browse the repository at this point in the history
Signed-off-by: Ricardo Zanini <[email protected]>
  • Loading branch information
ricardozanini authored Dec 10, 2020
1 parent 315a92e commit 24aa92b
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 181 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
"name": "AddLabels",
"type": "service",
"metadata": {
"interface": "org.kogito.examples.sw.github.workflow.GitHubServiceBackend",
"interface": "org.kogito.examples.sw.github.workflow.GitHubService",
"operation": "addLabels"
}
},
{
"name": "AddReviewers",
"type": "service",
"metadata": {
"interface": "org.kogito.examples.sw.github.workflow.GitHubServiceBackend",
"interface": "org.kogito.examples.sw.github.workflow.GitHubService",
"operation": "addReviewers"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
"name": "AddLabels",
"type": "service",
"metadata": {
"interface": "org.kogito.examples.sw.github.workflow.GitHubServiceFrontend",
"interface": "org.kogito.examples.sw.github.workflow.GitHubService",
"operation": "addLabels"
}
},
{
"name": "AddReviewers",
"type": "service",
"metadata": {
"interface": "org.kogito.examples.sw.github.workflow.GitHubServiceFrontend",
"interface": "org.kogito.examples.sw.github.workflow.GitHubService",
"operation": "addReviewers"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -35,7 +35,7 @@ class GitHubServiceTest {
final ObjectMapper objectMapper = new ObjectMapper();

@Inject
GitHubServiceBackend gitHubServiceBackend;
GitHubService gitHubServiceBackend;

@Test
void addLabels() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -51,5 +48,4 @@ void onPREdited() throws IOException {
.contentType(JsonFormat.CONTENT_TYPE)
.body(pullRequestEvent).post("/").then().statusCode(200);
}

}

0 comments on commit 24aa92b

Please sign in to comment.