Skip to content

Commit

Permalink
fix: use real issue API for issue linking (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Namchee authored Sep 25, 2022
1 parent 6f70f6e commit d513947
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 45 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ This validator checks if the branch name of the pull request satisfies the regul

Filling the input with an empty string will disabled this validator.

### Pull request refers to an issue
### Pull request links to an issue

<table>
<tr>
Expand All @@ -213,9 +213,9 @@ Filling the input with an empty string will disabled this validator.
</tr>
</table>

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

Do note that linking the issue directly on the pull request is currently unsupported and does not considered as issue mentions.
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.

### Pull request does not introduce too many changes

Expand Down Expand Up @@ -292,7 +292,6 @@ Ideally, Conventional PR workflow should only triggered when an event related to

## Caveats

- If the issues are linked manually and are not mentioned in the pull request body, the pull request is still considered to be invalid. Currently, there is no way to avoid this issue.
- `edit` feature cannot be used with `github-actions` credentials as it [doesn't have the `user` scope](https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token). If you want to use the `edit` feature, please generate a [personal access token](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token) instead with `read:user` scope. `edit` **cannot** be combined with `report`.
- It is recommended to use a dedicated dummy account if you want to use the `edit` feature as it may lead to unintended edits.

Expand Down
24 changes: 6 additions & 18 deletions internal/validator/issue.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
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"
Expand Down Expand Up @@ -41,21 +37,13 @@ func (v *issueValidator) IsValid(pullRequest *github.PullRequest) *entity.Valida
}
}

ctx := context.Background()
pattern := regexp.MustCompile(`#(\d+)`)

mentions := pattern.FindAllStringSubmatch(pullRequest.GetBody(), -1)

for _, mention := range mentions {
num, _ := strconv.Atoi(mention[1])
issue, err := v.client.GetIssue(ctx, v.meta.Owner, v.meta.Name, num)
issue := pullRequest.GetLinks().GetIssue()

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

Expand Down
36 changes: 13 additions & 23 deletions internal/validator/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

func TestIssueValidator_IsValid(t *testing.T) {
type args struct {
config bool
body string
config bool
pullRequest *github.PullRequest
}
tests := []struct {
name string
Expand All @@ -23,7 +23,13 @@ func TestIssueValidator_IsValid(t *testing.T) {
{
name: "should allow issue references",
args: args{
body: "Closes #123",
pullRequest: &github.PullRequest{
Links: &github.PRLinks{
Issue: &github.PRLink{
HRef: github.String("abc"),
},
},
},
config: true,
},
want: &entity.ValidationResult{
Expand All @@ -35,7 +41,7 @@ func TestIssueValidator_IsValid(t *testing.T) {
{
name: "should be skipped if disabled",
args: args{
body: "Closes #123",
pullRequest: &github.PullRequest{},
},
want: &entity.ValidationResult{
Name: constants.IssueValidatorName,
Expand All @@ -46,20 +52,8 @@ func TestIssueValidator_IsValid(t *testing.T) {
{
name: "should reject if no issue at all",
args: args{
body: "this is a fake body",
config: true,
},
want: &entity.ValidationResult{
Name: constants.IssueValidatorName,
Active: true,
Result: constants.ErrNoIssue,
},
},
{
name: "should distinguih false alarm",
args: args{
body: "This is a fake issue #69",
config: true,
pullRequest: &github.PullRequest{},
config: true,
},
want: &entity.ValidationResult{
Name: constants.IssueValidatorName,
Expand All @@ -71,18 +65,14 @@ func TestIssueValidator_IsValid(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
pull := &github.PullRequest{
Body: &tc.args.body,
}

client := mocks.NewGithubClientMock()
config := &entity.Configuration{
Issue: tc.args.config,
}

validator := NewIssueValidator(client, config, &entity.Meta{})

got := validator.IsValid(pull)
got := validator.IsValid(tc.args.pullRequest)

assert.Equal(t, got, tc.want)
})
Expand Down

0 comments on commit d513947

Please sign in to comment.