From 67cdbd250994be8b72f8587ca2c3c8efa962846e Mon Sep 17 00:00:00 2001 From: Laszlo Fogas Date: Mon, 3 Jun 2019 08:49:11 +0200 Subject: [PATCH] Simplified the interface --- remote/bitbucket/bitbucket.go | 7 +------ remote/bitbucketserver/bitbucketserver.go | 6 ------ remote/coding/coding.go | 10 ---------- remote/coding/coding_test.go | 5 ----- remote/gerrit/gerrit.go | 5 ----- remote/gitea/gitea.go | 5 ----- remote/github/github.go | 7 +------ remote/gitlab/gitlab.go | 11 +++-------- remote/gitlab3/gitlab.go | 15 --------------- remote/gogs/gogs.go | 7 +------ remote/mock/remote.go | 23 ----------------------- remote/remote.go | 17 ----------------- 12 files changed, 6 insertions(+), 112 deletions(-) diff --git a/remote/bitbucket/bitbucket.go b/remote/bitbucket/bitbucket.go index ebef2f011..5a61eab63 100644 --- a/remote/bitbucket/bitbucket.go +++ b/remote/bitbucket/bitbucket.go @@ -202,12 +202,7 @@ func (c *config) Perm(u *model.User, owner, name string) (*model.Perm, error) { // File fetches the file from the Bitbucket repository and returns its contents. func (c *config) File(u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error) { - return c.FileRef(u, r, b.Commit, f) -} - -// FileRef fetches the file from the Bitbucket repository and returns its contents. -func (c *config) FileRef(u *model.User, r *model.Repo, ref, f string) ([]byte, error) { - config, err := c.newClient(u).FindSource(r.Owner, r.Name, ref, f) + config, err := c.newClient(u).FindSource(r.Owner, r.Name, b.Commit, f) if err != nil { return nil, err } diff --git a/remote/bitbucketserver/bitbucketserver.go b/remote/bitbucketserver/bitbucketserver.go index 3fe2cd3d2..0b394821f 100644 --- a/remote/bitbucketserver/bitbucketserver.go +++ b/remote/bitbucketserver/bitbucketserver.go @@ -179,12 +179,6 @@ func (c *Config) File(u *model.User, r *model.Repo, b *model.Build, f string) ([ return client.FindFileForRepo(r.Owner, r.Name, f, b.Ref) } -func (c *Config) FileRef(u *model.User, r *model.Repo, ref, f string) ([]byte, error) { - client := internal.NewClientWithToken(c.URL, c.Consumer, u.Token) - - return client.FindFileForRepo(r.Owner, r.Name, f, ref) -} - // Status is not supported by the bitbucketserver driver. func (c *Config) Status(u *model.User, r *model.Repo, b *model.Build, link string) error { status := internal.BuildStatus{ diff --git a/remote/coding/coding.go b/remote/coding/coding.go index 115f94498..1d2229b13 100644 --- a/remote/coding/coding.go +++ b/remote/coding/coding.go @@ -238,16 +238,6 @@ func (c *Coding) File(u *model.User, r *model.Repo, b *model.Build, f string) ([ return data, nil } -// FileRef fetches a file from the remote repository for the given ref -// and returns in string format. -func (c *Coding) FileRef(u *model.User, r *model.Repo, ref, f string) ([]byte, error) { - data, err := c.newClient(u).GetFile(r.Owner, r.Name, ref, f) - if err != nil { - return nil, err - } - return data, nil -} - // Status sends the commit status to the remote system. func (c *Coding) Status(u *model.User, r *model.Repo, b *model.Build, link string) error { // EMPTY: not implemented in Coding OAuth API diff --git a/remote/coding/coding_test.go b/remote/coding/coding_test.go index bf17180cf..f24f34d33 100644 --- a/remote/coding/coding_test.go +++ b/remote/coding/coding_test.go @@ -184,11 +184,6 @@ func Test_coding(t *testing.T) { g.Assert(err == nil).IsTrue() g.Assert(string(data)).Equal("pipeline:\n test:\n image: golang:1.6\n commands:\n - go test\n") }) - g.It("Should return file for specified ref", func() { - data, err := c.FileRef(fakeUser, fakeRepo, "master", ".drone.yml") - g.Assert(err == nil).IsTrue() - g.Assert(string(data)).Equal("pipeline:\n test:\n image: golang:1.6\n commands:\n - go test\n") - }) }) g.Describe("When requesting a netrc config", func() { diff --git a/remote/gerrit/gerrit.go b/remote/gerrit/gerrit.go index f0fc91da5..c8f926108 100644 --- a/remote/gerrit/gerrit.go +++ b/remote/gerrit/gerrit.go @@ -103,11 +103,6 @@ func (c *client) File(u *model.User, r *model.Repo, b *model.Build, f string) ([ return nil, nil } -// File is not supported by the Gerrit driver. -func (c *client) FileRef(u *model.User, r *model.Repo, ref, f string) ([]byte, error) { - return nil, nil -} - // Status is not supported by the Gogs driver. func (c *client) Status(u *model.User, r *model.Repo, b *model.Build, link string) error { return nil diff --git a/remote/gitea/gitea.go b/remote/gitea/gitea.go index f1cfe4c60..39a5105d6 100644 --- a/remote/gitea/gitea.go +++ b/remote/gitea/gitea.go @@ -249,11 +249,6 @@ func (c *client) File(u *model.User, r *model.Repo, b *model.Build, f string) ([ return cfg, err } -// FileRef fetches the file from the Gitea repository and returns its contents. -func (c *client) FileRef(u *model.User, r *model.Repo, ref, f string) ([]byte, error) { - return c.newClientToken(u.Token).GetFile(r.Owner, r.Name, ref, f) -} - // Status is supported by the Gitea driver. func (c *client) Status(u *model.User, r *model.Repo, b *model.Build, link string) error { client := c.newClientToken(u.Token) diff --git a/remote/github/github.go b/remote/github/github.go index 5c142e71b..1fd057892 100644 --- a/remote/github/github.go +++ b/remote/github/github.go @@ -225,15 +225,10 @@ func (c *client) Perm(u *model.User, owner, name string) (*model.Perm, error) { // File fetches the file from the GitHub repository and returns its contents. func (c *client) File(u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error) { - return c.FileRef(u, r, b.Commit, f) -} - -// FileRef fetches the file from the GitHub repository and returns its contents. -func (c *client) FileRef(u *model.User, r *model.Repo, ref, f string) ([]byte, error) { client := c.newClientToken(u.Token) opts := new(github.RepositoryContentGetOptions) - opts.Ref = ref + opts.Ref = b.Commit data, _, _, err := client.Repositories.GetContents(r.Owner, r.Name, f, opts) if err != nil { return nil, err diff --git a/remote/gitlab/gitlab.go b/remote/gitlab/gitlab.go index 318c83428..e61776b79 100644 --- a/remote/gitlab/gitlab.go +++ b/remote/gitlab/gitlab.go @@ -325,18 +325,13 @@ func (g *Gitlab) Perm(u *model.User, owner, name string) (*model.Perm, error) { // File fetches a file from the remote repository and returns in string format. func (g *Gitlab) File(user *model.User, repo *model.Repo, build *model.Build, f string) ([]byte, error) { - return g.FileRef(user, repo, build.Commit, f) -} - -// FileRef fetches the file from the GitHub repository and returns its contents. -func (g *Gitlab) FileRef(u *model.User, r *model.Repo, ref, f string) ([]byte, error) { - var client = NewClient(g.URL, u.Token, g.SkipVerify) - id, err := GetProjectId(g, client, r.Owner, r.Name) + var client = NewClient(g.URL, user.Token, g.SkipVerify) + id, err := GetProjectId(g, client, repo.Owner, repo.Name) if err != nil { return nil, err } - out, err := client.RepoRawFileRef(id, ref, f) + out, err := client.RepoRawFileRef(id, build.Commit, f) if err != nil { return nil, err } diff --git a/remote/gitlab3/gitlab.go b/remote/gitlab3/gitlab.go index cd7eb1f25..7a0dde3d8 100644 --- a/remote/gitlab3/gitlab.go +++ b/remote/gitlab3/gitlab.go @@ -338,21 +338,6 @@ func (g *Gitlab) File(user *model.User, repo *model.Repo, build *model.Build, f return out, err } -// FileRef fetches the file from the GitHub repository and returns its contents. -func (g *Gitlab) FileRef(u *model.User, r *model.Repo, ref, f string) ([]byte, error) { - var client = NewClient(g.URL, u.Token, g.SkipVerify) - id, err := GetProjectId(g, client, r.Owner, r.Name) - if err != nil { - return nil, err - } - - out, err := client.RepoRawFileRef(id, ref, f) - if err != nil { - return nil, err - } - return out, err -} - // NOTE Currently gitlab doesn't support status for commits and events, // also if we want get MR status in gitlab we need implement a special plugin for gitlab, // gitlab uses API to fetch build status on client side. But for now we skip this. diff --git a/remote/gogs/gogs.go b/remote/gogs/gogs.go index 285c80795..13072c0ca 100644 --- a/remote/gogs/gogs.go +++ b/remote/gogs/gogs.go @@ -22,9 +22,9 @@ import ( "net/url" "strings" + "github.com/gogits/go-gogs-client" "github.com/laszlocph/drone-oss-08/model" "github.com/laszlocph/drone-oss-08/remote" - "github.com/gogits/go-gogs-client" ) // Opts defines configuration options. @@ -202,11 +202,6 @@ func (c *client) File(u *model.User, r *model.Repo, b *model.Build, f string) ([ return cfg, err } -// FileRef fetches the file from the Gogs repository and returns its contents. -func (c *client) FileRef(u *model.User, r *model.Repo, ref, f string) ([]byte, error) { - return c.newClientToken(u.Token).GetFile(r.Owner, r.Name, ref, f) -} - // Status is not supported by the Gogs driver. func (c *client) Status(u *model.User, r *model.Repo, b *model.Build, link string) error { return nil diff --git a/remote/mock/remote.go b/remote/mock/remote.go index 96f5fba16..97577c73f 100644 --- a/remote/mock/remote.go +++ b/remote/mock/remote.go @@ -98,29 +98,6 @@ func (_m *Remote) File(u *model.User, r *model.Repo, b *model.Build, f string) ( return r0, r1 } -// FileRef provides a mock function with given fields: u, r, ref, f -func (_m *Remote) FileRef(u *model.User, r *model.Repo, ref string, f string) ([]byte, error) { - ret := _m.Called(u, r, ref, f) - - var r0 []byte - if rf, ok := ret.Get(0).(func(*model.User, *model.Repo, string, string) []byte); ok { - r0 = rf(u, r, ref, f) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]byte) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func(*model.User, *model.Repo, string, string) error); ok { - r1 = rf(u, r, ref, f) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - // Hook provides a mock function with given fields: r func (_m *Remote) Hook(r *http.Request) (*model.Repo, *model.Build, error) { ret := _m.Called(r) diff --git a/remote/remote.go b/remote/remote.go index e6dd43339..297a77aad 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -51,10 +51,6 @@ type Remote interface { // format. File(u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error) - // FileRef fetches a file from the remote repository for the given ref - // and returns in string format. - FileRef(u *model.User, r *model.Repo, ref, f string) ([]byte, error) - // Status sends the commit status to the remote system. // An example would be the GitHub pull request status. Status(u *model.User, r *model.Repo, b *model.Build, link string) error @@ -115,18 +111,6 @@ func Perm(c context.Context, u *model.User, owner, repo string) (*model.Perm, er return FromContext(c).Perm(u, owner, repo) } -// File fetches a file from the remote repository and returns in string format. -func File(c context.Context, u *model.User, r *model.Repo, b *model.Build, f string) (out []byte, err error) { - for i := 0; i < 12; i++ { - out, err = FromContext(c).File(u, r, b, f) - if err == nil { - return - } - time.Sleep(5 * time.Second) - } - return -} - // Status sends the commit status to the remote system. // An example would be the GitHub pull request status. func Status(c context.Context, u *model.User, r *model.Repo, b *model.Build, link string) error { @@ -170,7 +154,6 @@ func Refresh(c context.Context, u *model.User) (bool, error) { } // FileBackoff fetches the file using an exponential backoff. -// TODO replace this with a proper backoff func FileBackoff(remote Remote, u *model.User, r *model.Repo, b *model.Build, f string) (out []byte, err error) { for i := 0; i < 5; i++ { select {