Parse email from Gitea webhook (#3420)

The CI_COMMIT_AUTHOR_EMAIL was empty for gitea/forgejo,
now the webhook is parsed correctly.
This commit is contained in:
6543 2024-02-22 08:02:19 +01:00 committed by GitHub
parent bf0a9455ce
commit 10e4bac936
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 713 additions and 124 deletions

View file

@ -75,6 +75,203 @@ const HookPush = `
}
`
// HookPushMulti push multible commits to a branch
const HookPushMulti = `
{
"ref": "refs/heads/main",
"before": "6efcf5b7c98f3e7a491675164b7a2e7acac27941",
"after": "29be01c073851cf0db0c6a466e396b725a670453",
"compare_url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets/compare/6efcf5b7c98f3e7a491675164b7a2e7acac27941...29be01c073851cf0db0c6a466e396b725a670453",
"commits": [
{
"id": "29be01c073851cf0db0c6a466e396b725a670453",
"message": "add some text\n",
"url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets/commit/29be01c073851cf0db0c6a466e396b725a670453",
"author": {
"name": "6543",
"email": "6543@obermui.de",
"username": "test-user"
},
"committer": {
"name": "6543",
"email": "6543@obermui.de",
"username": "test-user"
},
"verification": null,
"timestamp": "2024-02-22T00:18:07+01:00",
"added": [],
"removed": [],
"modified": [
"aaa"
]
},
{
"id": "29cd95250404bd007c13b03eabe521196bab98a5",
"message": "rm a a file\n",
"url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets/commit/29cd95250404bd007c13b03eabe521196bab98a5",
"author": {
"name": "6543",
"email": "6543@obermui.de",
"username": "test-user"
},
"committer": {
"name": "6543",
"email": "6543@obermui.de",
"username": "test-user"
},
"verification": null,
"timestamp": "2024-02-22T00:17:49+01:00",
"added": [],
"removed": [
"aa"
],
"modified": []
},
{
"id": "93787b87b3134d0d62c7a24c1ea5b1b6fd17ca91",
"message": "add some a files\n",
"url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets/commit/93787b87b3134d0d62c7a24c1ea5b1b6fd17ca91",
"author": {
"name": "6543",
"email": "6543@obermui.de",
"username": "test-user"
},
"committer": {
"name": "6543",
"email": "6543@obermui.de",
"username": "test-user"
},
"verification": null,
"timestamp": "2024-02-22T00:17:33+01:00",
"added": [
"aa",
"aaa"
],
"removed": [],
"modified": []
}
],
"total_commits": 3,
"head_commit": {
"id": "29be01c073851cf0db0c6a466e396b725a670453",
"message": "add some text\n",
"url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets/commit/29be01c073851cf0db0c6a466e396b725a670453",
"author": {
"name": "6543",
"email": "6543@obermui.de",
"username": "test-user"
},
"committer": {
"name": "6543",
"email": "6543@obermui.de",
"username": "test-user"
},
"verification": null,
"timestamp": "2024-02-22T00:18:07+01:00",
"added": [],
"removed": [],
"modified": [
"aaa"
]
},
"repository": {
"id": 6,
"owner": {
"id": 2,
"login": "Test-CI",
"login_name": "",
"full_name": "",
"email": "",
"avatar_url": "http://127.0.0.1:3000/avatars/5b0a83c2185b3cb1ebceb11062d6c2eb",
"language": "",
"is_admin": false,
"last_login": "0001-01-01T00:00:00Z",
"created": "2023-07-31T19:13:48+02:00",
"restricted": false,
"active": false,
"prohibit_login": false,
"location": "",
"website": "",
"description": "",
"visibility": "public",
"followers_count": 0,
"following_count": 0,
"starred_repos_count": 0,
"username": "Test-CI"
},
"name": "multi-line-secrets",
"full_name": "Test-CI/multi-line-secrets",
"description": "",
"empty": false,
"private": false,
"fork": false,
"template": false,
"parent": null,
"mirror": false,
"size": 35,
"language": "",
"languages_url": "http://127.0.0.1:3000/api/v1/repos/Test-CI/multi-line-secrets/languages",
"html_url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets",
"url": "http://127.0.0.1:3000/api/v1/repos/Test-CI/multi-line-secrets",
"link": "",
"ssh_url": "ssh://git@127.0.0.1:2200/Test-CI/multi-line-secrets.git",
"clone_url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets.git",
"original_url": "",
"website": "",
"watchers_count": 2,
"open_issues_count": 1,
"default_branch": "main",
"archived": false,
"created_at": "2023-10-31T19:53:15+01:00",
"updated_at": "2023-11-02T06:16:34+01:00",
"archived_at": "1970-01-01T01:00:00+01:00",
"permissions": {
"admin": true,
"push": true,
"pull": true
},
"has_issues": true,
"internal_tracker": {
"enable_time_tracker": true,
"allow_only_contributors_to_track_time": true,
"enable_issue_dependencies": true
},
"avatar_url": "",
"object_format_name": ""
},
"pusher": {
"id": 1,
"login": "test-user",
"login_name": "",
"full_name": "",
"email": "test@noreply.localhost",
"avatar_url": "http://127.0.0.1:3000/avatars/dd46a756faad4727fb679320751f6dea",
"is_admin": false,
"last_login": "0001-01-01T00:00:00Z",
"created": "2023-07-31T19:13:05+02:00",
"prohibit_login": false,
"description": "",
"visibility": "public",
"username": "test-user"
},
"sender": {
"id": 1,
"login": "test-user",
"login_name": "",
"full_name": "",
"email": "test@noreply.localhost",
"avatar_url": "http://127.0.0.1:3000/avatars/dd46a756faad4727fb679320751f6dea",
"is_admin": false,
"last_login": "0001-01-01T00:00:00Z",
"created": "2023-07-31T19:13:05+02:00",
"prohibit_login": false,
"description": "",
"visibility": "public",
"username": "test-user"
}
}
`
// HookPushBranch is a sample Gitea push hook where a new branch was created from an existing commit
const HookPushBranch = `
{
@ -226,8 +423,7 @@ const HookPushBranch = `
"starred_repos_count": 55,
"username": "6543"
}
}
`
}`
// HookTag is a sample Gitea tag hook
const HookTag = `{
@ -236,9 +432,9 @@ const HookTag = `{
"ref": "v1.0.0",
"ref_type": "tag",
"repository": {
"id": 1,
"id": 12,
"owner": {
"id": 1,
"id": 4,
"username": "gordon",
"login": "gordon",
"full_name": "Gordon the Gopher",
@ -247,7 +443,7 @@ const HookTag = `{
},
"name": "hello-world",
"full_name": "gordon/hello-world",
"description": "",
"description": "a hello world example",
"private": true,
"fork": false,
"html_url": "http://gitea.golang.org/gordon/hello-world",
@ -332,6 +528,144 @@ const HookPullRequest = `{
}
}`
const HookPullRequestUpdated = `{
"action": "synchronized",
"number": 2,
"pull_request": {
"id": 2,
"url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets/pulls/2",
"number": 2,
"user": {
"id": 1,
"login": "test",
"login_name": "",
"full_name": "",
"email": "test@noreply.localhost",
"avatar_url": "http://127.0.0.1:3000/avatars/dd46a756faad4727fb679320751f6dea",
"is_admin": false,
"last_login": "0001-01-01T00:00:00Z",
"created": "2023-07-31T19:13:05+02:00",
"visibility": "public",
"username": "test"
},
"title": "New Pull",
"body": "create an awesome pull",
"labels": [
{
"id": 8,
"name": "Kind/Bug",
"exclusive": false,
"is_archived": false,
"color": "ee0701",
"description": "Something is not working",
"url": "http://100.106.226.9:3000/api/v1/repos/Test-CI/multi-line-secrets/labels/8"
},
{
"id": 11,
"name": "Kind/Security",
"exclusive": false,
"is_archived": false,
"color": "9c27b0",
"description": "This is security issue",
"url": "http://100.106.226.9:3000/api/v1/repos/Test-CI/multi-line-secrets/labels/11"
}
],
"milestone": null,
"assignees": null,
"requested_reviewers": null,
"state": "open",
"is_locked": false,
"html_url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets/pulls/2",
"diff_url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets/pulls/2.diff",
"patch_url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets/pulls/2.patch",
"mergeable": true,
"merged": false,
"merged_at": null,
"merge_commit_sha": null,
"merged_by": null,
"base": {
"label": "main",
"ref": "main",
"sha": "29be01c073851cf0db0c6a466e396b725a670453",
"repo_id": 6
},
"head": {
"label": "test-patch-1",
"ref": "test-patch-1",
"sha": "788ed8d02d3b7fcfcf6386dbcbca696aa1d4dc25",
"repo_id": 6
},
"merge_base": "29be01c073851cf0db0c6a466e396b725a670453",
"due_date": null,
"created_at": "2024-02-22T01:38:39+01:00",
"updated_at": "2024-02-22T01:42:03+01:00",
"closed_at": null,
"pin_order": 0
},
"requested_reviewer": null,
"repository": {
"id": 6,
"owner": {
"id": 2,
"login": "Test-CI",
"login_name": "",
"full_name": "",
"email": "",
"avatar_url": "http://127.0.0.1:3000/avatars/5b0a83c2185b3cb1ebceb11062d6c2eb",
"language": "",
"is_admin": false,
"last_login": "0001-01-01T00:00:00Z",
"created": "2023-07-31T19:13:48+02:00",
"prohibit_login": false,
"visibility": "public",
"username": "Test-CI"
},
"name": "multi-line-secrets",
"full_name": "Test-CI/multi-line-secrets",
"description": "",
"private": false,
"languages_url": "http://127.0.0.1:3000/api/v1/repos/Test-CI/multi-line-secrets/languages",
"html_url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets",
"url": "http://127.0.0.1:3000/api/v1/repos/Test-CI/multi-line-secrets",
"link": "",
"ssh_url": "ssh://git@127.0.0.1:2200/Test-CI/multi-line-secrets.git",
"clone_url": "http://127.0.0.1:3000/Test-CI/multi-line-secrets.git",
"original_url": "",
"default_branch": "main",
"permissions": {
"admin": true,
"push": true,
"pull": true
},
"has_issues": true,
"internal_tracker": {
"enable_time_tracker": true,
"allow_only_contributors_to_track_time": true,
"enable_issue_dependencies": true
},
"has_pull_requests": true,
"avatar_url": "",
"internal": false,
"mirror_interval": "",
"object_format_name": ""
},
"sender": {
"id": 1,
"login": "test",
"login_name": "",
"full_name": "",
"email": "test@noreply.localhost",
"avatar_url": "http://127.0.0.1:3000/avatars/dd46a756faad4727fb679320751f6dea",
"is_admin": false,
"last_login": "0001-01-01T00:00:00Z",
"created": "2023-07-31T19:13:05+02:00",
"visibility": "public",
"username": "test"
},
"commit_id": "",
"review": null
}`
const HookPullRequestMerged = `
{
"action": "closed",
@ -726,7 +1060,7 @@ const HookPullRequestClosed = `
"login": "anbraten",
"login_name": "",
"full_name": "",
"email": "anbraten@noreply.gitea.com",
"email": "anbraten@gitea.com",
"avatar_url": "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon",
"language": "",
"is_admin": false,
@ -964,7 +1298,7 @@ const HookPullRequestClosed = `
"login": "anbraten",
"login_name": "",
"full_name": "",
"email": "anbraten@noreply.gitea.com",
"email": "anbraten@repo.gitea.com",
"avatar_url": "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon",
"language": "",
"is_admin": false,
@ -1049,7 +1383,7 @@ const HookPullRequestClosed = `
"login": "anbraten",
"login_name": "",
"full_name": "",
"email": "anbraten@noreply.gitea.com",
"email": "anbraten@sender.gitea.com",
"avatar_url": "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon",
"language": "",
"is_admin": false,

View file

@ -81,12 +81,12 @@ func pipelineFromPush(hook *pushHook) *model.Pipeline {
link := hook.Compare
if len(hook.Commits) > 0 {
message = hook.Commits[0].Message
if len(hook.Commits) == 1 {
link = hook.Commits[0].URL
}
} else {
message = hook.HeadCommit.Message
}
if len(hook.Commits) == 1 {
link = hook.Commits[0].URL
link = hook.HeadCommit.URL
}
return &model.Pipeline{
@ -133,11 +133,11 @@ func pipelineFromTag(hook *pushHook) *model.Pipeline {
Commit: hook.Sha,
Ref: fmt.Sprintf("refs/tags/%s", hook.Ref),
ForgeURL: fmt.Sprintf("%s/src/tag/%s", hook.Repo.HTMLURL, hook.Ref),
Branch: fmt.Sprintf("refs/tags/%s", hook.Ref),
Message: fmt.Sprintf("created tag %s", hook.Ref),
Avatar: avatar,
Author: hook.Sender.UserName,
Sender: hook.Sender.UserName,
Email: hook.Sender.Email,
Timestamp: time.Now().UTC().Unix(),
}
}
@ -157,13 +157,14 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
pipeline := &model.Pipeline{
Event: event,
Commit: hook.PullRequest.Head.Sha,
ForgeURL: hook.PullRequest.URL,
ForgeURL: hook.PullRequest.HTMLURL,
Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number),
Branch: hook.PullRequest.Base.Ref,
Message: hook.PullRequest.Title,
Author: hook.PullRequest.Poster.UserName,
Avatar: avatar,
Sender: hook.Sender.UserName,
Email: hook.Sender.Email,
Title: hook.PullRequest.Title,
Refspec: fmt.Sprintf("%s:%s",
hook.PullRequest.Head.Ref,
@ -190,6 +191,7 @@ func pipelineFromRelease(hook *releaseHook) *model.Pipeline {
Avatar: avatar,
Author: hook.Sender.UserName,
Sender: hook.Sender.UserName,
Email: hook.Sender.Email,
IsPrerelease: hook.Release.IsPrerelease,
}
}

View file

@ -124,7 +124,7 @@ func Test_parse(t *testing.T) {
g.Assert(pipeline.Event).Equal(model.EventTag)
g.Assert(pipeline.Commit).Equal(hook.Sha)
g.Assert(pipeline.Ref).Equal("refs/tags/v1.0.0")
g.Assert(pipeline.Branch).Equal("refs/tags/v1.0.0")
g.Assert(pipeline.Branch).Equal("")
g.Assert(pipeline.ForgeURL).Equal("http://gitea.golang.org/gordon/hello-world/src/tag/v1.0.0")
g.Assert(pipeline.Message).Equal("created tag v1.0.0")
})
@ -136,7 +136,7 @@ func Test_parse(t *testing.T) {
g.Assert(pipeline.Event).Equal(model.EventPull)
g.Assert(pipeline.Commit).Equal(hook.PullRequest.Head.Sha)
g.Assert(pipeline.Ref).Equal("refs/pull/1/head")
g.Assert(pipeline.ForgeURL).Equal(hook.PullRequest.URL)
g.Assert(pipeline.ForgeURL).Equal("http://gitea.golang.org/gordon/hello-world/pull/1")
g.Assert(pipeline.Branch).Equal("main")
g.Assert(pipeline.Refspec).Equal("feature/changes:main")
g.Assert(pipeline.Message).Equal(hook.PullRequest.Title)

View file

@ -20,121 +20,374 @@ import (
"net/http"
"testing"
"github.com/franela/goblin"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/v2/server/forge/gitea/fixtures"
"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"
)
func Test_parser(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Gitea parser", func() {
g.It("should ignore unsupported hook events", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequest)
req, _ := http.NewRequest("POST", "/hook", buf)
func TestGiteaParser(t *testing.T) {
tests := []struct {
name string
data string
event string
err error
repo *model.Repo
pipe *model.Pipeline
}{
{
name: "should ignore unsupported hook events",
data: fixtures.HookPullRequest,
event: "issues",
err: &types.ErrIgnoreEvent{},
},
{
name: "push event should handle a push hook",
data: fixtures.HookPushBranch,
event: "push",
repo: &model.Repo{
ForgeRemoteID: "50820",
Owner: "meisam",
Name: "woodpecktester",
FullName: "meisam/woodpecktester",
Avatar: "https://codeberg.org/avatars/96512da76a14cf44e0bb32d1640e878e",
ForgeURL: "https://codeberg.org/meisam/woodpecktester",
Clone: "https://codeberg.org/meisam/woodpecktester.git",
CloneSSH: "git@codeberg.org:meisam/woodpecktester.git",
Branch: "main",
SCMKind: "git",
PREnabled: true,
Perm: &model.Perm{
Pull: true,
Push: true,
Admin: true,
},
},
pipe: &model.Pipeline{
Author: "6543",
Event: "push",
Commit: "28c3613ae62640216bea5e7dc71aa65356e4298b",
Branch: "fdsafdsa",
Ref: "refs/heads/fdsafdsa",
Message: "Delete '.woodpecker/.check.yml'\n",
Sender: "6543",
Avatar: "https://codeberg.org/avatars/09a234c768cb9bca78f6b2f82d6af173",
Email: "6543@obermui.de",
ForgeURL: "https://codeberg.org/meisam/woodpecktester/commit/28c3613ae62640216bea5e7dc71aa65356e4298b",
ChangedFiles: []string{".woodpecker/.check.yml"},
},
},
{
name: "push event should extract repository and pipeline details",
data: fixtures.HookPush,
event: "push",
repo: &model.Repo{
ForgeRemoteID: "1",
Owner: "gordon",
Name: "hello-world",
FullName: "gordon/hello-world",
Avatar: "http://gitea.golang.org/gordon/hello-world",
ForgeURL: "http://gitea.golang.org/gordon/hello-world",
Clone: "http://gitea.golang.org/gordon/hello-world.git",
CloneSSH: "git@gitea.golang.org:gordon/hello-world.git",
SCMKind: "git",
IsSCMPrivate: true,
Perm: &model.Perm{
Pull: true,
Push: true,
Admin: true,
},
},
pipe: &model.Pipeline{
Author: "gordon",
Event: "push",
Commit: "ef98532add3b2feb7a137426bba1248724367df5",
Branch: "main",
Ref: "refs/heads/main",
Message: "bump\n",
Sender: "gordon",
Avatar: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
Email: "gordon@golang.org",
ForgeURL: "http://gitea.golang.org/gordon/hello-world/commit/ef98532add3b2feb7a137426bba1248724367df5",
ChangedFiles: []string{"CHANGELOG.md", "app/controller/application.rb"},
},
},
{
name: "push event should handle multi commit push",
data: fixtures.HookPushMulti,
event: "push",
repo: &model.Repo{
ForgeRemoteID: "6",
Owner: "Test-CI",
Name: "multi-line-secrets",
FullName: "Test-CI/multi-line-secrets",
Avatar: "http://127.0.0.1:3000/avatars/5b0a83c2185b3cb1ebceb11062d6c2eb",
ForgeURL: "http://127.0.0.1:3000/Test-CI/multi-line-secrets",
Clone: "http://127.0.0.1:3000/Test-CI/multi-line-secrets.git",
CloneSSH: "ssh://git@127.0.0.1:2200/Test-CI/multi-line-secrets.git",
Branch: "main",
SCMKind: "git",
Perm: &model.Perm{
Pull: true,
Push: true,
Admin: true,
},
},
pipe: &model.Pipeline{
Author: "test-user",
Event: "push",
Commit: "29be01c073851cf0db0c6a466e396b725a670453",
Branch: "main",
Ref: "refs/heads/main",
Message: "add some text\n",
Sender: "test-user",
Avatar: "http://127.0.0.1:3000/avatars/dd46a756faad4727fb679320751f6dea",
Email: "test@noreply.localhost",
ForgeURL: "http://127.0.0.1:3000/Test-CI/multi-line-secrets/compare/6efcf5b7c98f3e7a491675164b7a2e7acac27941...29be01c073851cf0db0c6a466e396b725a670453",
ChangedFiles: []string{"aaa", "aa"},
},
},
{
name: "tag event should handle a tag hook",
data: fixtures.HookTag,
event: "create",
repo: &model.Repo{
ForgeRemoteID: "12",
Owner: "gordon",
Name: "hello-world",
FullName: "gordon/hello-world",
Avatar: "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
ForgeURL: "http://gitea.golang.org/gordon/hello-world",
Clone: "http://gitea.golang.org/gordon/hello-world.git",
CloneSSH: "git@gitea.golang.org:gordon/hello-world.git",
Branch: "main",
SCMKind: "git",
IsSCMPrivate: true,
Perm: &model.Perm{
Pull: true,
Push: true,
Admin: true,
},
},
pipe: &model.Pipeline{
Author: "gordon",
Event: "tag",
Commit: "ef98532add3b2feb7a137426bba1248724367df5",
Ref: "refs/tags/v1.0.0",
Message: "created tag v1.0.0",
Sender: "gordon",
Avatar: "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
Email: "gordon@golang.org",
ForgeURL: "http://gitea.golang.org/gordon/hello-world/src/tag/v1.0.0",
},
},
{
name: "pull-request events should handle a PR hook when PR got created",
data: fixtures.HookPullRequest,
event: "pull_request",
repo: &model.Repo{
ForgeRemoteID: "35129377",
Owner: "gordon",
Name: "hello-world",
FullName: "gordon/hello-world",
Avatar: "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
ForgeURL: "http://gitea.golang.org/gordon/hello-world",
Clone: "https://gitea.golang.org/gordon/hello-world.git",
CloneSSH: "",
Branch: "main",
SCMKind: "git",
IsSCMPrivate: true,
Perm: &model.Perm{
Pull: true,
Push: true,
Admin: true,
},
},
pipe: &model.Pipeline{
Author: "gordon",
Event: "pull_request",
Commit: "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
Branch: "main",
Ref: "refs/pull/1/head",
Refspec: "feature/changes:main",
Title: "Update the README with new information",
Message: "Update the README with new information",
Sender: "gordon",
Avatar: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
Email: "gordon@golang.org",
ForgeURL: "http://gitea.golang.org/gordon/hello-world/pull/1",
PullRequestLabels: []string{},
},
},
{
name: "pull-request events should handle a PR hook when PR got updated",
data: fixtures.HookPullRequestUpdated,
event: "pull_request",
repo: &model.Repo{
ForgeRemoteID: "6",
Owner: "Test-CI",
Name: "multi-line-secrets",
FullName: "Test-CI/multi-line-secrets",
Avatar: "http://127.0.0.1:3000/avatars/5b0a83c2185b3cb1ebceb11062d6c2eb",
ForgeURL: "http://127.0.0.1:3000/Test-CI/multi-line-secrets",
Clone: "http://127.0.0.1:3000/Test-CI/multi-line-secrets.git",
CloneSSH: "ssh://git@127.0.0.1:2200/Test-CI/multi-line-secrets.git",
Branch: "main",
SCMKind: "git",
PREnabled: true,
IsSCMPrivate: false,
Perm: &model.Perm{
Pull: true,
Push: true,
Admin: true,
},
},
pipe: &model.Pipeline{
Author: "test",
Event: "pull_request",
Commit: "788ed8d02d3b7fcfcf6386dbcbca696aa1d4dc25",
Branch: "main",
Ref: "refs/pull/2/head",
Refspec: "test-patch-1:main",
Title: "New Pull",
Message: "New Pull",
Sender: "test",
Avatar: "http://127.0.0.1:3000/avatars/dd46a756faad4727fb679320751f6dea",
Email: "test@noreply.localhost",
ForgeURL: "http://127.0.0.1:3000/Test-CI/multi-line-secrets/pulls/2",
PullRequestLabels: []string{
"Kind/Bug",
"Kind/Security",
},
},
},
{
name: "pull-request events should handle a PR closed hook when PR got closed",
data: fixtures.HookPullRequestClosed,
event: "pull_request",
repo: &model.Repo{
ForgeRemoteID: "46534",
Owner: "anbraten",
Name: "test-repo",
FullName: "anbraten/test-repo",
Avatar: "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon",
ForgeURL: "https://gitea.com/anbraten/test-repo",
Clone: "https://gitea.com/anbraten/test-repo.git",
CloneSSH: "git@gitea.com:anbraten/test-repo.git",
Branch: "main",
SCMKind: "git",
PREnabled: true,
Perm: &model.Perm{
Pull: true,
Push: true,
Admin: true,
},
},
pipe: &model.Pipeline{
Author: "anbraten",
Event: "pull_request_closed",
Commit: "d555a5dd07f4d0148a58d4686ec381502ae6a2d4",
Branch: "main",
Ref: "refs/pull/1/head",
Refspec: "anbraten-patch-1:main",
Title: "Adjust file",
Message: "Adjust file",
Sender: "anbraten",
Avatar: "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon",
Email: "anbraten@sender.gitea.com",
ForgeURL: "https://gitea.com/anbraten/test-repo/pulls/1",
PullRequestLabels: []string{},
},
},
{
name: "pull-request events should handle a PR closed hook when PR was merged",
data: fixtures.HookPullRequestMerged,
event: "pull_request",
repo: &model.Repo{
ForgeRemoteID: "46534",
Owner: "anbraten",
Name: "test-repo",
FullName: "anbraten/test-repo",
Avatar: "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon",
ForgeURL: "https://gitea.com/anbraten/test-repo",
Clone: "https://gitea.com/anbraten/test-repo.git",
CloneSSH: "git@gitea.com:anbraten/test-repo.git",
Branch: "main",
SCMKind: "git",
PREnabled: true,
Perm: &model.Perm{
Pull: true,
Push: true,
Admin: true,
},
},
pipe: &model.Pipeline{
Author: "anbraten",
Event: "pull_request_closed",
Commit: "d555a5dd07f4d0148a58d4686ec381502ae6a2d4",
Branch: "main",
Ref: "refs/pull/1/head",
Refspec: "anbraten-patch-1:main",
Title: "Adjust file",
Message: "Adjust file",
Sender: "anbraten",
Avatar: "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon",
Email: "anbraten@noreply.gitea.com",
ForgeURL: "https://gitea.com/anbraten/test-repo/pulls/1",
PullRequestLabels: []string{},
},
},
{
name: "release events should handle release hook",
data: fixtures.HookRelease,
event: "release",
repo: &model.Repo{
ForgeRemoteID: "77",
Owner: "anbraten",
Name: "demo",
FullName: "anbraten/demo",
Avatar: "https://git.xxx/user/avatar/anbraten/-1",
ForgeURL: "https://git.xxx/anbraten/demo",
Clone: "https://git.xxx/anbraten/demo.git",
CloneSSH: "ssh://git@git.xxx:22/anbraten/demo.git",
Branch: "main",
SCMKind: "git",
PREnabled: true,
IsSCMPrivate: true,
Perm: &model.Perm{
Pull: true,
Push: true,
Admin: true,
},
},
pipe: &model.Pipeline{
Author: "anbraten",
Event: "release",
Branch: "main",
Ref: "refs/tags/0.0.5",
Message: "created release Version 0.0.5",
Sender: "anbraten",
Avatar: "https://git.xxx/user/avatar/anbraten/-1",
Email: "anbraten@noreply.xxx",
ForgeURL: "https://git.xxx/anbraten/demo/releases/tag/0.0.5",
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
req, _ := http.NewRequest("POST", "/api/hook", bytes.NewBufferString(tc.data))
req.Header = http.Header{}
req.Header.Set(hookEvent, "issues")
r, b, err := parseHook(req)
g.Assert(r).IsNil()
g.Assert(b).IsNil()
assert.ErrorIs(t, err, &types.ErrIgnoreEvent{})
req.Header.Set(hookEvent, tc.event)
r, p, err := parseHook(req)
if tc.err != nil {
assert.ErrorIs(t, err, tc.err)
} else if assert.NoError(t, err) {
assert.EqualValues(t, tc.repo, r)
p.Timestamp = 0
assert.EqualValues(t, tc.pipe, p)
}
})
g.Describe("push event", func() {
g.It("should handle a push hook", func() {
buf := bytes.NewBufferString(fixtures.HookPushBranch)
req, _ := http.NewRequest("POST", "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPush)
r, b, err := parseHook(req)
g.Assert(err).IsNil()
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(b.Event).Equal(model.EventPush)
g.Assert(b.Message).Equal("Delete '.woodpecker/.check.yml'\n")
g.Assert(b.ChangedFiles).Equal([]string{".woodpecker/.check.yml"})
})
g.It("should extract repository and pipeline details", func() {
buf := bytes.NewBufferString(fixtures.HookPush)
req, _ := http.NewRequest("POST", "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPush)
r, b, err := parseHook(req)
g.Assert(err).IsNil()
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(b.Event).Equal(model.EventPush)
g.Assert(utils.EqualSliceValues(b.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue()
})
})
g.Describe("tag event", func() {
g.It("should handle a tag hook", func() {
buf := bytes.NewBufferString(fixtures.HookTag)
req, _ := http.NewRequest("POST", "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookCreated)
r, b, err := parseHook(req)
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(err).IsNil()
g.Assert(b.Event).Equal(model.EventTag)
})
})
g.Describe("pull-request events", func() {
// g.It("should handle a PR hook when PR got created")
g.It("should handle a PR hook when PR got updated", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequest)
req, _ := http.NewRequest("POST", "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullRequest)
r, b, err := parseHook(req)
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(err).IsNil()
g.Assert(b.Event).Equal(model.EventPull)
})
g.It("should handle a PR closed hook when PR got closed", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequestClosed)
req, _ := http.NewRequest("POST", "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullRequest)
r, b, err := parseHook(req)
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(err).IsNil()
g.Assert(b.Event).Equal(model.EventPullClosed)
})
g.It("should handle a PR closed hook when PR was merged", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequestMerged)
req, _ := http.NewRequest("POST", "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullRequest)
r, b, err := parseHook(req)
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(err).IsNil()
g.Assert(b.Event).Equal(model.EventPullClosed)
})
g.It("should handle release hook", func() {
buf := bytes.NewBufferString(fixtures.HookRelease)
req, _ := http.NewRequest("POST", "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookRelease)
r, b, err := parseHook(req)
g.Assert(err).IsNil()
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(b.Event).Equal(model.EventRelease)
})
})
})
}
}