Skip to content

Commit bebbdcf

Browse files
committed
fix: multiple tag params
1 parent 9f267ce commit bebbdcf

File tree

4 files changed

+67
-62
lines changed

4 files changed

+67
-62
lines changed

api/index.go

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -126,43 +126,25 @@ func approvePullRequest(client *github.Client, event github.IssueCommentEvent) {
126126
}
127127

128128
func addLabelsToIssue(commentBody string, githubClient *github.Client, issueCommentEvent github.IssueCommentEvent) {
129-
wordArray := strings.Fields(commentBody)
130-
labelIndex := utils.StringIndexOf(wordArray, Label)
131-
for i := 0; i < len(labelIndex); i++ {
132-
param, err := utils.GetTagNextOneParam(commentBody, Label)
133-
if err == nil {
134-
labels := []string{param}
135-
issue, response, githubErr := githubClient.Issues.AddLabelsToIssue(ctx, *issueCommentEvent.GetRepo().Owner.Login,
136-
*issueCommentEvent.GetRepo().Name,
137-
*issueCommentEvent.GetIssue().Number, labels)
138-
if githubErr != nil {
139-
log.Print(githubErr)
140-
}
141-
log.Println(response, issue)
142-
} else {
143-
log.Println(err)
144-
}
145-
}
129+
params := utils.GetTagNextAllParams(commentBody, Label)
130+
issue, response, githubErr := githubClient.Issues.AddLabelsToIssue(ctx, *issueCommentEvent.GetRepo().Owner.Login,
131+
*issueCommentEvent.GetRepo().Name,
132+
*issueCommentEvent.GetIssue().Number, params)
133+
log.Println(response, issue, githubErr)
146134
}
147135

148136
func removeLabelFromIssue(commentBody string, githubClient *github.Client, issueCommentEvent github.IssueCommentEvent) {
149-
wordArray := strings.Fields(commentBody)
150-
unLabelIndex := utils.StringIndexOf(wordArray, UnLabel)
151-
for i := 0; i < len(unLabelIndex); i++ {
152-
param, err := utils.GetTagNextOneParam(commentBody, UnLabel)
153-
if err == nil {
154-
response, githubErr := githubClient.Issues.RemoveLabelForIssue(ctx,
155-
*issueCommentEvent.GetRepo().Owner.Login,
156-
*issueCommentEvent.GetRepo().Name,
157-
*issueCommentEvent.GetIssue().Number,
158-
param)
159-
if githubErr != nil {
160-
log.Print(githubErr)
161-
}
162-
log.Println(response)
163-
} else {
164-
log.Println(err)
137+
params := utils.GetTagNextAllParams(commentBody, UnLabel)
138+
for _, param := range params {
139+
response, githubErr := githubClient.Issues.RemoveLabelForIssue(ctx,
140+
*issueCommentEvent.GetRepo().Owner.Login,
141+
*issueCommentEvent.GetRepo().Name,
142+
*issueCommentEvent.GetIssue().Number,
143+
param)
144+
if githubErr != nil {
145+
log.Print(githubErr)
165146
}
147+
log.Println(response)
166148
}
167149
}
168150

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ go 1.15
44

55
require (
66
github.com/golang/protobuf v1.4.3 // indirect
7-
github.com/google/go-github v17.0.0+incompatible // indirect
7+
github.com/google/go-github v17.0.0+incompatible
88
github.com/google/go-querystring v1.0.0 // indirect
99
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
10-
golang.org/x/oauth2 v0.0.0-20210113205817-d3ed898aa8a3 // indirect
10+
golang.org/x/oauth2 v0.0.0-20210113205817-d3ed898aa8a3
1111
google.golang.org/appengine v1.6.7 // indirect
1212
)

utils/stringutils.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package utils
22

33
import (
44
"fmt"
5-
"strconv"
65
"strings"
76
)
87

@@ -22,16 +21,6 @@ func StringIndexOf(originalArray []string, wordToFind interface{}) []int {
2221
return indexArray
2322
}
2423

25-
func StringToInt32(numberString string) (int32, error) {
26-
parseNumber, err := strconv.ParseInt(numberString, 10, 32)
27-
if err == nil {
28-
return int32(parseNumber), nil
29-
} else {
30-
return 0, err
31-
}
32-
}
33-
34-
// 封装:获取 /tag 后一位字符串
3524
func GetTagNextOneParam(originalMessage string, tagName string) (nextString string, err error) {
3625
wordArray := strings.Fields(originalMessage)
3726
indexes := StringIndexOf(wordArray, tagName)
@@ -41,3 +30,15 @@ func GetTagNextOneParam(originalMessage string, tagName string) (nextString stri
4130
}
4231
return nextString, fmt.Errorf("param of %s required", tagName)
4332
}
33+
34+
func GetTagNextAllParams(originalMessage string, tagName string) (params []string) {
35+
wordArray := strings.Fields(originalMessage)
36+
tagIndexes := StringIndexOf(wordArray, tagName)
37+
for _, v := range tagIndexes {
38+
if v+1 < len(wordArray) {
39+
param := wordArray[v+1]
40+
params = append(params, param)
41+
}
42+
}
43+
return
44+
}

utils/stringutils_test.go

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,6 @@ var (
1212
tagParam = "object"
1313
)
1414

15-
func TestStringToInt32(t *testing.T) {
16-
17-
t.Run("NormalNumber", func(t *testing.T) {
18-
normalNumber := "1"
19-
if resultInt, err := StringToInt32(normalNumber); resultInt != 1 && err == nil {
20-
t.Errorf("The String %s expected be int32 %s, but %d got", normalNumber, normalNumber, resultInt)
21-
}
22-
})
23-
24-
t.Run("UnexpectedString", func(t *testing.T) {
25-
if _, err := StringToInt32("not a number"); err == nil {
26-
t.Errorf("The String must be the number string, otherwise the error wouldn't be nil")
27-
}
28-
})
29-
}
30-
3115
func TestStringIndexOf(t *testing.T) {
3216
// the string word must exists in the array.
3317
indexes := StringIndexOf([]string{"x", tagName, "y"}, tagName)
@@ -70,3 +54,41 @@ func TestGetTagNextOneParam(t *testing.T) {
7054
t.Errorf(err.Error())
7155
}
7256
}
57+
58+
func stringSliceEqual(a, b []string) bool {
59+
if len(a) != len(b) {
60+
return false
61+
}
62+
if (a == nil) != (b == nil) {
63+
return false
64+
}
65+
for i, v := range a {
66+
if v != b[i] {
67+
return false
68+
}
69+
}
70+
return true
71+
}
72+
73+
func TestGetTagNextAllParams(t *testing.T) {
74+
// use table-driven tests
75+
cases := []struct {
76+
Name string
77+
Message string
78+
ExpectedLabel []string
79+
}{
80+
{"single tag with param", "/tag a", []string{"a"}},
81+
{"two same tags with different params", "/tag a /tag b", []string{"a", "b"}},
82+
{"two same tags but one lack of param", "/tag a /tag", []string{"a"}},
83+
{"two different tags", "/tag a /untag b", []string{"a"}},
84+
}
85+
86+
for _, c := range cases {
87+
t.Run(c.Name, func(t *testing.T) {
88+
params := GetTagNextAllParams(c.Message, tagName)
89+
if !stringSliceEqual(params, c.ExpectedLabel) {
90+
t.Errorf("The param after the tag expected to be %s, but %s got", c.ExpectedLabel, params)
91+
}
92+
})
93+
}
94+
}

0 commit comments

Comments
 (0)