Forgejo and Gitea: find the pull request that brought a commit in
You have a commit ID, or a line in blame that looks wrong, and you want the pull request it came through: the discussion, the review, who merged it. Forgejo and Gitea, including Codeberg and gitea.com, answer this with one API call. Below are the call, the case where it answers 404 although a pull request exists, a git command for that case, and the same thing in IntelliJ IDEA. From the API description that Codeberg and gitea.com publish (codeberg.org/api/swagger, gitea.com/api/swagger) and the Gitea source, models/issues/pull.go.
The API call
GET https://{server}/api/v1/repos/{owner}/{repo}/commits/{sha}/pull- The answer is one pull request, with its number, title, state and
html_url. A 404 means the server knows no pull request for that commit. - On a public repository no token is needed. On a private one, send a token that can read the repository (
Authorization: token <token>, with the read:repository scope). - Gitea has the call since version 1.22 (go-gitea/gitea#29243), Forgejo since 7.0. An older server answers 404 for every commit.
Which commits it finds
- The server looks the commit up among the commits that pull request merges made: it searches the pull requests of the repository by their merged commit ID (
merged_commit_id), nothing else. - With a squash merge, the target branch gets one new commit; blame shows that commit, and the call finds it.
- With a merge commit, the merge commit is found, but the branch's own commits reach the target branch as they are, and a blame line usually shows one of them. For those the call answers 404, although the pull request exists.
- With a rebase merge, the merged commit ID is the last of the rebased commits: the call finds that one, not the earlier ones.
For a commit merged with a merge commit: git
Find the merge on the target branch that brought the commit in, then ask the call above with that merge's ID. In Bash (Git Bash on Windows), replace <commit>:
for m in $(git rev-list --first-parent --merges --reverse origin/main); do
git merge-base --is-ancestor <commit> $m && { git log -1 --oneline $m; break; }
done
It walks the merges of origin/main from the oldest and prints the first one that contains the commit. The shorter git log --merges --ancestry-path <commit>..origin/main can stop at the wrong merge when main was merged into the branch after the commit. When the commit was pushed to main directly, or came in with a rebase merge, the merge this prints did not bring it in; check that the commit is not on git rev-list --first-parent origin/main first.
Find Pull Request on Forgejo / Gitea in IntelliJ IDEA
Pull Request Inbox for Forgejo and Gitea, a plugin by Lesstep for JetBrains IDEs 2024.1 or later, lists the Forgejo and Gitea pull requests waiting for your review in an IDE tool window. Find Pull Request on Forgejo / Gitea is on the annotate (blame) gutter and on a commit in the Git log. It asks the call above; from version 1.0.47, when the server answers 404, it finds the merge on the checked-out branch (HEAD) that brought the commit in, as above, and asks about that merge. The pull request it finds opens on the server; when there is none it says so, with the likely reasons. Finding the pull request is free; approving, commenting and merging are Pro, with a 30-day trial.
Get it on JetBrains Marketplace
Forgejo, Gitea and Codeberg are names of their respective owners; IntelliJ IDEA is a trademark of JetBrains s.r.o. This page is not affiliated with or endorsed by the Forgejo project, Codeberg e.V., the Gitea project or JetBrains.
More: the plugin · Forgejo and Gitea Actions runs in the IDE · 日本語