diff options
author | Ethan Koenig <ethantkoenig@gmail.com> | 2018-04-28 23:21:33 -0700 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2018-04-29 14:21:33 +0800 |
commit | 7ea4bfc56172faf588142ee5637f35c0344f7534 (patch) | |
tree | f4d47e2d4209b90d340dead5e3abb7b4a02cb475 /routers/api | |
parent | 5a62eb30df3a04e76e465824f525b4ffd920b562 (diff) | |
download | gitea-7ea4bfc56172faf588142ee5637f35c0344f7534.tar.gz gitea-7ea4bfc56172faf588142ee5637f35c0344f7534.zip |
API endpoint for testing webhook (#3550)
* API endpoint for testing webhook
* Empty commit to rerun CI
Diffstat (limited to 'routers/api')
-rw-r--r-- | routers/api/v1/api.go | 9 | ||||
-rw-r--r-- | routers/api/v1/repo/hook.go | 58 | ||||
-rw-r--r-- | routers/api/v1/repo/hook_test.go | 33 | ||||
-rw-r--r-- | routers/api/v1/repo/main_test.go | 16 |
4 files changed, 112 insertions, 4 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 02606bdfd0..eec55cac67 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -382,9 +382,12 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/hooks", func() { m.Combo("").Get(repo.ListHooks). Post(bind(api.CreateHookOption{}), repo.CreateHook) - m.Combo("/:id").Get(repo.GetHook). - Patch(bind(api.EditHookOption{}), repo.EditHook). - Delete(repo.DeleteHook) + m.Group("/:id", func() { + m.Combo("").Get(repo.GetHook). + Patch(bind(api.EditHookOption{}), repo.EditHook). + Delete(repo.DeleteHook) + m.Post("/tests", context.RepoRef(), repo.TestHook) + }) }, reqToken(), reqRepoWriter()) m.Group("/collaborators", func() { m.Get("", repo.ListCollaborators) diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index 9c39094bae..e412a7f1f2 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -5,11 +5,11 @@ package repo import ( + "code.gitea.io/git" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/routers/api/v1/convert" "code.gitea.io/gitea/routers/api/v1/utils" - api "code.gitea.io/sdk/gitea" ) @@ -82,6 +82,62 @@ func GetHook(ctx *context.APIContext) { ctx.JSON(200, convert.ToHook(repo.RepoLink, hook)) } +// TestHook tests a hook +func TestHook(ctx *context.APIContext) { + // swagger:operation POST /repos/{owner}/{repo}/hooks/{id}/tests repository repoTestHook + // --- + // summary: Test a push webhook + // produces: + // - application/json + // parameters: + // - name: owner + // in: path + // description: owner of the repo + // type: string + // required: true + // - name: repo + // in: path + // description: name of the repo + // type: string + // required: true + // - name: id + // in: path + // description: id of the hook to test + // type: integer + // required: true + // responses: + // "204": + // "$ref": "#/responses/empty" + if ctx.Repo.Commit == nil { + // if repo does not have any commits, then don't send a webhook + ctx.Status(204) + return + } + + hookID := ctx.ParamsInt64(":id") + hook, err := utils.GetRepoHook(ctx, ctx.Repo.Repository.ID, hookID) + if err != nil { + return + } + + if err := models.PrepareWebhook(hook, ctx.Repo.Repository, models.HookEventPush, &api.PushPayload{ + Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch, + Before: ctx.Repo.Commit.ID.String(), + After: ctx.Repo.Commit.ID.String(), + Commits: []*api.PayloadCommit{ + convert.ToCommit(ctx.Repo.Repository, ctx.Repo.Commit), + }, + Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone), + Pusher: ctx.User.APIFormat(), + Sender: ctx.User.APIFormat(), + }); err != nil { + ctx.Error(500, "PrepareWebhook: ", err) + return + } + go models.HookQueue.Add(ctx.Repo.Repository.ID) + ctx.Status(204) +} + // CreateHook create a hook for a repository func CreateHook(ctx *context.APIContext, form api.CreateHookOption) { // swagger:operation POST /repos/{owner}/{repo}/hooks repository repoCreateHook diff --git a/routers/api/v1/repo/hook_test.go b/routers/api/v1/repo/hook_test.go new file mode 100644 index 0000000000..8ed4bc4b0c --- /dev/null +++ b/routers/api/v1/repo/hook_test.go @@ -0,0 +1,33 @@ +// Copyright 2018 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repo + +import ( + "net/http" + "testing" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/test" + + "github.com/stretchr/testify/assert" +) + +func TestTestHook(t *testing.T) { + models.PrepareTestEnv(t) + + ctx := test.MockContext(t, "user2/repo1/wiki/_pages") + ctx.SetParams(":id", "1") + test.LoadRepo(t, ctx, 1) + test.LoadRepoCommit(t, ctx) + test.LoadUser(t, ctx, 2) + TestHook(&context.APIContext{Context: ctx, Org: nil}) + assert.EqualValues(t, http.StatusNoContent, ctx.Resp.Status()) + + models.AssertExistsAndLoadBean(t, &models.HookTask{ + RepoID: 1, + HookID: 1, + }, models.Cond("is_delivered=?", false)) +} diff --git a/routers/api/v1/repo/main_test.go b/routers/api/v1/repo/main_test.go new file mode 100644 index 0000000000..656758ffba --- /dev/null +++ b/routers/api/v1/repo/main_test.go @@ -0,0 +1,16 @@ +// Copyright 2018 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repo + +import ( + "path/filepath" + "testing" + + "code.gitea.io/gitea/models" +) + +func TestMain(m *testing.M) { + models.MainTest(m, filepath.Join("..", "..", "..", "..")) +} |