Day 68: Accessing VSO Version Control using Powershell–Part 1

Welcome to Day 68 of the “100 Days of DevOps with PowerShell”! For background on our goals in this series, see Announcing the “100 Days of Devops with PowerShell” Series here at SCC.

In my Day 58 post, I showed you how to configure your alternate authentication credentials for accessing Visual Studio Online, and used the PowerShell Invoke-RestMethod to access VSO using its REST-based APIs.  In this post, I’m going to show you how to use the APIs and PowerShell to retrieve changeset information from VSO.

Get a List of Changesets

I can retrieve a list of all the changesets in a project collection using the following URI:

https://{account}.visualstudio.com/defaultcollection/_apis/tfvc/changesets?api-version={version}

Building off the script from Day 58, here is a script that will retrieve all the changesets from my team project collection

I’m using Invoke-RestMethod to send a GET request to the URI listed above.  When I execute the above script, I see the following output

image

Ok, that looks a little strange.  That’s because most of the APIs used by VSO return JSON data. JSON stands for JavaScript Object Notation, and is a lightweight data-interchange format that is easy for people to read and write.  Essentially it is a bunch of name/value pairs.  An easy way to see the format of the JSON is to take the URI above and enter it directly into a web browser. You will be prompted to download a .json file (which is just a text file).  Here is a partial example of what the JSON data looks like from the above script:

I have a “count” variable, that tells me I have 14 changesets in this file. Then, for each changeset, I have the changeset id, the URL for accessing detailed information about the changeset, who created the changeset, who checked in the changeset, the date it was created, and any comments added to it.

Get the details specific changeset

As I mentioned above, I can use the specific changeset URL to get detailed information about the changeset. For example, for changeset 20, I would use the following URL:Change set 20

https://{account}.visualstudio.com/defaultcollection/_apis/tfvc/changesets/20?api-version=1.0

The following script uses the URL for changeset 20 to retrieve details about the changeset.

 

I get the following output from the script:

image

As you can see above, I can see all the information that I saw in the previous JSON file, such as Changeset Id, URL, and author.  But now I also have a _links area, which is itself a section of JSON data.  If I take the URL for Changeset 20 and enter it into a web browser, and I see the detailed JSON data:

 

Notice that the _links section gives me URLs for three different pieces of information: self, changes, and workitems.  Self is the same URL I used above to retrieve the detailed changeset information. Changes is a URL that will allow me to see all the files and folders associated with this changeset. And Workitems is a URL that will allow me to see all the workitems associated with this changeset.

This gets us started down the path of interacting with VSO version control. In my next post, I’ll show you how to pull a list of files and work items associated with a changeset, and how to download the files.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.