Skip to content

fix: correct the issues with obtaining pr or branch name #1901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ func GetPipelineRunAndBranchNameFromCancelComment(comment string) (string, strin
// by /test, /retest or /cancel to return branch name and pipelinerun name.
func getPipelineRunAndBranchNameFromComment(typeOfComment, comment string) (string, string, error) {
var prName, branchName string
splitTest := strings.Split(comment, typeOfComment)
// avoid parsing error due to branch name contains /test, /retest or /cancel,
// here only split the first keyword and not split the later keywords.
splitTest := strings.SplitN(comment, typeOfComment, 2)

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

Expand All @@ -108,7 +110,8 @@ func getPipelineRunAndBranchNameFromComment(typeOfComment, comment string) (stri
// ex: /test prname
getFirstLine := strings.Split(splitTest[1], "\n")
// trim spaces
prName = strings.TrimSpace(getFirstLine[0])
// adapt for the comment contains the key=value pair
prName = strings.Split(strings.TrimSpace(getFirstLine[0]), " ")[0]
}
return prName, branchName, nil
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ func TestGetPipelineRunAndBranchNameFromTestComment(t *testing.T) {
branchName: "test",
wantError: false,
},
{
name: "branch name contains /test",
comment: "/test abc-01-pr abc \n branch:chore/test",
prName: "abc-01-pr",
branchName: "chore/test",
wantError: false,
},
{
name: "different word other than branch for retest command",
comment: "/retest invalidname:nightly",
Expand All @@ -341,6 +348,13 @@ func TestGetPipelineRunAndBranchNameFromTestComment(t *testing.T) {
prName: "abc-01-pr",
wantError: false,
},
{
name: "test a pipeline with key value",
comment: "/test abc-01-pr key=value",
prName: "abc-01-pr",
branchName: "",
wantError: false,
},
{
name: "string before retest command",
comment: "abc \n /retest abc-01-pr",
Expand Down
Loading