1
0
Fork 0
forked from forgejo/forgejo

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>
(cherry picked from commit 0a426cc575734e5eff410d6a790f40473117f753)
This commit is contained in:
qwerty287 2024-02-24 09:18:39 +01:00 committed by Earl Warren
parent f0acc71ba1
commit 1608ef0ce9
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
6 changed files with 128 additions and 0 deletions

View file

@ -1122,3 +1122,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
}