How to get the Pull Request from the commit hash

Katsuhiko Yoshida
kyoshida’s log
Published in
3 min readMar 31, 2018

--

There is something You want to know about why it was implemented now with Git managed code. At this time, although I do not know the intention of the commit even if it sees the difference of the commitment, the person who committed has already retired, actually I do not remember well though I committed it. Is not there thing like that?

If you are managing the code with GitHub, you can see the history and code review log if you can refer to the pull request page, so development efficiency improves. Also for the future pull request will motivate the team to root the culture of leaving such change logs properly.

Search from Github API

Github API is very powerful and provides various functions for the Github project. It is also possible to search for Pull Request using Search issues API.

<commit_hash> type:pr is:merged repo:<repository_name>

Why issue search API? Although it may be thought that there is no endpoint to search for PR only, PR will be part of the issue (type:prfocusing on PR).

In addition, as a search condition, it is acquired in descending order of creation date, and it is assumed that the last value of PR is expected PR. For example, since multiple hits are made when PR of the release branch is created at the time of release, it is necessary to acquire the PR created first.

Example

In the case of a commit aa18a09 of this repository, the request URL is next.

https://api.github.com/search/issues?q=aa18a09+type%3Apr+is%3Amerged+repo%3Akyoshidajp%2Fvscode-tosa&sort=created&order=desc

It is the result of partially omitting the acquired response.

{
"total_count": 1,
"incomplete_results": false,
"items": [
{
"url": "https://api.github.com/repos/kyoshidajp/vscode-tosa/issues/7",
"repository_url": "https://api.github.com/repos/kyoshidajp/vscode-tosa",
"labels_url": "https://api.github.com/repos/kyoshidajp/vscode-tosa/issues/7/labels{/name}",
"comments_url": "https://api.github.com/repos/kyoshidajp/vscode-tosa/issues/7/comments",
"events_url": "https://api.github.com/repos/kyoshidajp/vscode-tosa/issues/7/events",
"html_url": "https://github.com/kyoshidajp/vscode-tosa/pull/7",
"id": 304157764,
"number": 7,
"title": "Add search terms to identify a PR",
"user": {
"login": "kyoshidajp", ...
}
}
]
}

This html_urlwill be the URL of PR you expect.

Get by VS Code Extension

I have developed VS Code extension. So, you can get it from here.

VS Code extension “TOSA”

Open the PR from the right-click menu “Open Pull Request” on the line you want to check.

Get by CLI

You can also get the PR by CLI.

CLI version “TOSA”

Install

$ brew tap kyoshidajp/tosa
$ brew install tosa

Usage

$ tosa <sha>

Run on tig

If you are a tig user, you should configure the following keybind in$HOME/.tigrc

bind main O @tosa %(commit)
bind blame O @tosa %(commit)

Open the URL of the pull request from the commit hash at the cursor position when O (Shift + o) is pressed in the main view and blame view.

Search with tig

--

--