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: github release event support - RK-19329 #101

Merged
merged 3 commits into from
Jul 16, 2023
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
2 changes: 1 addition & 1 deletion pkg/event_handler/github_event_notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
34 changes: 31 additions & 3 deletions pkg/git_provider/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -298,6 +312,20 @@ 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 {
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
}

func (c *GithubClientImpl) extractLabelNames(labels []*github.Label) []string {
var returnLabelsList []string
for _, label := range labels {
Expand Down
2 changes: 1 addition & 1 deletion pkg/git_provider/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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
}
2 changes: 1 addition & 1 deletion pkg/server/routes/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook_handler/webhook_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading