From 925dbc5ebb8ab59f1d5a821235dade36f6f47a33 Mon Sep 17 00:00:00 2001 From: GoshaDozoretz Date: Mon, 10 Jul 2023 16:48:36 +0300 Subject: [PATCH] feat: add tag handler - RK-19248 (#92) * feat: add tag handler Signed-off-by: Gosha * fix: improve logs verbosity Signed-off-by: Gosha * fix: fix missing parameters.yaml file error Signed-off-by: Gosha * fix: tag branch is the tag Signed-off-by: Gosha * fix: add ListFiles mock Signed-off-by: Gosha --------- Signed-off-by: Gosha --- pkg/git_provider/github.go | 14 ++++++++-- pkg/webhook_handler/webhook_handler.go | 31 ++++++++++++--------- pkg/webhook_handler/webhook_handler_test.go | 14 +++++++++- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/pkg/git_provider/github.go b/pkg/git_provider/github.go index c8985b34..66c8fa5b 100644 --- a/pkg/git_provider/github.go +++ b/pkg/git_provider/github.go @@ -108,7 +108,7 @@ func (c *GithubClientImpl) SetWebhook() error { "content_type": "json", "secret": c.cfg.GitProviderConfig.WebhookSecret, }, - Events: []string{"push", "pull_request"}, + Events: []string{"push", "pull_request", "create"}, Active: github.Bool(true), } if c.cfg.GitProviderConfig.OrgLevelWebhook { @@ -252,12 +252,22 @@ func (c *GithubClientImpl) HandlePayload(request *http.Request, secret []byte) ( Branch: e.GetPullRequest().GetHead().GetRef(), Commit: e.GetPullRequest().GetHead().GetSHA(), User: e.GetPullRequest().GetUser().GetLogin(), - UserEmail: e.GetPullRequest().GetUser().GetEmail(), // Not working. GitHub missing email for PR events in payload. + UserEmail: e.GetSender().GetEmail(), // e.GetPullRequest().GetUser().GetEmail() Not working. GitHub missing email for PR events in payload. PullRequestTitle: e.GetPullRequest().GetTitle(), PullRequestURL: e.GetPullRequest().GetURL(), DestBranch: e.GetPullRequest().GetBase().GetRef(), Labels: e.GetPullRequest().Labels, } + case *github.CreateEvent: + webhookPayload = &WebhookPayload{ + Event: "create", + Action: e.GetRefType(), // Possible values are: "repository", "branch", "tag". + Repo: e.GetRepo().GetName(), + Branch: e.GetRef(), + Commit: e.GetRef(), + User: e.GetSender().GetLogin(), + UserEmail: e.GetSender().GetEmail(), + } } return webhookPayload, nil diff --git a/pkg/webhook_handler/webhook_handler.go b/pkg/webhook_handler/webhook_handler.go index 6d92436f..86452173 100644 --- a/pkg/webhook_handler/webhook_handler.go +++ b/pkg/webhook_handler/webhook_handler.go @@ -83,7 +83,7 @@ func (wh *WebhookHandlerImpl) PrepareBatchForMatchingTriggers(ctx *context.Conte utils.AddPrefixToList(*trigger.OnStart, ".workflows/"), ) if len(onStartFiles) == 0 { - return nil, fmt.Errorf("one or more of onStart: %s files found", *trigger.OnStart) + return nil, fmt.Errorf("one or more of onStart: %s files found in repo: %s branch %s", *trigger.OnStart, wh.Payload.Repo, wh.Payload.Branch) } if err != nil { return nil, err @@ -98,7 +98,7 @@ func (wh *WebhookHandlerImpl) PrepareBatchForMatchingTriggers(ctx *context.Conte utils.AddPrefixToList(*trigger.OnExit, ".workflows/"), ) if len(onExitFiles) == 0 { - log.Printf("onExist: %s files not found", *trigger.OnExit) + log.Printf("one or more of onExist: %s files not found in repo: %s branch %s", *trigger.OnExit, wh.Payload.Repo, wh.Payload.Branch) } if err != nil { return nil, err @@ -114,23 +114,28 @@ func (wh *WebhookHandlerImpl) PrepareBatchForMatchingTriggers(ctx *context.Conte utils.AddPrefixToList(*trigger.Templates, ".workflows/"), ) if len(templatesFiles) == 0 { - log.Printf("parameters: %s files not found", *trigger.Templates) + log.Printf("one or more of templates: %s files not found in repo: %s branch %s", *trigger.Templates, wh.Payload.Repo, wh.Payload.Branch) } if err != nil { return nil, err } } - parameters, err := wh.clients.GitProvider.GetFile( - ctx, - wh.Payload.Repo, - wh.Payload.Branch, - ".workflows/parameters.yaml", - ) - if err != nil { - return nil, err + parameters := &git_provider.CommitFile{ + Path: nil, + Content: nil, } - if parameters == nil { + if IsFileExists(ctx, wh, ".workflows", "parameters.yaml") { + parameters, err = wh.clients.GitProvider.GetFile( + ctx, + wh.Payload.Repo, + wh.Payload.Branch, + ".workflows/parameters.yaml", + ) + if err != nil { + return nil, err + } + } else { log.Printf("parameters.yaml not found in repo: %s branch %s", wh.Payload.Repo, wh.Payload.Branch) } @@ -145,7 +150,7 @@ func (wh *WebhookHandlerImpl) PrepareBatchForMatchingTriggers(ctx *context.Conte } } if !triggered { - return nil, fmt.Errorf("no matching trigger found for %s in branch %s", wh.Payload.Event, wh.Payload.Branch) + return nil, fmt.Errorf("no matching trigger found for event: %s action: %s in branch :%s", wh.Payload.Event, wh.Payload.Action, wh.Payload.Branch) } return workflowBatches, nil } diff --git a/pkg/webhook_handler/webhook_handler_test.go b/pkg/webhook_handler/webhook_handler_test.go index d9c5098e..71aa94d7 100644 --- a/pkg/webhook_handler/webhook_handler_test.go +++ b/pkg/webhook_handler/webhook_handler_test.go @@ -9,6 +9,7 @@ import ( "github.com/rookout/piper/pkg/utils" assertion "github.com/stretchr/testify/assert" "net/http" + "strings" "testing" ) @@ -65,7 +66,18 @@ func (m *MockGitProvider) GetFiles(ctx *context.Context, repo string, branch str } func (m *MockGitProvider) ListFiles(ctx *context.Context, repo string, branch string, path string) ([]string, error) { - return nil, nil + var files []string + + fullPath := fmt.Sprintf("%s/%s/%s/", repo, branch, path) + + for key := range commitFileMap { + if strings.Contains(key, fullPath) { + trimmed := strings.Replace(key, fullPath, "", -1) + files = append(files, trimmed) + } + } + + return files, nil } func (m *MockGitProvider) SetWebhook() error {