Skip to content

Commit f1e817f

Browse files
authored
feat: added provider struct method: GetCorrelatingEvent (#41)
* feat: added provider struct method: GetCorrelatingEvent * fix: fixed webhook tests
1 parent 075cbf2 commit f1e817f

File tree

10 files changed

+151
-114
lines changed

10 files changed

+151
-114
lines changed

pkg/event_handler/event_notifier.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package event_handler
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
7+
"github.com/quickube/piper/pkg/clients"
8+
"github.com/quickube/piper/pkg/conf"
9+
"github.com/quickube/piper/pkg/utils"
10+
)
11+
12+
type eventNotifier struct {
13+
cfg *conf.GlobalConfig
14+
clients *clients.Clients
15+
}
16+
17+
func NewEventNotifier(cfg *conf.GlobalConfig, clients *clients.Clients) EventNotifier {
18+
return &eventNotifier{
19+
cfg: cfg,
20+
clients: clients,
21+
}
22+
}
23+
24+
func (en *eventNotifier) Notify(ctx context.Context, workflow *v1alpha1.Workflow) error {
25+
fmt.Printf("Notifing workflow, %s\n", workflow.GetName())
26+
27+
repo, ok := workflow.GetLabels()["repo"]
28+
if !ok {
29+
return fmt.Errorf("failed get repo label for workflow: %s", workflow.GetName())
30+
}
31+
commit, ok := workflow.GetLabels()["commit"]
32+
if !ok {
33+
return fmt.Errorf("failed get commit label for workflow: %s", workflow.GetName())
34+
}
35+
36+
workflowLink := fmt.Sprintf("%s/workflows/%s/%s", en.cfg.WorkflowServerConfig.ArgoAddress, en.cfg.Namespace, workflow.GetName())
37+
38+
status, err := en.clients.GitProvider.GetCorrelatingEvent(ctx, &workflow.Status.Phase)
39+
if err != nil {
40+
return fmt.Errorf("failed to translate workflow status for phase: %s status: %s", string(workflow.Status.Phase), status)
41+
}
42+
43+
message := utils.TrimString(workflow.Status.Message, 140) // Max length of message is 140 characters
44+
err = en.clients.GitProvider.SetStatus(ctx, &repo, &commit, &workflowLink, &status, &message)
45+
if err != nil {
46+
return fmt.Errorf("failed to set status for workflow %s: %s", workflow.GetName(), err)
47+
}
48+
49+
return nil
50+
}

