Skip to content

Instantly share code, notes, and snippets.

@kkamegawa
Last active December 24, 2016 10:40
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 kkamegawa/eb47e252d1f9529ab306fc1f4f756ff6 to your computer and use it in GitHub Desktop.
Save kkamegawa/eb47e252d1f9529ab306fc1f4f756ff6 to your computer and use it in GitHub Desktop.
Copy from VSTS Git Repository to public Gist. It does not complete to copy ...
# Requires -Version 4.0
# {account} is VSTS account name
# {repositoryName} is VSTS Git Repository Name
# {GitHubAccount} is GitHub Account name
$vstshost = 'https://{account}.visualstudio.com/Private/_apis/git/repositories/{repositoryName}/'
#GitHub PAT
$tokenGitHub = ''
$Base64TokenGitHub = [System.Convert]::ToBase64String([char[]]$tokenGitHub);
$headersGitHub = @{
Autorization = 'Basic {0}' -f $Base64TokenGitHub;
};
$gists = Invoke-RestMethod -Headers $headersGitHub -Method Get -uri 'https://api.github.com/users/{GitHubAccount}/gists'
#VSTS PAT
$TokenVSTS = ''
$authVSTS = ("{0}:{1}" -f '{VSTSUserName}',$TokenVSTS)
$Base64TokenVSTS = [System.Convert]::ToBase64String([char[]]$authVSTS);
$headersVSTS = @{
Autorization = 'Basic {0}' -f $Base64TokenVSTS;
};
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f '{VSTSUserName}', $TokenVSTS)))
$vstsuri = $vstshost + 'commits?api-version=1.0'
#get recent commits
$result = Invoke-RestMethod -Uri $vstsuri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
#get commit detail
$vstsuri = $result[0].value[0].url
$result = Invoke-RestMethod -Uri $vstsuri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
#get change files
$vstsuri = $result._links.changes.href
$result = Invoke-RestMethod -Uri $vstsuri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
#action is add or modify?
foreach($commit in $result.changes){
$vstsfile = $commit.item.path.substring(1)
foreach($gist in $gists){
foreach($gistfile in $gist.files){
$gistfile = $gistfile | Get-Member -MemberType NoteProperty | select Name
if($gistfile.Name -eq $vstsfile){
if($commit.changetype -eq "edit") {
}
$target = $gist
break;
}
}
}
#these codes under constraction...
if($target -eq $null){
if($commit.changetype -eq "add"){
$vstsuri = $vstshost + 'items?scopePath=' + $vstsfile + '&api-version=1.0'
$result = Invoke-RestMethod -Uri $vstsuri -Method Get -ContentType "application/octet-stream" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$gists = Invoke-RestMethod -Headers $headersGitHub -Method post -uri 'https://api.github.com/users/{GitHubAccount}/gists'
}
}
}
#these codes under constraction...
$commitresult = Invoke-RestMethod -Uri $commituri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment