Skip to content

Instantly share code, notes, and snippets.

@MarkTiedemann
Created August 31, 2017 15:54
Show Gist options
  • Save MarkTiedemann/acf70afab3acfaf56479b654c78d4a53 to your computer and use it in GitHub Desktop.
Save MarkTiedemann/acf70afab3acfaf56479b654c78d4a53 to your computer and use it in GitHub Desktop.
Get build minutes of a Bitbucket repository
@echo off
powershell -f %~dp0buildmins.ps1 -repository %1
param(
[parameter(mandatory=$true)] $repository
)
function base64Encode ($text) {
return [Convert]::ToBase64String(
[System.Text.Encoding]::ASCII.GetBytes($text)
)
}
function pageLink ($number) {
"https://api.bitbucket.org/2.0/repositories/$repository/pipelines/?page=$number"
}
function roundUp ($number) {
[Math]::Round($number, [System.MidpointRounding]::AwayFromZero)
}
function remaining ($page) {
if ($page.pagelen -eq 0) {
0
} else {
(roundUp ($page.size / $page.pagelen)) - $page.page
}
}
function nextLink ($page) {
if ((remaining $page) -gt 0) {
pageLink ($page.page + 1)
} else {
$null
}
}
function buildSecs ($page) {
$total = 0
$page.values | % {
$total += $_.build_seconds_used
}
$total
}
$basicAuth = base64Encode ($env:BITBUCKET_USERNAME + ':' + $env:BITBUCKET_PASSWORD)
function api ($url) {
invoke-webrequest $url -headers @{
authorization = 'basic ' + $basicAuth
} |
convertfrom-json
}
$next = pageLink 1
$buildSecs = 0
do {
$page = api $next
$buildSecs += (buildSecs $page)
$next = nextLink $page
} while ($next -ne $null)
roundUp ($buildSecs / 60)
λ set BITBUCKET_USERNAME=me
λ set BITBUCKET_PASSWORD=secret
λ buildmins org/repo
42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment