VSTS Extensions–Getting the latest pull request on a file

In the current VSTS extension that I am writing (Surface Owner of  File Extension), one of the things I needed to do was given a file path in a git repo, I needed to display who was the last person to perform a code review/pull request on that file.  After combing through the VSTS REST api, I couldn’t figure out how to get any information about pull requests from the file object.  The only connection that I could find is I could get all the pull requests in a repo, and from a pull request, I could get the commit id.  And from the commit id, I could use the REST api to get all the files involved in that commit.  So now, I could compare my file path with the files in the commit to see if this was a pull request that touched my file.

One word. Painful!  Basically, in order to find the latest pull request on my file, I was now doing n number of REST calls (n being the number of pull requests in the repo). Can you say SLOW?

I figured there had to be a better way but looking at the REST api was making my head spin so I pinged my buddy Wouter de Kort (@wouterdekort) who is an absolute VSTS REST api wizard.  Within minutes, he replied to my email explaining how he could do this with just 2 REST api calls.  First, use the REST api to get all the pull requests in the repo.  Next use this rest api call to get all the commits on the file (https://www.visualstudio.com/en-us/integrate/api/git/commits#GetalistofcommitsForanitem).  Now, it’s a piece of cake to cross reference the pull request list and the commit list to find the first pull request that has a matching commit id.  Boom!  Performance problem fixed.

Remember that algorithm analysis class we all had to take in undergrad?  From a REST api call perspective, we are looking at O( n ) vs O( 2 )! Moral of the story, Wouter is an absolute BEAST when it comes to the VSTS REST api.  Wouter rocks!


Leave a Reply

Your email address will not be published. Required fields are marked *