From b327a9a220d19ad173482c6e824402594394c25a Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Thu, 19 Sep 2024 15:57:02 +0800 Subject: [PATCH 1/3] fix GetCommitPullRequest --- routers/api/v1/repo/commits.go | 6 +++--- templates/swagger/v1_json.tmpl | 2 +- tests/integration/api_pull_test.go | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index cec7c3d534c62..647c4d35289d2 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("ref")) 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 d9f1a8f5e931f..598350550276e 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 8239878d2b789..b7e01d4a20c94 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) +} From 1b68fb944c46d5d2730040724940c379f2b5e378 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Thu, 19 Sep 2024 23:37:41 +0800 Subject: [PATCH 2/3] fix swagger --- routers/api/v1/repo/commits.go | 4 +- templates/swagger/v1_json.tmpl | 86 +++++++++++++++++----------------- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 647c4d35289d2..2fe8af91a4c0c 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -327,7 +327,7 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) { // 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 + // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/pull repository repoGetCommitPullRequest // --- // summary: Get the merged pull request of the commit // produces: @@ -343,7 +343,7 @@ func GetCommitPullRequest(ctx *context.APIContext) { // description: name of the repo // type: string // required: true - // - name: sha + // - name: ref // in: path // description: SHA of the commit to get // type: string diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 598350550276e..5ae0498c6a2ca 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -5296,6 +5296,49 @@ } } }, + "/repos/{owner}/{repo}/commits/{ref}/pull": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "repository" + ], + "summary": "Get the merged pull request of the commit", + "operationId": "repoGetCommitPullRequest", + "parameters": [ + { + "type": "string", + "description": "owner of the repo", + "name": "owner", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "name of the repo", + "name": "repo", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "SHA of the commit to get", + "name": "ref", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "$ref": "#/responses/PullRequest" + }, + "404": { + "$ref": "#/responses/notFound" + } + } + } + }, "/repos/{owner}/{repo}/commits/{ref}/status": { "get": { "produces": [ @@ -5438,49 +5481,6 @@ } } }, - "/repos/{owner}/{repo}/commits/{sha}/pull": { - "get": { - "produces": [ - "application/json" - ], - "tags": [ - "repository" - ], - "summary": "Get the merged pull request of the commit", - "operationId": "repoGetCommitPullRequest", - "parameters": [ - { - "type": "string", - "description": "owner of the repo", - "name": "owner", - "in": "path", - "required": true - }, - { - "type": "string", - "description": "name of the repo", - "name": "repo", - "in": "path", - "required": true - }, - { - "type": "string", - "description": "SHA of the commit to get", - "name": "sha", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "$ref": "#/responses/PullRequest" - }, - "404": { - "$ref": "#/responses/notFound" - } - } - } - }, "/repos/{owner}/{repo}/compare/{basehead}": { "get": { "produces": [ From fd35dc9e5cbf47eef0494661aef07a929ad4092f Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Mon, 23 Sep 2024 09:59:07 +0800 Subject: [PATCH 3/3] fix GetCommitPullRequest --- routers/api/v1/api.go | 2 + routers/api/v1/repo/commits.go | 6 +-- templates/swagger/v1_json.tmpl | 86 +++++++++++++++++----------------- 3 files changed, 48 insertions(+), 46 deletions(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index be67ec1695b3b..1244676508ee5 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 2fe8af91a4c0c..788c75fab2ffe 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -327,7 +327,7 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) { // GetCommitPullRequest returns the merged pull request of the commit func GetCommitPullRequest(ctx *context.APIContext) { - // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/pull repository repoGetCommitPullRequest + // swagger:operation GET /repos/{owner}/{repo}/commits/{sha}/pull repository repoGetCommitPullRequest // --- // summary: Get the merged pull request of the commit // produces: @@ -343,7 +343,7 @@ func GetCommitPullRequest(ctx *context.APIContext) { // description: name of the repo // type: string // required: true - // - name: ref + // - name: sha // in: path // description: SHA of the commit to get // type: string @@ -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("ref")) + 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 5ae0498c6a2ca..598350550276e 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -5296,49 +5296,6 @@ } } }, - "/repos/{owner}/{repo}/commits/{ref}/pull": { - "get": { - "produces": [ - "application/json" - ], - "tags": [ - "repository" - ], - "summary": "Get the merged pull request of the commit", - "operationId": "repoGetCommitPullRequest", - "parameters": [ - { - "type": "string", - "description": "owner of the repo", - "name": "owner", - "in": "path", - "required": true - }, - { - "type": "string", - "description": "name of the repo", - "name": "repo", - "in": "path", - "required": true - }, - { - "type": "string", - "description": "SHA of the commit to get", - "name": "ref", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "$ref": "#/responses/PullRequest" - }, - "404": { - "$ref": "#/responses/notFound" - } - } - } - }, "/repos/{owner}/{repo}/commits/{ref}/status": { "get": { "produces": [ @@ -5481,6 +5438,49 @@ } } }, + "/repos/{owner}/{repo}/commits/{sha}/pull": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "repository" + ], + "summary": "Get the merged pull request of the commit", + "operationId": "repoGetCommitPullRequest", + "parameters": [ + { + "type": "string", + "description": "owner of the repo", + "name": "owner", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "name of the repo", + "name": "repo", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "SHA of the commit to get", + "name": "sha", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "$ref": "#/responses/PullRequest" + }, + "404": { + "$ref": "#/responses/notFound" + } + } + } + }, "/repos/{owner}/{repo}/compare/{basehead}": { "get": { "produces": [