From af2c278723ee55e32f9ff0099f9d685aa66c324e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 25 Dec 2022 21:50:57 +0100 Subject: [PATCH] Dont panic on hook parsing (#1501) close #1422 --- server/forge/gitea/gitea.go | 5 +++++ server/forge/gitea/parse.go | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/server/forge/gitea/gitea.go b/server/forge/gitea/gitea.go index c00102a4a..a87740f4a 100644 --- a/server/forge/gitea/gitea.go +++ b/server/forge/gitea/gitea.go @@ -482,6 +482,11 @@ func (c *Gitea) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model. return nil, nil, err } + if repo == nil || pipeline == nil { + // ignore hook + return nil, nil, nil + } + if pipeline.Event == model.EventPull && len(pipeline.ChangedFiles) == 0 { index, err := strconv.ParseInt(strings.Split(pipeline.Ref, "/")[2], 10, 64) if err != nil { diff --git a/server/forge/gitea/parse.go b/server/forge/gitea/parse.go index 65599d729..d617a21cf 100644 --- a/server/forge/gitea/parse.go +++ b/server/forge/gitea/parse.go @@ -20,6 +20,8 @@ import ( "net/http" "strings" + "github.com/rs/zerolog/log" + "github.com/woodpecker-ci/woodpecker/server/model" ) @@ -41,7 +43,8 @@ const ( // parseHook parses a Gitea hook from an http.Request request and returns // Repo and Pipeline detail. If a hook type is unsupported nil values are returned. func parseHook(r *http.Request) (*model.Repo, *model.Pipeline, error) { - switch r.Header.Get(hookEvent) { + hookType := r.Header.Get(hookEvent) + switch hookType { case hookPush: return parsePushHook(r.Body) case hookCreated: @@ -49,6 +52,7 @@ func parseHook(r *http.Request) (*model.Repo, *model.Pipeline, error) { case hookPullRequest: return parsePullRequestHook(r.Body) } + log.Debug().Msgf("unsuported hook type: '%s'", hookType) return nil, nil, nil } @@ -104,11 +108,14 @@ func parsePullRequestHook(payload io.Reader) (*model.Repo, *model.Pipeline, erro return nil, nil, err } - // Don't trigger pipelines for non-code changes, or if PR is not open + // Don't trigger pipelines for non-code changes ... if pr.Action != actionOpen && pr.Action != actionSync { + log.Debug().Msgf("pull_request action is '%s' and no open or sync", pr.Action) return nil, nil, nil } + // ... or if PR is not open if pr.PullRequest.State != stateOpen { + log.Debug().Msg("pull_request is closed") return nil, nil, nil }