Search code examples
gitgithub-pagesgithub-apipull-request

List commits on a pull request


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?


Solution

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