From 7fb82189260b6f609e9faaf0d3d4a994f635e482 Mon Sep 17 00:00:00 2001 From: Jenna <33305772+polyrain@users.noreply.github.com> Date: Fri, 7 Jul 2023 13:02:56 +1000 Subject: [PATCH] Skip certain events (#25) * Skip type * Change switch * newline * Change to 202 --- pkg/github_client/client.go | 6 +++--- pkg/gitlab_client/client.go | 7 ++++--- pkg/server/hook_handler.go | 12 +++++++++--- pkg/vcs_clients/client.go | 4 ++++ 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/pkg/github_client/client.go b/pkg/github_client/client.go index fe584b5c..f1290e61 100644 --- a/pkg/github_client/client.go +++ b/pkg/github_client/client.go @@ -2,7 +2,6 @@ package github_client import ( "context" - "fmt" "io" "net/http" "strings" @@ -88,10 +87,11 @@ func (c *Client) CreateRepo(ctx context.Context, payload interface{}) (*repo.Rep return buildRepoFromEvent(p), nil default: log.Info().Str("action", p.GetAction()).Msg("ignoring Github pull request event due to non commit based action") - return nil, fmt.Errorf("ignoring Github pull request event due to non commit based action") + return nil, vcs_clients.ErrInvalidType } default: - return nil, fmt.Errorf("invalid event provided to Github client") + log.Error().Msg("invalid event provided to Github client") + return nil, vcs_clients.ErrInvalidType } } diff --git a/pkg/gitlab_client/client.go b/pkg/gitlab_client/client.go index 2f7ead45..e74f03ee 100644 --- a/pkg/gitlab_client/client.go +++ b/pkg/gitlab_client/client.go @@ -11,6 +11,7 @@ import ( "github.com/spf13/viper" "github.com/xanzy/go-gitlab" "github.com/zapier/kubechecks/pkg/repo" + "github.com/zapier/kubechecks/pkg/vcs_clients" ) var gitlabClient *Client @@ -89,13 +90,13 @@ func (c *Client) CreateRepo(ctx context.Context, eventRequest interface{}) (*rep return buildRepoFromEvent(event), nil default: log.Trace().Msgf("Unhandled Action %s", event.ObjectAttributes.Action) - return nil, fmt.Errorf("unhandled action %s", event.ObjectAttributes.Action) + return nil, vcs_clients.ErrInvalidType } default: log.Trace().Msgf("Unhandled Event: %T", event) - return nil, fmt.Errorf("unhandled Event %T", event) + return nil, vcs_clients.ErrInvalidType } - return nil, fmt.Errorf("unhandled Event %T", eventRequest) + return nil, vcs_clients.ErrInvalidType } func buildRepoFromEvent(event *gitlab.MergeEvent) *repo.Repo { diff --git a/pkg/server/hook_handler.go b/pkg/server/hook_handler.go index 24e1ac21..a8426597 100644 --- a/pkg/server/hook_handler.go +++ b/pkg/server/hook_handler.go @@ -97,9 +97,15 @@ func (h *VCSHookHandler) groupHandler(c echo.Context) error { // We try to build a generic Repo from this data, to construct our CheckEvent repo, err := h.client.CreateRepo(ctx, eventRequest) if err != nil { - // TODO: do something ELSE with the error - log.Error().Err(err).Msg("Failed to create a repository locally") - return echo.ErrBadRequest + switch err { + case vcs_clients.ErrInvalidType: + log.Debug().Msg("Ignoring event, not a merge request") + return c.String(http.StatusAccepted, "Skipped") + default: + // TODO: do something ELSE with the error + log.Error().Err(err).Msg("Failed to create a repository locally") + return echo.ErrBadRequest + } } // We now have a generic repo with all the info we need to start processing an event. Hand off to the event processor diff --git a/pkg/vcs_clients/client.go b/pkg/vcs_clients/client.go index a3a6658e..bed645dd 100644 --- a/pkg/vcs_clients/client.go +++ b/pkg/vcs_clients/client.go @@ -2,6 +2,7 @@ package vcs_clients import ( "context" + "errors" "net/http" "github.com/zapier/kubechecks/pkg/repo" @@ -47,6 +48,9 @@ func (s CommitState) StateToDesc() string { return "unknown" } +// Sentinel errors for use in client implementations +var ErrInvalidType = errors.New("Invalid event type") + // Clients need to implement this interface to allow CheckEvents to talk to their respective PR etc type Client interface { // Take in project name in form "owner/repo" (ie zapier/kubechecks), the PR/MR id, and the actual message