Skip to content

Commit fcedf63

Browse files
authored
Fix bug in getting merged pull request by commit (#32079)
1 parent 6afb224 commit fcedf63

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

routers/api/v1/api.go

+2
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,8 @@ func Routes() *web.Router {
12861286
m.Group("/{ref}", func() {
12871287
m.Get("/status", repo.GetCombinedCommitStatusByRef)
12881288
m.Get("/statuses", repo.GetCommitStatusesByRef)
1289+
}, context.ReferencesGitRepo())
1290+
m.Group("/{sha}", func() {
12891291
m.Get("/pull", repo.GetCommitPullRequest)
12901292
}, context.ReferencesGitRepo())
12911293
}, reqRepoReader(unit.TypeCode))

routers/api/v1/repo/commits.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,11 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) {
325325
}
326326
}
327327

328-
// GetCommitPullRequest returns the pull request of the commit
328+
// GetCommitPullRequest returns the merged pull request of the commit
329329
func GetCommitPullRequest(ctx *context.APIContext) {
330330
// swagger:operation GET /repos/{owner}/{repo}/commits/{sha}/pull repository repoGetCommitPullRequest
331331
// ---
332-
// summary: Get the pull request of the commit
332+
// summary: Get the merged pull request of the commit
333333
// produces:
334334
// - application/json
335335
// parameters:
@@ -354,7 +354,7 @@ func GetCommitPullRequest(ctx *context.APIContext) {
354354
// "404":
355355
// "$ref": "#/responses/notFound"
356356

357-
pr, err := issues_model.GetPullRequestByMergedCommit(ctx, ctx.Repo.Repository.ID, ctx.PathParam(":sha"))
357+
pr, err := issues_model.GetPullRequestByMergedCommit(ctx, ctx.Repo.Repository.ID, ctx.PathParam("sha"))
358358
if err != nil {
359359
if issues_model.IsErrPullRequestNotExist(err) {
360360
ctx.Error(http.StatusNotFound, "GetPullRequestByMergedCommit", err)

templates/swagger/v1_json.tmpl

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/api_pull_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,19 @@ func doAPIGetPullFiles(ctx APITestContext, pr *api.PullRequest, callback func(*t
334334
}
335335
}
336336
}
337+
338+
func TestAPICommitPullRequest(t *testing.T) {
339+
defer tests.PrepareTestEnv(t)()
340+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
341+
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
342+
343+
ctx := NewAPITestContext(t, "user2", repo.Name, auth_model.AccessTokenScopeReadRepository)
344+
345+
mergedCommitSHA := "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3"
346+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/commits/%s/pull", owner.Name, repo.Name, mergedCommitSHA).AddTokenAuth(ctx.Token)
347+
ctx.Session.MakeRequest(t, req, http.StatusOK)
348+
349+
invalidCommitSHA := "abcd1234abcd1234abcd1234abcd1234abcd1234"
350+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/commits/%s/pull", owner.Name, repo.Name, invalidCommitSHA).AddTokenAuth(ctx.Token)
351+
ctx.Session.MakeRequest(t, req, http.StatusNotFound)
352+
}

0 commit comments

Comments
 (0)