2022-10-18 01:24:12 +00:00
|
|
|
// 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 (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2021-08-31 01:00:29 +00:00
|
|
|
"strings"
|
2017-05-01 10:33:06 +00:00
|
|
|
|
2022-12-25 20:50:57 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2023-11-07 07:04:33 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/server/forge/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/server/model"
|
2017-05-01 10:33:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-05-24 02:14:47 +00:00
|
|
|
hookEvent = "X-Gitea-Event"
|
2017-05-01 10:33:06 +00:00
|
|
|
hookPush = "push"
|
|
|
|
hookCreated = "create"
|
|
|
|
hookPullRequest = "pull_request"
|
|
|
|
|
|
|
|
actionOpen = "opened"
|
2018-02-20 17:53:03 +00:00
|
|
|
actionSync = "synchronized"
|
2017-05-01 10:33:06 +00:00
|
|
|
|
|
|
|
stateOpen = "open"
|
|
|
|
|
|
|
|
refBranch = "branch"
|
|
|
|
refTag = "tag"
|
|
|
|
)
|
|
|
|
|
2017-05-03 02:06:11 +00:00
|
|
|
// parseHook parses a Gitea hook from an http.Request request and returns
|
2022-10-18 01:24:12 +00:00
|
|
|
// Repo and Pipeline detail. If a hook type is unsupported nil values are returned.
|
|
|
|
func parseHook(r *http.Request) (*model.Repo, *model.Pipeline, error) {
|
2022-12-25 20:50:57 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-12-25 20:50:57 +00:00
|
|
|
log.Debug().Msgf("unsuported hook type: '%s'", hookType)
|
2023-07-14 00:03:54 +00:00
|
|
|
return nil, nil, &types.ErrIgnoreEvent{Event: hookType}
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +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.
|
2022-10-18 01:24:12 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-31 01:00:29 +00:00
|
|
|
// ignore push events for tags
|
|
|
|
if strings.HasPrefix(push.Ref, "refs/tags/") {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:02:49 +00:00
|
|
|
// TODO is this even needed?
|
2017-05-01 10:33:06 +00:00
|
|
|
if push.RefType == refBranch {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2022-09-07 17:16:40 +00:00
|
|
|
repo = toRepo(push.Repo)
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline = pipelineFromPush(push)
|
|
|
|
return repo, pipeline, err
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +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.
|
2022-10-18 01:24:12 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-07 17:16:40 +00:00
|
|
|
repo = toRepo(push.Repo)
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline = pipelineFromTag(push)
|
|
|
|
return repo, pipeline, nil
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +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 (
|
2022-10-18 01:24:12 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-25 20:50:57 +00:00
|
|
|
// Don't trigger pipelines for non-code changes ...
|
2017-05-01 10:33:06 +00:00
|
|
|
if pr.Action != actionOpen && pr.Action != actionSync {
|
2022-12-25 20:50:57 +00:00
|
|
|
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
|
|
|
|
}
|
2022-12-25 20:50:57 +00:00
|
|
|
// ... or if PR is not open
|
2017-05-01 10:33:06 +00:00
|
|
|
if pr.PullRequest.State != stateOpen {
|
2022-12-25 20:50:57 +00:00
|
|
|
log.Debug().Msg("pull_request is closed")
|
2017-05-01 10:33:06 +00:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2022-09-07 17:16:40 +00:00
|
|
|
repo = toRepo(pr.Repo)
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline = pipelineFromPullRequest(pr)
|
|
|
|
return repo, pipeline, err
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|