Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 3

How to get most recent successful deployment by REST

Solved! Go to Solution.

As part of our deployment flow, before creating a new deployment I'd like to show the user a compare link and summary commit log between the current active deployment and the SHA that's now ready to deploy.

 

However, it looks like this is actually a bit tricky to figure out through the API?

 

I don't see an option to filter the list deployments API call (e.g. to only show successful ones) and the response to that call doesn't itself include any info about the current status of that deployment.

 

It looks like the only way to get this info would be to call "List deployments" then iterate over the result and call "list deployment statuses" for each deployment in turn until I find one that's successful? That doesn't seem very efficient (though hopefully usually I'd not have to go too far back in history to find it).

 

Is there a better way to do this? I note that both the most recent successful and the status of each deployment are shown on the deployments page within github so I'm wondering if that data is actually available somewhere and I'm just not seeing it.

2 Replies
Community Manager
Message 2 of 3

Re: How to get most recent successful deployment by REST

Hi @acoulton,

 

Thank you for being here! Perhaps one of our users might have more insight into this, but I can't think of a better way to reach your goal other than how you are working it thus far.

 

As far as I know, there is no way to get the computed value in the deployments payload to indicate the latest status. I'll dig a little deeper and post any updates here.

 

Best,

Andrea

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!

Solution
Copilot Lvl 2
Message 3 of 3

Re: How to get most recent successful deployment by REST

Thanks Andrea,

 

I've actually realised it's much easier to solve with GraphQL. In case it helps anyone in future, the following query does the trick:

query($repo_owner:String!, $repo_name:String!, $deployment_env:String!) {
repository(owner: $repo_owner, name: $repo_name) {
deployments(environments: [$deployment_env], first: 30) {
edges {
cursor
node {
state
commit {
oid
}
}
}
}
}
}

 

And then it's just a case of finding the first node in the returned result that has state === 'ACTIVE'.

 

Obviously this could also be done with a smaller page size and then load the next page in the unlikely event there's loads of failed / pending / whatever deployments since the last successful one but it was simpler for me to just assume it's not going to be more than 30 deployments ago and take a bigger page in a single request.

 

One thing that did trip me up was that at first running the GraphQL query with my app's installation token was just producing a "Could not resolve to a Repository with the name '...'" error.

 

After a bit of trial and error, I realised that although the app had access to deployments and code, it didn't have access to repository metadata.

 

This isn't an issue with REST - you can call /repos/foo/bar/deployments etc even though the /repos/foo/bar method isn't accessible.

 

But it seems with GraphQL it prevents the query finding the repo at all, even if you don't ask for any metadata and just want to traverse through into the collection of deployments.

 

Granting read access to repository metadata cleared the error.