mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-26 11:51:02 +00:00
support deactivate method and refactor file method
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
7f072e331a
commit
25ddaf858b
7 changed files with 110 additions and 20 deletions
|
@ -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": {
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
36
vendor/code.gitea.io/sdk/gitea/hook.go
generated
vendored
36
vendor/code.gitea.io/sdk/gitea/hook.go
generated
vendored
|
@ -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, "", " ")
|
||||
}
|
||||
|
|
3
vendor/code.gitea.io/sdk/gitea/repo.go
generated
vendored
3
vendor/code.gitea.io/sdk/gitea/repo.go
generated
vendored
|
@ -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"`
|
||||
|
|
6
vendor/vendor.json
vendored
6
vendor/vendor.json
vendored
|
@ -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=",
|
||||
|
|
Loading…
Reference in a new issue