Skip to content

Commit

Permalink
Dont panic on hook parsing (woodpecker-ci#1501)
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 authored Dec 25, 2022
1 parent 5214813 commit af2c278
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions server/forge/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 9 additions & 2 deletions server/forge/gitea/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"net/http"
"strings"

"github.com/rs/zerolog/log"

"github.com/woodpecker-ci/woodpecker/server/model"
)

Expand All @@ -41,14 +43,16 @@ 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:
return parseCreatedHook(r.Body)
case hookPullRequest:
return parsePullRequestHook(r.Body)
}
log.Debug().Msgf("unsuported hook type: '%s'", hookType)
return nil, nil, nil
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit af2c278

Please sign in to comment.