/
BitBucketTeamCloneAll.ps1
44 lines (39 loc) · 1.66 KB
/
BitBucketTeamCloneAll.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$urlBase = "https://api.bitbucket.org/2.0"
$team = "<team name>"
$apiUser = "<bitbucket api user name>"
$apiPass = "<bitbucket api password>"
$authHeader = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $apiUser,$apiPass)))
$bitBucketUser = "<user>"
$bitBucketPass = "<password>"
$backupDir = "c:\bitbucket\backup"
function clone-repo(){
param([object] $repotToClone)
if ($repo.scm -eq "git"){
$gitclone = "git clone https://{0}:{1}@bitbucket.org/{2} -q" -f $bitBucketUser, $bitBucketPass, $repo.full_name
iex $gitclone
}
if ($repo.scm -eq "hg"){
$hgclone = "hg clone https://{0}:{1}@bitbucket.org/{2}" -f $bitBucketUser, $bitBucketPass, $repo.full_name
iex $hgclone
}
}
Clear-Host
set-location $backupDir
#trial and error (and Fiddler) show that using -Credential option does not send the auth header. the server does not challege back, so manually send the header on the first request
$repositories = Invoke-RestMethod ("{0}/repositories/{1}" -f $urlBase, $team) -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $authHeader)}
while ($repositories){
for($i=0;$i -lt $repositories.pagelen;$i++){
$repo = $repositories.values[$i]
if ($repo.full_name){
"Start Clone {0}" -f $repo.full_name
clone-repo -repotToClone $repo
"Finished Clone {0}" -f $repo.full_name
}
}
if ($repositories.next){
$repositories = Invoke-RestMethod $repositories.next -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $authHeader)}
}
else{
$repositories = $null;
}
}