How to get most recent successful deployment by REST

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.

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.



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 {  
 node {  
 commit {  

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.