Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse email from Gitea webhook #3420

Merged
merged 9 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions server/forge/gitea/fixtures/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,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,
Expand Down Expand Up @@ -964,7 +964,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,
Expand Down Expand Up @@ -1049,7 +1049,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,
Expand Down
2 changes: 2 additions & 0 deletions server/forge/gitea/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func pipelineFromTag(hook *pushHook) *model.Pipeline {
Avatar: avatar,
Author: hook.Sender.UserName,
Sender: hook.Sender.UserName,
Email: hook.Sender.Email,
Timestamp: time.Now().UTC().Unix(),
}
}
Expand All @@ -164,6 +165,7 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
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,
Expand Down
49 changes: 41 additions & 8 deletions server/forge/gitea/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/shared/utils"
)

func Test_parser(t *testing.T) {
func TestGiteaParser(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Gitea parser", func() {
g.It("should ignore unsupported hook events", func() {
Expand All @@ -49,13 +49,42 @@ func Test_parser(t *testing.T) {
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"})
r, p, err := parseHook(req)
if assert.NoError(t, err) {
assert.EqualValues(t, &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,
},
}, r)
p.Timestamp = 0
assert.EqualValues(t, &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/compare/main...28c3613ae62640216bea5e7dc71aa65356e4298b",
ChangedFiles: []string{".woodpecker/.check.yml"},
Title: "", // TODO!
6543 marked this conversation as resolved.
Show resolved Hide resolved
}, p)
}
})

g.It("should extract repository and pipeline details", func() {
Expand All @@ -68,6 +97,7 @@ func Test_parser(t *testing.T) {
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(b.Event).Equal(model.EventPush)
assert.EqualValues(t, "gordon@golang.org", b.Email)
g.Assert(utils.EqualSliceValues(b.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue()
})
})
Expand All @@ -83,6 +113,7 @@ func Test_parser(t *testing.T) {
g.Assert(b).IsNotNil()
g.Assert(err).IsNil()
g.Assert(b.Event).Equal(model.EventTag)
assert.EqualValues(t, "gordon@golang.org", b.Email)
})
})

Expand All @@ -97,6 +128,7 @@ func Test_parser(t *testing.T) {
r, b, err := parseHook(req)
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
assert.EqualValues(t, "gordon@golang.org", b.Email)
g.Assert(err).IsNil()
g.Assert(b.Event).Equal(model.EventPull)
})
Expand All @@ -109,6 +141,7 @@ func Test_parser(t *testing.T) {
r, b, err := parseHook(req)
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
assert.EqualValues(t, "anbraten@sender.gitea.com", b.Email)
g.Assert(err).IsNil()
g.Assert(b.Event).Equal(model.EventPullClosed)
})
Expand Down