From 25ddaf858b286e98f3e158ebcfb4a4ee97e1cad8 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 24 May 2017 10:02:11 +0800 Subject: [PATCH] support deactivate method and refactor file method Signed-off-by: Bo-Yi Wu --- remote/gitea/fixtures/handler.go | 23 +++++++++++++++ remote/gitea/fixtures/hooks.go | 10 +++---- remote/gitea/gitea.go | 40 ++++++++++++++++++++++---- remote/gitea/gitea_test.go | 12 ++++---- vendor/code.gitea.io/sdk/gitea/hook.go | 36 +++++++++++++++++++++++ vendor/code.gitea.io/sdk/gitea/repo.go | 3 ++ vendor/vendor.json | 6 ++-- 7 files changed, 110 insertions(+), 20 deletions(-) diff --git a/remote/gitea/fixtures/handler.go b/remote/gitea/fixtures/handler.go index 94e0d3dc3..ca6d319af 100644 --- a/remote/gitea/fixtures/handler.go +++ b/remote/gitea/fixtures/handler.go @@ -15,12 +15,18 @@ func Handler() http.Handler { e.GET("/api/v1/repos/:owner/:name", getRepo) e.GET("/api/v1/repos/:owner/:name/raw/:commit/:file", getRepoFile) e.POST("/api/v1/repos/:owner/:name/hooks", createRepoHook) + e.GET("/api/v1/repos/:owner/:name/hooks", listRepoHooks) + e.DELETE("/api/v1/repos/:owner/:name/hooks/:id", deleteRepoHook) e.POST("/api/v1/repos/:owner/:name/statuses/:commit", createRepoCommitStatus) e.GET("/api/v1/user/repos", getUserRepos) return e } +func listRepoHooks(c *gin.Context) { + c.String(200, listRepoHookPayloads) +} + func getRepo(c *gin.Context) { switch c.Param("name") { case "repo_not_found": @@ -66,6 +72,10 @@ func createRepoHook(c *gin.Context) { c.String(200, "{}") } +func deleteRepoHook(c *gin.Context) { + c.String(200, "{}") +} + func getUserRepos(c *gin.Context) { switch c.Request.Header.Get("Authorization") { case "token repos_not_found": @@ -75,6 +85,19 @@ func getUserRepos(c *gin.Context) { } } +const listRepoHookPayloads = ` +[ + { + "id": 1, + "type": "gitea", + "config": { + "content_type": "json", + "url": "http:\/\/localhost\/hook?access_token=1234567890" + } + } +] +` + const repoPayload = ` { "owner": { diff --git a/remote/gitea/fixtures/hooks.go b/remote/gitea/fixtures/hooks.go index fe693f7d4..b8ab4d6ad 100644 --- a/remote/gitea/fixtures/hooks.go +++ b/remote/gitea/fixtures/hooks.go @@ -1,7 +1,7 @@ package fixtures -// Sample Gitea push hook -var HookPush = ` +// HookPush is a sample Gitea push hook +const HookPush = ` { "ref": "refs/heads/master", "before": "4b2626259b5a97b6b4eab5e6cca66adb986b672b", @@ -49,8 +49,8 @@ var HookPush = ` } ` -// Sample Gitea tag hook -var HookPushTag = `{ +// HookPushTag is a sample Gitea tag hook +const HookPushTag = `{ "secret": "l26Un7G7HXogLAvsyf2hOA4EMARSTsR3", "ref": "v1.0.0", "ref_type": "tag", @@ -85,7 +85,7 @@ var HookPushTag = `{ }` // HookPullRequest is a sample pull_request webhook payload -var HookPullRequest = `{ +const HookPullRequest = `{ "action": "opened", "number": 1, "pull_request": { diff --git a/remote/gitea/gitea.go b/remote/gitea/gitea.go index 22da5043d..29fd80ef7 100644 --- a/remote/gitea/gitea.go +++ b/remote/gitea/gitea.go @@ -249,10 +249,10 @@ func (c *client) Status(u *model.User, r *model.Repo, b *model.Build, link strin r.Name, b.Commit, gitea.CreateStatusOption{ - status, - link, - desc, - "", + State: status, + TargetURL: link, + Description: desc, + Context: "", }, ) @@ -297,8 +297,21 @@ func (c *client) Activate(u *model.User, r *model.Repo, link string) error { return err } -// Deactivate is not supported by the Gitea driver. +// Deactivate deactives the repository be removing repository push hooks from +// the Gitea repository. func (c *client) Deactivate(u *model.User, r *model.Repo, link string) error { + client := c.newClientToken(u.Token) + + hooks, err := client.ListRepoHooks(r.Owner, r.Name) + if err != nil { + return err + } + + hook := matchingHooks(hooks, link) + if hook != nil { + return client.DeleteRepoHook(r.Owner, r.Name, hook.ID) + } + return nil } @@ -325,3 +338,20 @@ func (c *client) newClientToken(token string) *gitea.Client { } return client } + +// helper function to return matching hooks. +func matchingHooks(hooks []*gitea.Hook, rawurl string) *gitea.Hook { + link, err := url.Parse(rawurl) + if err != nil { + return nil + } + for _, hook := range hooks { + if val, ok := hook.Config["url"]; ok { + hookurl, err := url.Parse(val) + if err == nil && hookurl.Host == link.Host { + return hook + } + } + } + return nil +} diff --git a/remote/gitea/gitea_test.go b/remote/gitea/gitea_test.go index 1b427be3b..15f00e0a7 100644 --- a/remote/gitea/gitea_test.go +++ b/remote/gitea/gitea_test.go @@ -121,6 +121,11 @@ func Test_gitea(t *testing.T) { g.Assert(err == nil).IsTrue() }) + g.It("Should remove repository hooks", func() { + err := c.Deactivate(fakeUser, fakeRepo, "http://localhost") + g.Assert(err == nil).IsTrue() + }) + g.It("Should return a repository file", func() { raw, err := c.File(fakeUser, fakeRepo, fakeBuild, ".drone.yml") g.Assert(err == nil).IsTrue() @@ -144,13 +149,6 @@ func Test_gitea(t *testing.T) { g.It("Should return push details") g.It("Should handle a parsing error") }) - - g.It("Should return no-op for usupporeted features", func() { - _, err1 := c.Auth("octocat", "4vyW6b49Z") - err2 := c.Deactivate(nil, nil, "") - g.Assert(err1 != nil).IsTrue() - g.Assert(err2 == nil).IsTrue() - }) }) } diff --git a/vendor/code.gitea.io/sdk/gitea/hook.go b/vendor/code.gitea.io/sdk/gitea/hook.go index 4b4506812..f93a8ba46 100644 --- a/vendor/code.gitea.io/sdk/gitea/hook.go +++ b/vendor/code.gitea.io/sdk/gitea/hook.go @@ -353,3 +353,39 @@ func (p *PullRequestPayload) SetSecret(secret string) { func (p *PullRequestPayload) JSONPayload() ([]byte, error) { return json.MarshalIndent(p, "", " ") } + +//__________ .__ __ +//\______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__. +// | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | | +// | | \ ___/| |_> > <_> )___ \| || | ( <_> ) | \/\___ | +// |____|_ /\___ > __/ \____/____ >__||__| \____/|__| / ____| +// \/ \/|__| \/ \/ + +// HookRepoAction an action that happens to a repo +type HookRepoAction string + +const ( + // HookRepoCreated created + HookRepoCreated HookRepoAction = "created" + // HookRepoDeleted deleted + HookRepoDeleted HookRepoAction = "deleted" +) + +// RepositoryPayload payload for repository webhooks +type RepositoryPayload struct { + Secret string `json:"secret"` + Action HookRepoAction `json:"action"` + Repository *Repository `json:"repository"` + Organization *User `json:"organization"` + Sender *User `json:"sender"` +} + +// SetSecret set the payload's secret +func (p *RepositoryPayload) SetSecret(secret string) { + p.Secret = secret +} + +// JSONPayload JSON representation of the payload +func (p *RepositoryPayload) JSONPayload() ([]byte, error) { + return json.MarshalIndent(p, "", " ") +} diff --git a/vendor/code.gitea.io/sdk/gitea/repo.go b/vendor/code.gitea.io/sdk/gitea/repo.go index 17ad0ac4b..7e997cd5d 100644 --- a/vendor/code.gitea.io/sdk/gitea/repo.go +++ b/vendor/code.gitea.io/sdk/gitea/repo.go @@ -26,9 +26,12 @@ type Repository struct { Name string `json:"name"` FullName string `json:"full_name"` Description string `json:"description"` + Empty bool `json:"empty"` Private bool `json:"private"` Fork bool `json:"fork"` + Parent *Repository `json:"parent"` Mirror bool `json:"mirror"` + Size int `json:"size"` HTMLURL string `json:"html_url"` SSHURL string `json:"ssh_url"` CloneURL string `json:"clone_url"` diff --git a/vendor/vendor.json b/vendor/vendor.json index 9a8546113..34d812257 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -7,10 +7,10 @@ "revision": "" }, { - "checksumSHA1": "Vrt1uoeOOk9fp1aa9d7huQhzexU=", + "checksumSHA1": "nLhT+bLMj8uLICP+EZbrdoQe6mM=", "path": "code.gitea.io/sdk/gitea", - "revision": "e1c76d42f2b84aa49af5b7fc6564e4b5c2814f2f", - "revisionTime": "2017-05-02T15:01:11Z" + "revision": "8cff72208aa458f4efa8fdfbad29b03aee485b8c", + "revisionTime": "2017-05-06T01:37:21Z" }, { "checksumSHA1": "zTn0jzjOiJlScR1px66MvrgrlLs=",