Skip to content

Commit

Permalink
Skip certain events (#25)
Browse files Browse the repository at this point in the history
* Skip type

* Change switch

* newline

* Change to 202
  • Loading branch information
polyrain authored Jul 7, 2023
1 parent b6cf8d1 commit 7fb8218
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
6 changes: 3 additions & 3 deletions pkg/github_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package github_client

import (
"context"
"fmt"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -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
}
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/gitlab_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions pkg/server/hook_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pkg/vcs_clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vcs_clients

import (
"context"
"errors"
"net/http"

"github.com/zapier/kubechecks/pkg/repo"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7fb8218

Please sign in to comment.