Migrate away from goblin (#4624)

Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: Robert Kaussow <mail@thegeeklab.de>
This commit is contained in:
qwerty287
2024-12-30 08:08:53 +02:00
committed by GitHub
parent 9325e9e70b
commit afa6dee30b
31 changed files with 3030 additions and 3665 deletions

View File

@@ -18,280 +18,261 @@ package github
import (
"testing"
"github.com/franela/goblin"
"github.com/google/go-github/v68/github"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/v3/server/model"
)
func Test_helper(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("GitHub converter", func() {
g.It("should convert passing status", func() {
g.Assert(convertStatus(model.StatusSuccess)).Equal(statusSuccess)
})
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))
}
g.It("should convert pending status", func() {
g.Assert(convertStatus(model.StatusPending)).Equal(statusPending)
g.Assert(convertStatus(model.StatusRunning)).Equal(statusPending)
})
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))
}
g.It("should convert failing status", func() {
g.Assert(convertStatus(model.StatusFailure)).Equal(statusFailure)
})
g.It("should convert error status", func() {
g.Assert(convertStatus(model.StatusKilled)).Equal(statusError)
g.Assert(convertStatus(model.StatusError)).Equal(statusError)
})
g.It("should convert passing desc", func() {
g.Assert(convertDesc(model.StatusSuccess)).Equal(descSuccess)
})
g.It("should convert pending desc", func() {
g.Assert(convertDesc(model.StatusPending)).Equal(descPending)
g.Assert(convertDesc(model.StatusRunning)).Equal(descPending)
})
g.It("should convert failing desc", func() {
g.Assert(convertDesc(model.StatusFailure)).Equal(descFailure)
})
g.It("should convert error desc", func() {
g.Assert(convertDesc(model.StatusKilled)).Equal(descError)
g.Assert(convertDesc(model.StatusError)).Equal(descError)
})
g.It("should convert repository list", func() {
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)
g.Assert(to[0].Avatar).Equal("http://...")
g.Assert(to[0].FullName).Equal("octocat/hello-world")
g.Assert(to[0].Owner).Equal("octocat")
g.Assert(to[0].Name).Equal("hello-world")
})
g.It("should convert repository", func() {
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)
g.Assert(to.Avatar).Equal("http://...")
g.Assert(to.FullName).Equal("octocat/hello-world")
g.Assert(to.Owner).Equal("octocat")
g.Assert(to.Name).Equal("hello-world")
g.Assert(to.Branch).Equal("develop")
g.Assert(to.IsSCMPrivate).IsTrue()
g.Assert(to.Clone).Equal("https://github.com/octocat/hello-world.git")
g.Assert(to.ForgeURL).Equal("https://github.com/octocat/hello-world")
})
g.It("should convert repository permissions", func() {
from := &github.Repository{
Permissions: map[string]bool{
"admin": true,
"push": true,
"pull": true,
},
}
to := convertPerm(from.GetPermissions())
g.Assert(to.Push).IsTrue()
g.Assert(to.Pull).IsTrue()
g.Assert(to.Admin).IsTrue()
})
g.It("should convert team", func() {
from := &github.Organization{
Login: github.Ptr("octocat"),
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://..."),
}
to := convertTeam(from)
g.Assert(to.Login).Equal("octocat")
g.Assert(to.Avatar).Equal("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,
},
},
}
g.It("should convert team list", func() {
from := []*github.Organization{
{
Login: github.Ptr("octocat"),
AvatarURL: github.Ptr("http://..."),
},
}
to := convertTeamList(from)
g.Assert(to[0].Login).Equal("octocat")
g.Assert(to[0].Avatar).Equal("http://...")
})
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)
}
g.It("should convert a repository from webhook", func() {
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")
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,
},
}
repo := convertRepoHook(from)
g.Assert(repo.Owner).Equal(*from.Owner.Login)
g.Assert(repo.Name).Equal(*from.Name)
g.Assert(repo.FullName).Equal(*from.FullName)
g.Assert(repo.IsSCMPrivate).Equal(*from.Private)
g.Assert(repo.ForgeURL).Equal(*from.HTMLURL)
g.Assert(repo.Clone).Equal(*from.CloneURL)
g.Assert(repo.Branch).Equal(*from.DefaultBranch)
})
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)
}
g.It("should convert a pull request from webhook", func() {
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)
g.Assert(err).IsNil()
g.Assert(pull).IsNotNil()
g.Assert(pipeline.Event).Equal(model.EventPull)
g.Assert(pipeline.Branch).Equal(*from.PullRequest.Base.Ref)
g.Assert(pipeline.Ref).Equal("refs/pull/42/merge")
g.Assert(pipeline.Refspec).Equal("changes:main")
g.Assert(pipeline.Commit).Equal(*from.PullRequest.Head.SHA)
g.Assert(pipeline.Message).Equal(*from.PullRequest.Title)
g.Assert(pipeline.Title).Equal(*from.PullRequest.Title)
g.Assert(pipeline.Author).Equal(*from.PullRequest.User.Login)
g.Assert(pipeline.Avatar).Equal(*from.PullRequest.User.AvatarURL)
g.Assert(pipeline.Sender).Equal(*from.Sender.Login)
})
func Test_convertPerm(t *testing.T) {
from := &github.Repository{
Permissions: map[string]bool{
"admin": true,
"push": true,
"pull": true,
},
}
g.It("should convert a deployment from webhook", func() {
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")
to := convertPerm(from.GetPermissions())
assert.True(t, to.Push)
assert.True(t, to.Pull)
assert.True(t, to.Admin)
}
_, pipeline := parseDeployHook(from)
g.Assert(pipeline.Event).Equal(model.EventDeploy)
g.Assert(pipeline.Branch).Equal("main")
g.Assert(pipeline.Ref).Equal("refs/heads/main")
g.Assert(pipeline.Commit).Equal(*from.Deployment.SHA)
g.Assert(pipeline.Message).Equal(*from.Deployment.Description)
g.Assert(pipeline.ForgeURL).Equal(*from.Deployment.URL)
g.Assert(pipeline.Author).Equal(*from.Sender.Login)
g.Assert(pipeline.Avatar).Equal(*from.Sender.AvatarURL)
})
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)
}
g.It("should convert a push from webhook", func() {
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")
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)
}
_, pipeline := parsePushHook(from)
g.Assert(pipeline.Event).Equal(model.EventPush)
g.Assert(pipeline.Branch).Equal("main")
g.Assert(pipeline.Ref).Equal("refs/heads/main")
g.Assert(pipeline.Commit).Equal(*from.HeadCommit.ID)
g.Assert(pipeline.Message).Equal(*from.HeadCommit.Message)
g.Assert(pipeline.ForgeURL).Equal(*from.HeadCommit.URL)
g.Assert(pipeline.Author).Equal(*from.Sender.Login)
g.Assert(pipeline.Avatar).Equal(*from.Sender.AvatarURL)
g.Assert(pipeline.Email).Equal(*from.HeadCommit.Author.Email)
})
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")
g.It("should convert a tag from webhook", func() {
from := &github.PushEvent{}
from.Ref = github.Ptr("refs/tags/v1.0.0")
_, pipeline := parsePushHook(from)
g.Assert(pipeline.Event).Equal(model.EventTag)
g.Assert(pipeline.Ref).Equal("refs/tags/v1.0.0")
})
g.It("should convert tag's base branch from webhook to pipeline's branch ", func() {
from := &github.PushEvent{}
from.Ref = github.Ptr("refs/tags/v1.0.0")
from.BaseRef = github.Ptr("refs/heads/main")
_, pipeline := parsePushHook(from)
g.Assert(pipeline.Event).Equal(model.EventTag)
g.Assert(pipeline.Branch).Equal("main")
})
g.It("should not convert tag's base_ref from webhook if not prefixed with 'ref/heads/'", func() {
from := &github.PushEvent{}
from.Ref = github.Ptr("refs/tags/v1.0.0")
from.BaseRef = github.Ptr("refs/refs/main")
_, pipeline := parsePushHook(from)
g.Assert(pipeline.Event).Equal(model.EventTag)
g.Assert(pipeline.Branch).Equal("refs/tags/v1.0.0")
})
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)
})
}

