1
0
mirror of https://codeberg.org/crowci/crow.git synced 2025-08-09 07:42:52 +03:00
Files
crow/server/forge/github/convert_test.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

279 lines
9.9 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 (
"testing"
"github.com/google/go-github/v70/github"
"github.com/stretchr/testify/assert"
"codeberg.org/crowci/crow/v3/server/model"
)
func Test_convertStatus(t *testing.T) {
assert.Equal(t, statusSuccess, convertStatus(model.StatusSuccess))
assert.Equal(t, statusPending, convertStatus(model.StatusPending))
assert.Equal(t, statusPending, convertStatus(model.StatusRunning))
assert.Equal(t, statusFailure, convertStatus(model.StatusFailure))
assert.Equal(t, statusError, convertStatus(model.StatusKilled))
assert.Equal(t, statusError, convertStatus(model.StatusError))
}
func Test_convertDesc(t *testing.T) {
assert.Equal(t, descSuccess, convertDesc(model.StatusSuccess))
assert.Equal(t, descPending, convertDesc(model.StatusPending))
assert.Equal(t, descPending, convertDesc(model.StatusRunning))
assert.Equal(t, descFailure, convertDesc(model.StatusFailure))
assert.Equal(t, descError, convertDesc(model.StatusKilled))
assert.Equal(t, descError, convertDesc(model.StatusError))
}
func Test_convertRepoList(t *testing.T) {
from := []*github.Repository{
{
Private: github.Ptr(false),
FullName: github.Ptr("octocat/hello-world"),
Name: github.Ptr("hello-world"),
Owner: &github.User{
AvatarURL: github.Ptr("http://..."),
Login: github.Ptr("octocat"),
},
HTMLURL: github.Ptr("https://github.com/octocat/hello-world"),
CloneURL: github.Ptr("https://github.com/octocat/hello-world.git"),
Permissions: map[string]bool{
"push": true,
"pull": true,
"admin": true,
},
},
}
to := convertRepoList(from)
assert.Equal(t, "http://...", to[0].Avatar)
assert.Equal(t, "octocat/hello-world", to[0].FullName)
assert.Equal(t, "octocat", to[0].Owner)
assert.Equal(t, "hello-world", to[0].Name)
}
func Test_convertRepo(t *testing.T) {
from := github.Repository{
FullName: github.Ptr("octocat/hello-world"),
Name: github.Ptr("hello-world"),
HTMLURL: github.Ptr("https://github.com/octocat/hello-world"),
CloneURL: github.Ptr("https://github.com/octocat/hello-world.git"),
DefaultBranch: github.Ptr("develop"),
Private: github.Ptr(true),
Owner: &github.User{
AvatarURL: github.Ptr("http://..."),
Login: github.Ptr("octocat"),
},
Permissions: map[string]bool{
"push": true,
"pull": true,
"admin": true,
},
}
to := convertRepo(&from)
assert.Equal(t, "http://...", to.Avatar)
assert.Equal(t, "octocat/hello-world", to.FullName)
assert.Equal(t, "octocat", to.Owner)
assert.Equal(t, "hello-world", to.Name)
assert.Equal(t, "develop", to.Branch)
assert.True(t, to.IsSCMPrivate)
assert.Equal(t, "https://github.com/octocat/hello-world.git", to.Clone)
assert.Equal(t, "https://github.com/octocat/hello-world", to.ForgeURL)
}
func Test_convertPerm(t *testing.T) {
from := &github.Repository{
Permissions: map[string]bool{
"admin": true,
"push": true,
"pull": true,
},
}
to := convertPerm(from.GetPermissions())
assert.True(t, to.Push)
assert.True(t, to.Pull)
assert.True(t, to.Admin)
}
func Test_convertTeam(t *testing.T) {
from := &github.Organization{
Login: github.Ptr("octocat"),
AvatarURL: github.Ptr("http://..."),
}
to := convertTeam(from)
assert.Equal(t, "octocat", to.Login)
assert.Equal(t, "http://...", to.Avatar)
}
func Test_convertTeamList(t *testing.T) {
from := []*github.Organization{
{
Login: github.Ptr("octocat"),
AvatarURL: github.Ptr("http://..."),
},
}
to := convertTeamList(from)
assert.Equal(t, "octocat", to[0].Login)
assert.Equal(t, "http://...", to[0].Avatar)
}
func Test_convertRepoHook(t *testing.T) {
t.Run("should convert a repository from webhook", func(t *testing.T) {
from := &github.PushEventRepository{Owner: &github.User{}}
from.Owner.Login = github.Ptr("octocat")
from.Owner.Name = github.Ptr("octocat")
from.Name = github.Ptr("hello-world")
from.FullName = github.Ptr("octocat/hello-world")
from.Private = github.Ptr(true)
from.HTMLURL = github.Ptr("https://github.com/octocat/hello-world")
from.CloneURL = github.Ptr("https://github.com/octocat/hello-world.git")
from.DefaultBranch = github.Ptr("develop")
repo := convertRepoHook(from)
assert.Equal(t, *from.Owner.Login, repo.Owner)
assert.Equal(t, *from.Name, repo.Name)
assert.Equal(t, *from.FullName, repo.FullName)
assert.Equal(t, *from.Private, repo.IsSCMPrivate)
assert.Equal(t, *from.HTMLURL, repo.ForgeURL)
assert.Equal(t, *from.CloneURL, repo.Clone)
assert.Equal(t, *from.DefaultBranch, repo.Branch)
})
}
func Test_parsePullHook(t *testing.T) {
from := &github.PullRequestEvent{
Action: github.Ptr(actionOpen),
PullRequest: &github.PullRequest{
State: github.Ptr(stateOpen),
HTMLURL: github.Ptr("https://github.com/octocat/hello-world/pulls/42"),
Number: github.Ptr(42),
Title: github.Ptr("Updated README.md"),
Base: &github.PullRequestBranch{
Ref: github.Ptr("main"),
},
Head: &github.PullRequestBranch{
Ref: github.Ptr("changes"),
SHA: github.Ptr("f72fc19"),
Repo: &github.Repository{
CloneURL: github.Ptr("https://github.com/octocat/hello-world-fork"),
},
},
User: &github.User{
Login: github.Ptr("octocat"),
AvatarURL: github.Ptr("https://avatars1.githubusercontent.com/u/583231"),
},
}, Sender: &github.User{
Login: github.Ptr("octocat"),
},
}
pull, _, pipeline, err := parsePullHook(from, true)
assert.NoError(t, err)
assert.NotNil(t, pull)
assert.Equal(t, model.EventPull, pipeline.Event)
assert.Equal(t, *from.PullRequest.Base.Ref, pipeline.Branch)
assert.Equal(t, "refs/pull/42/merge", pipeline.Ref)
assert.Equal(t, "changes:main", pipeline.Refspec)
assert.Equal(t, *from.PullRequest.Head.SHA, pipeline.Commit)
assert.Equal(t, *from.PullRequest.Title, pipeline.Message)
assert.Equal(t, *from.PullRequest.Title, pipeline.Title)
assert.Equal(t, *from.PullRequest.User.Login, pipeline.Author)
assert.Equal(t, *from.PullRequest.User.AvatarURL, pipeline.Avatar)
assert.Equal(t, *from.Sender.Login, pipeline.Sender)
}
func Test_parseDeployHook(t *testing.T) {
from := &github.DeploymentEvent{Deployment: &github.Deployment{}, Sender: &github.User{}}
from.Deployment.Description = github.Ptr(":shipit:")
from.Deployment.Environment = github.Ptr("production")
from.Deployment.Task = github.Ptr("deploy")
from.Deployment.ID = github.Ptr(int64(42))
from.Deployment.Ref = github.Ptr("main")
from.Deployment.SHA = github.Ptr("f72fc19")
from.Deployment.URL = github.Ptr("https://github.com/octocat/hello-world")
from.Sender.Login = github.Ptr("octocat")
from.Sender.AvatarURL = github.Ptr("https://avatars1.githubusercontent.com/u/583231")
_, pipeline := parseDeployHook(from)
assert.Equal(t, model.EventDeploy, pipeline.Event)
assert.Equal(t, "main", pipeline.Branch)
assert.Equal(t, "refs/heads/main", pipeline.Ref)
assert.Equal(t, *from.Deployment.SHA, pipeline.Commit)
assert.Equal(t, *from.Deployment.Description, pipeline.Message)
assert.Equal(t, *from.Deployment.URL, pipeline.ForgeURL)
assert.Equal(t, *from.Sender.Login, pipeline.Author)
assert.Equal(t, *from.Sender.AvatarURL, pipeline.Avatar)
}
func Test_parsePushHook(t *testing.T) {
t.Run("convert push from webhook", func(t *testing.T) {
from := &github.PushEvent{Sender: &github.User{}, Repo: &github.PushEventRepository{}, HeadCommit: &github.HeadCommit{Author: &github.CommitAuthor{}}}
from.Sender.Login = github.Ptr("octocat")
from.Sender.AvatarURL = github.Ptr("https://avatars1.githubusercontent.com/u/583231")
from.Repo.CloneURL = github.Ptr("https://github.com/octocat/hello-world.git")
from.HeadCommit.Author.Email = github.Ptr("github.Ptr(octocat@github.com")
from.HeadCommit.Message = github.Ptr("updated README.md")
from.HeadCommit.URL = github.Ptr("https://github.com/octocat/hello-world")
from.HeadCommit.ID = github.Ptr("f72fc19")
from.Ref = github.Ptr("refs/heads/main")
_, pipeline := parsePushHook(from)
assert.Equal(t, model.EventPush, pipeline.Event)
assert.Equal(t, "main", pipeline.Branch)
assert.Equal(t, "refs/heads/main", pipeline.Ref)
assert.Equal(t, *from.HeadCommit.ID, pipeline.Commit)
assert.Equal(t, *from.HeadCommit.Message, pipeline.Message)
assert.Equal(t, *from.HeadCommit.URL, pipeline.ForgeURL)
assert.Equal(t, *from.Sender.Login, pipeline.Author)
assert.Equal(t, *from.Sender.AvatarURL, pipeline.Avatar)
assert.Equal(t, *from.HeadCommit.Author.Email, pipeline.Email)
})
t.Run("convert tag from webhook", func(t *testing.T) {
from := &github.PushEvent{}
from.Ref = github.Ptr("refs/tags/v1.0.0")
_, pipeline := parsePushHook(from)
assert.Equal(t, model.EventTag, pipeline.Event)
assert.Equal(t, "refs/tags/v1.0.0", pipeline.Ref)
})
t.Run("convert tag's base branch to pipeline's branch ", func(t *testing.T) {
from := &github.PushEvent{}
from.Ref = github.Ptr("refs/tags/v1.0.0")
from.BaseRef = github.Ptr("refs/heads/main")
_, pipeline := parsePushHook(from)
assert.Equal(t, model.EventTag, pipeline.Event)
assert.Equal(t, "main", pipeline.Branch)
})
t.Run("not convert tag's base_ref from webhook if not prefixed with 'ref/heads/'", func(t *testing.T) {
from := &github.PushEvent{}
from.Ref = github.Ptr("refs/tags/v1.0.0")
from.BaseRef = github.Ptr("refs/refs/main")
_, pipeline := parsePushHook(from)
assert.Equal(t, model.EventTag, pipeline.Event)
assert.Equal(t, "refs/tags/v1.0.0", pipeline.Branch)
})
}