2022-10-18 01:24:12 +00:00
|
|
|
// 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.
|
|
|
|
|
2016-04-30 08:00:39 +00:00
|
|
|
package bitbucket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/franela/goblin"
|
|
|
|
"golang.org/x/oauth2"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/bitbucket/internal"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2016-04-30 08:00:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_helper(t *testing.T) {
|
|
|
|
g := goblin.Goblin(t)
|
|
|
|
g.Describe("Bitbucket converter", func() {
|
|
|
|
g.It("should convert passing status", func() {
|
|
|
|
g.Assert(convertStatus(model.StatusSuccess)).Equal(statusSuccess)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("should convert pending status", func() {
|
|
|
|
g.Assert(convertStatus(model.StatusPending)).Equal(statusPending)
|
|
|
|
g.Assert(convertStatus(model.StatusRunning)).Equal(statusPending)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("should convert failing status", func() {
|
|
|
|
g.Assert(convertStatus(model.StatusFailure)).Equal(statusFailure)
|
|
|
|
g.Assert(convertStatus(model.StatusKilled)).Equal(statusFailure)
|
|
|
|
g.Assert(convertStatus(model.StatusError)).Equal(statusFailure)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("should convert repository", func() {
|
|
|
|
from := &internal.Repo{
|
|
|
|
FullName: "octocat/hello-world",
|
|
|
|
IsPrivate: true,
|
|
|
|
Scm: "hg",
|
|
|
|
}
|
|
|
|
from.Owner.Links.Avatar.Href = "http://..."
|
2021-12-01 13:22:06 +00:00
|
|
|
from.Links.HTML.Href = "https://bitbucket.org/foo/bar"
|
2023-03-21 22:01:59 +00:00
|
|
|
fromPerm := &internal.RepoPerm{
|
|
|
|
Permission: "write",
|
|
|
|
}
|
2016-04-30 08:00:39 +00:00
|
|
|
|
2023-03-21 22:01:59 +00:00
|
|
|
to := convertRepo(from, fromPerm)
|
2016-04-30 08:00:39 +00:00
|
|
|
g.Assert(to.Avatar).Equal(from.Owner.Links.Avatar.Href)
|
|
|
|
g.Assert(to.FullName).Equal(from.FullName)
|
|
|
|
g.Assert(to.Owner).Equal("octocat")
|
|
|
|
g.Assert(to.Name).Equal("hello-world")
|
|
|
|
g.Assert(to.Branch).Equal("default")
|
2021-11-22 11:55:13 +00:00
|
|
|
g.Assert(string(to.SCMKind)).Equal(from.Scm)
|
|
|
|
g.Assert(to.IsSCMPrivate).Equal(from.IsPrivate)
|
2021-12-01 13:22:06 +00:00
|
|
|
g.Assert(to.Clone).Equal(from.Links.HTML.Href)
|
2023-11-14 16:12:12 +00:00
|
|
|
g.Assert(to.ForgeURL).Equal(from.Links.HTML.Href)
|
2023-03-21 22:01:59 +00:00
|
|
|
g.Assert(to.Perm.Push).IsTrue()
|
|
|
|
g.Assert(to.Perm.Admin).IsFalse()
|
2016-04-30 08:00:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("should convert team", func() {
|
2022-09-18 07:26:13 +00:00
|
|
|
from := &internal.Workspace{Slug: "octocat"}
|
2016-04-30 08:00:39 +00:00
|
|
|
from.Links.Avatar.Href = "http://..."
|
2022-09-18 07:26:13 +00:00
|
|
|
to := convertWorkspace(from)
|
2016-04-30 08:00:39 +00:00
|
|
|
g.Assert(to.Avatar).Equal(from.Links.Avatar.Href)
|
2022-09-18 07:26:13 +00:00
|
|
|
g.Assert(to.Login).Equal(from.Slug)
|
2016-04-30 08:00:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("should convert team list", func() {
|
2022-09-18 07:26:13 +00:00
|
|
|
from := &internal.Workspace{Slug: "octocat"}
|
2016-04-30 08:00:39 +00:00
|
|
|
from.Links.Avatar.Href = "http://..."
|
2022-09-18 07:26:13 +00:00
|
|
|
to := convertWorkspaceList([]*internal.Workspace{from})
|
2016-04-30 08:00:39 +00:00
|
|
|
g.Assert(to[0].Avatar).Equal(from.Links.Avatar.Href)
|
2022-09-18 07:26:13 +00:00
|
|
|
g.Assert(to[0].Login).Equal(from.Slug)
|
2016-04-30 08:00:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("should convert user", func() {
|
|
|
|
token := &oauth2.Token{
|
|
|
|
AccessToken: "foo",
|
|
|
|
RefreshToken: "bar",
|
|
|
|
Expiry: time.Now(),
|
|
|
|
}
|
|
|
|
user := &internal.Account{Login: "octocat"}
|
|
|
|
user.Links.Avatar.Href = "http://..."
|
|
|
|
|
|
|
|
result := convertUser(user, token)
|
|
|
|
g.Assert(result.Avatar).Equal(user.Links.Avatar.Href)
|
|
|
|
g.Assert(result.Login).Equal(user.Login)
|
|
|
|
g.Assert(result.Token).Equal(token.AccessToken)
|
|
|
|
g.Assert(result.Secret).Equal(token.RefreshToken)
|
|
|
|
g.Assert(result.Expiry).Equal(token.Expiry.UTC().Unix())
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("should use clone url", func() {
|
|
|
|
repo := &internal.Repo{}
|
|
|
|
repo.Links.Clone = append(repo.Links.Clone, internal.Link{
|
|
|
|
Name: "https",
|
|
|
|
Href: "https://bitbucket.org/foo/bar.git",
|
|
|
|
})
|
|
|
|
link := cloneLink(repo)
|
|
|
|
g.Assert(link).Equal(repo.Links.Clone[0].Href)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("should build clone url", func() {
|
|
|
|
repo := &internal.Repo{}
|
2021-12-01 13:22:06 +00:00
|
|
|
repo.Links.HTML.Href = "https://foo:bar@bitbucket.org/foo/bar.git"
|
2016-04-30 08:00:39 +00:00
|
|
|
link := cloneLink(repo)
|
|
|
|
g.Assert(link).Equal("https://bitbucket.org/foo/bar.git")
|
|
|
|
})
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
g.It("should convert pull hook to pipeline", func() {
|
2016-04-30 08:00:39 +00:00
|
|
|
hook := &internal.PullRequestHook{}
|
|
|
|
hook.Actor.Login = "octocat"
|
|
|
|
hook.Actor.Links.Avatar.Href = "https://..."
|
|
|
|
hook.PullRequest.Dest.Commit.Hash = "73f9c44d"
|
2023-07-31 03:47:23 +00:00
|
|
|
hook.PullRequest.Dest.Branch.Name = "main"
|
2021-12-01 13:22:06 +00:00
|
|
|
hook.PullRequest.Dest.Repo.Links.HTML.Href = "https://bitbucket.org/foo/bar"
|
2016-11-19 22:44:22 +00:00
|
|
|
hook.PullRequest.Source.Branch.Name = "change"
|
|
|
|
hook.PullRequest.Source.Repo.FullName = "baz/bar"
|
2021-12-01 13:22:06 +00:00
|
|
|
hook.PullRequest.Links.HTML.Href = "https://bitbucket.org/foo/bar/pulls/5"
|
2016-04-30 08:00:39 +00:00
|
|
|
hook.PullRequest.Desc = "updated README"
|
|
|
|
hook.PullRequest.Updated = time.Now()
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := convertPullHook(hook)
|
|
|
|
g.Assert(pipeline.Event).Equal(model.EventPull)
|
|
|
|
g.Assert(pipeline.Author).Equal(hook.Actor.Login)
|
|
|
|
g.Assert(pipeline.Avatar).Equal(hook.Actor.Links.Avatar.Href)
|
|
|
|
g.Assert(pipeline.Commit).Equal(hook.PullRequest.Dest.Commit.Hash)
|
|
|
|
g.Assert(pipeline.Branch).Equal(hook.PullRequest.Dest.Branch.Name)
|
2023-11-14 16:12:12 +00:00
|
|
|
g.Assert(pipeline.ForgeURL).Equal(hook.PullRequest.Links.HTML.Href)
|
2023-07-31 03:47:23 +00:00
|
|
|
g.Assert(pipeline.Ref).Equal("refs/heads/main")
|
|
|
|
g.Assert(pipeline.Refspec).Equal("change:main")
|
2022-10-18 01:24:12 +00:00
|
|
|
g.Assert(pipeline.Message).Equal(hook.PullRequest.Desc)
|
|
|
|
g.Assert(pipeline.Timestamp).Equal(hook.PullRequest.Updated.Unix())
|
2016-04-30 08:00:39 +00:00
|
|
|
})
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
g.It("should convert push hook to pipeline", func() {
|
2016-04-30 08:00:39 +00:00
|
|
|
change := internal.Change{}
|
|
|
|
change.New.Target.Hash = "73f9c44d"
|
2023-07-31 03:47:23 +00:00
|
|
|
change.New.Name = "main"
|
2021-12-01 13:22:06 +00:00
|
|
|
change.New.Target.Links.HTML.Href = "https://bitbucket.org/foo/bar/commits/73f9c44d"
|
2016-04-30 08:00:39 +00:00
|
|
|
change.New.Target.Message = "updated README"
|
|
|
|
change.New.Target.Date = time.Now()
|
2016-11-25 21:37:58 +00:00
|
|
|
change.New.Target.Author.Raw = "Test <test@domain.tld>"
|
2016-04-30 08:00:39 +00:00
|
|
|
|
|
|
|
hook := internal.PushHook{}
|
|
|
|
hook.Actor.Login = "octocat"
|
|
|
|
hook.Actor.Links.Avatar.Href = "https://..."
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := convertPushHook(&hook, &change)
|
|
|
|
g.Assert(pipeline.Event).Equal(model.EventPush)
|
|
|
|
g.Assert(pipeline.Email).Equal("test@domain.tld")
|
|
|
|
g.Assert(pipeline.Author).Equal(hook.Actor.Login)
|
|
|
|
g.Assert(pipeline.Avatar).Equal(hook.Actor.Links.Avatar.Href)
|
|
|
|
g.Assert(pipeline.Commit).Equal(change.New.Target.Hash)
|
|
|
|
g.Assert(pipeline.Branch).Equal(change.New.Name)
|
2023-11-14 16:12:12 +00:00
|
|
|
g.Assert(pipeline.ForgeURL).Equal(change.New.Target.Links.HTML.Href)
|
2023-07-31 03:47:23 +00:00
|
|
|
g.Assert(pipeline.Ref).Equal("refs/heads/main")
|
2022-10-18 01:24:12 +00:00
|
|
|
g.Assert(pipeline.Message).Equal(change.New.Target.Message)
|
|
|
|
g.Assert(pipeline.Timestamp).Equal(change.New.Target.Date.Unix())
|
2016-04-30 08:00:39 +00:00
|
|
|
})
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
g.It("should convert tag hook to pipeline", func() {
|
2016-04-30 08:00:39 +00:00
|
|
|
change := internal.Change{}
|
|
|
|
change.New.Name = "v1.0.0"
|
|
|
|
change.New.Type = "tag"
|
|
|
|
|
|
|
|
hook := internal.PushHook{}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := convertPushHook(&hook, &change)
|
|
|
|
g.Assert(pipeline.Event).Equal(model.EventTag)
|
|
|
|
g.Assert(pipeline.Ref).Equal("refs/tags/v1.0.0")
|
2016-04-30 08:00:39 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|