Skip to content

Instantly share code, notes, and snippets.

@agarman
Last active September 14, 2020 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agarman/98cf5c1f3fc0fe03f4f58fe756193831 to your computer and use it in GitHub Desktop.
Save agarman/98cf5c1f3fc0fe03f4f58fe756193831 to your computer and use it in GitHub Desktop.
Lists all repos in project in bitbucket
#!/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
@agarman
Copy link
Author

agarman commented Sep 14, 2020

Prereqs...
from https://bitbucket.org/account/settings/ get your username... save it to .user
from https://bitbucket.org/account/settings/app-passwords/ create an app password... save it to .secret

List teams with:
./bitbucket.ps1 -teams

List 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:

./bitbucket.ps1 -repos -team "$TEAM" -project "$PROJECT" | ForEach-Object {
  git clone git@bitbucket.org:$($_.full_name).git $PROJECT/$($_.slug)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment