]> source.dussan.org Git - gitea.git/commitdiff
Fill the specified ref in webhook test payload (#20961) (#21055)
authorJason Song <i@wolfogre.com>
Sun, 4 Sep 2022 15:12:01 +0000 (23:12 +0800)
committerGitHub <noreply@github.com>
Sun, 4 Sep 2022 15:12:01 +0000 (16:12 +0100)
Backport #20961

The webhook payload should use the right ref when it‘s specified in the testing request.

The compare URL should not be empty, a URL like `compare/A...A` seems useless in most cases but is helpful when testing.

routers/api/v1/repo/hook.go
routers/web/repo/webhook.go
templates/swagger/v1_json.tmpl

index 8a546e581ad5c74d7af2c6c531fa1b7159951e6a..b17142c0f647fc1c71a5600c3505b5f517b89748 100644 (file)
@@ -13,6 +13,7 @@ import (
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/convert"
        "code.gitea.io/gitea/modules/git"
+       "code.gitea.io/gitea/modules/setting"
        api "code.gitea.io/gitea/modules/structs"
        "code.gitea.io/gitea/modules/web"
        "code.gitea.io/gitea/routers/api/v1/utils"
@@ -140,7 +141,7 @@ func TestHook(ctx *context.APIContext) {
        //   required: true
        // - name: ref
        //   in: query
-       //   description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
+       //   description: "The name of the commit/branch/tag, indicates which commit will be loaded to the webhook payload."
        //   type: string
        //   required: false
        // responses:
@@ -153,6 +154,11 @@ func TestHook(ctx *context.APIContext) {
                return
        }
 
+       ref := git.BranchPrefix + ctx.Repo.Repository.DefaultBranch
+       if r := ctx.FormTrim("ref"); r != "" {
+               ref = r
+       }
+
        hookID := ctx.ParamsInt64(":id")
        hook, err := utils.GetRepoHook(ctx, ctx.Repo.Repository.ID, hookID)
        if err != nil {
@@ -161,10 +167,12 @@ func TestHook(ctx *context.APIContext) {
 
        commit := convert.ToPayloadCommit(ctx.Repo.Repository, ctx.Repo.Commit)
 
+       commitID := ctx.Repo.Commit.ID.String()
        if err := webhook_service.PrepareWebhook(hook, ctx.Repo.Repository, webhook.HookEventPush, &api.PushPayload{
-               Ref:        git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
-               Before:     ctx.Repo.Commit.ID.String(),
-               After:      ctx.Repo.Commit.ID.String(),
+               Ref:        ref,
+               Before:     commitID,
+               After:      commitID,
+               CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
                Commits:    []*api.PayloadCommit{commit},
                HeadCommit: commit,
                Repo:       convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
index a9b14ee21f453c1c1afec70ffbf95d72b1455aa3..2b9f78ab018e024256273df02f991f532989666d 100644 (file)
@@ -1271,10 +1271,12 @@ func TestWebhook(ctx *context.Context) {
                },
        }
 
+       commitID := commit.ID.String()
        p := &api.PushPayload{
                Ref:        git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
-               Before:     commit.ID.String(),
-               After:      commit.ID.String(),
+               Before:     commitID,
+               After:      commitID,
+               CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
                Commits:    []*api.PayloadCommit{apiCommit},
                HeadCommit: apiCommit,
                Repo:       convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
index 63ef828606e9c2ee3332e3703edc01283678b121..25088d8b604746dfc62af9783cda3761fe15e6f7 100644 (file)
           },
           {
             "type": "string",
-            "description": "The name of the commit/branch/tag. Default the repository’s default branch (usually master)",
+            "description": "The name of the commit/branch/tag, indicates which commit will be loaded to the webhook payload.",
             "name": "ref",
             "in": "query"
           }