Skip to content

Commit

Permalink
feat: added provider struct method: GetCorrelatingEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
omri2001 committed Jan 11, 2025
1 parent 075cbf2 commit 02e1c17
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 105 deletions.
50 changes: 50 additions & 0 deletions pkg/event_handler/event_notifier.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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

Expand Down
101 changes: 0 additions & 101 deletions pkg/event_handler/github_event_notifier.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/event_handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions pkg/git_provider/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
25 changes: 25 additions & 0 deletions pkg/git_provider/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions pkg/git_provider/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git_provider
import (
"context"
"fmt"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions pkg/git_provider/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git_provider

import (
"context"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"net/http"
)

Expand Down Expand Up @@ -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)
}

0 comments on commit 02e1c17

Please sign in to comment.