From 2e0e0b48f086496a5c7df5ba1d3a6f58b01a1ebf Mon Sep 17 00:00:00 2001
From: Gusted <postmaster@gusted.xyz>
Date: Sat, 6 Jul 2024 22:04:31 +0200
Subject: [PATCH] [BUG] Use correct SHA in `GetCommitPullRequest`

- The param wasn't `sha`, it was `ref`. Use this instead.
- Adds new integration tests.
- Resolves #4190
- Resolves #4025

(cherry picked from commit a8460bb132f92a6f3c627f8bed42fb728c8e243d)
---
 routers/api/v1/repo/commits.go     |  2 +-
 tests/integration/api_repo_test.go | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go
index d06a3b4e49..5e7d10e6c2 100644
--- a/routers/api/v1/repo/commits.go
+++ b/routers/api/v1/repo/commits.go
@@ -354,7 +354,7 @@ func GetCommitPullRequest(ctx *context.APIContext) {
 	//   "404":
 	//     "$ref": "#/responses/notFound"
 
-	pr, err := issues_model.GetPullRequestByMergedCommit(ctx, ctx.Repo.Repository.ID, ctx.Params(":sha"))
+	pr, err := issues_model.GetPullRequestByMergedCommit(ctx, ctx.Repo.Repository.ID, ctx.Params("ref"))
 	if err != nil {
 		if issues_model.IsErrPullRequestNotExist(err) {
 			ctx.Error(http.StatusNotFound, "GetPullRequestByMergedCommit", err)
diff --git a/tests/integration/api_repo_test.go b/tests/integration/api_repo_test.go
index 4c2c43b197..f171d731a2 100644
--- a/tests/integration/api_repo_test.go
+++ b/tests/integration/api_repo_test.go
@@ -749,3 +749,17 @@ func TestAPIViewRepoObjectFormat(t *testing.T) {
 	DecodeJSON(t, resp, &repo)
 	assert.EqualValues(t, "sha1", repo.ObjectFormatName)
 }
+
+func TestAPIRepoCommitPull(t *testing.T) {
+	defer tests.PrepareTestEnv(t)()
+
+	var pr api.PullRequest
+	req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/1a8823cd1a9549fde083f992f6b9b87a7ab74fb3/pull")
+	resp := MakeRequest(t, req, http.StatusOK)
+
+	DecodeJSON(t, resp, &pr)
+	assert.EqualValues(t, 1, pr.ID)
+
+	req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/not-a-commit/pull")
+	MakeRequest(t, req, http.StatusNotFound)
+}