1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-11-30 08:06:52 +02:00

Dont panic on hook parsing (#1501)

close #1422
This commit is contained in:
6543 2022-12-25 21:50:57 +01:00 committed by GitHub
parent 5214813a23
commit af2c278723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -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 {

View File

@ -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
}