-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Refactor GitHubIssueSearch and remove redundant files (#1377)
- Loading branch information
1 parent
89053a7
commit 202b20e
Showing
11 changed files
with
150 additions
and
214 deletions.
There are no files selected for viewing
11 changes: 0 additions & 11 deletions
11
github-bot/src/main/java/io/github/martinwitt/laughing_train/data/FindIssueRequest.java
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
github-bot/src/main/java/io/github/martinwitt/laughing_train/data/FindIssueResult.java
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
github-bot/src/main/java/io/github/martinwitt/laughing_train/data/FindPrResult.java
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
github-bot/src/main/java/io/github/martinwitt/laughing_train/data/FindPullRequestResult.java
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
github-bot/src/main/java/io/github/martinwitt/laughing_train/data/ProjectRequest.java
This file was deleted.
Oops, something went wrong.
106 changes: 106 additions & 0 deletions
106
github-bot/src/main/java/io/github/martinwitt/laughing_train/github/GitHubIssueSearch.java
This file contains 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,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()); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...witt/laughing_train/data/GitHubState.java → ...tt/laughing_train/github/GitHubState.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...martinwitt/laughing_train/data/Issue.java → ...rtinwitt/laughing_train/github/Issue.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...witt/laughing_train/data/PullRequest.java → ...tt/laughing_train/github/PullRequest.java
This file contains 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
115 changes: 0 additions & 115 deletions
115
github-bot/src/main/java/io/github/martinwitt/laughing_train/services/GitHubIssueSearch.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.