Skip to content

Commit

Permalink
refactor: Refactor GitHubIssueSearch and remove redundant files (#1377)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Dec 19, 2023
1 parent 89053a7 commit 202b20e
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 214 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package io.github.martinwitt.laughing_train.github;

import com.google.common.flogger.FluentLogger;
import io.github.martinwitt.laughing_train.commons.result.Result;
import jakarta.enterprise.context.ApplicationScoped;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueSearchBuilder;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GitHub;

@ApplicationScoped
public class GitHubIssueSearch {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();

public Result<List<PullRequest>> findPullRequests(String userName) {
return searchIssuesWithFixes(userName);
}

private Result<List<PullRequest>> searchIssuesWithFixes(String userName) {
try {
return Result.ok(
convertGHIssueToPullRequest(
getGitHubIssues("is:pr", "author:" + userName, "fingerprint in:body")));
} catch (IOException e) {
logger.atSevere().withCause(e).log(
"Error while searching for issues with fixes for user %s", userName);
return Result.error(e);
}
}

private List<PullRequest> convertGHIssueToPullRequest(Collection<? extends GHIssue> issues) {
return issues.stream().map(this::toPullRequest).collect(Collectors.toList());
}

private List<GHIssue> getGitHubIssues(String... queries) throws IOException {
GitHub gitHub = connectToGitHubUsingOAuth();
GHIssueSearchBuilder issueQueryBuilder = gitHub.searchIssues();
for (String query : queries) {
issueQueryBuilder = issueQueryBuilder.q(query);
}
return issueQueryBuilder.list().toList().stream()
.filter(v -> !v.getRepository().isFork())
.toList();
}

private static GitHub connectToGitHubUsingOAuth() throws IOException {
return GitHub.connectUsingOAuth(System.getenv("GITHUB_TOKEN"));
}

private PullRequest toPullRequest(GHIssue issue) {
return new PullRequest(
toPullRequestState(issue.getState()),
issue.getTitle(),
issue.getBody(),
issue.getRepository().getOwnerName(),
issue.getRepository().getName(),
issue.getNumber(),
issue.getHtmlUrl().toString());
}

private GitHubState toPullRequestState(GHIssueState state) {
return Enum.valueOf(GitHubState.class, state.name());
}

public Result<Issue> findSummaryIssue() {
return searchSummaryIssue();
}

private Result<Issue> searchSummaryIssue() {
try {
GHIssue issue = getFirstIssue("laughing-train-summary");
return Result.ofNullable(toIssue(issue));
} catch (Exception e) {
logger.atSevere().withCause(e).log("Error while searching for summary issue");
return Result.error(e);
}
}

private GHIssue getFirstIssue(String label) throws IOException {
return connectToGitHubUsingOAuth()
.getRepository("MartinWitt/laughing-train")
.queryIssues()
.pageSize(100)
.label(label)
.state(GHIssueState.OPEN)
.pageSize(1)
.list()
.toList()
.getFirst();
}

private Issue toIssue(GHIssue issue) {
return new Issue(
toPullRequestState(issue.getState()),
issue.getTitle(),
issue.getBody(),
issue.getRepository().getOwnerName(),
issue.getRepository().getName(),
issue.getNumber(),
issue.getHtmlUrl().toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.martinwitt.laughing_train.data;
package io.github.martinwitt.laughing_train.github;

public enum GitHubState {
OPEN,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.martinwitt.laughing_train.data;
package io.github.martinwitt.laughing_train.github;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.martinwitt.laughing_train.data;
package io.github.martinwitt.laughing_train.github;

import java.io.Serializable;

Expand Down

This file was deleted.

Loading

0 comments on commit 202b20e

Please sign in to comment.