View File

@@ -20,13 +20,28 @@ import (
"net/http/httptest"
"testing"
"github.com/franela/goblin"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/v3/server/forge/github/fixtures"
"go.woodpecker-ci.org/woodpecker/v3/server/model"
)
func TestNew(t *testing.T) {
forge, _ := New(Opts{
URL: "http://localhost:8080/",
Client: "0ZXh0IjoiI",
Secret: "I1NiIsInR5",
SkipVerify: true,
})
f, _ := forge.(*client)
assert.Equal(t, "http://localhost:8080", f.url)
assert.Equal(t, "http://localhost:8080/api/v3/", f.API)
assert.Equal(t, "0ZXh0IjoiI", f.Client)
assert.Equal(t, "I1NiIsInR5", f.Secret)
assert.True(t, f.SkipVerify)
}
func Test_github(t *testing.T) {
gin.SetMode(gin.TestMode)
@@ -36,80 +51,39 @@ func Test_github(t *testing.T) {
SkipVerify: true,
})
defer s.Close()
ctx := context.Background()
g := goblin.Goblin(t)
g.Describe("GitHub", func() {
g.After(func() {
s.Close()
})
g.Describe("Creating a forge", func() {
g.It("Should return client with specified options", func() {
forge, _ := New(Opts{
URL: "http://localhost:8080/",
Client: "0ZXh0IjoiI",
Secret: "I1NiIsInR5",
SkipVerify: true,
})
f, _ := forge.(*client)
g.Assert(f.url).Equal("http://localhost:8080")
g.Assert(f.API).Equal("http://localhost:8080/api/v3/")
g.Assert(f.Client).Equal("0ZXh0IjoiI")
g.Assert(f.Secret).Equal("I1NiIsInR5")
g.Assert(f.SkipVerify).Equal(true)
})
})
t.Run("netrc with user token", func(t *testing.T) {
forge, _ := New(Opts{})
netrc, _ := forge.Netrc(fakeUser, fakeRepo)
assert.Equal(t, "github.com", netrc.Machine)
assert.Equal(t, fakeUser.AccessToken, netrc.Login)
assert.Equal(t, "x-oauth-basic", netrc.Password)
})
t.Run("netrc with machine account", func(t *testing.T) {
forge, _ := New(Opts{})
netrc, _ := forge.Netrc(nil, fakeRepo)
assert.Equal(t, "github.com", netrc.Machine)
assert.Empty(t, netrc.Login)
assert.Empty(t, netrc.Password)
})
g.Describe("Generating a netrc file", func() {
g.It("Should return a netrc with the user token", func() {
forge, _ := New(Opts{})
netrc, _ := forge.Netrc(fakeUser, fakeRepo)
g.Assert(netrc.Machine).Equal("github.com")
g.Assert(netrc.Login).Equal(fakeUser.AccessToken)
g.Assert(netrc.Password).Equal("x-oauth-basic")
})
g.It("Should return a netrc with the machine account", func() {
forge, _ := New(Opts{})
netrc, _ := forge.Netrc(nil, fakeRepo)
g.Assert(netrc.Machine).Equal("github.com")
g.Assert(netrc.Login).Equal("")
g.Assert(netrc.Password).Equal("")
})
})
g.Describe("Requesting a repository", func() {
g.It("Should return the repository details", func() {
repo, err := c.Repo(ctx, fakeUser, fakeRepo.ForgeRemoteID, fakeRepo.Owner, fakeRepo.Name)
g.Assert(err).IsNil()
g.Assert(repo.ForgeRemoteID).Equal(fakeRepo.ForgeRemoteID)
g.Assert(repo.Owner).Equal(fakeRepo.Owner)
g.Assert(repo.Name).Equal(fakeRepo.Name)
g.Assert(repo.FullName).Equal(fakeRepo.FullName)
g.Assert(repo.IsSCMPrivate).IsTrue()
g.Assert(repo.Clone).Equal(fakeRepo.Clone)
g.Assert(repo.ForgeURL).Equal(fakeRepo.ForgeURL)
})
g.It("Should handle a not found error", func() {
_, err := c.Repo(ctx, fakeUser, "0", fakeRepoNotFound.Owner, fakeRepoNotFound.Name)
g.Assert(err).IsNotNil()
})
})
g.It("Should return a user repository list")
g.It("Should return a user team list")
g.It("Should register repository hooks")
g.It("Should return a repository file")
g.Describe("Given an authentication request", func() {
g.It("Should redirect to the GitHub login page")
g.It("Should create an access token")
g.It("Should handle an access token error")
g.It("Should return the authenticated user")
g.It("Should handle authentication errors")
})
t.Run("Should return the repository details", func(t *testing.T) {
repo, err := c.Repo(ctx, fakeUser, fakeRepo.ForgeRemoteID, fakeRepo.Owner, fakeRepo.Name)
assert.NoError(t, err)
assert.Equal(t, fakeRepo.ForgeRemoteID, repo.ForgeRemoteID)
assert.Equal(t, fakeRepo.Owner, repo.Owner)
assert.Equal(t, fakeRepo.Name, repo.Name)
assert.Equal(t, fakeRepo.FullName, repo.FullName)
assert.True(t, repo.IsSCMPrivate)
assert.Equal(t, fakeRepo.Clone, repo.Clone)
assert.Equal(t, fakeRepo.ForgeURL, repo.ForgeURL)
})
t.Run("repo not found error", func(t *testing.T) {
_, err := c.Repo(ctx, fakeUser, "0", fakeRepoNotFound.Owner, fakeRepoNotFound.Name)
assert.Error(t, err)
})
}

View File

@@ -22,7 +22,6 @@ import (
"strings"
"testing"
"github.com/franela/goblin"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/v3/server/forge/github/fixtures"
@@ -46,96 +45,85 @@ func testHookRequest(payload []byte, event string) *http.Request {
return req
}
func Test_parser(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("GitHub parser", func() {
g.It("should ignore unsupported hook events", func() {
req := testHookRequest([]byte(fixtures.HookPullRequest), "issues")
p, r, b, err := parseHook(req, false)
g.Assert(r).IsNil()
g.Assert(b).IsNil()
g.Assert(p).IsNil()
assert.ErrorIs(t, err, &types.ErrIgnoreEvent{})
})
func Test_parseHook(t *testing.T) {
t.Run("ignore unsupported hook events", func(t *testing.T) {
req := testHookRequest([]byte(fixtures.HookPullRequest), "issues")
p, r, b, err := parseHook(req, false)
assert.Nil(t, r)
assert.Nil(t, b)
assert.Nil(t, p)
assert.ErrorIs(t, err, &types.ErrIgnoreEvent{})
})
g.Describe("given a push hook", func() {
g.It("should skip when action is deleted", func() {
req := testHookRequest([]byte(fixtures.HookPushDeleted), hookPush)
p, r, b, err := parseHook(req, false)
g.Assert(r).IsNil()
g.Assert(b).IsNil()
g.Assert(err).IsNil()
g.Assert(p).IsNil()
})
g.It("should extract repository and pipeline details", func() {
req := testHookRequest([]byte(fixtures.HookPush), hookPush)
p, r, b, err := parseHook(req, false)
g.Assert(err).IsNil()
g.Assert(p).IsNil()
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(b.Event).Equal(model.EventPush)
sort.Strings(b.ChangedFiles)
g.Assert(b.ChangedFiles).Equal([]string{"pipeline/shared/replace_secrets.go", "pipeline/shared/replace_secrets_test.go"})
})
})
t.Run("skip skip push hook when action is deleted", func(t *testing.T) {
req := testHookRequest([]byte(fixtures.HookPushDeleted), hookPush)
p, r, b, err := parseHook(req, false)
assert.Nil(t, r)
assert.Nil(t, b)
assert.NoError(t, err)
assert.Nil(t, p)
})
t.Run("push hook", func(t *testing.T) {
req := testHookRequest([]byte(fixtures.HookPush), hookPush)
p, r, b, err := parseHook(req, false)
assert.NoError(t, err)
assert.Nil(t, p)
assert.NotNil(t, r)
assert.NotNil(t, b)
assert.Equal(t, model.EventPush, b.Event)
sort.Strings(b.ChangedFiles)
assert.Equal(t, []string{"pipeline/shared/replace_secrets.go", "pipeline/shared/replace_secrets_test.go"}, b.ChangedFiles)
})
g.Describe("given a pull request hook", func() {
g.It("should handle a PR hook when PR got opened or pushed to", func() {
req := testHookRequest([]byte(fixtures.HookPullRequest), hookPull)
p, r, b, err := parseHook(req, false)
g.Assert(err).IsNil()
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(p).IsNotNil()
g.Assert(b.Event).Equal(model.EventPull)
})
g.It("should handle a PR closed hook when PR got closed", func() {
req := testHookRequest([]byte(fixtures.HookPullRequestClosed), hookPull)
p, r, b, err := parseHook(req, false)
g.Assert(err).IsNil()
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(p).IsNotNil()
g.Assert(b.Event).Equal(model.EventPullClosed)
})
g.It("should handle a PR closed hook when PR got merged", func() {
req := testHookRequest([]byte(fixtures.HookPullRequestMerged), hookPull)
p, r, b, err := parseHook(req, false)
g.Assert(err).IsNil()
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(p).IsNotNil()
g.Assert(b.Event).Equal(model.EventPullClosed)
})
})
t.Run("PR hook", func(t *testing.T) {
req := testHookRequest([]byte(fixtures.HookPullRequest), hookPull)
p, r, b, err := parseHook(req, false)
assert.NoError(t, err)
assert.NotNil(t, r)
assert.NotNil(t, b)
assert.NotNil(t, p)
assert.Equal(t, model.EventPull, b.Event)
})
t.Run("PR closed hook", func(t *testing.T) {
req := testHookRequest([]byte(fixtures.HookPullRequestClosed), hookPull)
p, r, b, err := parseHook(req, false)
assert.NoError(t, err)
assert.NotNil(t, r)
assert.NotNil(t, b)
assert.NotNil(t, p)
assert.Equal(t, model.EventPullClosed, b.Event)
})
t.Run("PR merged hook", func(t *testing.T) {
req := testHookRequest([]byte(fixtures.HookPullRequestMerged), hookPull)
p, r, b, err := parseHook(req, false)
assert.NoError(t, err)
assert.NotNil(t, r)
assert.NotNil(t, b)
assert.NotNil(t, p)
assert.Equal(t, model.EventPullClosed, b.Event)
})
g.Describe("given a deployment hook", func() {
g.It("should extract repository and pipeline details", func() {
req := testHookRequest([]byte(fixtures.HookDeploy), hookDeploy)
p, r, b, err := parseHook(req, false)
g.Assert(err).IsNil()
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(p).IsNil()
g.Assert(b.Event).Equal(model.EventDeploy)
g.Assert(b.DeployTo).Equal("production")
g.Assert(b.DeployTask).Equal("deploy")
})
})
t.Run("deploy hook", func(t *testing.T) {
req := testHookRequest([]byte(fixtures.HookDeploy), hookDeploy)
p, r, b, err := parseHook(req, false)
assert.NoError(t, err)
assert.NotNil(t, r)
assert.NotNil(t, b)
assert.Nil(t, p)
assert.Equal(t, model.EventDeploy, b.Event)
assert.Equal(t, "production", b.DeployTo)
assert.Equal(t, "deploy", b.DeployTask)
})
g.Describe("given a release hook", func() {
g.It("should extract repository and build details", func() {
req := testHookRequest([]byte(fixtures.HookRelease), hookRelease)
p, r, b, err := parseHook(req, false)
g.Assert(err).IsNil()
g.Assert(r).IsNotNil()
g.Assert(b).IsNotNil()
g.Assert(p).IsNil()
g.Assert(b.Event).Equal(model.EventRelease)
g.Assert(len(strings.Split(b.Ref, "/")) == 3).IsTrue()
g.Assert(strings.HasPrefix(b.Ref, "refs/tags/")).IsTrue()
})
})
t.Run("release hook", func(t *testing.T) {
req := testHookRequest([]byte(fixtures.HookRelease), hookRelease)
p, r, b, err := parseHook(req, false)
assert.NoError(t, err)
assert.NotNil(t, r)
assert.NotNil(t, b)
assert.Nil(t, p)
assert.Equal(t, model.EventRelease, b.Event)
assert.Len(t, strings.Split(b.Ref, "/"), 3)
assert.True(t, strings.HasPrefix(b.Ref, "refs/tags/"))
})
}