Skip to content

Commit

Permalink
#395 - Apply regex on 'bodyHtml' instead of 'body' of pullRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico Peeters committed Jun 28, 2018
1 parent 02f610e commit 3289cec
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class GithubSolverResolver {

private static final List<String> CLOSING_KEYWORDS = Arrays.asList("close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved");
private static final String CLOSING_KEYWORD_ISSUE_MATCHER_REGEX = "(?:<span.+class=\"issue-keyword.*\".*>)?\\b%1$s\\b(?:</span>)?:?\\s*<a.+class=\"issue-link js-issue-link\".*>(?:%2$s/%3$s)?#%4$s";

private final GithubGateway githubGateway;

Expand All @@ -31,9 +32,9 @@ public Optional<String> resolve(final Document document, final GithubId issueGit
.filter(this::isPullRequest)
.filter(this::isMerged)
.map(this::resolvePullRequestGithubId)
.map(pullRequestGithubId -> fetchAuthorFromPullRequest(pullRequestGithubId, issueGithubId))
.filter(Optional::isPresent)
.map(Optional::get)
.map(this::fetchPullrequest)
.filter(pullRequest -> pullRequest != null && pullRequestFixesIssue(pullRequest, issueGithubId))
.map(pullRequest -> pullRequest.getUser().getLogin())
.filter(StringUtils::isNotEmpty)
.findFirst();
}
Expand All @@ -56,21 +57,21 @@ private GithubId getPullRequestGithubIdFromInlineDiscussionItem(final Element di
.orElseThrow(() -> new RuntimeException("No pullrequest identifier is found"));
}

private Optional<String> fetchAuthorFromPullRequest(final GithubId pullRequestGithubId, final GithubId issueGithubId) {
final GithubResult pullRequest = githubGateway.getPullrequest(pullRequestGithubId.getOwner(), pullRequestGithubId.getRepo(), pullRequestGithubId.getNumber());
if (pullRequest != null && pullRequestFixesIssue(pullRequest, issueGithubId)) {
return Optional.of(pullRequest.getUser().getLogin());
}
return Optional.empty();
private GithubResult fetchPullrequest(GithubId pullRequestGithubId) {
return githubGateway.getPullrequest(pullRequestGithubId.getOwner(), pullRequestGithubId.getRepo(), pullRequestGithubId.getNumber());
}

private boolean pullRequestFixesIssue(final GithubResult pullRequest, final GithubId issueGithubId) {
final String pullRequestBody = pullRequest.getBody();

final String pullRequestBody = pullRequest.getBodyHtml();
return pullRequestBody != null && CLOSING_KEYWORDS.stream()
.anyMatch(keyword -> Pattern.compile("\\b" + keyword.toLowerCase() + "\\b:?\\s*#" + issueGithubId.getNumber())
.matcher(pullRequestBody.toLowerCase())
.find());
.map(keyword -> String.format(CLOSING_KEYWORD_ISSUE_MATCHER_REGEX,
keyword.toLowerCase(),
issueGithubId.getOwner(),
issueGithubId.getRepo(),
issueGithubId.getNumber()))
.anyMatch(regex -> Pattern.compile(regex)
.matcher(pullRequestBody.toLowerCase())
.find());
}

private boolean isPullRequestInSingleDiscussionItem(final Element discussionItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,17 @@ public void fetch_githubClosingKeywordsAreFound() {
assertThat(githubIssue.getSolver()).isEqualTo("katibest");
assertThat(githubIssue.getStatus()).isEqualTo("Closed");
}

@Test
public void fetch_issueFixedByPullRequestFromOtherRepo() {
final String owner = "brave";
final String repo = "brave-browser";
final String number = "240";

final GithubIssue githubIssue = scraper.fetchGithubIssue(owner, repo, number);

assertThat(githubIssue.getNumber()).isEqualTo("240");
assertThat(githubIssue.getSolver()).isEqualTo("cezaraugusto");
assertThat(githubIssue.getStatus()).isEqualTo("Closed");
}
}
Loading

0 comments on commit 3289cec

Please sign in to comment.