How to loop through array and use its values in a request

I have two API requests, one that creates an array of pull request ID’s (PRIDs) from a git repo, and another that gets a count of comments associated with an individual pull request. I would like to be able to loop through the array of PRIDs to return a total count of existing pull requests (PRIDs.length) and average comments per PR measure (Total comments/PRIDs.length).

I am a very young Postman user (think newborn, I started with it yesterday), but it seems like there might be a way to store the PRID array in a collection variable, then loop through that array with the second request to obtain the desired counts, I just don’t know how to use the software well enough to do that.

I have a written the test script that creates an array of Pull Request ID’s as such:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repository}/pullrequests?$top=1000&$count=true&searchCriteria.status=all&api-version=5.0

var body = JSON.parse(responseBody);
var PRIDs = ;
for (var i = 0; i<body.value.length;i++){
PRIDs.push(body.value[i].pullRequestId)
}

And have written the test script that counts the number of comments in a single pull request as so:

https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repository}/pullRequests/{PRID}/threads?api-version=5.0

var body = JSON.parse(responseBody);
var count = 0;
for (var i = 0;i<body.value.length;i++) {
if (body.value[i].comments[0].commentType ==“text”) {
count++;
}
}

Any help, or just a nudge in the general right direction, would be very greatly appreciated, thanks!

Also, if this helps at all, or if anyone has suggestions for another problem I’ve encountered I am using the Azure Devops REST API. I have noticed that when returning the Pull Requests, it caps the $top field at 1000 instances, and since my repo has 2194 Pull Requests thus far, it means I will have to run this script (assuming it is possible) three times to get all of the data I want. If you know of an easy way to automate this into one request that would be splendid!

Hello @shepsims, welcome to the community! :wave:

I can help you get around the first problem that you’re facing regarding storing the PRIDs and using that to get the comments per PR using the second request.

I wrote this collection to help you around with some sample code:
https://www.getpostman.com/collections/4f4d45aadc1c9e0f9eaf

Step 1: We’ll be using the collection runner feature in Postman to get around this problem.
If you need to run it one request at one time manually then you can do that too by tweaking the logic a bit. I am sure you’ll get your head around that once you see the scripts inside the collection that I wrote.

Also, we need to build a control logic which will help us in determining when to stop and when to keep the collection running, so for that I have used the feature of environments and postman.setNextRequest

Step 2: You need to create a new empty environment (you can refer the docs as to how you can create a new environment).

Step 3: Import the collection that I gave the link to above (again - https://www.getpostman.com/collections/4f4d45aadc1c9e0f9eaf) and you can see that there are two requests inside, the first is to fetch the PRIDS and the second is to get comments for each PRID.

Now, what we are gonna do is something like this:

  1. Get all PRIDS
  2. Get comment for each PRID and write some logic around how you plan to use the comments.

Step 3: Launch the collection runner and make sure you select the new environment that we created just now.

Step 4: Run the collection and right now, it uses some sample data that I faked using the Postman Mocks

You’ll see that initially I get an array for 4 PRIds and the collection runner loops over each ID and gets the comment for the same.
This logic will run for n number of PRIDs.

I have documented the Tests scripts of both the requests and I am hoping that by reading the docs and the comments and code, you’ll be able to get around this problem.

All in all, you need to just basically replace the endpoints that I wrote with the actual endpoints and build the request.

Next you can write your logic and get your work done!

If you’re stuck somewhere, let me know!
Happy to help :smiley:

Edit: FYI we’ve also published a collection demonstrating this that you can fork and try for yourself here:
https://www.postman.com/postman/workspace/postman-answers/collection/9215231-25b72aab-2c6a-4941-832d-dd47e142ff2a?ctx=documentation

4 Likes

Thank you! This is super useful!

1 Like

I too need some help with Postman.

I need to be able to collect a lot of users ID’s (using Okta API’s)
Then be able to Use all of the ID’s to find out what Applications they have access to.
I’m able to do one at a time, but not sure how to get it to work with multiple people.

Thanks in Advance
Bryan

Can you please send v2 for this collection

Hey @singhsivcan could you add some clarity here. I’m not understanding how this code in the collection you reference would stop being executed:

const usernames = pm.collectionVariables.get("usernames");

if (usernames && usernames.length > 0){
    postman.setNextRequest("Loop Post Parameter");
} else {
    postman.setNextRequest(null);
}
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

What is it that would be “trimming down” on the usernames array so that its length would no longer be > 0? i.e at what point would the execution fall into the else code block?

Thank you.

P.S. And this will only work or “kick in” when the entire collection is run? Not just when running one call/request?

This is a closed\answered topic, so ideally this should have been a new topic\question.

The usernames variable is an array, and its being reduced by this code in the pre-request script.

let currentUsername = usernames.shift();
console.log(currentUsername)
pm.collectionVariables.set("username", currentUsername);
pm.collectionVariables.set("usernames", usernames);

Postman uses JavaScript under the hood and this is using a method available to arrays called shift().

This retrieves the first element in an array (and also deletes it from the array).

The last line in that code is re-saving the array (which should have one less entry each time its run).

Thank you @michaelderekjones, now that makes sense. Gotta keep remembering to look in all the various tabs in the Postman UI.

I’m curious, how can I tell if a topic is closed? I saw the reply button was enabled so I just used it.

It normally tells you when you try to respond covering the preview pane. Like its doing for me now.

image