Skip to content

Commit 93a610a

Browse files
authored
Fill the specified ref in webhook test payload (#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.
1 parent 0887459 commit 93a610a

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

routers/api/v1/repo/hook.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/modules/context"
1414
"code.gitea.io/gitea/modules/convert"
1515
"code.gitea.io/gitea/modules/git"
16+
"code.gitea.io/gitea/modules/setting"
1617
api "code.gitea.io/gitea/modules/structs"
1718
"code.gitea.io/gitea/modules/web"
1819
"code.gitea.io/gitea/routers/api/v1/utils"
@@ -140,7 +141,7 @@ func TestHook(ctx *context.APIContext) {
140141
// required: true
141142
// - name: ref
142143
// in: query
143-
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
144+
// description: "The name of the commit/branch/tag, indicates which commit will be loaded to the webhook payload."
144145
// type: string
145146
// required: false
146147
// responses:
@@ -153,6 +154,11 @@ func TestHook(ctx *context.APIContext) {
153154
return
154155
}
155156

157+
ref := git.BranchPrefix + ctx.Repo.Repository.DefaultBranch
158+
if r := ctx.FormTrim("ref"); r != "" {
159+
ref = r
160+
}
161+
156162
hookID := ctx.ParamsInt64(":id")
157163
hook, err := utils.GetRepoHook(ctx, ctx.Repo.Repository.ID, hookID)
158164
if err != nil {
@@ -161,10 +167,12 @@ func TestHook(ctx *context.APIContext) {
161167

162168
commit := convert.ToPayloadCommit(ctx.Repo.Repository, ctx.Repo.Commit)
163169

170+
commitID := ctx.Repo.Commit.ID.String()
164171
if err := webhook_service.PrepareWebhook(hook, ctx.Repo.Repository, webhook.HookEventPush, &api.PushPayload{
165-
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
166-
Before: ctx.Repo.Commit.ID.String(),
167-
After: ctx.Repo.Commit.ID.String(),
172+
Ref: ref,
173+
Before: commitID,
174+
After: commitID,
175+
CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
168176
Commits: []*api.PayloadCommit{commit},
169177
HeadCommit: commit,
170178
Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),

routers/web/repo/webhook.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,12 @@ func TestWebhook(ctx *context.Context) {
665665
},
666666
}
667667

668+
commitID := commit.ID.String()
668669
p := &api.PushPayload{
669670
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
670-
Before: commit.ID.String(),
671-
After: commit.ID.String(),
671+
Before: commitID,
672+
After: commitID,
673+
CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
672674
Commits: []*api.PayloadCommit{apiCommit},
673675
HeadCommit: apiCommit,
674676
Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),

templates/swagger/v1_json.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4666,7 +4666,7 @@
46664666
},
46674667
{
46684668
"type": "string",
4669-
"description": "The name of the commit/branch/tag. Default the repository’s default branch (usually master)",
4669+
"description": "The name of the commit/branch/tag, indicates which commit will be loaded to the webhook payload.",
46704670
"name": "ref",
46714671
"in": "query"
46724672
}

0 commit comments

Comments
 (0)