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-05-03 20:01:16 +00:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-01-17 22:46:59 +00:00
|
|
|
"fmt"
|
2016-05-03 20:01:16 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2022-01-17 22:46:59 +00:00
|
|
|
"strings"
|
|
|
|
|
2024-07-14 05:58:35 +00:00
|
|
|
"github.com/google/go-github/v63/github"
|
2016-05-03 20:01:16 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/utils"
|
2016-05-03 20:01:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-01-17 22:46:59 +00:00
|
|
|
hookField = "payload"
|
2016-05-03 20:01:16 +00:00
|
|
|
|
2024-01-30 16:39:00 +00:00
|
|
|
actionOpen = "opened"
|
|
|
|
actionClose = "closed"
|
|
|
|
actionSync = "synchronize"
|
|
|
|
actionReleased = "released"
|
2016-05-03 20:01:16 +00:00
|
|
|
|
2023-12-26 18:22:52 +00:00
|
|
|
stateOpen = "open"
|
|
|
|
stateClose = "closed"
|
2016-05-03 20:01:16 +00:00
|
|
|
)
|
|
|
|
|
2021-09-21 15:35:32 +00:00
|
|
|
// parseHook parses a GitHub 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, merge bool) (*github.PullRequest, *model.Repo, *model.Pipeline, error) {
|
2016-05-03 20:01:16 +00:00
|
|
|
var reader io.Reader = r.Body
|
|
|
|
|
|
|
|
if payload := r.FormValue(hookField); payload != "" {
|
|
|
|
reader = bytes.NewBufferString(payload)
|
|
|
|
}
|
|
|
|
|
2022-08-29 23:14:07 +00:00
|
|
|
raw, err := io.ReadAll(reader)
|
2016-05-03 20:01:16 +00:00
|
|
|
if err != nil {
|
2022-01-17 22:46:59 +00:00
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := github.ParseWebHook(github.WebHookType(r), raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 22:46:59 +00:00
|
|
|
switch hook := payload.(type) {
|
|
|
|
case *github.PushEvent:
|
2024-01-09 20:35:37 +00:00
|
|
|
repo, pipeline := parsePushHook(hook)
|
|
|
|
return nil, repo, pipeline, nil
|
2022-01-17 22:46:59 +00:00
|
|
|
case *github.DeploymentEvent:
|
2024-01-09 20:35:37 +00:00
|
|
|
repo, pipeline := parseDeployHook(hook)
|
|
|
|
return nil, repo, pipeline, nil
|
2022-01-17 22:46:59 +00:00
|
|
|
case *github.PullRequestEvent:
|
2022-01-31 14:38:00 +00:00
|
|
|
return parsePullHook(hook, merge)
|
2024-01-30 16:39:00 +00:00
|
|
|
case *github.ReleaseEvent:
|
|
|
|
repo, pipeline := parseReleaseHook(hook)
|
|
|
|
return nil, repo, pipeline, nil
|
2023-07-14 00:03:54 +00:00
|
|
|
default:
|
|
|
|
return nil, nil, nil, &types.ErrIgnoreEvent{Event: github.Stringify(hook)}
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// parsePushHook parses a push hook and returns the Repo and Pipeline details.
|
2016-05-03 20:01:16 +00:00
|
|
|
// If the commit type is unsupported nil values are returned.
|
2024-01-09 20:35:37 +00:00
|
|
|
func parsePushHook(hook *github.PushEvent) (*model.Repo, *model.Pipeline) {
|
2022-01-17 22:46:59 +00:00
|
|
|
if hook.Deleted != nil && *hook.Deleted {
|
2024-01-09 20:35:37 +00:00
|
|
|
return nil, nil
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
2022-01-17 22:46:59 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := &model.Pipeline{
|
2022-01-17 22:46:59 +00:00
|
|
|
Event: model.EventPush,
|
|
|
|
Commit: hook.GetHeadCommit().GetID(),
|
|
|
|
Ref: hook.GetRef(),
|
2023-11-14 16:12:12 +00:00
|
|
|
ForgeURL: hook.GetHeadCommit().GetURL(),
|
2024-01-10 14:34:44 +00:00
|
|
|
Branch: strings.ReplaceAll(hook.GetRef(), "refs/heads/", ""),
|
2022-01-17 22:46:59 +00:00
|
|
|
Message: hook.GetHeadCommit().GetMessage(),
|
|
|
|
Email: hook.GetHeadCommit().GetAuthor().GetEmail(),
|
|
|
|
Avatar: hook.GetSender().GetAvatarURL(),
|
|
|
|
Author: hook.GetSender().GetLogin(),
|
|
|
|
Sender: hook.GetSender().GetLogin(),
|
|
|
|
ChangedFiles: getChangedFilesFromCommits(hook.Commits),
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
if len(pipeline.Author) == 0 {
|
|
|
|
pipeline.Author = hook.GetHeadCommit().GetAuthor().GetLogin()
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if strings.HasPrefix(pipeline.Ref, "refs/tags/") {
|
2022-01-17 22:46:59 +00:00
|
|
|
// just kidding, this is actually a tag event. Why did this come as a push
|
|
|
|
// event we'll never know!
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Event = model.EventTag
|
|
|
|
pipeline.ChangedFiles = nil
|
2022-01-17 22:46:59 +00:00
|
|
|
// For tags, if the base_ref (tag's base branch) is set, we're using it
|
2022-10-18 01:24:12 +00:00
|
|
|
// as pipeline's branch so that we can filter events base on it
|
2022-01-17 22:46:59 +00:00
|
|
|
if strings.HasPrefix(hook.GetBaseRef(), "refs/heads/") {
|
2024-01-10 14:34:44 +00:00
|
|
|
pipeline.Branch = strings.ReplaceAll(hook.GetBaseRef(), "refs/heads/", "")
|
2022-01-17 22:46:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 20:35:37 +00:00
|
|
|
return convertRepoHook(hook.GetRepo()), pipeline
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// parseDeployHook parses a deployment and returns the Repo and Pipeline details.
|
2016-05-03 20:01:16 +00:00
|
|
|
// If the commit type is unsupported nil values are returned.
|
2024-01-09 20:35:37 +00:00
|
|
|
func parseDeployHook(hook *github.DeploymentEvent) (*model.Repo, *model.Pipeline) {
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := &model.Pipeline{
|
2024-05-02 16:56:19 +00:00
|
|
|
Event: model.EventDeploy,
|
|
|
|
Commit: hook.GetDeployment().GetSHA(),
|
|
|
|
ForgeURL: hook.GetDeployment().GetURL(),
|
|
|
|
Message: hook.GetDeployment().GetDescription(),
|
|
|
|
Ref: hook.GetDeployment().GetRef(),
|
|
|
|
Branch: hook.GetDeployment().GetRef(),
|
|
|
|
Avatar: hook.GetSender().GetAvatarURL(),
|
|
|
|
Author: hook.GetSender().GetLogin(),
|
|
|
|
Sender: hook.GetSender().GetLogin(),
|
2024-07-07 11:43:07 +00:00
|
|
|
DeployTo: hook.GetDeployment().GetEnvironment(),
|
2024-05-02 16:56:19 +00:00
|
|
|
DeployTask: hook.GetDeployment().GetTask(),
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
2022-01-17 22:46:59 +00:00
|
|
|
// if the ref is a sha or short sha we need to manually construct the ref.
|
2022-10-18 01:24:12 +00:00
|
|
|
if strings.HasPrefix(pipeline.Commit, pipeline.Ref) || pipeline.Commit == pipeline.Ref {
|
|
|
|
pipeline.Branch = hook.GetRepo().GetDefaultBranch()
|
|
|
|
pipeline.Ref = fmt.Sprintf("refs/heads/%s", pipeline.Branch)
|
2022-01-17 22:46:59 +00:00
|
|
|
}
|
|
|
|
// if the ref is a branch we should make sure it has refs/heads prefix
|
2022-10-18 01:24:12 +00:00
|
|
|
if !strings.HasPrefix(pipeline.Ref, "refs/") { // branch or tag
|
|
|
|
pipeline.Ref = fmt.Sprintf("refs/heads/%s", pipeline.Branch)
|
2022-01-17 22:46:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 20:35:37 +00:00
|
|
|
return convertRepo(hook.GetRepo()), pipeline
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// parsePullHook parses a pull request hook and returns the Repo and Pipeline
|
2023-12-26 18:22:52 +00:00
|
|
|
// details.
|
2022-10-18 01:24:12 +00:00
|
|
|
func parsePullHook(hook *github.PullRequestEvent, merge bool) (*github.PullRequest, *model.Repo, *model.Pipeline, error) {
|
2023-12-26 18:22:52 +00:00
|
|
|
if hook.GetAction() != actionOpen && hook.GetAction() != actionSync && hook.GetAction() != actionClose {
|
2022-01-17 22:46:59 +00:00
|
|
|
return nil, nil, nil, nil
|
|
|
|
}
|
2023-12-26 18:22:52 +00:00
|
|
|
|
|
|
|
event := model.EventPull
|
|
|
|
if hook.GetPullRequest().GetState() == stateClose {
|
|
|
|
event = model.EventPullClosed
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := &model.Pipeline{
|
2023-12-26 18:22:52 +00:00
|
|
|
Event: event,
|
2022-11-04 23:35:06 +00:00
|
|
|
Commit: hook.GetPullRequest().GetHead().GetSHA(),
|
2023-11-14 16:12:12 +00:00
|
|
|
ForgeURL: hook.GetPullRequest().GetHTMLURL(),
|
2022-11-04 23:35:06 +00:00
|
|
|
Ref: fmt.Sprintf(headRefs, hook.GetPullRequest().GetNumber()),
|
|
|
|
Branch: hook.GetPullRequest().GetBase().GetRef(),
|
|
|
|
Message: hook.GetPullRequest().GetTitle(),
|
|
|
|
Author: hook.GetPullRequest().GetUser().GetLogin(),
|
|
|
|
Avatar: hook.GetPullRequest().GetUser().GetAvatarURL(),
|
|
|
|
Title: hook.GetPullRequest().GetTitle(),
|
|
|
|
Sender: hook.GetSender().GetLogin(),
|
2022-01-17 22:46:59 +00:00
|
|
|
Refspec: fmt.Sprintf(refSpec,
|
|
|
|
hook.GetPullRequest().GetHead().GetRef(),
|
|
|
|
hook.GetPullRequest().GetBase().GetRef(),
|
|
|
|
),
|
2023-03-17 02:43:04 +00:00
|
|
|
PullRequestLabels: convertLabels(hook.GetPullRequest().Labels),
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
2022-01-17 22:46:59 +00:00
|
|
|
if merge {
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Ref = fmt.Sprintf(mergeRefs, hook.GetPullRequest().GetNumber())
|
2022-01-17 22:46:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return hook.GetPullRequest(), convertRepo(hook.GetRepo()), pipeline, nil
|
2022-01-17 22:46:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 16:39:00 +00:00
|
|
|
// parseReleaseHook parses a release hook and returns the Repo and Pipeline
|
|
|
|
// details.
|
|
|
|
func parseReleaseHook(hook *github.ReleaseEvent) (*model.Repo, *model.Pipeline) {
|
|
|
|
if hook.GetAction() != actionReleased {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
name := hook.GetRelease().GetName()
|
|
|
|
if name == "" {
|
|
|
|
name = hook.GetRelease().GetTagName()
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeline := &model.Pipeline{
|
|
|
|
Event: model.EventRelease,
|
|
|
|
ForgeURL: hook.GetRelease().GetHTMLURL(),
|
|
|
|
Ref: fmt.Sprintf("refs/tags/%s", hook.GetRelease().GetTagName()),
|
2024-06-04 06:30:54 +00:00
|
|
|
Branch: hook.GetRelease().GetTargetCommitish(), // cspell:disable-line
|
2024-01-30 16:39:00 +00:00
|
|
|
Message: fmt.Sprintf("created release %s", name),
|
|
|
|
Author: hook.GetRelease().GetAuthor().GetLogin(),
|
|
|
|
Avatar: hook.GetRelease().GetAuthor().GetAvatarURL(),
|
|
|
|
Sender: hook.GetSender().GetLogin(),
|
|
|
|
IsPrerelease: hook.GetRelease().GetPrerelease(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return convertRepo(hook.GetRepo()), pipeline
|
|
|
|
}
|
|
|
|
|
2022-01-17 22:46:59 +00:00
|
|
|
func getChangedFilesFromCommits(commits []*github.HeadCommit) []string {
|
|
|
|
// assume a capacity of 4 changed files per commit
|
|
|
|
files := make([]string, 0, len(commits)*4)
|
|
|
|
for _, cm := range commits {
|
|
|
|
files = append(files, cm.Added...)
|
|
|
|
files = append(files, cm.Removed...)
|
|
|
|
files = append(files, cm.Modified...)
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
2024-04-09 07:04:53 +00:00
|
|
|
return utils.DeduplicateStrings(files)
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|