1
0
mirror of https://codeberg.org/crowci/crow.git synced 2025-08-09 07:42:52 +03:00
Files
crow/server/forge/github/parse.go
crowci-bot 28a66d2e7d chore(deps): update module github.com/google/go-github/v69 to v70 (#115)
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [github.com/google/go-github/v69](https://github.com/google/go-github) | require | major | `v69.2.0` -> `v70.0.0` |

---

### Release Notes

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

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

[Compare Source](https://github.com/google/go-github/compare/v69.2.0...v70.0.0)

This release contains the following breaking API changes:

-   feat!: Add support for network-configurations endpoints for organization ([#&#8203;3511](https://github.com/google/go-github/issues/3511))
    BREAKING CHANGE: `EnterpriseNetwork*` structs have been replaced with `Network*` structs.
-   refactor!: Update package types to align with webhook event ([#&#8203;3515](https://github.com/google/go-github/issues/3515))
    BREAKING CHANGE: `PackageVersion.Body` and `PackageVersion.Metadata` are both now `json.RawMessage`.

...and the following additional changes:

-   build(deps): bump github.com/alecthomas/kong from 1.8.0 to 1.8.1 in /tools ([#&#8203;3485](https://github.com/google/go-github/issues/3485))
-   build(deps): bump github.com/google/go-github/v69 from 69.0.0 to 69.2.0 in /scrape ([#&#8203;3483](https://github.com/google/go-github/issues/3483))
-   build(deps): bump github.com/google/go-cmp from 0.6.0 to 0.7.0 in /tools ([#&#8203;3490](https://github.com/google/go-github/issues/3490))
-   Bump github.com/google/go-cmp from 0.6.0 to 0.7.0 ([#&#8203;3494](https://github.com/google/go-github/issues/3494))
-   build(deps): bump actions/cache from 4.2.0 to 4.2.1 in the actions group ([#&#8203;3492](https://github.com/google/go-github/issues/3492))
-   fix: Resolve '400 Custom domains' error on GitHub Enterprise Server ([#&#8203;3489](https://github.com/google/go-github/issues/3489))
-   Correct the runIDFromURLRE regex to properly match the callbackURL ([#&#8203;3495](https://github.com/google/go-github/issues/3495))
-   feat: Add support for GitHub-hosted runner API endpoints ([#&#8203;3487](https://github.com/google/go-github/issues/3487))
-   feat: Add support for network-configurations endpoints ([#&#8203;3497](https://github.com/google/go-github/issues/3497))
-   build(deps): bump codecov/codecov-action from 5.3.1 to 5.4.0 ([#&#8203;3500](https://github.com/google/go-github/issues/3500))
-   build(deps): bump actions/cache from 4.2.1 to 4.2.2 in the actions group ([#&#8203;3499](https://github.com/google/go-github/issues/3499))
-   Add created_at field to Reaction ([#&#8203;3501](https://github.com/google/go-github/issues/3501))
-   Add reason parameter to MergeGroupEvent ([#&#8203;3508](https://github.com/google/go-github/issues/3508))
-   fix(ci): ensure 'auto' toolchain applies to generate ([#&#8203;3436](https://github.com/google/go-github/issues/3436))
-   Bump dependency versions from dependabot warnings ([#&#8203;3512](https://github.com/google/go-github/issues/3512))
-   Bump go-jose to v4.0.5 ([#&#8203;3513](https://github.com/google/go-github/issues/3513))
-   build(deps): bump golang.org/x/net from 0.33.0 to 0.36.0 in /example ([#&#8203;3514](https://github.com/google/go-github/issues/3514))
-   docs: Update readme and examples for updated go-github-ratelimit and introduce go-github-pagination ([#&#8203;3504](https://github.com/google/go-github/issues/3504))
-   Add validity filter to secret scanning alert list options ([#&#8203;3516](https://github.com/google/go-github/issues/3516))
-   build(deps): bump github.com/alecthomas/kong from 1.8.1 to 1.9.0 in /tools ([#&#8203;3518](https://github.com/google/go-github/issues/3518))
-   build(deps): bump github.com/getkin/kin-openapi from 0.129.0 to 0.130.0 in /tools ([#&#8203;3517](https://github.com/google/go-github/issues/3517))
-   feat: Add automatic_copilot_code_review_enabled parameter to ruleset API ([#&#8203;3506](https://github.com/google/go-github/issues/3506))
-   Bump version of go-github to v70.0.0 ([#&#8203;3520](https://github.com/google/go-github/issues/3520))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "* * * * 0,6" (UTC), Automerge - "* 0-3 * * *" (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 [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4yMTEuMCIsInVwZGF0ZWRJblZlciI6IjM5LjIxMS4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJkZXBlbmRlbmN5IPCfk6bvuI8iXX0=-->

Reviewed-on: https://codeberg.org/crowci/crow/pulls/115
Co-authored-by: crowci-bot <admin@crowci.dev>
Co-committed-by: crowci-bot <admin@crowci.dev>
2025-03-27 18:06:43 +00:00

224 lines
7.6 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/v70/github"
"codeberg.org/crowci/crow/v3/server/forge/types"
"codeberg.org/crowci/crow/v3/server/model"
"codeberg.org/crowci/crow/v3/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(),
Avatar: hook.GetSender().GetAvatarURL(),
Author: hook.GetSender().GetLogin(),
Sender: hook.GetSender().GetLogin(),
DeployTo: hook.GetDeployment().GetEnvironment(),
DeployTask: hook.GetDeployment().GetTask(),
}
// 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
}
fromFork := hook.GetPullRequest().GetHead().GetRepo().GetID() != hook.GetPullRequest().GetBase().GetRepo().GetID()
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),
FromFork: fromFork,
}
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(), // cspell:disable-line
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.DeduplicateStrings(files)
}