List commits on a pull request
Asked Answered
T

3

7

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?

Taxi answered 25/8, 2020 at 18:25 Comment(0)
N
4

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.

Nippur answered 25/8, 2020 at 18:47 Comment(1)
A good start! But i want to add that using the created_at field from the PR will miss out all commits pushed to the branch before the PR was created. Better get the PR base sha and request the created_at from that commit.Ronn
E
6

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"
    }
  ]
}
Elm answered 22/11, 2022 at 10:22 Comment(0)
N
4

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.

Nippur answered 25/8, 2020 at 18:47 Comment(1)
A good start! But i want to add that using the created_at field from the PR will miss out all commits pushed to the branch before the PR was created. Better get the PR base sha and request the created_at from that commit.Ronn
B
1

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 :

  1. 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 :

  • you could set up a clone of the repo, whith the above refspec,
  • and a hook on github which would notify this clone each time a push is made to a pull request,
  • so that the clone would simply run git fetch origin when notified

That 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.

Beluga answered 25/8, 2020 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.