Skip to content

Commit cab45f0

Browse files
unknwonmpsonntag
authored andcommittedDec 2, 2020
action: fix issue reference regexp and error handling (#6352)
1 parent 2cca223 commit cab45f0

File tree

5 files changed

+50
-29
lines changed

5 files changed

+50
-29
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ All notable changes to Gogs are documented in this file.
1919
- _Regression:_ Pages are correctly rendered when requesting `?go-get=1` for subdirectories. [#6314](https://github.com/gogs/gogs/issues/6314)
2020
- _Regression:_ Submodule with a relative path is linked correctly. [#6319](https://github.com/gogs/gogs/issues/6319)
2121
- Backup can be processed when `--target` is specified on Windows. [#6339](https://github.com/gogs/gogs/issues/6339)
22+
- Commit message contains keywords look like an issue reference no longer fails the push entirely. [#6289](https://github.com/gogs/gogs/issues/6289)
2223

2324
### Removed
2425

‎internal/db/action.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ var (
5757
IssueCloseKeywords = []string{"close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved"}
5858
IssueReopenKeywords = []string{"reopen", "reopens", "reopened"}
5959

60-
IssueCloseKeywordsPat = lazyregexp.New(assembleKeywordsPattern(IssueCloseKeywords))
61-
IssueReopenKeywordsPat = lazyregexp.New(assembleKeywordsPattern(IssueReopenKeywords))
62-
IssueReferenceKeywordsPat = lazyregexp.New(`(?i)(?:)(^| )\S+`)
60+
IssueCloseKeywordsPat = lazyregexp.New(assembleKeywordsPattern(IssueCloseKeywords))
61+
IssueReopenKeywordsPat = lazyregexp.New(assembleKeywordsPattern(IssueReopenKeywords))
62+
issueReferencePattern = lazyregexp.New(`(?i)(?:)(^| )\S*#\d+`)
6363
)
6464

6565
func assembleKeywordsPattern(words []string) string {
@@ -321,8 +321,8 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit) err
321321
c := commits[i]
322322

323323
refMarked := make(map[int64]bool)
324-
for _, ref := range IssueReferenceKeywordsPat.FindAllString(c.Message, -1) {
325-
ref = ref[strings.IndexByte(ref, byte(' '))+1:]
324+
for _, ref := range issueReferencePattern.FindAllString(c.Message, -1) {
325+
ref = strings.TrimSpace(ref)
326326
ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
327327

328328
if len(ref) == 0 {
@@ -455,7 +455,7 @@ type CommitRepoActionOptions struct {
455455
Commits *PushCommits
456456
}
457457

458-
// CommitRepoAction adds new commit actio to the repository, and prepare corresponding webhooks.
458+
// CommitRepoAction adds new commit action to the repository, and prepare corresponding webhooks.
459459
func CommitRepoAction(opts CommitRepoActionOptions) error {
460460
pusher, err := GetUserByName(opts.PusherName)
461461
if err != nil {

‎internal/db/action_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2020 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package db
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func Test_issueReferencePattern(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
message string
17+
expStrings []string
18+
}{
19+
{
20+
name: "no match",
21+
message: "Hello world!",
22+
expStrings: nil,
23+
},
24+
{
25+
name: "contains issue numbers",
26+
message: "#123 is fixed, and #456 is WIP",
27+
expStrings: []string{"#123", " #456"},
28+
},
29+
{
30+
name: "contains full issue references",
31+
message: "#123 is fixed, and user/repo#456 is WIP",
32+
expStrings: []string{"#123", " user/repo#456"},
33+
},
34+
}
35+
for _, test := range tests {
36+
t.Run(test.name, func(t *testing.T) {
37+
strs := issueReferencePattern.FindAllString(test.message, -1)
38+
assert.Equal(t, test.expStrings, strs)
39+
})
40+
}
41+
}

‎internal/db/errors/issue.go

-20
This file was deleted.

‎internal/db/issue.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -817,12 +817,11 @@ func (ErrIssueNotExist) NotFound() bool {
817817
return true
818818
}
819819

820-
// GetIssueByRef returns an Issue specified by a GFM reference.
821-
// See https://help.github.com/articles/writing-on-github#references for more information on the syntax.
820+
// GetIssueByRef returns an Issue specified by a GFM reference, e.g. owner/repo#123.
822821
func GetIssueByRef(ref string) (*Issue, error) {
823822
n := strings.IndexByte(ref, byte('#'))
824823
if n == -1 {
825-
return nil, errors.InvalidIssueReference{Ref: ref}
824+
return nil, ErrIssueNotExist{args: map[string]interface{}{"ref": ref}}
826825
}
827826

828827
index := com.StrTo(ref[n+1:]).MustInt64()

0 commit comments

Comments
 (0)