Skip to content
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

feat: provide a default label for the workflow status #7

Merged
merged 1 commit into from
Dec 23, 2022
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
4 changes: 2 additions & 2 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
make build
- name: Test
run: |
go test ./... -coverprofile coverage.out
make test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
Expand All @@ -53,7 +53,7 @@ jobs:
uses: actions/checkout@v3.0.0
- name: Test
run: |
make build-workflow-executor-gogit
make build-workflow-executor-gogit test-workflow-executor-gogit

goreleaser:
name: Run GoReleaser
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
build:
CGO_ENABLE=0 go build -ldflags "-w -s" -o bin/gogit
test:
go test ./... -coverprofile coverage.out
goreleaser:
goreleaser build --snapshot --rm-dist
image:
Expand All @@ -8,3 +10,5 @@ build-workflow-executor-gogit:
cd cmd/argoworkflow && CGO_ENABLE=0 go build -ldflags "-w -s" -o ../../bin/workflow-executor-gogit
image-workflow-executor-gogit:
cd cmd/argoworkflow && docker build . -t ghcr.io/linuxsuren/workflow-executor-gogit
test-workflow-executor-gogit:
cd cmd/argoworkflow && go test ./...
27 changes: 18 additions & 9 deletions cmd/argoworkflow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type pluginOption struct {
Option *option `json:"gogit-executor-plugin"`
}

func (e *DefaultPluginExecutor) Execute(args executor.ExecuteTemplateArgs, status wfv1.WorkflowStatus) (
func (e *DefaultPluginExecutor) Execute(args executor.ExecuteTemplateArgs, name string, status wfv1.WorkflowStatus) (
resp executor.ExecuteTemplateResponse, err error) {
p := args.Template.Plugin.Value

Expand All @@ -103,8 +103,8 @@ func (e *DefaultPluginExecutor) Execute(args executor.ExecuteTemplateArgs, statu
Username: opt.Option.Username,
Token: opt.Option.Token,
Status: opt.Option.Status,
Label: opt.Option.Label,
Description: opt.Option.Description,
Label: EmptyThen(opt.Option.Label, name),
Description: EmptyThen(opt.Option.Description, status.Message),
}
if repo.Status == "" {
switch status.Phase {
Expand All @@ -125,9 +125,6 @@ func (e *DefaultPluginExecutor) Execute(args executor.ExecuteTemplateArgs, statu
}
}
}
if repo.Description == "" {
repo.Description = status.Message
}
if repo.PrNumber, err = strconv.Atoi(opt.Option.PR); err != nil {
err = fmt.Errorf("wrong pull-request number, %v", err)
return
Expand Down Expand Up @@ -159,14 +156,13 @@ func (e *DefaultPluginExecutor) Execute(args executor.ExecuteTemplateArgs, statu

type PluginExecutor interface {
// Execute commands based on the args provided from the workflow
Execute(args executor.ExecuteTemplateArgs, status wfv1.WorkflowStatus) (executor.ExecuteTemplateResponse, error)
Execute(args executor.ExecuteTemplateArgs, name string, status wfv1.WorkflowStatus) (executor.ExecuteTemplateResponse, error)
}

var (
ErrWrongContentType = errors.New("Content-Type header is not set to 'appliaction/json'")
ErrReadingBody = errors.New("Couldn't read request body")
ErrMarshallingBody = errors.New("Couldn't unmrashal request body")
ErrExecutingPlugin = errors.New("Error occured while executing plugin")
)

func plugin(p PluginExecutor, client *wfclientset.Clientset) func(w http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -204,7 +200,11 @@ func plugin(p PluginExecutor, client *wfclientset.Clientset) func(w http.Respons
return
}

_, _ = p.Execute(args, workflow.Status)
var name string
if workflow.Spec.WorkflowTemplateRef != nil {
name = workflow.Spec.WorkflowTemplateRef.Name
}
_, _ = p.Execute(args, name, workflow.Status)
}(client, args)

jsonResp, err := json.Marshal(executor.ExecuteTemplateReply{
Expand All @@ -222,3 +222,12 @@ func plugin(p PluginExecutor, client *wfclientset.Clientset) func(w http.Respons
return
}
}

// EmptyThen return second if the first is empty
func EmptyThen(first, second string) string {
if first == "" {
return second
} else {
return first
}
}
31 changes: 31 additions & 0 deletions cmd/argoworkflow/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestEmptyThen(t *testing.T) {
tests := []struct {
name string
first string
second string
expect string
}{{
name: "first is empty",
first: "",
second: "second",
expect: "second",
}, {
name: "first is blank",
first: " ",
second: "second",
expect: " ",
}}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := EmptyThen(tt.first, tt.second)
assert.Equal(t, tt.expect, result, "not match with", i)
})
}
}