Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I get the most recent pipeline build from the API?

Douglas Reith May 24, 2018

Bitbucket does not offer an overview of a team's repo pipeline build statuses. I think this is crucial if you have a lot of repositories and you want to see which builds are failing. Clicking through each repository is a nightmare.

I asked that question here:

https://community.atlassian.com/t5/Bitbucket-questions/Where-can-I-get-a-view-of-all-the-pipeline-builds-for-a-team/qaq-p/786264

 

There is no solution, so to resolve the issue I wrote a script that highlights the status of the build with the highest number:

# Requires `curl` and `jq`
# Goes something like this:

echo -n $repo pipeline state:
pipelineStatus=$(curl -s -u <BITBUCKET_CREDENTIALS> -H 'Content-Type: application/json' 'https://api.bitbucket.org/2.0/repositories/$account/$repo/pipelines/?pagelen=100' | jq '.values |max_by(.build_number) |.state.result.name')
if [[ "$pipelineStatus" =~ FAIL ]]
then
echo -e "\e[0;31m\e[1m"$pipelineStatus"\e[0m"
else
echo $pipelineStatus
fi

 

`pagelen` is required to ensure that we get more than 10 builds listed. They're listed OLDEST to NEWEST, e.g. 1 -> 10.

The problem is that even with `pagelen` set to 100 eventually it'll tail off after 100 builds. And besides we're really only really interested in max(build_number) - we don't need all the other content.

A solution to get the most recent build via API would be ideal (i.e max(build_number))

However, without that, can we use sorting? So we could sort it like:

# Adding this to the API request does not work:
?pagelen=1&sort=-build_number

# the error is
# {"error": {"message": "Bad request", "data": {"arguments": {}, "key": "rest-service.rest-service.invalid-sort-attribute"}, "detail": "Invalid sort attribute provided 'build_number'"

Then we would only need `pagelen=1` because we'd have the highest build_number.

1 answer

1 vote
Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 31, 2018

Hi Douglas,

You could try sorting with created_on. For example:

'https://api.bitbucket.org/2.0/repositories/$account/$repo/pipelines/?pagelen=1&sort=-created_on'

That will get you the latest build for that repository.

We currently don't have support for ordering by build_number at the moment. So created_on will need to be used instead.

Thanks,

Phil

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events