From 02e1c17f9b2ab332143823d7160c306053fa125a Mon Sep 17 00:00:00 2001 From: Omri Assa Date: Sat, 11 Jan 2025 21:22:14 +0200 Subject: [PATCH] feat: added provider struct method: GetCorrelatingEvent --- pkg/event_handler/event_notifier.go | 50 +++++++++ ...otifier_test.go => event_notifier_test.go} | 8 +- pkg/event_handler/github_event_notifier.go | 101 ------------------ pkg/event_handler/main.go | 2 +- pkg/git_provider/bitbucket.go | 23 ++++ pkg/git_provider/github.go | 25 +++++ pkg/git_provider/gitlab.go | 25 +++++ pkg/git_provider/types.go | 2 + 8 files changed, 131 insertions(+), 105 deletions(-) create mode 100644 pkg/event_handler/event_notifier.go rename pkg/event_handler/{github_event_notifier_test.go => event_notifier_test.go} (95%) delete mode 100644 pkg/event_handler/github_event_notifier.go diff --git a/pkg/event_handler/event_notifier.go b/pkg/event_handler/event_notifier.go new file mode 100644 index 0000000..7defff2 --- /dev/null +++ b/pkg/event_handler/event_notifier.go @@ -0,0 +1,50 @@ +package event_handler + +import ( + "context" + "fmt" + "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" + "github.com/quickube/piper/pkg/clients" + "github.com/quickube/piper/pkg/conf" + "github.com/quickube/piper/pkg/utils" +) + +type eventNotifier struct { + cfg *conf.GlobalConfig + clients *clients.Clients +} + +func NewEventNotifier(cfg *conf.GlobalConfig, clients *clients.Clients) EventNotifier { + return &eventNotifier{ + cfg: cfg, + clients: clients, + } +} + +func (en *eventNotifier) Notify(ctx context.Context, workflow *v1alpha1.Workflow) error { + fmt.Printf("Notifing workflow, %s\n", workflow.GetName()) + + repo, ok := workflow.GetLabels()["repo"] + if !ok { + return fmt.Errorf("failed get repo label for workflow: %s", workflow.GetName()) + } + commit, ok := workflow.GetLabels()["commit"] + if !ok { + return fmt.Errorf("failed get commit label for workflow: %s", workflow.GetName()) + } + + workflowLink := fmt.Sprintf("%s/workflows/%s/%s", en.cfg.WorkflowServerConfig.ArgoAddress, en.cfg.Namespace, workflow.GetName()) + + status, err := en.clients.GitProvider.GetCorrelatingEvent(ctx, &workflow.Status.Phase) + if err != nil { + return fmt.Errorf("failed to translate workflow status for phase: %s status: %s", string(workflow.Status.Phase), status) + } + + message := utils.TrimString(workflow.Status.Message, 140) // Max length of message is 140 characters + err = en.clients.GitProvider.SetStatus(ctx, &repo, &commit, &workflowLink, &status, &message) + if err != nil { + return fmt.Errorf("failed to set status for workflow %s: %s", workflow.GetName(), err) + } + + return nil +} diff --git a/pkg/event_handler/github_event_notifier_test.go b/pkg/event_handler/event_notifier_test.go similarity index 95% rename from pkg/event_handler/github_event_notifier_test.go rename to pkg/event_handler/event_notifier_test.go index 1877d0e..a7f4515 100644 --- a/pkg/event_handler/github_event_notifier_test.go +++ b/pkg/event_handler/event_notifier_test.go @@ -43,7 +43,9 @@ func (m *mockGitProvider) HandlePayload(ctx context.Context, request *http.Reque func (m *mockGitProvider) SetStatus(ctx context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error { return nil } - +func (m *mockGitProvider) GetCorrelatingEvent(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error) { + return "", nil +} func (m *mockGitProvider) PingHook(ctx context.Context, hook *git_provider.HookWithStatus) error { return nil } @@ -193,8 +195,8 @@ func TestNotify(t *testing.T) { GitProvider: &mockGitProvider{}, } - // Create a new githubNotifier instance - gn := NewGithubEventNotifier(cfg, globalClients) + // Create a new eventNotifier instance + gn := NewEventNotifier(cfg, globalClients) // Call the Notify method diff --git a/pkg/event_handler/github_event_notifier.go b/pkg/event_handler/github_event_notifier.go deleted file mode 100644 index 2fec379..0000000 --- a/pkg/event_handler/github_event_notifier.go +++ /dev/null @@ -1,101 +0,0 @@ -package event_handler - -import ( - "context" - "fmt" - "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" - "github.com/quickube/piper/pkg/clients" - "github.com/quickube/piper/pkg/conf" - "github.com/quickube/piper/pkg/utils" -) - -var workflowTranslationToGithubMap = map[string]string{ - "": "pending", - "Pending": "pending", - "Running": "pending", - "Succeeded": "success", - "Failed": "failure", - "Error": "error", -} - -var workflowTranslationToBitbucketMap = map[string]string{ - "": "INPROGRESS", - "Pending": "INPROGRESS", - "Running": "INPROGRESS", - "Succeeded": "SUCCESSFUL", - "Failed": "FAILED", - "Error": "STOPPED", -} - -var workflowTranslationToGitlabMap = map[string]string{ - "": "pending", - "Pending": "pending", - "Running": "running", - "Succeeded": "success", - "Failed": "failed", - "Error": "failed", -} - -type githubNotifier struct { - cfg *conf.GlobalConfig - clients *clients.Clients -} - -func NewGithubEventNotifier(cfg *conf.GlobalConfig, clients *clients.Clients) EventNotifier { - return &githubNotifier{ - cfg: cfg, - clients: clients, - } -} - -func (gn *githubNotifier) Notify(ctx context.Context, workflow *v1alpha1.Workflow) error { - fmt.Printf("Notifing workflow, %s\n", workflow.GetName()) - - repo, ok := workflow.GetLabels()["repo"] - if !ok { - return fmt.Errorf("failed get repo label for workflow: %s", workflow.GetName()) - } - commit, ok := workflow.GetLabels()["commit"] - if !ok { - return fmt.Errorf("failed get commit label for workflow: %s", workflow.GetName()) - } - - workflowLink := fmt.Sprintf("%s/workflows/%s/%s", gn.cfg.WorkflowServerConfig.ArgoAddress, gn.cfg.Namespace, workflow.GetName()) - - status, err := gn.translateWorkflowStatus(string(workflow.Status.Phase), workflow.GetName()) - if err != nil { - return err - } - - message := utils.TrimString(workflow.Status.Message, 140) // Max length of message is 140 characters - err = gn.clients.GitProvider.SetStatus(ctx, &repo, &commit, &workflowLink, &status, &message) - if err != nil { - return fmt.Errorf("failed to set status for workflow %s: %s", workflow.GetName(), err) - } - - return nil -} - -func (gn *githubNotifier) translateWorkflowStatus(status string, workflowName string) (string, error) { - switch gn.cfg.GitProviderConfig.Provider { - case "github": - result, ok := workflowTranslationToGithubMap[status] - if !ok { - return "", fmt.Errorf("failed to translate workflow status to github status for %s status: %s", workflowName, status) - } - return result, nil - case "bitbucket": - result, ok := workflowTranslationToBitbucketMap[status] - if !ok { - return "", fmt.Errorf("failed to translate workflow status to bitbucket status for %s status: %s", workflowName, status) - } - return result, nil - case "gitlab": - result, ok := workflowTranslationToGitlabMap[status] - if !ok { - return "", fmt.Errorf("failed to translate workflow status to gitlab status for %s status: %s", workflowName, status) - } - return result, nil - } - return "", fmt.Errorf("failed to translate workflow status") -} diff --git a/pkg/event_handler/main.go b/pkg/event_handler/main.go index 92daed9..89d6b8a 100644 --- a/pkg/event_handler/main.go +++ b/pkg/event_handler/main.go @@ -21,7 +21,7 @@ func Start(ctx context.Context, stop context.CancelFunc, cfg *conf.GlobalConfig, return } - notifier := NewGithubEventNotifier(cfg, clients) + notifier := NewEventNotifier(cfg, clients) handler := &workflowEventHandler{ Clients: clients, Notifier: notifier, diff --git a/pkg/git_provider/bitbucket.go b/pkg/git_provider/bitbucket.go index dcb7a09..170b441 100644 --- a/pkg/git_provider/bitbucket.go +++ b/pkg/git_provider/bitbucket.go @@ -5,6 +5,7 @@ import ( context2 "context" "encoding/json" "fmt" + "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" "github.com/ktrysmt/go-bitbucket" "github.com/quickube/piper/pkg/conf" "github.com/quickube/piper/pkg/utils" @@ -225,6 +226,28 @@ func (b BitbucketClientImpl) SetStatus(ctx context2.Context, repo *string, commi return nil } +func (b BitbucketClientImpl) GetCorrelatingEvent(ctx context2.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error) { + var event string + switch *workflowEvent { + case v1alpha1.WorkflowUnknown: + event = "INPROGRESS" + case v1alpha1.WorkflowPending: + event = "INPROGRESS" + case v1alpha1.WorkflowRunning: + + event = "INPROGRESS" + case v1alpha1.WorkflowSucceeded: + event = "SUCCESSFUL" + case v1alpha1.WorkflowFailed: + event = "FAILED" + case v1alpha1.WorkflowError: + event = "STOPPED" + default: + return "", fmt.Errorf("unimplemented workflow event") + } + return event, nil +} + func (b BitbucketClientImpl) PingHook(ctx context2.Context, hook *HookWithStatus) error { //TODO implement me panic("implement me") diff --git a/pkg/git_provider/github.go b/pkg/git_provider/github.go index 8fdf7fe..588f5ce 100644 --- a/pkg/git_provider/github.go +++ b/pkg/git_provider/github.go @@ -3,6 +3,7 @@ package git_provider import ( "context" "fmt" + "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" "github.com/quickube/piper/pkg/utils" "log" "net/http" @@ -332,6 +333,30 @@ func (c *GithubClientImpl) SetStatus(ctx context.Context, repo *string, commit * return nil } +func (c *GithubClientImpl) GetCorrelatingEvent(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error) { + var event string + switch *workflowEvent { + case v1alpha1.WorkflowUnknown: + event = "pending" + case v1alpha1.WorkflowPending: + + event = "pending" + case v1alpha1.WorkflowRunning: + + event = "pending" + case v1alpha1.WorkflowSucceeded: + event = "success" + case v1alpha1.WorkflowFailed: + event = "failure" + case v1alpha1.WorkflowError: + event = "error" + default: + return "", fmt.Errorf("unimplemented workflow event") + } + + return event, nil +} + func (c *GithubClientImpl) PingHook(ctx context.Context, hook *HookWithStatus) error { if c.cfg.OrgLevelWebhook && hook.RepoName != nil { return fmt.Errorf("trying to ping repo scope webhook while configured for org level webhook. repo: %s", *hook.RepoName) diff --git a/pkg/git_provider/gitlab.go b/pkg/git_provider/gitlab.go index 65397b4..a9849b1 100644 --- a/pkg/git_provider/gitlab.go +++ b/pkg/git_provider/gitlab.go @@ -3,6 +3,7 @@ package git_provider import ( "context" "fmt" + "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" "io" "log" "net/http" @@ -368,6 +369,30 @@ func (c *GitlabClientImpl) SetStatus(ctx context.Context, repo *string, commit * return nil } +func (c *GitlabClientImpl) GetCorrelatingEvent(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error) { + var event string + switch *workflowEvent { + case v1alpha1.WorkflowUnknown: + event = "pending" + case v1alpha1.WorkflowPending: + + event = "pending" + case v1alpha1.WorkflowRunning: + + event = "running" + case v1alpha1.WorkflowSucceeded: + event = "success" + case v1alpha1.WorkflowFailed: + event = "failed" + case v1alpha1.WorkflowError: + event = "failed" + default: + return "", fmt.Errorf("unimplemented workflow event") + } + + return event, nil +} + func (c *GitlabClientImpl) PingHook(ctx context.Context, hook *HookWithStatus) error { //TODO implement me panic("implement me") diff --git a/pkg/git_provider/types.go b/pkg/git_provider/types.go index f1ac20e..57fc218 100644 --- a/pkg/git_provider/types.go +++ b/pkg/git_provider/types.go @@ -2,6 +2,7 @@ package git_provider import ( "context" + "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" "net/http" ) @@ -42,4 +43,5 @@ type Client interface { HandlePayload(ctx context.Context, request *http.Request, secret []byte) (*WebhookPayload, error) SetStatus(ctx context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error PingHook(ctx context.Context, hook *HookWithStatus) error + GetCorrelatingEvent(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error) }