From 2e709fb6a994432d4804f81cc9a15f569678cb09 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 11 May 2016 12:32:18 -0700 Subject: [PATCH] ability to get team list from Gogs --- remote/gogs/gogs.go | 13 +++++++++++-- remote/gogs/gogs_test.go | 6 ++---- remote/gogs/helper.go | 8 ++++++++ remote/gogs/helper_test.go | 11 +++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/remote/gogs/gogs.go b/remote/gogs/gogs.go index b4306a827..db467964c 100644 --- a/remote/gogs/gogs.go +++ b/remote/gogs/gogs.go @@ -113,8 +113,17 @@ func (c *client) Auth(token, secret string) (string, error) { // Teams is not supported by the Gogs driver. func (c *client) Teams(u *model.User) ([]*model.Team, error) { - var empty []*model.Team - return empty, nil + client := c.newClientToken(u.Token) + orgs, err := client.ListMyOrgs() + if err != nil { + return nil, err + } + + var teams []*model.Team + for _, org := range orgs { + teams = append(teams, toTeam(org, c.URL)) + } + return teams, nil } // Repo returns the named Gogs repository. diff --git a/remote/gogs/gogs_test.go b/remote/gogs/gogs_test.go index 792935956..1d88d1302 100644 --- a/remote/gogs/gogs_test.go +++ b/remote/gogs/gogs_test.go @@ -143,13 +143,11 @@ func Test_gogs(t *testing.T) { g.It("Should return no-op for usupporeted features", func() { _, err1 := c.Auth("octocat", "4vyW6b49Z") - _, err2 := c.Teams(nil) - err3 := c.Status(nil, nil, nil, "") - err4 := c.Deactivate(nil, nil, "") + err2 := c.Status(nil, nil, nil, "") + err3 := c.Deactivate(nil, nil, "") g.Assert(err1 != nil).IsTrue() g.Assert(err2 == nil).IsTrue() g.Assert(err3 == nil).IsTrue() - g.Assert(err4 == nil).IsTrue() }) }) } diff --git a/remote/gogs/helper.go b/remote/gogs/helper.go index c6941faa3..32b1fb52b 100644 --- a/remote/gogs/helper.go +++ b/remote/gogs/helper.go @@ -56,6 +56,14 @@ func toPerm(from gogs.Permission) *model.Perm { } } +// helper function that converts a Gogs team to a Drone team. +func toTeam(from *gogs.Organization, link string) *model.Team { + return &model.Team{ + Login: from.UserName, + Avatar: expandAvatar(link, from.AvatarUrl), + } +} + // helper function that extracts the Build data from a Gogs push hook func buildFromPush(hook *pushHook) *model.Build { avatar := expandAvatar( diff --git a/remote/gogs/helper_test.go b/remote/gogs/helper_test.go index 9ec3d635b..c203a8561 100644 --- a/remote/gogs/helper_test.go +++ b/remote/gogs/helper_test.go @@ -76,6 +76,17 @@ func Test_parse(t *testing.T) { } }) + g.It("Should return a Team struct from a Gogs Org", func() { + from := &gogs.Organization{ + UserName: "drone", + AvatarUrl: "/avatars/1", + } + + to := toTeam(from, "http://localhost:80") + g.Assert(to.Login).Equal(from.UserName) + g.Assert(to.Avatar).Equal("http://localhost:80/avatars/1") + }) + g.It("Should return a Repo struct from a Gogs Repo", func() { from := gogs.Repository{ FullName: "gophers/hello-world",