woodpecker/server/forge/gitea/parse.go

147 lines
4 KiB
Go
Raw Permalink Normal View History

// Copyright 2022 Woodpecker Authors
2018-02-19 22:24:10 +00:00
// Copyright 2018 Drone.IO Inc.
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2017-05-01 10:33:06 +00:00
package gitea
import (
"fmt"
2017-05-01 10:33:06 +00:00
"io"
"net/http"
"strings"
2017-05-01 10:33:06 +00:00
"github.com/rs/zerolog/log"
"go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
2017-05-01 10:33:06 +00:00
)
const (
hookEvent = "X-Gitea-Event"
2017-05-01 10:33:06 +00:00
hookPush = "push"
hookCreated = "create"
hookPullRequest = "pull_request"
hookRelease = "release"
2017-05-01 10:33:06 +00:00
actionOpen = "opened"
actionSync = "synchronized"
actionClose = "closed"
2017-05-01 10:33:06 +00:00
refBranch = "branch"
refTag = "tag"
)
// parseHook parses a Gitea hook from an http.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) {
hookType := r.Header.Get(hookEvent)
switch hookType {
2017-05-01 10:33:06 +00:00
case hookPush:
return parsePushHook(r.Body)
case hookCreated:
return parseCreatedHook(r.Body)
case hookPullRequest:
return parsePullRequestHook(r.Body)
case hookRelease:
return parseReleaseHook(r.Body)
2017-05-01 10:33:06 +00:00
}
log.Debug().Msgf("unsupported hook type: '%s'", hookType)
return nil, nil, &types.ErrIgnoreEvent{Event: hookType}
2017-05-01 10:33:06 +00:00
}
// parsePushHook parses a push hook and returns the Repo and Pipeline details.
2017-05-01 10:33:06 +00:00
// If the commit type is unsupported nil values are returned.
func parsePushHook(payload io.Reader) (repo *model.Repo, pipeline *model.Pipeline, err error) {
2017-05-01 10:33:06 +00:00
push, err := parsePush(payload)
if err != nil {
return nil, nil, err
}
// ignore push events for tags
if strings.HasPrefix(push.Ref, "refs/tags/") {
return nil, nil, nil
}
// TODO: is this even needed?
2017-05-01 10:33:06 +00:00
if push.RefType == refBranch {
return nil, nil, nil
}
repo = toRepo(push.Repo)
pipeline = pipelineFromPush(push)
return repo, pipeline, err
2017-05-01 10:33:06 +00:00
}
// parseCreatedHook parses a push hook and returns the Repo and Pipeline details.
2017-05-01 10:33:06 +00:00
// If the commit type is unsupported nil values are returned.
func parseCreatedHook(payload io.Reader) (repo *model.Repo, pipeline *model.Pipeline, err error) {
2017-05-01 10:33:06 +00:00
push, err := parsePush(payload)
if err != nil {
return nil, nil, err
}
if push.RefType != refTag {
return nil, nil, nil
}
repo = toRepo(push.Repo)
pipeline = pipelineFromTag(push)
return repo, pipeline, nil
2017-05-01 10:33:06 +00:00
}
// parsePullRequestHook parses a pull_request hook and returns the Repo and Pipeline details.
func parsePullRequestHook(payload io.Reader) (*model.Repo, *model.Pipeline, error) {
2017-05-01 10:33:06 +00:00
var (
repo *model.Repo
pipeline *model.Pipeline
2017-05-01 10:33:06 +00:00
)
pr, err := parsePullRequest(payload)
if err != nil {
return nil, nil, err
}
if pr.PullRequest == nil {
// this should never have happened but it did - so we check
return nil, nil, fmt.Errorf("parsed pull_request webhook does not contain pull_request info")
}
// Don't trigger pipelines for non-code changes ...
if pr.Action != actionOpen && pr.Action != actionSync && pr.Action != actionClose {
log.Debug().Msgf("pull_request action is '%s' and no open or sync", pr.Action)
2017-05-01 10:33:06 +00:00
return nil, nil, nil
}
repo = toRepo(pr.Repo)
pipeline = pipelineFromPullRequest(pr)
return repo, pipeline, err
2017-05-01 10:33:06 +00:00
}
// parseReleaseHook parses a release hook and returns the Repo and Pipeline details.
func parseReleaseHook(payload io.Reader) (*model.Repo, *model.Pipeline, error) {
var (
repo *model.Repo
pipeline *model.Pipeline
)
release, err := parseRelease(payload)
if err != nil {
return nil, nil, err
}
repo = toRepo(release.Repo)
pipeline = pipelineFromRelease(release)
return repo, pipeline, err
}