From 48ea9bf1b3277207ce7f045007eafb2ebe7151e3 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Fri, 26 Aug 2022 10:02:29 +0800 Subject: [PATCH 1/3] fix: fill ref and compare url in webhook payload --- routers/api/v1/repo/hook.go | 20 +++++++++++++++++--- routers/web/repo/webhook.go | 6 ++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index 8a546e581ad5c..1045693c256a5 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -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" @@ -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 { @@ -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), diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index a5270a7761d16..7e659de0da756 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -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), From 5011b1694906977a6e46d2ebaa69d2eabb0087af Mon Sep 17 00:00:00 2001 From: Jason Song Date: Fri, 26 Aug 2022 11:59:43 +0800 Subject: [PATCH 2/3] docs: update comment of ref --- routers/api/v1/repo/hook.go | 2 +- templates/swagger/v1_json.tmpl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index 1045693c256a5..30232eef0f833 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -141,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: diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 8a8ca9a924fe6..eb84f1593cd86 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -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" } From 278b92bebb9c42d52b8a6b050875573091bd83fe Mon Sep 17 00:00:00 2001 From: Jason Song Date: Sun, 4 Sep 2022 10:03:53 +0800 Subject: [PATCH 3/3] fix: pass accurate ref name --- routers/api/v1/repo/hook.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index 30232eef0f833..b17142c0f647f 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -155,14 +155,8 @@ func TestHook(ctx *context.APIContext) { } 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. + if r := ctx.FormTrim("ref"); r != "" { + ref = r } hookID := ctx.ParamsInt64(":id")