Reputation: 4805
Pull Requests are great for understanding the larger thinking around a change or set of changes made to a repo. Reading pull requests are a great way to quickly "grok" a project as, instead of small atomic changes to the source, you get larger groupings of logical changes. Analogous to organizing the lines in your code into related "stanzas" to make it easier to read.
I find myself looking at a file or a commit, and I wonder if there is a way to backtrack the commit to the Pull Request that originally created it. That Pull Request would have been merged eventually, but not necessary with a merge-commit.
Upvotes: 273
Views: 111445
Reputation: 351
Automated way, using GitHub GraphQL API:
query="query {
search(
query: \"repo:${OWNER}/${REPO} is:pr ${COMMIT_ID}\"
type: ISSUE
first: 1
) {
edges {
node {
... on PullRequest {
number
title
body
author {
login
}
}
}
}
}
}"
query=$(jq -n --arg query "$query" '{ query: $query }')
curl -sS \
-X POST \
-H "Authorization: Bearer ${BEARER_TOKEN}" \
-H "Content-Type: application/json" \
-d "$query" \
https://api.github.com/graphql
More info:
Upvotes: 0
Reputation: 19
This command retrieves the pull request number associated with a specific commit hash.
Here's how it works:
git ls-remote
lists all the remote references for a Git repository.
The grep command filters the results to only show references that include the commit hash $SHA_COMMIT_ID
.
awk
processes the filtered results and extracts the pull request number from the reference string. The split function splits the reference string by forward slashes, and the print command outputs the third element of the resulting array, which is the pull request number.
The pull request number is stored in the $PR_NUMBER
variable, and a message is printed to the console that displays the pull request number.
export PR_NUMBER=$(git ls-remote |grep '$SHA_COMMIT_ID'|awk '/refs\/pull\/.*\/./{split($2,a,"/");print a[3]}')
echo "PR Number: $PR_NUMBER"
Upvotes: 2
Reputation: 732
I wanted something similar to find a PR for a given commit, then comment on the PR with Jenkins build info. Here's how that works using GH API:
# for a given SHA and repo
SHA=58d8b4407ab5d2b9a696236202308d92d3e25340
ownerRepo=redhat-developer/devspaces
# a. use gh to query a given repo for closed pulls for a given commitSHA; return the PR URL
PR_COMMENTS_URL=$(curl -sSL -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${ownerRepo}/pulls?state=closed" | \
yq -r --arg SHA "$SHA" '.[]|select(.head.sha == $SHA)|.comments_url')
# b. comment on the PR by URL: https://api.github.com/repos/redhat-developer/devspaces/issues/848/comments
if [[ $PR_COMMENTS_URL ]]; then
curl -sSL -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" \
-X POST -d '{"body": "Building in currentBuild.absoluteUrl (GH API TEST)"}' "${PR_COMMENTS_URL}" | yq -r '.html_url'
fi
Then you can wrap this script inside a Jenkins groovy/bash block, and pass in currentBuild.absoluteUrl
from the running build to the GH pull request.
Equivalent code using gh cli:
# 0. install gh CLI
sudo yum -y -q install https://github.com/cli/cli/releases/download/v2.20.2/gh_2.20.2_linux_amd64.rpm
# a. use gh to query a given repo for merged PRs for a given commitSHA; return the PR URL
PR_HTML_URL=$(gh pr list --repo ${ownerRepo} --state merged --json url --jq '.[].url' \
--search $SHA)
# b. comment on the PR by URL: https://github.com/redhat-developer/devspaces/pull/848
if [[ $PR_HTML_URL ]]; then
gh pr comment $PR_HTML_URL -b "Building in currentBuild.absoluteUrl (GH CLI TEST)"
fi
For a sample of Jenkins pipeline code:
Upvotes: 0
Reputation: 171
You can also do this using the gh cli, here's an example:
gh pr list --search "30aedc5aaab4708b2144c648a9c7ace9aff4cd31" --state merged --json url --jq '.[0].url'
For more info, see - https://cli.github.com/manual/gh_pr_list
Upvotes: 16
Reputation: 19475
git config --add remote.origin.fetch +refs/pull/*/head:refs/remotes/origin/pull/*
git fetch origin
git describe --all --contains <COMMIT>
If necessary, change origin
to the name of the remote that points to the
GitHub repository to which the pull request would have been sent. The first
command only needs to be run once for any given remote, and the second will
generally be done when getting other updates.
This will cause git to get information about pull requests along with actual
branches. They'll show up as remote-tracking branches like origin/pull/123
.
Once that is done, you can use git describe
with the --all
and --contains
options to show the first branch which has the referenced commit.
However, this won't work if the commit you're looking for is actually a modified version of the commit from the pull request such as if the changes were rebased onto other work or the person doing the merge decided to make some changes.
Upvotes: 72
Reputation: 7810
You can just go to GitHub and enter the SHA into the search bar, make sure you select the "Issues" link on the left.
UPDATED 13 July 2017
Via the GitHub UI there is a now a really easy way to do this. If you are looking at a commit in the list of commits in a branch in the UI, click on the link to the commit itself. If there is a PR for that commit and it wasn't added directly to the branch, a link to the PR listing the PR number and the branch it went into will be directly under the commit message at the top of the page.
If you have the commit SHA and nothing else and don't want to go digging around for it, just add /commit/[commit SHA]
to the repo url, and you will see the commit page, with the PR link if it exists.
For example, if the SHA is 52797a7a3b087231e4e391e11ea861569205aaf4 and the repo is https://github.com/glimmerjs/glimmer-vm , then go to https://github.com/glimmerjs/glimmer-vm/commit/52797a7a3b087231e4e391e11ea861569205aaf4
Upvotes: 357
Reputation: 11
I've been a heavy user of the cheeky little link on the GitHub web UI but wanted a faster way that would take me straight there from the terminal, basically a git pr SHA
command. It took a bit of doing, but here's a series of git aliases that will set that up for you on MacOS:
git config --global alias.merge-commits '!funct() { git log --merges --reverse --oneline --ancestry-path $1..origin | grep "Merge pull request"; }; funct'
git config --global alias.pr-number '!funct() { git merge-commits $1 | head -n1 | sed -n "s/^.*Merge pull request #\\s*\\([0-9]*\\).*$/\\1/p"; }; funct'
git config --global alias.web-url '!funct() { git config remote.origin.url | sed -e"s/git@/https:\/\//" -e"s/\.git$//" | sed -E "s/(\/\/[^:]*):/\1\//"; }; funct'
git config --global alias.pr '!funct() { open "`git web-url`/pull/`git pr-number $1`" ;}; funct'
If you're on Linux, replace open
with xdg-open
and you're golden. It shouldn't be too difficult to adapt to work with GitLab either.
Note this will only work if you practicing GitHub flow and creating explicit merge commits.
I've written a more detailed explanation of how this all works here: https://tekin.co.uk/2020/06/jump-from-a-git-commit-to-the-pr-in-one-command
Upvotes: 1
Reputation: 69757
Put the commit hash into the Pull Request filters field on GitHub.
Upvotes: 27
Reputation: 1323953
Since Oct. 13, 2014, this should be straightforward:
For example:
You can see for the file hakimel/reveal.js/plugin/markdown/markdown.js
, my contribution now comes with a reference to the PR #734 it originated.
This comes from Linking merged pull requests from commits:
We've been including the containing branches and tags on commit pages to give you more context around changes. Now, commits in a repository's default branch will also show you the pull request that introduced them.
In the pull request, you can see the discussion around why the commit was introduced, and get a clearer picture of the reason for the change.
As always, if you know the commit SHA, you can skip the commit page and search for the pull request directly.
Upvotes: 23
Reputation: 439
I had this same problem and wrote the pr_for_sha bash helper, documented here:
http://joey.aghion.com/find-the-github-pull-request-for-a-commit/
Call it like pr_for_sha <COMMIT>
and it will open the corresponding github pull request page in a browser.
Upvotes: 3