Skip to content

Commit

Permalink
fix: fix workflow events are triggered in both the local and fork rep…
Browse files Browse the repository at this point in the history
…ositories (#33)
  • Loading branch information
wongearl authored Jun 20, 2023
1 parent 90f10ff commit d727540
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
16 changes: 14 additions & 2 deletions pkg/argo-workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func k8sStyleName(name string) (result string) {
func (w *Workflow) GetWorkflowBindings() (wfbs []WorkflowEventBinding) {
projectName := w.GitRepository
if strings.Contains(projectName, "/") {
projectName = strings.TrimSuffix(projectName, ".git")
projectName = strings.Split(projectName, "/")[1]
projectName = getProjectName(projectName)
}

for _, e := range w.GetEvent() {
Expand Down Expand Up @@ -229,3 +228,16 @@ func addQuotationMark(jobs map[string]Job) map[string]Job {
}
return jobs
}

func getProjectName(projectName string) string {
projectName = strings.TrimSuffix(projectName, ".git")
var index int
if strings.HasPrefix(projectName, "git@") {
index = strings.Index(projectName, ":")
projectName = projectName[index+1:]
} else {
parts := strings.SplitN(projectName, "/", 4)
projectName = parts[3]
}
return projectName
}
40 changes: 40 additions & 0 deletions pkg/argo-workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,43 @@ func TestWorkflow_ConvertToArgoWorkflow(t *testing.T) {
assert.Equal(t, "", result)
assert.Nil(t, err)
}

func Test_getProjectName(t *testing.T) {
type args struct {
projectName string
}
tests := []struct {
name string
args args
want string
}{
{
name: "gaw1",
args: args{
projectName: "https://github.com/LinuxSuRen/github-action-workflow.git",
},
want: "LinuxSuRen/github-action-workflow",
},
{
name: "gaw2",
args: args{
projectName: "[email protected]:LinuxSuRen/github-action-workflow.git",
},
want: "LinuxSuRen/github-action-workflow",
},
{
name: "gaw3",
args: args{
projectName: "[email protected]:group0/group1/LinuxSuRen/github-action-workflow.git",
},
want: "group0/group1/LinuxSuRen/github-action-workflow",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getProjectName(tt.args.projectName); got != tt.want {
t.Errorf("getProjectName() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions pkg/testdata/argo-workflows-complex-event.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ metadata:
name: imagetest-push
spec:
event:
selector: payload.object_kind == "push" && payload.project.path_with_namespace endsWith "" && (payload.ref matches "refs/heads/main" || payload.ref matches "refs/heads/test" || payload.ref matches "refs/heads/release-*")
selector: payload.object_kind == "push" && payload.project.path_with_namespace endsWith "LinuxSuRen/yaml-readme" && (payload.ref matches "refs/heads/main" || payload.ref matches "refs/heads/test" || payload.ref matches "refs/heads/release-*")
submit:
workflowTemplateRef:
name: imagetest
Expand All @@ -60,7 +60,7 @@ metadata:
name: imagetest-merge-request
spec:
event:
selector: payload.object_kind == "merge_request" && payload.project.path_with_namespace endsWith "" && (payload.object_attributes.target_branch == "main") && payload.object_attributes.state == "opened"
selector: payload.object_kind == "merge_request" && payload.project.path_with_namespace endsWith "LinuxSuRen/yaml-readme" && (payload.object_attributes.target_branch == "main") && payload.object_attributes.state == "opened"
submit:
workflowTemplateRef:
name: imagetest
Expand Down
2 changes: 1 addition & 1 deletion pkg/testdata/argo-workflows-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ metadata:
name: imagetest-push
spec:
event:
selector: payload.object_kind == "push" && payload.project.path_with_namespace endsWith ""
selector: payload.object_kind == "push" && payload.project.path_with_namespace endsWith "LinuxSuRen/yaml-readme"
submit:
workflowTemplateRef:
name: imagetest
Expand Down
2 changes: 1 addition & 1 deletion pkg/testdata/argo-workflows-retry-event.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ metadata:
name: imagetest-retry
spec:
event:
selector: payload.event_type == "note" && payload.merge_request.state == "opened" && payload.object_attributes.note == "/retry" && payload.project.path_with_namespace endsWith ""
selector: payload.event_type == "note" && payload.merge_request.state == "opened" && payload.object_attributes.note == "/retry" && payload.project.path_with_namespace endsWith "LinuxSuRen/yaml-readme"
submit:
workflowTemplateRef:
name: imagetest
Expand Down
2 changes: 1 addition & 1 deletion pkg/testdata/argo-workflows-test-event.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ metadata:
name: imagetest-test
spec:
event:
selector: payload.event_type == "note" && payload.merge_request.state == "opened" && payload.object_attributes.note == "/test" && payload.project.path_with_namespace endsWith ""
selector: payload.event_type == "note" && payload.merge_request.state == "opened" && payload.object_attributes.note == "/test" && payload.project.path_with_namespace endsWith "LinuxSuRen/yaml-readme"
submit:
workflowTemplateRef:
name: imagetest
Expand Down

0 comments on commit d727540

Please sign in to comment.