Skip to content

Commit c75f739

Browse files
committed
fix: correct the issues with obtaining pr or branch name
1 parent d146051 commit c75f739

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

pkg/provider/provider.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@ func GetPipelineRunAndBranchNameFromCancelComment(comment string) (string, strin
8383
// by /test, /retest or /cancel to return branch name and pipelinerun name.
8484
func getPipelineRunAndBranchNameFromComment(typeOfComment, comment string) (string, string, error) {
8585
var prName, branchName string
86-
splitTest := strings.Split(comment, typeOfComment)
86+
// avoid parsing error due to branch name contains /test, /retest or /cancel,
87+
// here only split the first keyword and not split the later keywords.
88+
splitTest := strings.SplitN(comment, typeOfComment, 2)
8789

8890
// after the split get the second part of the typeOfComment (/test, /retest or /cancel)
8991
// as second part can be branch name or pipelinerun name and branch name
90-
// ex: /test branch:nightly, /test prname branch:nightly
92+
// ex: /test branch:nightly, /test prname branch:nightly, /test prname branch:nightly key=value
9193
if splitTest[1] != "" && strings.Contains(splitTest[1], ":") {
9294
branchData := strings.Split(splitTest[1], ":")
9395

@@ -108,7 +110,8 @@ func getPipelineRunAndBranchNameFromComment(typeOfComment, comment string) (stri
108110
// ex: /test prname
109111
getFirstLine := strings.Split(splitTest[1], "\n")
110112
// trim spaces
111-
prName = strings.TrimSpace(getFirstLine[0])
113+
// adapt for the comment contains the key=value pair
114+
prName = strings.Split(strings.TrimSpace(getFirstLine[0]), " ")[0]
112115
}
113116
return prName, branchName, nil
114117
}

pkg/provider/provider_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,13 @@ func TestGetPipelineRunAndBranchNameFromTestComment(t *testing.T) {
325325
branchName: "test",
326326
wantError: false,
327327
},
328+
{
329+
name: "branch name contains /test",
330+
comment: "/test abc-01-pr abc \n branch:chore/test",
331+
prName: "abc-01-pr",
332+
branchName: "chore/test",
333+
wantError: false,
334+
},
328335
{
329336
name: "different word other than branch for retest command",
330337
comment: "/retest invalidname:nightly",
@@ -341,6 +348,13 @@ func TestGetPipelineRunAndBranchNameFromTestComment(t *testing.T) {
341348
prName: "abc-01-pr",
342349
wantError: false,
343350
},
351+
{
352+
name: "test a pipeline with key value",
353+
comment: "/test abc-01-pr key=value",
354+
prName: "abc-01-pr",
355+
branchName: "",
356+
wantError: false,
357+
},
344358
{
345359
name: "string before retest command",
346360
comment: "abc \n /retest abc-01-pr",

0 commit comments

Comments
 (0)