Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions routers/api/v1/repo/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand All @@ -153,6 +154,17 @@ func TestHook(ctx *context.APIContext) {
return
}

ref := git.BranchPrefix + ctx.Repo.Repository.DefaultBranch
if refName := ctx.FormTrim("ref"); refName != "" {
if ctx.Repo.GitRepo.IsBranchExist(refName) {
ref = git.BranchPrefix + refName
} else if ctx.Repo.GitRepo.IsTagExist(refName) {
ref = git.TagPrefix + refName
}
// If the ref is a commit id, it's not worth finding out all branches contain the commit
// and picking the most likely one, so just use the default branch for now.
}

hookID := ctx.ParamsInt64(":id")
hook, err := utils.GetRepoHook(ctx, ctx.Repo.Repository.ID, hookID)
if err != nil {
Expand All @@ -161,10 +173,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),
Expand Down
6 changes: 4 additions & 2 deletions routers/web/repo/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,10 +665,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),
Expand Down
2 changes: 1 addition & 1 deletion templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4665,7 +4665,7 @@
},
{
"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"
}
Expand Down