Last active
September 14, 2020 14:33
-
-
Save agarman/98cf5c1f3fc0fe03f4f58fe756193831 to your computer and use it in GitHub Desktop.
Lists all repos in project in bitbucket
This file contains hidden or 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
| #!/usr/bin/env pwsh -File | |
| param( | |
| [switch] [Parameter(ParameterSetName = "teams")] $teams, | |
| [switch] [Parameter(ParameterSetName = "projects")] $projects, | |
| [switch] [Parameter(ParameterSetName = "repos")] $repos, | |
| [switch] [Parameter(ParameterSetName = "prs")] $prs, | |
| # Parameters specified by switch command | |
| [Parameter(Mandatory = $true, ParameterSetName = "projects")] | |
| [Parameter(Mandatory = $true, ParameterSetName = "repos")] | |
| [Parameter(Mandatory = $false, ParameterSetName = "prs")] | |
| $team, | |
| [Parameter(Mandatory = $false, ParameterSetName = "prs")] | |
| $repo, | |
| [Parameter(Mandatory = $false, ParameterSetName = "repos")] | |
| $project, | |
| # The following are valid for all commands | |
| $fields, | |
| $user = (Get-Content -Path "./.user"), | |
| $secret = (Get-Content -Path "./.secret"), | |
| $bitbucket = "https://api.bitbucket.org/2.0/" | |
| ) | |
| $APIs = @{ | |
| teams = @{ | |
| path = "teams?role=member" | |
| fields = $fields ?? "values.display_name,values.uuid" | |
| } | |
| projects = @{ | |
| path = "teams/$($team)/projects/" | |
| fields = $fields ?? "values.key,values.description" | |
| } | |
| repos = @{ | |
| path = "repositories/$($team)/" | |
| fields = $fields ?? "values.full_name,values.updated_on,values.language,values.slug" | |
| } | |
| prs = @{ | |
| path = "repositories/$($team)/$($repo)/pullrequests" | |
| fields = $fields ?? "values.title,values.author.display_name,values.updated_on,values.state" | |
| } | |
| } | |
| function base64 { | |
| param ($str) | |
| [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($str)) | |
| } | |
| function headers { | |
| @{ Authorization = "Basic $((base64 "$($user):$($secret)"))" } | |
| } | |
| function request { | |
| param ($api) | |
| $request = @{ | |
| sort = "-updated_on" | |
| fields = "next,$($fields ?? $api.fields ?? "*")" | |
| } | |
| if ($project) { $request.Add("q", "project.key=""$($project)""") } | |
| $request | |
| } | |
| function invoke-bitbucket { | |
| param ($command) | |
| $api = $APIs[$command] | |
| $uri = "$($bitbucket)$($api.path)" | |
| # Invoke Bitbucket API | |
| while ($uri) { | |
| $resp = (Invoke-RestMethod -Uri $uri -Body $(request $api) -Headers $(headers)) | |
| $values += $resp.values | |
| $uri = $resp.next | |
| } | |
| $values | |
| } | |
| invoke-bitbucket $PSCmdlet.ParameterSetName |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prereqs...
from https://bitbucket.org/account/settings/ get your username... save it to
.userfrom https://bitbucket.org/account/settings/app-passwords/ create an app password... save it to
.secretList teams with:
./bitbucket.ps1 -teamsList projects with:
./bitbucket.ps1 -projects -team "$TEAM"List repos with:
./bitbucket.ps1 -repos -team "$TEAM" -project "$PROJECT"Clone all your git repos in a project: