λ set BITBUCKET_USERNAME=me
λ set BITBUCKET_PASSWORD=secret
λ buildmins org/repo
42
Created
August 31, 2017 15:54
-
-
Save MarkTiedemann/acf70afab3acfaf56479b654c78d4a53 to your computer and use it in GitHub Desktop.
Get build minutes of a Bitbucket repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
powershell -f %~dp0buildmins.ps1 -repository %1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment