woodpecker/server/forge/github/parse.go
renovate[bot] 50ddc61576
Update module github.com/google/go-github/v60 to v61 (#3595)
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[github.com/google/go-github/v60](https://togithub.com/google/go-github)
| `v60.0.0` -> `v61.0.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fgoogle%2fgo-github%2fv60/v61.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fgoogle%2fgo-github%2fv60/v61.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fgoogle%2fgo-github%2fv60/v60.0.0/v61.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fgoogle%2fgo-github%2fv60/v60.0.0/v61.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>google/go-github (github.com/google/go-github/v60)</summary>

###
[`v61.0.0`](https://togithub.com/google/go-github/releases/tag/v61.0.0)

[Compare
Source](https://togithub.com/google/go-github/compare/v60.0.0...v61.0.0)

This release contains the following breaking API changes:

- feat!: Update deprecated endpoints in github/action_variables.go
([#&#8203;3104](https://togithub.com/google/go-github/issues/3104))

...and the following additional changes:

- Bump go-github from v59 to v60 in /scrape
([#&#8203;3087](https://togithub.com/google/go-github/issues/3087))
- Allow querying rule set information by ID with information returned
from GetRulesFromBranch
([#&#8203;3089](https://togithub.com/google/go-github/issues/3089))
- Bump codecov/codecov-action from 4.0.2 to 4.1.0
([#&#8203;3091](https://togithub.com/google/go-github/issues/3091))
- Bump github.com/PuerkitoBio/goquery from 1.9.0 to 1.9.1 in /scrape
([#&#8203;3092](https://togithub.com/google/go-github/issues/3092))
- Add Protection to Branch struct
([#&#8203;3095](https://togithub.com/google/go-github/issues/3095))
- Bump github.com/alecthomas/kong from 0.8.1 to 0.9.0 in /tools
([#&#8203;3097](https://togithub.com/google/go-github/issues/3097))
- Bump golang.org/x/net from 0.21.0 to 0.22.0 in /scrape
([#&#8203;3096](https://togithub.com/google/go-github/issues/3096))
- Bump google.golang.org/protobuf from 1.28.0 to 1.33.0 in /example
([#&#8203;3099](https://togithub.com/google/go-github/issues/3099))
- Add audit log rate limit category and make rate limit category getter
public
([#&#8203;3088](https://togithub.com/google/go-github/issues/3088))
- Update README.md
([#&#8203;3110](https://togithub.com/google/go-github/issues/3110))
- Allow Installation of Custom Properties Permissions
([#&#8203;3108](https://togithub.com/google/go-github/issues/3108))
- Add NotificationSetting to NewTeam
([#&#8203;3111](https://togithub.com/google/go-github/issues/3111))
- Fix pagination for ListCopilotSeats
([#&#8203;3112](https://togithub.com/google/go-github/issues/3112))
- Add .\*.local to .gitignore
([#&#8203;3115](https://togithub.com/google/go-github/issues/3115))
- Add CreateOrUpdateRepoCustomPropertyValues
([#&#8203;3109](https://togithub.com/google/go-github/issues/3109))
- Bump version of go-github to v61.0.0
([#&#8203;3118](https://togithub.com/google/go-github/issues/3118))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "every weekend" (UTC), Automerge -
"before 4am" (UTC).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/woodpecker-ci/woodpecker).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yNjkuMiIsInVwZGF0ZWRJblZlciI6IjM3LjI2OS4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-04-06 08:00:59 +02:00

219 lines
7.4 KiB
Go

// Copyright 2022 Woodpecker Authors
// Copyright 2018 Drone.IO Inc.
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
package github
import (
"bytes"
"fmt"
"io"
"net/http"
"strings"
"github.com/google/go-github/v61/github"
"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"
)
const (
hookField = "payload"
actionOpen = "opened"
actionClose = "closed"
actionSync = "synchronize"
actionReleased = "released"
stateOpen = "open"
stateClose = "closed"
)
// parseHook parses a GitHub 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, merge bool) (*github.PullRequest, *model.Repo, *model.Pipeline, error) {
var reader io.Reader = r.Body
if payload := r.FormValue(hookField); payload != "" {
reader = bytes.NewBufferString(payload)
}
raw, err := io.ReadAll(reader)
if err != nil {
return nil, nil, nil, err
}
payload, err := github.ParseWebHook(github.WebHookType(r), raw)
if err != nil {
return nil, nil, nil, err
}
switch hook := payload.(type) {
case *github.PushEvent:
repo, pipeline := parsePushHook(hook)
return nil, repo, pipeline, nil
case *github.DeploymentEvent:
repo, pipeline := parseDeployHook(hook)
return nil, repo, pipeline, nil
case *github.PullRequestEvent:
return parsePullHook(hook, merge)
case *github.ReleaseEvent:
repo, pipeline := parseReleaseHook(hook)
return nil, repo, pipeline, nil
default:
return nil, nil, nil, &types.ErrIgnoreEvent{Event: github.Stringify(hook)}
}
}
// parsePushHook parses a push hook and returns the Repo and Pipeline details.
// If the commit type is unsupported nil values are returned.
func parsePushHook(hook *github.PushEvent) (*model.Repo, *model.Pipeline) {
if hook.Deleted != nil && *hook.Deleted {
return nil, nil
}
pipeline := &model.Pipeline{
Event: model.EventPush,
Commit: hook.GetHeadCommit().GetID(),
Ref: hook.GetRef(),
ForgeURL: hook.GetHeadCommit().GetURL(),
Branch: strings.ReplaceAll(hook.GetRef(), "refs/heads/", ""),
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),
}
if len(pipeline.Author) == 0 {
pipeline.Author = hook.GetHeadCommit().GetAuthor().GetLogin()
}
if strings.HasPrefix(pipeline.Ref, "refs/tags/") {
// just kidding, this is actually a tag event. Why did this come as a push
// event we'll never know!
pipeline.Event = model.EventTag
pipeline.ChangedFiles = nil
// For tags, if the base_ref (tag's base branch) is set, we're using it
// as pipeline's branch so that we can filter events base on it
if strings.HasPrefix(hook.GetBaseRef(), "refs/heads/") {
pipeline.Branch = strings.ReplaceAll(hook.GetBaseRef(), "refs/heads/", "")
}
}
return convertRepoHook(hook.GetRepo()), pipeline
}
// parseDeployHook parses a deployment and returns the Repo and Pipeline details.
// If the commit type is unsupported nil values are returned.
func parseDeployHook(hook *github.DeploymentEvent) (*model.Repo, *model.Pipeline) {
pipeline := &model.Pipeline{
Event: model.EventDeploy,
Commit: hook.GetDeployment().GetSHA(),
ForgeURL: hook.GetDeployment().GetURL(),
Message: hook.GetDeployment().GetDescription(),
Ref: hook.GetDeployment().GetRef(),
Branch: hook.GetDeployment().GetRef(),
Deploy: hook.GetDeployment().GetEnvironment(),
Avatar: hook.GetSender().GetAvatarURL(),
Author: hook.GetSender().GetLogin(),
Sender: hook.GetSender().GetLogin(),
}
// if the ref is a sha or short sha we need to manually construct the ref.
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)
}
// if the ref is a branch we should make sure it has refs/heads prefix
if !strings.HasPrefix(pipeline.Ref, "refs/") { // branch or tag
pipeline.Ref = fmt.Sprintf("refs/heads/%s", pipeline.Branch)
}
return convertRepo(hook.GetRepo()), pipeline
}
// parsePullHook parses a pull request hook and returns the Repo and Pipeline
// details.
func parsePullHook(hook *github.PullRequestEvent, merge bool) (*github.PullRequest, *model.Repo, *model.Pipeline, error) {
if hook.GetAction() != actionOpen && hook.GetAction() != actionSync && hook.GetAction() != actionClose {
return nil, nil, nil, nil
}
event := model.EventPull
if hook.GetPullRequest().GetState() == stateClose {
event = model.EventPullClosed
}
pipeline := &model.Pipeline{
Event: event,
Commit: hook.GetPullRequest().GetHead().GetSHA(),
ForgeURL: hook.GetPullRequest().GetHTMLURL(),
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(),
Refspec: fmt.Sprintf(refSpec,
hook.GetPullRequest().GetHead().GetRef(),
hook.GetPullRequest().GetBase().GetRef(),
),
PullRequestLabels: convertLabels(hook.GetPullRequest().Labels),
}
if merge {
pipeline.Ref = fmt.Sprintf(mergeRefs, hook.GetPullRequest().GetNumber())
}
return hook.GetPullRequest(), convertRepo(hook.GetRepo()), pipeline, nil
}
// 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()),
Branch: hook.GetRelease().GetTargetCommitish(),
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
}
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...)
}
return utils.DedupStrings(files)
}