Skip to content

Commit

Permalink
fix: use magic keywords to detect issue linking (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
Namchee authored Oct 22, 2023
1 parent d250974 commit cc86d48
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 31 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Conventional PR

[![Go Version Badge](https://img.shields.io/github/go-mod/go-version/namchee/conventional-pr)](https://github.com/Namchee/conventional-pr) [![Go Report Card](https://goreportcard.com/badge/github.com/Namchee/conventional-pr)](https://goreportcard.com/report/github.com/Namchee/conventional-pr) [![codecov](https://codecov.io/gh/Namchee/conventional-pr/branch/master/graph/badge.svg)](https://codecov.io/gh/Namchee/conventional-pr) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors-)
[![Go Version Badge](https://img.shields.io/github/go-mod/go-version/namchee/conventional-pr)](https://github.com/Namchee/conventional-pr) [![Go Report Card](https://goreportcard.com/badge/github.com/Namchee/conventional-pr)](https://goreportcard.com/report/github.com/Namchee/conventional-pr) [![codecov](https://codecov.io/gh/Namchee/conventional-pr/branch/master/graph/badge.svg)](https://codecov.io/gh/Namchee/conventional-pr) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

Conventional PR is a GitHub Action that validates all pull requests sent to a GitHub-hosted repository.
Expand Down Expand Up @@ -215,7 +215,7 @@ Filling the input with an empty string will disabled this validator.

This validator checks if the pull request is linked one or more issues.

Do note that linking the issue using [special keywords](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) only works when the target branch is the default branch.
> Due to current limitations, an issue is considered linked only if the body has [special keywords](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue). Please see [this issue](https://github.com/Namchee/conventional-pr/issues/94) for future solutionss.

### Pull request does not introduce too many changes

Expand Down Expand Up @@ -320,24 +320,24 @@ Do note that `pull_request_target` allows unsafe code to be executed from the he
## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Namchee"><img src="https://avatars.githubusercontent.com/u/32661241?v=4?s=100" width="100px;" alt="Cristopher"/><br /><sub><b>Cristopher</b></sub></a><br /><a href="https://github.com/Namchee/conventional-pr/commits?author=Namchee" title="Code">💻</a> <a href="https://github.com/Namchee/conventional-pr/issues?q=author%3ANamchee" title="Bug reports">🐛</a> <a href="https://github.com/Namchee/conventional-pr/commits?author=Namchee" title="Documentation">📖</a> <a href="#example-Namchee" title="Examples">💡</a> <a href="#ideas-Namchee" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/smutel"><img src="https://avatars.githubusercontent.com/u/12967891?v=4?s=100" width="100px;" alt="Samuel Mutel"/><br /><sub><b>Samuel Mutel</b></sub></a><br /><a href="https://github.com/Namchee/conventional-pr/issues?q=author%3Asmutel" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/BinToss"><img src="https://avatars.githubusercontent.com/u/7243190?v=4?s=100" width="100px;" alt="Noah Sherwin"/><br /><sub><b>Noah Sherwin</b></sub></a><br /><a href="https://github.com/Namchee/conventional-pr/issues?q=author%3ABinToss" title="Bug reports">🐛</a> <a href="#ideas-BinToss" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/avorima"><img src="https://avatars.githubusercontent.com/u/15158349?v=4?s=100" width="100px;" alt="Mario Valderrama"/><br /><sub><b>Mario Valderrama</b></sub></a><br /><a href="#ideas-avorima" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://th3s4mur41.me"><img src="https://avatars.githubusercontent.com/u/5543595?v=4?s=100" width="100px;" alt="Michaël Vanderheyden"/><br /><sub><b>Michaël Vanderheyden</b></sub></a><br /><a href="https://github.com/Namchee/conventional-pr/issues?q=author%3ATh3S4mur41" title="Bug reports">🐛</a></td>
</tr>
</tbody>
</table>

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Namchee"><img src="https://avatars.githubusercontent.com/u/32661241?v=4?s=100" width="100px;" alt="Cristopher"/><br /><sub><b>Cristopher</b></sub></a><br /><a href="https://github.com/Namchee/conventional-pr/commits?author=Namchee" title="Code">💻</a> <a href="https://github.com/Namchee/conventional-pr/issues?q=author%3ANamchee" title="Bug reports">🐛</a> <a href="https://github.com/Namchee/conventional-pr/commits?author=Namchee" title="Documentation">📖</a> <a href="#example-Namchee" title="Examples">💡</a> <a href="#ideas-Namchee" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/smutel"><img src="https://avatars.githubusercontent.com/u/12967891?v=4?s=100" width="100px;" alt="Samuel Mutel"/><br /><sub><b>Samuel Mutel</b></sub></a><br /><a href="https://github.com/Namchee/conventional-pr/issues?q=author%3Asmutel" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/BinToss"><img src="https://avatars.githubusercontent.com/u/7243190?v=4?s=100" width="100px;" alt="Noah Sherwin"/><br /><sub><b>Noah Sherwin</b></sub></a><br /><a href="https://github.com/Namchee/conventional-pr/issues?q=author%3ABinToss" title="Bug reports">🐛</a> <a href="#ideas-BinToss" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/avorima"><img src="https://avatars.githubusercontent.com/u/15158349?v=4?s=100" width="100px;" alt="Mario Valderrama"/><br /><sub><b>Mario Valderrama</b></sub></a><br /><a href="#ideas-avorima" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://th3s4mur41.me"><img src="https://avatars.githubusercontent.com/u/5543595?v=4?s=100" width="100px;" alt="Michaël Vanderheyden"/><br /><sub><b>Michaël Vanderheyden</b></sub></a><br /><a href="https://github.com/Namchee/conventional-pr/issues?q=author%3ATh3S4mur41" title="Bug reports">🐛</a></td>
</tr>
</tbody>
</table>

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://allcontributors.org) specification.
Expand Down
35 changes: 31 additions & 4 deletions internal/validator/issue.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package validator

import (
"context"
"regexp"
"strconv"

"github.com/Namchee/conventional-pr/internal"
"github.com/Namchee/conventional-pr/internal/constants"
"github.com/Namchee/conventional-pr/internal/entity"
"github.com/google/go-github/v32/github"
)

var (
keywordPattern = regexp.MustCompile(`(?mi)\b(close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved) #(\d+)\b`)
)

type issueValidator struct {
client internal.GithubClient
config *entity.Configuration
Expand Down Expand Up @@ -37,16 +45,35 @@ func (v *issueValidator) IsValid(pullRequest *github.PullRequest) *entity.Valida
}
}

issue := pullRequest.GetLinks().GetIssue()

if issue != nil {
keywords := keywordPattern.FindAllStringSubmatch(pullRequest.GetBody(), -1)
if keywords == nil {
return &entity.ValidationResult{
Name: v.Name,
Active: true,
Result: nil,
Result: constants.ErrNoIssue,
}
}

for _, number := range keywords {
num, _ := strconv.Atoi(number[2])

issue, _ := v.client.GetIssue(
context.Background(),
v.meta.Owner,
v.meta.Name,
num,
)

if issue != nil {
return &entity.ValidationResult{
Name: v.Name,
Active: true,
Result: nil,
}
}
}

return &entity.ValidationResult{
Name: v.Name,
Active: true,
Expand Down
43 changes: 37 additions & 6 deletions internal/validator/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ func TestIssueValidator_IsValid(t *testing.T) {
name: "should allow issue references",
args: args{
pullRequest: &github.PullRequest{
Links: &github.PRLinks{
Issue: &github.PRLink{
HRef: github.String("abc"),
},
},
Body: github.String(`
## Overview
Closes #1
This pull request resolves #123 by using xxx/yyy/zzz.
`),
},
config: true,
},
Expand All @@ -52,7 +54,36 @@ func TestIssueValidator_IsValid(t *testing.T) {
{
name: "should reject if no issue at all",
args: args{
pullRequest: &github.PullRequest{},
pullRequest: &github.PullRequest{
Body: github.String("I don't reference any issues!"),
},
config: true,
},
want: &entity.ValidationResult{
Name: constants.IssueValidatorName,
Active: true,
Result: constants.ErrNoIssue,
},
},
{
name: "should reject if references are fake",
args: args{
pullRequest: &github.PullRequest{
Body: github.String(`
## Overview
Closes #1. resolved #2. fixes #3. Fix #321Closed #123
This pull request resolve #124 by using xxx/yyy/zzz.
close #9. Fixed #54.
`),
},
config: true,
},
want: &entity.ValidationResult{
Expand Down

0 comments on commit cc86d48

Please sign in to comment.