aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorqwerty287 <80460567+qwerty287@users.noreply.github.com>2024-02-24 09:18:39 +0100
committerGitHub <noreply@github.com>2024-02-24 08:18:39 +0000
commit0a426cc575734e5eff410d6a790f40473117f753 (patch)
treea393ab5f374e83fd240626c1aca1f95f4ce79cc7 /models
parent6e5966597c2d498d1a8540dad965461d44ff8e57 (diff)
downloadgitea-0a426cc575734e5eff410d6a790f40473117f753.tar.gz
gitea-0a426cc575734e5eff410d6a790f40473117f753.zip
Add API to get merged PR of a commit (#29243)
Adds a new API `/repos/{owner}/{repo}/commits/{sha}/pull` that allows you to get the merged PR associated to a commit. --------- Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'models')
-rw-r--r--models/fixtures/pull_request.yml1
-rw-r--r--models/issues/pull.go20
-rw-r--r--models/issues/pull_test.go12
3 files changed, 33 insertions, 0 deletions
diff --git a/models/fixtures/pull_request.yml b/models/fixtures/pull_request.yml
index 560674c370..54590fb830 100644
--- a/models/fixtures/pull_request.yml
+++ b/models/fixtures/pull_request.yml
@@ -9,6 +9,7 @@
head_branch: branch1
base_branch: master
merge_base: 4a357436d925b5c974181ff12a994538ddc5a269
+ merged_commit_id: 1a8823cd1a9549fde083f992f6b9b87a7ab74fb3
has_merged: true
merger_id: 2
diff --git a/models/issues/pull.go b/models/issues/pull.go
index 2cb1e1b971..18e6b2776d 100644
--- a/models/issues/pull.go
+++ b/models/issues/pull.go
@@ -1093,3 +1093,23 @@ func InsertPullRequests(ctx context.Context, prs ...*PullRequest) error {
}
return committer.Commit()
}
+
+// GetPullRequestByMergedCommit returns a merged pull request by the given commit
+func GetPullRequestByMergedCommit(ctx context.Context, repoID int64, sha string) (*PullRequest, error) {
+ pr := new(PullRequest)
+ has, err := db.GetEngine(ctx).Where("base_repo_id = ? AND merged_commit_id = ?", repoID, sha).Get(pr)
+ if err != nil {
+ return nil, err
+ } else if !has {
+ return nil, ErrPullRequestNotExist{0, 0, 0, repoID, "", ""}
+ }
+
+ if err = pr.LoadAttributes(ctx); err != nil {
+ return nil, err
+ }
+ if err = pr.LoadIssue(ctx); err != nil {
+ return nil, err
+ }
+
+ return pr, nil
+}
diff --git a/models/issues/pull_test.go b/models/issues/pull_test.go
index 173417136c..3a30b2f3de 100644
--- a/models/issues/pull_test.go
+++ b/models/issues/pull_test.go
@@ -339,6 +339,18 @@ func TestGetApprovers(t *testing.T) {
assert.EqualValues(t, expected, approvers)
}
+func TestGetPullRequestByMergedCommit(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+ pr, err := issues_model.GetPullRequestByMergedCommit(db.DefaultContext, 1, "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3")
+ assert.NoError(t, err)
+ assert.EqualValues(t, 1, pr.ID)
+
+ _, err = issues_model.GetPullRequestByMergedCommit(db.DefaultContext, 0, "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3")
+ assert.ErrorAs(t, err, &issues_model.ErrPullRequestNotExist{})
+ _, err = issues_model.GetPullRequestByMergedCommit(db.DefaultContext, 1, "")
+ assert.ErrorAs(t, err, &issues_model.ErrPullRequestNotExist{})
+}
+
func TestMigrate_InsertPullRequests(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
reponame := "repo1"