Reputation: 2411
I see that for commits on a pull request, max limit is 250 as per the document: List commits on a Pull Request and if the pull request exceeds 250 commits then another end-point is suggested which is: List Commits
Lists a maximum of 250 commits for a pull request. To receive a complete commit list for pull requests with more than 250 commits, use the List commits endpoint.
GET /repos/:owner/:repo/pulls/:pull_number/commits
But, I dont see how using List Commits end-point I can figure out if its tied to the pull request.
EDIT: Wondering, if I should rely on git commands here instead. i.e clone the repo, run git log to get a list of all commits.. Any better approach? Issue: Not all commits would have been pushed to the pull request?
Also, I am looking for a way to see if there are any new commits incrementally added to pull request since it was first raised. For cases, where review comments are worked on and added to existing pull request, in that case I wish to just validate incremental changes. Any pointers or document on how to achieve that?
Upvotes: 7
Views: 19702
Reputation: 25639
The GitHub CLI can list PR commits. Handily, the CLI is pre-installed on GitHub Actions runners.
gh pr view 19 --json commits
{
"commits": [
{
"authoredDate": "2022-11-21T20:36:33Z",
"authors": [
{
"email": "[email protected]",
"id": "MDQ6VXNlcjE2Nzc5NTU=",
"login": "someone",
"name": "someone"
}
],
"committedDate": "2022-11-21T20:36:33Z",
"messageBody": "",
"messageHeadline": "chore(main): release 0.3.5",
"oid": "7da95da24f502d32bbdc01a117bc881bad6007df"
}
]
}
Upvotes: 9
Reputation: 51780
The github documentation indicates how to check a pull request locally :
on the remote repo, a pull request's current "active commit" is stored under refs/pull/<id>/head
; when a merge request is merged, another ref appears at refs/pul/<id>/merge
.
Quoting the docs : you can fetch on your local copy an individual merge request :
Fetch the reference to the pull request based on its ID number, creating a new branch in the process.
$ git fetch origin pull/ID/head:BRANCHNAME
You can now compare BRANCHNAME
and master
(*) to see the list of commits on the merge request :
git log master..BRANCHNAME
git rev-list master..BRANCHNAME
Note that you can choose to fetch the pull/ID/head
into some ref other than a branch :
git fetch origin pull/ID/head:refs/remotes/origin/pr/ID
# now the pr appears as 'origin/pr/ID'
(*) if the target branch is not master
, you would need to get this information otherwise, from the api for example.
You can also set your refspec to automatically fetch all merge requests ; see for example this gist :
Locate the section for your github remote in the .git/config file. It looks like this:
[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = [email protected]:joyent/node.git
Now add the line
fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
to this section. Obviously, change the github url to match your project's URL. It ends up looking like this:[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = [email protected]:joyent/node.git fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
The next git fetch
will download all refs for all pull requests (closed, merged and open together).
I don't see the history of the pull request represented in the refspec :
git fetch origin
when notifiedThat way : the history of the pull request would appear in its reflog :
git reflog origin/pr/<id>
A more direct way : with a hook, you would have access to the commits being pushed, and the pull request being updated, so you can set up something to store the history of that pull request.
Upvotes: 1
Reputation: 3251
You can list the pull requests associated with a commit using GET /repos/:owner/:repo/commits/:commit_sha/pulls
, which will show the pull requests which the given commit is associated with. This does mean that you'll need to check every commit to see if its associated with the PR. This will create A LOT of excess network traffic, so unless its absolutely imperative I wouldn't' suggest looking for PR associated commits using this endpoint.
The best solution I can see for finding new commits for a PR is to get all the commits of the branch after the pull request was created. You'd need to GET
the PR, pull out the created_at
field, and use the commits endpoint to retrieve the commits from the branch, and use the created_at
field of the PR for the since
field in the commit request body and specify the target branch.
Upvotes: 4