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.
|
|
|
|
|
2016-11-25 12:06:07 +00:00
|
|
|
package gogs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2016-11-25 12:06:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-01-03 08:38:05 +00:00
|
|
|
hookEvent = "X-Gogs-Event"
|
|
|
|
hookPush = "push"
|
|
|
|
hookCreated = "create"
|
|
|
|
hookPullRequest = "pull_request"
|
|
|
|
|
|
|
|
actionOpen = "opened"
|
2017-10-12 10:32:17 +00:00
|
|
|
actionSync = "synchronized"
|
2017-01-03 08:38:05 +00:00
|
|
|
|
|
|
|
stateOpen = "open"
|
2016-11-25 12:06:07 +00:00
|
|
|
|
|
|
|
refBranch = "branch"
|
|
|
|
refTag = "tag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// parseHook parses a Bitbucket 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, privateMode bool) (*model.Repo, *model.Pipeline, error) {
|
2016-11-25 12:06:07 +00:00
|
|
|
switch r.Header.Get(hookEvent) {
|
|
|
|
case hookPush:
|
2022-09-07 17:16:40 +00:00
|
|
|
return parsePushHook(r.Body, privateMode)
|
2016-11-25 12:06:07 +00:00
|
|
|
case hookCreated:
|
2022-09-07 17:16:40 +00:00
|
|
|
return parseCreatedHook(r.Body, privateMode)
|
2017-01-03 08:38:05 +00:00
|
|
|
case hookPullRequest:
|
2022-09-07 17:16:40 +00:00
|
|
|
return parsePullRequestHook(r.Body, privateMode)
|
2016-11-25 12:06:07 +00:00
|
|
|
}
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// parsePushHook parses a push hook and returns the Repo and Pipeline details.
|
2016-11-25 12:06:07 +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, privateMode bool) (*model.Repo, *model.Pipeline, error) {
|
2016-11-25 12:06:07 +00:00
|
|
|
var (
|
2022-10-18 01:24:12 +00:00
|
|
|
repo *model.Repo
|
|
|
|
pipeline *model.Pipeline
|
2016-11-25 12:06:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
push, err := parsePush(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// is this even needed?
|
|
|
|
if push.RefType == refBranch {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2022-09-07 17:16:40 +00:00
|
|
|
repo = toRepo(push.Repo, privateMode)
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline = pipelineFromPush(push)
|
|
|
|
return repo, pipeline, err
|
2016-11-25 12:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// parseCreatedHook parses a push hook and returns the Repo and Pipeline details.
|
2016-11-25 12:06:07 +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, privateMode bool) (*model.Repo, *model.Pipeline, error) {
|
2016-11-25 12:06:07 +00:00
|
|
|
var (
|
2022-10-18 01:24:12 +00:00
|
|
|
repo *model.Repo
|
|
|
|
pipeline *model.Pipeline
|
2016-11-25 12:06:07 +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, privateMode)
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline = pipelineFromTag(push)
|
|
|
|
return repo, pipeline, err
|
2016-11-25 12:06:07 +00:00
|
|
|
}
|
2017-01-03 08:38:05 +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, privateMode bool) (*model.Repo, *model.Pipeline, error) {
|
2017-01-03 08:38:05 +00:00
|
|
|
var (
|
2022-10-18 01:24:12 +00:00
|
|
|
repo *model.Repo
|
|
|
|
pipeline *model.Pipeline
|
2017-01-03 08:38:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
pr, err := parsePullRequest(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// Don't trigger pipelines for non-code changes, or if PR is not open
|
2017-01-03 08:38:05 +00:00
|
|
|
if pr.Action != actionOpen && pr.Action != actionSync {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
if pr.PullRequest.State != stateOpen {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2022-09-07 17:16:40 +00:00
|
|
|
repo = toRepo(pr.Repo, privateMode)
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline = pipelineFromPullRequest(pr)
|
|
|
|
return repo, pipeline, err
|
2017-01-03 08:38:05 +00:00
|
|
|
}
|