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: added provider struct method: GetCorrelatingEvent #41

Merged
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
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)
}
24 changes: 16 additions & 8 deletions pkg/webhook_creator/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ package webhook_creator
import (
context2 "context"
"errors"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/quickube/piper/pkg/git_provider"
"golang.org/x/net/context"
"net/http"
)

type MockGitProviderClient struct {
ListFilesFunc func(ctx context.Context, repo string, branch string, path string) ([]string, error)
GetFileFunc func(ctx context.Context, repo string, branch string, path string) (*git_provider.CommitFile, error)
GetFilesFunc func(ctx context.Context, repo string, branch string, paths []string) ([]*git_provider.CommitFile, error)
SetWebhookFunc func(ctx context.Context, repo *string) (*git_provider.HookWithStatus, error)
UnsetWebhookFunc func(ctx context.Context, hook *git_provider.HookWithStatus) error
HandlePayloadFunc func(request *http.Request, secret []byte) (*git_provider.WebhookPayload, error)
SetStatusFunc func(ctx context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error
PingHookFunc func(ctx context.Context, hook *git_provider.HookWithStatus) error
ListFilesFunc func(ctx context.Context, repo string, branch string, path string) ([]string, error)
GetFileFunc func(ctx context.Context, repo string, branch string, path string) (*git_provider.CommitFile, error)
GetFilesFunc func(ctx context.Context, repo string, branch string, paths []string) ([]*git_provider.CommitFile, error)
SetWebhookFunc func(ctx context.Context, repo *string) (*git_provider.HookWithStatus, error)
UnsetWebhookFunc func(ctx context.Context, hook *git_provider.HookWithStatus) error
HandlePayloadFunc func(request *http.Request, secret []byte) (*git_provider.WebhookPayload, error)
SetStatusFunc func(ctx context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error
PingHookFunc func(ctx context.Context, hook *git_provider.HookWithStatus) error
GetCorrelatingEventFunc func(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error)
}

func (m *MockGitProviderClient) ListFiles(ctx context2.Context, repo string, branch string, path string) ([]string, error) {
Expand Down Expand Up @@ -67,6 +69,12 @@ func (m *MockGitProviderClient) SetStatus(ctx context2.Context, repo *string, co
}
return errors.New("unimplemented")
}
func (m *MockGitProviderClient) GetCorrelatingEvent(ctx context.Context, workflowEvent *v1alpha1.WorkflowPhase) (string, error) {
if m.GetCorrelatingEventFunc != nil {
return m.GetCorrelatingEventFunc(ctx, workflowEvent)
}
return "", errors.New("unimplemented")
}

func (m *MockGitProviderClient) PingHook(ctx context2.Context, hook *git_provider.HookWithStatus) error {
if m.PingHookFunc != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/webhook_handler/webhook_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package webhook_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/common"
"github.com/quickube/piper/pkg/git_provider"
Expand Down Expand Up @@ -95,7 +96,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
Loading