From fcedf634d5864a9e4ec3f866b99076872aafd07d Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Tue, 24 Sep 2024 09:00:09 +0800 Subject: [PATCH] Fix bug in getting merged pull request by commit (#32079) --- routers/api/v1/api.go | 2 ++ routers/api/v1/repo/commits.go | 6 +++--- templates/swagger/v1_json.tmpl | 2 +- tests/integration/api_pull_test.go | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index be67ec1695b..1244676508e 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -1286,6 +1286,8 @@ func Routes() *web.Router { m.Group("/{ref}", func() { m.Get("/status", repo.GetCombinedCommitStatusByRef) m.Get("/statuses", repo.GetCommitStatusesByRef) + }, context.ReferencesGitRepo()) + m.Group("/{sha}", func() { m.Get("/pull", repo.GetCommitPullRequest) }, context.ReferencesGitRepo()) }, reqRepoReader(unit.TypeCode)) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index cec7c3d534c..788c75fab2f 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -325,11 +325,11 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) { } } -// GetCommitPullRequest returns the pull request of the commit +// GetCommitPullRequest returns the merged pull request of the commit func GetCommitPullRequest(ctx *context.APIContext) { // swagger:operation GET /repos/{owner}/{repo}/commits/{sha}/pull repository repoGetCommitPullRequest // --- - // summary: Get the pull request of the commit + // summary: Get the merged pull request of the commit // produces: // - application/json // parameters: @@ -354,7 +354,7 @@ func GetCommitPullRequest(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - pr, err := issues_model.GetPullRequestByMergedCommit(ctx, ctx.Repo.Repository.ID, ctx.PathParam(":sha")) + pr, err := issues_model.GetPullRequestByMergedCommit(ctx, ctx.Repo.Repository.ID, ctx.PathParam("sha")) if err != nil { if issues_model.IsErrPullRequestNotExist(err) { ctx.Error(http.StatusNotFound, "GetPullRequestByMergedCommit", err) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index d9f1a8f5e93..59835055027 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -5446,7 +5446,7 @@ "tags": [ "repository" ], - "summary": "Get the pull request of the commit", + "summary": "Get the merged pull request of the commit", "operationId": "repoGetCommitPullRequest", "parameters": [ { diff --git a/tests/integration/api_pull_test.go b/tests/integration/api_pull_test.go index 8239878d2b7..b7e01d4a20c 100644 --- a/tests/integration/api_pull_test.go +++ b/tests/integration/api_pull_test.go @@ -334,3 +334,19 @@ func doAPIGetPullFiles(ctx APITestContext, pr *api.PullRequest, callback func(*t } } } + +func TestAPICommitPullRequest(t *testing.T) { + defer tests.PrepareTestEnv(t)() + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) + owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) + + ctx := NewAPITestContext(t, "user2", repo.Name, auth_model.AccessTokenScopeReadRepository) + + mergedCommitSHA := "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3" + req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/commits/%s/pull", owner.Name, repo.Name, mergedCommitSHA).AddTokenAuth(ctx.Token) + ctx.Session.MakeRequest(t, req, http.StatusOK) + + invalidCommitSHA := "abcd1234abcd1234abcd1234abcd1234abcd1234" + req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/commits/%s/pull", owner.Name, repo.Name, invalidCommitSHA).AddTokenAuth(ctx.Token) + ctx.Session.MakeRequest(t, req, http.StatusNotFound) +}