woodpecker/server/forge/gitea/helper_test.go

275 lines
10 KiB
Go
Raw Normal View History

// Copyright 2022 Woodpecker Authors
2018-02-19 22:24:10 +00:00
// Copyright 2018 Drone.IO Inc.
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// 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
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// 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.
2017-05-01 10:33:06 +00:00
package gitea
import (
"bytes"
"testing"
"code.gitea.io/sdk/gitea"
2019-09-14 12:21:16 +00:00
"github.com/franela/goblin"
"github.com/woodpecker-ci/woodpecker/server/forge/gitea/fixtures"
"github.com/woodpecker-ci/woodpecker/server/model"
"github.com/woodpecker-ci/woodpecker/shared/utils"
2017-05-01 10:33:06 +00:00
)
func Test_parse(t *testing.T) {
g := goblin.Goblin(t)
2017-05-01 13:08:57 +00:00
g.Describe("Gitea", func() {
2017-05-01 10:33:06 +00:00
g.It("Should parse push hook payload", func() {
buf := bytes.NewBufferString(fixtures.HookPush)
hook, err := parsePush(buf)
g.Assert(err).IsNil()
2017-05-01 10:33:06 +00:00
g.Assert(hook.Ref).Equal("refs/heads/master")
g.Assert(hook.After).Equal("ef98532add3b2feb7a137426bba1248724367df5")
g.Assert(hook.Before).Equal("4b2626259b5a97b6b4eab5e6cca66adb986b672b")
2017-05-01 12:29:57 +00:00
g.Assert(hook.Compare).Equal("http://gitea.golang.org/gordon/hello-world/compare/4b2626259b5a97b6b4eab5e6cca66adb986b672b...ef98532add3b2feb7a137426bba1248724367df5")
2017-05-01 10:33:06 +00:00
g.Assert(hook.Repo.Name).Equal("hello-world")
g.Assert(hook.Repo.HTMLURL).Equal("http://gitea.golang.org/gordon/hello-world")
g.Assert(hook.Repo.Owner.UserName).Equal("gordon")
2017-05-01 10:33:06 +00:00
g.Assert(hook.Repo.FullName).Equal("gordon/hello-world")
g.Assert(hook.Repo.Owner.Email).Equal("gordon@golang.org")
g.Assert(hook.Repo.Private).Equal(true)
g.Assert(hook.Pusher.Email).Equal("gordon@golang.org")
g.Assert(hook.Pusher.UserName).Equal("gordon")
g.Assert(hook.Sender.UserName).Equal("gordon")
g.Assert(hook.Sender.AvatarURL).Equal("http://gitea.golang.org///1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
2017-05-01 10:33:06 +00:00
})
g.It("Should parse tag hook payload", func() {
buf := bytes.NewBufferString(fixtures.HookPushTag)
hook, err := parsePush(buf)
g.Assert(err).IsNil()
2017-05-01 10:33:06 +00:00
g.Assert(hook.Ref).Equal("v1.0.0")
g.Assert(hook.Sha).Equal("ef98532add3b2feb7a137426bba1248724367df5")
2017-05-01 10:33:06 +00:00
g.Assert(hook.Repo.Name).Equal("hello-world")
g.Assert(hook.Repo.HTMLURL).Equal("http://gitea.golang.org/gordon/hello-world")
2017-05-01 10:33:06 +00:00
g.Assert(hook.Repo.FullName).Equal("gordon/hello-world")
g.Assert(hook.Repo.Owner.Email).Equal("gordon@golang.org")
g.Assert(hook.Repo.Owner.UserName).Equal("gordon")
2017-05-01 10:33:06 +00:00
g.Assert(hook.Repo.Private).Equal(true)
g.Assert(hook.Sender.UserName).Equal("gordon")
g.Assert(hook.Sender.AvatarURL).Equal("https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
2017-05-01 10:33:06 +00:00
})
g.It("Should parse pull_request hook payload", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequest)
hook, err := parsePullRequest(buf)
g.Assert(err).IsNil()
2017-05-01 10:33:06 +00:00
g.Assert(hook.Action).Equal("opened")
g.Assert(hook.Number).Equal(int64(1))
g.Assert(hook.Repo.Name).Equal("hello-world")
g.Assert(hook.Repo.HTMLURL).Equal("http://gitea.golang.org/gordon/hello-world")
2017-05-01 10:33:06 +00:00
g.Assert(hook.Repo.FullName).Equal("gordon/hello-world")
g.Assert(hook.Repo.Owner.Email).Equal("gordon@golang.org")
g.Assert(hook.Repo.Owner.UserName).Equal("gordon")
2017-05-01 10:33:06 +00:00
g.Assert(hook.Repo.Private).Equal(true)
g.Assert(hook.Sender.UserName).Equal("gordon")
g.Assert(hook.Sender.AvatarURL).Equal("https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
2017-05-01 10:33:06 +00:00
g.Assert(hook.PullRequest.Title).Equal("Update the README with new information")
g.Assert(hook.PullRequest.Body).Equal("please merge")
g.Assert(hook.PullRequest.State).Equal(gitea.StateOpen)
g.Assert(hook.PullRequest.Poster.UserName).Equal("gordon")
g.Assert(hook.PullRequest.Base.Name).Equal("master")
2017-05-01 10:33:06 +00:00
g.Assert(hook.PullRequest.Base.Ref).Equal("master")
g.Assert(hook.PullRequest.Head.Name).Equal("feature/changes")
2017-05-01 10:33:06 +00:00
g.Assert(hook.PullRequest.Head.Ref).Equal("feature/changes")
})
g.It("Should return a Pipeline struct from a push hook", func() {
2017-05-01 10:33:06 +00:00
buf := bytes.NewBufferString(fixtures.HookPush)
hook, _ := parsePush(buf)
pipeline := pipelineFromPush(hook)
g.Assert(pipeline.Event).Equal(model.EventPush)
g.Assert(pipeline.Commit).Equal(hook.After)
g.Assert(pipeline.Ref).Equal(hook.Ref)
g.Assert(pipeline.Link).Equal(hook.Commits[0].URL)
g.Assert(pipeline.Branch).Equal("master")
g.Assert(pipeline.Message).Equal(hook.Commits[0].Message)
g.Assert(pipeline.Avatar).Equal("http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(pipeline.Author).Equal(hook.Sender.UserName)
g.Assert(utils.EqualStringSlice(pipeline.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue()
2017-05-01 10:33:06 +00:00
})
g.It("Should return a Repo struct from a push hook", func() {
buf := bytes.NewBufferString(fixtures.HookPush)
hook, _ := parsePush(buf)
repo := toRepo(hook.Repo)
2017-05-01 10:33:06 +00:00
g.Assert(repo.Name).Equal(hook.Repo.Name)
g.Assert(repo.Owner).Equal(hook.Repo.Owner.UserName)
2017-05-01 10:33:06 +00:00
g.Assert(repo.FullName).Equal("gordon/hello-world")
g.Assert(repo.Link).Equal(hook.Repo.HTMLURL)
2017-05-01 10:33:06 +00:00
})
g.It("Should return a Pipeline struct from a tag hook", func() {
buf := bytes.NewBufferString(fixtures.HookPushTag)
hook, _ := parsePush(buf)
pipeline := pipelineFromTag(hook)
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.Link).Equal("http://gitea.golang.org/gordon/hello-world/src/tag/v1.0.0")
g.Assert(pipeline.Message).Equal("created tag v1.0.0")
})
g.It("Should return a Pipeline struct from a pull_request hook", func() {
2017-05-01 10:33:06 +00:00
buf := bytes.NewBufferString(fixtures.HookPullRequest)
hook, _ := parsePullRequest(buf)
pipeline := pipelineFromPullRequest(hook)
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.Link).Equal(hook.PullRequest.URL)
g.Assert(pipeline.Branch).Equal("master")
g.Assert(pipeline.Refspec).Equal("feature/changes:master")
g.Assert(pipeline.Message).Equal(hook.PullRequest.Title)
g.Assert(pipeline.Avatar).Equal("http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(pipeline.Author).Equal(hook.PullRequest.Poster.UserName)
2017-05-01 10:33:06 +00:00
})
g.It("Should return a Repo struct from a pull_request hook", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequest)
hook, _ := parsePullRequest(buf)
repo := toRepo(hook.Repo)
2017-05-01 10:33:06 +00:00
g.Assert(repo.Name).Equal(hook.Repo.Name)
g.Assert(repo.Owner).Equal(hook.Repo.Owner.UserName)
2017-05-01 10:33:06 +00:00
g.Assert(repo.FullName).Equal("gordon/hello-world")
g.Assert(repo.Link).Equal(hook.Repo.HTMLURL)
2017-05-01 10:33:06 +00:00
})
2017-05-01 12:29:57 +00:00
g.It("Should return a Perm struct from a Gitea Perm", func() {
perms := []gitea.Permission{
{
Admin: true,
Push: true,
Pull: true,
},
{
Admin: true,
Push: true,
Pull: false,
},
{
Admin: true,
Push: false,
Pull: false,
},
2017-05-01 10:33:06 +00:00
}
for _, from := range perms {
2017-05-01 12:29:57 +00:00
perm := toPerm(&from)
2017-05-01 10:33:06 +00:00
g.Assert(perm.Pull).Equal(from.Pull)
g.Assert(perm.Push).Equal(from.Push)
g.Assert(perm.Admin).Equal(from.Admin)
}
})
2017-05-01 13:08:57 +00:00
g.It("Should return a Team struct from a Gitea Org", func() {
2017-05-01 12:29:57 +00:00
from := &gitea.Organization{
UserName: "woodpecker",
2017-05-01 12:29:57 +00:00
AvatarURL: "/avatars/1",
2017-05-01 10:33:06 +00:00
}
to := toTeam(from, "http://localhost:80")
g.Assert(to.Login).Equal(from.UserName)
g.Assert(to.Avatar).Equal("http://localhost:80/avatars/1")
})
2017-05-01 13:08:57 +00:00
g.It("Should return a Repo struct from a Gitea Repo", func() {
2017-05-01 12:29:57 +00:00
from := gitea.Repository{
2017-05-01 10:33:06 +00:00
FullName: "gophers/hello-world",
2017-05-01 12:29:57 +00:00
Owner: &gitea.User{
2017-05-01 10:33:06 +00:00
UserName: "gordon",
2017-05-01 12:29:57 +00:00
AvatarURL: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
2017-05-01 10:33:06 +00:00
},
CloneURL: "http://gitea.golang.org/gophers/hello-world.git",
HTMLURL: "http://gitea.golang.org/gophers/hello-world",
Private: true,
DefaultBranch: "master",
Permissions: &gitea.Permission{Admin: true},
2017-05-01 10:33:06 +00:00
}
repo := toRepo(&from)
2017-05-01 10:33:06 +00:00
g.Assert(repo.FullName).Equal(from.FullName)
g.Assert(repo.Owner).Equal(from.Owner.UserName)
g.Assert(repo.Name).Equal("hello-world")
g.Assert(repo.Branch).Equal("master")
2017-05-01 12:29:57 +00:00
g.Assert(repo.Link).Equal(from.HTMLURL)
g.Assert(repo.Clone).Equal(from.CloneURL)
g.Assert(repo.Avatar).Equal(from.Owner.AvatarURL)
g.Assert(repo.IsSCMPrivate).Equal(from.Private)
g.Assert(repo.Perm.Admin).IsTrue()
2017-05-01 10:33:06 +00:00
})
g.It("Should correct a malformed avatar url", func() {
2022-01-05 20:50:23 +00:00
urls := []struct {
2017-05-01 10:33:06 +00:00
Before string
After string
}{
{
2017-05-01 12:29:57 +00:00
"http://gitea.golang.org///1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
2017-05-01 10:33:06 +00:00
"//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
},
{
"//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
"//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
},
{
2017-05-01 12:29:57 +00:00
"http://gitea.golang.org/avatars/1",
"http://gitea.golang.org/avatars/1",
2017-05-01 10:33:06 +00:00
},
{
2017-05-01 12:29:57 +00:00
"http://gitea.golang.org//avatars/1",
"http://gitea.golang.org/avatars/1",
2017-05-01 10:33:06 +00:00
},
}
for _, url := range urls {
got := fixMalformedAvatar(url.Before)
g.Assert(got).Equal(url.After)
}
})
g.It("Should expand the avatar url", func() {
2022-01-05 20:50:23 +00:00
urls := []struct {
2017-05-01 10:33:06 +00:00
Before string
After string
}{
{
"/avatars/1",
2017-05-01 12:29:57 +00:00
"http://gitea.io/avatars/1",
2017-05-01 10:33:06 +00:00
},
{
"//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
"http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
},
{
2017-05-01 13:08:57 +00:00
"/gitea/avatars/2",
"http://gitea.io/gitea/avatars/2",
2017-05-01 10:33:06 +00:00
},
}
2022-01-05 20:50:23 +00:00
repo := "http://gitea.io/foo/bar"
2017-05-01 10:33:06 +00:00
for _, url := range urls {
got := expandAvatar(repo, url.Before)
g.Assert(got).Equal(url.After)
}
})
})
}