Using Azure DevOps REST API with Node.js to retrieve repo permissions

Premier Developer

Premier

I, Brian, have been at Microsoft a very long time. How long? When I joined Microsoft straight out of graduate school, how I remember things, it was a time when the Mac division lead the way in revenue, we also had the Office products for the Mac, we wrote Microsoft Mail for Mac, and I used an Unix email system at work which I remember was one of our email products at the time, and I did my debugging over a serial port. Today, I feel like we are the Microsoft I initially joined; we write software and we don’t care where it runs. Software is our forte.

Today, I have had the great fortune of working with someone that was not raised on the Microsoft stack as I have been, and it has been inspiring and invigorating sharing our knowledge of different languages and platforms.

Allow me to introduce Sidi Merzouk, one of our newest members of Premier Developer. See the following link on Forbes to get an introduction and a sense of Sidi’s developer vigor. Sidi comes with strengths in languages and platforms that is not customary to find in a Microsoft stack developer and has supercharged me with his talents; for example, the node.js code project below, Sidi wrote this code with input from me.

You can find the full REST API Reference at https://docs.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-5.0 used in the sample solution.

Sidi and I had a challenge of pulling/getting permissions of an Azure DevOps Organization programmatically, but we managed to get something going. Thus, we decided to share our findings with you in this blog post.

The following sample can be download from our repo in GitHub using the following link https://github.com/PremierDeveloper/Azure-DevOps. You will need the code to go along with this post.

You will need npm which is distributed with Node.js. So, when you download Node.js, you automatically get npm installed on your computer. After downloading, check that you have node and npm installed by running this command in your shell: node -v. If you have Visual Studio installed, you will have Node.exe but it may not be on your path.

Once you have the project downloaded or cloned, confirmed that Node is installed by navigating to the project directory and run npm install to install the needed dependencies; in this case we will be installing the request library and azure-devops-node-api library.

To begin, you will need to create a personal token from the Azure DevOps dashboard portal as seen in figures 1 and 2.

Figure 1: Navigate to Security

Figure 2: Create new token

Edit the index.js file in the project directory; you will be inserting the personal token you just created and your Azure DevOps services organization URL and saving your file. The URL should look like the this: https://dev.azure.com/YOURORGNAME as in the following figure.

Figure 3: Azure DevOps Services organization URL.

Before we can run our script, we will need to do one last thing which is replacing this line with the actual personal token and URL that points to your Azure DevOps Organization.

E.g.:

  • let’s say your token is the following string “jdfnjdngfjn238fbeifbisdnksknjfdf12”
  • Your organization URL is the following dev.azure.com/simerzou0646

The result should look something like this:

Now we can safely open the terminal navigate to the folder and run node index.js

The result would look something like this:

Et Voila!

For those of you who want to know what’s happening let me give you a quick walkthrough of what’s happening in the index.js file.

There three major components to the code:

  • First, JavaScript is async by default and when we look closely at the code in index.js, you’d find that we are making multiple http request using the azure-devops-node-api library. As you might have picked up that could be a challenge because what if our for loop ends before we get the response back from the server (which is more than likely to be the case), the result will be an empty object. Thus, I had to create a custom for loop function (asyncForEach()) that takes two arguments an array and callback, and we are using async await keyword to halt the code and wait to get the response from our server.

  • Second, Our run() function simply calls Azure DevOps library and get the teams for our org. then we loop through it construct a temporary object called teamsToUse and we pass that our constructorTeams() function.

 

  • Lastly, contructorTeams() function is first taking the object that was passed to it (teamsToUse) turning it into an array of keys and looping through it. In the for loop we are using the REST API to get the permission that a given group has. Once we get the response, we will either create a new object or append the permissions to the existing object. Last but not least, when the for loop ends, we then return the final object called obj.

With that we’ve concluded our little tour that we’ve put together for you. We hope that you’ve enjoyed reading it as much as we’ve enjoyed putting it together. If you have any feedback, questions, comments or suggestions please share your thoughts with us.

Happy Hacking!

 

Premier Developer
Premier Developer

Premier Support for Developers

Follow Premier   

0 comments

    Leave a comment