Day 73: Accessing VSO Version Control using Powershell–Part 2

Welcome to Day 73 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 68 post, I showed you how to retrieve a list of changesets from Visual Studio Online, and how to retrieve information related to a specific changeset.  In this post I’m going to build off that foundation, showing you how to retrieve both the work items related to a changeset, as well as the files.

Let’s take the script I was using in Day 68, and modify it to retrieve information for ChangeSet 29.

Here is the output from that script:

image

If I take the url parameter for the changeset and enter it in a web browser, I can retrieve the detailed JSON data:

We discussed this data in the previous post. The portion I’m interested in for this post is the _links section. Using the _links section of the JSON data, I can retrieve both the work items associated with the changeset, as well as the files associated with the changeset.

Retrieving Work Items

To retrieve the work items associated with the changeset, I need to access the URL from the workitems section of the _links section. I can use this URL to retrieve all work items associated with the changeset, and can then iterate over those work items to grab individual work item data. I can use the following script:

And here is the output from that script:

image

if I want to see what the JSON payload looks like for this, I can enter the $wihref URL in a web browser:

As I can see, I could have multiple values (i.e. multiple work items) associated with a particular changeset. The code in the above script will iterate over all the returned work items.

Retrieve Checked In Files From Changeset

One of the reasons to access a changeset is to retrieve the files that were checked in. I can do that using the changes section of the _link section. The changes URL will return a list of all the changes (i.e. files) that were included in the changeset.  I can then iterate over that list of changes to view the changed files, using the following PowerShell script:

The output from my script is as follows:

image

As you can see, the file variable contains the file that was checked in (shown by the red box above).

You should now have  a good understanding of how you can use the PowerShell Invoke-RestMethod to access Visual Studio Online using its REST-based APIs. Make sure to consult the Visual Studio Online documentation, to see information on new APIs as new functionality is turned on.

Leave a Reply

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