From 2821b3ebb4199e21e8ac0067798f1a5b46b471cb Mon Sep 17 00:00:00 2001 From: Gosha Date: Sun, 16 Jul 2023 12:28:53 +0300 Subject: [PATCH 1/2] feat: github release event support Signed-off-by: Gosha --- .../github_event_notifier_test.go | 2 +- pkg/git_provider/github.go | 34 +++++++++++++++++-- pkg/git_provider/types.go | 2 +- pkg/server/routes/webhook.go | 2 +- pkg/webhook_handler/webhook_handler_test.go | 2 +- 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/pkg/event_handler/github_event_notifier_test.go b/pkg/event_handler/github_event_notifier_test.go index 8ba501aa..1d01826c 100644 --- a/pkg/event_handler/github_event_notifier_test.go +++ b/pkg/event_handler/github_event_notifier_test.go @@ -36,7 +36,7 @@ func (m *mockGitProvider) UnsetWebhook(ctx *context.Context) error { return nil } -func (m *mockGitProvider) HandlePayload(request *http.Request, secret []byte) (*git_provider.WebhookPayload, error) { +func (m *mockGitProvider) HandlePayload(ctx *context.Context, request *http.Request, secret []byte) (*git_provider.WebhookPayload, error) { return nil, nil } diff --git a/pkg/git_provider/github.go b/pkg/git_provider/github.go index c0ac610a..a1d49d04 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", "create"}, + Events: []string{"push", "pull_request", "create", "release"}, Active: github.Bool(true), } if c.cfg.GitProviderConfig.OrgLevelWebhook { @@ -214,8 +214,7 @@ func (c *GithubClientImpl) UnsetWebhook(ctx *context.Context) error { return nil } -func (c *GithubClientImpl) HandlePayload(request *http.Request, secret []byte) (*WebhookPayload, error) { - +func (c *GithubClientImpl) HandlePayload(ctx *context.Context, request *http.Request, secret []byte) (*WebhookPayload, error) { var webhookPayload *WebhookPayload payload, err := github.ValidatePayload(request, secret) @@ -268,6 +267,20 @@ func (c *GithubClientImpl) HandlePayload(request *http.Request, secret []byte) ( User: e.GetSender().GetLogin(), UserEmail: e.GetSender().GetEmail(), } + case *github.ReleaseEvent: + commitSHA, _err := c.refToSHA(ctx, e.GetRelease().GetName(), e.GetRepo().GetName()) + if _err != nil { + return webhookPayload, _err + } + webhookPayload = &WebhookPayload{ + Event: "release", + Action: e.GetAction(), // "created", "edited", "deleted", or "prereleased". + Repo: e.GetRepo().GetName(), + Branch: e.GetRelease().GetTagName(), + Commit: *commitSHA, + User: e.GetSender().GetLogin(), + UserEmail: e.GetSender().GetEmail(), + } } return webhookPayload, nil @@ -285,6 +298,7 @@ func (c *GithubClientImpl) SetStatus(ctx *context.Context, repo *string, commit Context: utils.SPtr("Piper/ArgoWorkflows"), AvatarURL: utils.SPtr("https://argoproj.github.io/argo-workflows/assets/logo.png"), } + _, resp, err := c.client.Repositories.CreateStatus(*ctx, c.cfg.OrgName, *repo, *commit, repoStatus) if err != nil { return err @@ -297,3 +311,17 @@ func (c *GithubClientImpl) SetStatus(ctx *context.Context, repo *string, commit log.Printf("successfully set status on repo:%s commit: %s to status: %s\n", *repo, *commit, *status) return nil } + +func (c *GithubClientImpl) refToSHA(ctx *context.Context, ref string, repo string) (*string, error) { + respSHA, resp, err := c.client.Repositories.GetCommitSHA1(*ctx, c.cfg.OrgName, repo, ref, "") + if err != nil { + return nil, err + } + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("failed to set status on repo:%s, commit:%s, API call returned %d", repo, ref, resp.StatusCode) + } + + log.Printf("resolved ref: %s to SHA: %s", ref, respSHA) + return &respSHA, nil +} diff --git a/pkg/git_provider/types.go b/pkg/git_provider/types.go index 2ef0c734..339680fc 100644 --- a/pkg/git_provider/types.go +++ b/pkg/git_provider/types.go @@ -31,6 +31,6 @@ type Client interface { GetFiles(ctx *context.Context, repo string, branch string, paths []string) ([]*CommitFile, error) SetWebhook() error UnsetWebhook(ctx *context.Context) error - HandlePayload(request *http.Request, secret []byte) (*WebhookPayload, error) + 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 } diff --git a/pkg/server/routes/webhook.go b/pkg/server/routes/webhook.go index eda8a0d1..7d57548e 100644 --- a/pkg/server/routes/webhook.go +++ b/pkg/server/routes/webhook.go @@ -15,7 +15,7 @@ func AddWebhookRoutes(cfg *conf.GlobalConfig, clients *clients.Clients, rg *gin. webhook.POST("", func(c *gin.Context) { ctx := c.Copy().Request.Context() - webhookPayload, err := clients.GitProvider.HandlePayload(c.Request, []byte(cfg.GitProviderConfig.WebhookSecret)) + webhookPayload, err := clients.GitProvider.HandlePayload(&ctx, c.Request, []byte(cfg.GitProviderConfig.WebhookSecret)) if err != nil { log.Println(err) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) diff --git a/pkg/webhook_handler/webhook_handler_test.go b/pkg/webhook_handler/webhook_handler_test.go index 71aa94d7..d154b4ec 100644 --- a/pkg/webhook_handler/webhook_handler_test.go +++ b/pkg/webhook_handler/webhook_handler_test.go @@ -88,7 +88,7 @@ func (m *MockGitProvider) UnsetWebhook(ctx *context.Context) error { return nil } -func (m *MockGitProvider) HandlePayload(request *http.Request, secret []byte) (*git_provider.WebhookPayload, error) { +func (m *MockGitProvider) HandlePayload(ctx *context.Context, request *http.Request, secret []byte) (*git_provider.WebhookPayload, error) { return nil, nil } From c53abef17fe1cea7f81ef383d96ba0b659f11c75 Mon Sep 17 00:00:00 2001 From: Gosha Date: Sun, 16 Jul 2023 15:33:33 +0300 Subject: [PATCH 2/2] fix: minor fixes Signed-off-by: Gosha --- pkg/git_provider/github.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/git_provider/github.go b/pkg/git_provider/github.go index 696f946f..7b74cd33 100644 --- a/pkg/git_provider/github.go +++ b/pkg/git_provider/github.go @@ -312,7 +312,6 @@ func (c *GithubClientImpl) SetStatus(ctx *context.Context, repo *string, commit return nil } - func (c *GithubClientImpl) refToSHA(ctx *context.Context, ref string, repo string) (*string, error) { respSHA, resp, err := c.client.Repositories.GetCommitSHA1(*ctx, c.cfg.OrgName, repo, ref, "") if err != nil { @@ -325,6 +324,7 @@ func (c *GithubClientImpl) refToSHA(ctx *context.Context, ref string, repo strin log.Printf("resolved ref: %s to SHA: %s", ref, respSHA) return &respSHA, nil +} func (c *GithubClientImpl) extractLabelNames(labels []*github.Label) []string { var returnLabelsList []string @@ -332,3 +332,4 @@ func (c *GithubClientImpl) extractLabelNames(labels []*github.Label) []string { returnLabelsList = append(returnLabelsList, *label.Name) } return returnLabelsList +}