pkg/event_handler/github_event_notifier_test.go pkg/event_handler/event_notifier_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ func (m *mockGitProvider) HandlePayload(ctx context.Context, request *http.Reque
4343
func (m *mockGitProvider) SetStatus(ctx context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error {
4444
return nil
4545
}
46-
46+
func (m *mockGitProvider) GetCorrelatingEvent(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error) {
47+
return "", nil
48+
}
4749
func (m *mockGitProvider) PingHook(ctx context.Context, hook *git_provider.HookWithStatus) error {
4850
return nil
4951
}
@@ -193,8 +195,8 @@ func TestNotify(t *testing.T) {
193195
GitProvider: &mockGitProvider{},
194196
}
195197

196-
// Create a new githubNotifier instance
197-
gn := NewGithubEventNotifier(cfg, globalClients)
198+
// Create a new eventNotifier instance
199+
gn := NewEventNotifier(cfg, globalClients)
198200

199201
// Call the Notify method
200202

pkg/event_handler/github_event_notifier.go

-101
This file was deleted.

pkg/event_handler/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func Start(ctx context.Context, stop context.CancelFunc, cfg *conf.GlobalConfig,
2121
return
2222
}
2323

24-
notifier := NewGithubEventNotifier(cfg, clients)
24+
notifier := NewEventNotifier(cfg, clients)
2525
handler := &workflowEventHandler{
2626
Clients: clients,
2727
Notifier: notifier,

pkg/git_provider/bitbucket.go

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
context2 "context"
66
"encoding/json"
77
"fmt"
8+
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
89
"github.com/ktrysmt/go-bitbucket"
910
"github.com/quickube/piper/pkg/conf"
1011
"github.com/quickube/piper/pkg/utils"
@@ -225,6 +226,28 @@ func (b BitbucketClientImpl) SetStatus(ctx context2.Context, repo *string, commi
225226
return nil
226227
}
227228

229+
func (b BitbucketClientImpl) GetCorrelatingEvent(ctx context2.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error) {
230+
var event string
231+
switch *workflowEvent {
232+
case v1alpha1.WorkflowUnknown:
233+
event = "INPROGRESS"
234+
case v1alpha1.WorkflowPending:
235+
event = "INPROGRESS"
236+
case v1alpha1.WorkflowRunning:
237+
238+
event = "INPROGRESS"
239+
case v1alpha1.WorkflowSucceeded:
240+
event = "SUCCESSFUL"
241+
case v1alpha1.WorkflowFailed:
242+
event = "FAILED"
243+
case v1alpha1.WorkflowError:
244+
event = "STOPPED"
245+
default:
246+
return "", fmt.Errorf("unimplemented workflow event")
247+
}
248+
return event, nil
249+
}
250+
228251
func (b BitbucketClientImpl) PingHook(ctx context2.Context, hook *HookWithStatus) error {
229252
//TODO implement me
230253
panic("implement me")

pkg/git_provider/github.go

+25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package git_provider
33
import (
44
"context"
55
"fmt"
6+
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
67
"github.com/quickube/piper/pkg/utils"
78
"log"
89
"net/http"
@@ -332,6 +333,30 @@ func (c *GithubClientImpl) SetStatus(ctx context.Context, repo *string, commit *
332333
return nil
333334
}
334335

336+
func (c *GithubClientImpl) GetCorrelatingEvent(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error) {
337+
var event string
338+
switch *workflowEvent {
339+
case v1alpha1.WorkflowUnknown:
340+
event = "pending"
341+
case v1alpha1.WorkflowPending:
342+
343+
event = "pending"
344+
case v1alpha1.WorkflowRunning:
345+
346+
event = "pending"
347+
case v1alpha1.WorkflowSucceeded:
348+
event = "success"
349+
case v1alpha1.WorkflowFailed:
350+
event = "failure"
351+
case v1alpha1.WorkflowError:
352+
event = "error"
353+
default:
354+
return "", fmt.Errorf("unimplemented workflow event")
355+
}
356+
357+
return event, nil
358+
}
359+
335360
func (c *GithubClientImpl) PingHook(ctx context.Context, hook *HookWithStatus) error {
336361
if c.cfg.OrgLevelWebhook && hook.RepoName != nil {
337362
return fmt.Errorf("trying to ping repo scope webhook while configured for org level webhook. repo: %s", *hook.RepoName)

pkg/git_provider/gitlab.go

+25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package git_provider
33
import (
44
"context"
55
"fmt"
6+
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
67
"io"
78
"log"
89
"net/http"
@@ -368,6 +369,30 @@ func (c *GitlabClientImpl) SetStatus(ctx context.Context, repo *string, commit *
368369
return nil
369370
}
370371

372+
func (c *GitlabClientImpl) GetCorrelatingEvent(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error) {
373+
var event string
374+
switch *workflowEvent {
375+
case v1alpha1.WorkflowUnknown:
376+
event = "pending"
377+
case v1alpha1.WorkflowPending:
378+
379+
event = "pending"
380+
case v1alpha1.WorkflowRunning:
381+
382+
event = "running"
383+
case v1alpha1.WorkflowSucceeded:
384+
event = "success"
385+
case v1alpha1.WorkflowFailed:
386+
event = "failed"
387+
case v1alpha1.WorkflowError:
388+
event = "failed"
389+
default:
390+
return "", fmt.Errorf("unimplemented workflow event")
391+
}
392+
393+
return event, nil
394+
}
395+
371396
func (c *GitlabClientImpl) PingHook(ctx context.Context, hook *HookWithStatus) error {
372397
//TODO implement me
373398
panic("implement me")

pkg/git_provider/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package git_provider
22

33
import (
44
"context"
5+
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
56
"net/http"
67
)
78

@@ -42,4 +43,5 @@ type Client interface {
4243
HandlePayload(ctx context.Context, request *http.Request, secret []byte) (*WebhookPayload, error)
4344
SetStatus(ctx context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error
4445
PingHook(ctx context.Context, hook *HookWithStatus) error
46+
GetCorrelatingEvent(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error)
4547
}

pkg/webhook_creator/mocks.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ package webhook_creator
33
import (
44
context2 "context"
55
"errors"
6+
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
67
"github.com/quickube/piper/pkg/git_provider"
78
"golang.org/x/net/context"
89
"net/http"
910
)
1011

1112
type MockGitProviderClient struct {
12-
ListFilesFunc func(ctx context.Context, repo string, branch string, path string) ([]string, error)
13-
GetFileFunc func(ctx context.Context, repo string, branch string, path string) (*git_provider.CommitFile, error)
14-
GetFilesFunc func(ctx context.Context, repo string, branch string, paths []string) ([]*git_provider.CommitFile, error)
15-
SetWebhookFunc func(ctx context.Context, repo *string) (*git_provider.HookWithStatus, error)
16-
UnsetWebhookFunc func(ctx context.Context, hook *git_provider.HookWithStatus) error
17-
HandlePayloadFunc func(request *http.Request, secret []byte) (*git_provider.WebhookPayload, error)
18-
SetStatusFunc func(ctx context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error
19-
PingHookFunc func(ctx context.Context, hook *git_provider.HookWithStatus) error
13+
ListFilesFunc func(ctx context.Context, repo string, branch string, path string) ([]string, error)
14+
GetFileFunc func(ctx context.Context, repo string, branch string, path string) (*git_provider.CommitFile, error)
15+
GetFilesFunc func(ctx context.Context, repo string, branch string, paths []string) ([]*git_provider.CommitFile, error)
16+
SetWebhookFunc func(ctx context.Context, repo *string) (*git_provider.HookWithStatus, error)
17+
UnsetWebhookFunc func(ctx context.Context, hook *git_provider.HookWithStatus) error
18+
HandlePayloadFunc func(request *http.Request, secret []byte) (*git_provider.WebhookPayload, error)
19+
SetStatusFunc func(ctx context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error
20+
PingHookFunc func(ctx context.Context, hook *git_provider.HookWithStatus) error
21+
GetCorrelatingEventFunc func(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error)
2022
}
2123

2224
func (m *MockGitProviderClient) ListFiles(ctx context2.Context, repo string, branch string, path string) ([]string, error) {
@@ -67,6 +69,12 @@ func (m *MockGitProviderClient) SetStatus(ctx context2.Context, repo *string, co
6769
}
6870
return errors.New("unimplemented")
6971
}
72+
func (m *MockGitProviderClient) GetCorrelatingEvent(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error) {
73+
if m.GetCorrelatingEventFunc != nil {
74+
return m.GetCorrelatingEventFunc(ctx, workflowEvent)
75+
}
76+
return "", errors.New("unimplemented")
77+
}
7078

7179
func (m *MockGitProviderClient) PingHook(ctx context2.Context, hook *git_provider.HookWithStatus) error {
7280
if m.PingHookFunc != nil {

pkg/webhook_handler/webhook_handler_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package webhook_handler
33
import (
44
"context"
55
"fmt"
6+
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
67
"github.com/quickube/piper/pkg/clients"
78
"github.com/quickube/piper/pkg/common"
89
"github.com/quickube/piper/pkg/git_provider"
@@ -95,7 +96,9 @@ func (m *mockGitProvider) HandlePayload(ctx context.Context, request *http.Reque
9596
func (m *mockGitProvider) SetStatus(ctx context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error {
9697
return nil
9798
}
98-
99+
func (m *mockGitProvider) GetCorrelatingEvent(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error) {
100+
return "", nil
101+
}
99102
func (m *mockGitProvider) PingHook(ctx context.Context, hook *git_provider.HookWithStatus) error {
100103
return nil
101104
}

0 commit comments

Comments
 (0)