2017-07-14 157 views
1

根據MS文檔,VSTS的'refs'API應該允許您從特定的提交中創建一個新的分支,但我似乎無法使其工作。這裏的POC代碼我(在PowerShell中):VSTS REST參考文獻創建GIT分支的API

$uri = 'https://{account}.visualstudio.com/{project}/_apis/git/repositories/{repository}/refs?api-version=1.0'; 

[array]$requestList = @(); 
$requestObj = New-Object -TypeName psobject; 
$requestObj | Add-Member -MemberType NoteProperty -Name "name" -Value 'refs/heads/api-branch1'; 
$requestObj | Add-Member -MemberType NoteProperty -Name "oldObjectId" -Value "0000000000000000000000000000000000000000"; 
$requestObj | Add-Member -MemberType NoteProperty -Name "newObjectId" -Value "272c5f931889e5c6cc61a6fdb19ad00eeebf2d77"; 
$requestList += @($requestObj); 

$header = Get-AuthHeader; 
$body = ConvertTo-Json -InputObject @($requestList); 
Write-Host $body; 

$response = Invoke-RestMethod -Uri $uri -Headers $header -Method Post -Body $body -ContentType application/json; 

Write-Host $response; 

的請求主體的格式是否正確,如由寫主機聲明報道,我已經驗證了newObjectId是正確的提交ID。然而,當我運行該腳本,我得到以下錯誤:

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: refUpdates","typeName":"System.ArgumentNullException, mscorlib, Version=14.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089","typeKey":"ArgumentNullException","errorCode":0,"eventId":0} 
At C:\Users\gappleton\Documents\VSTS\Scripts\Test-Methods.ps1:119 char:13 
+ $response = Invoke-RestMethod -Uri $uri -Headers $header -Method Post ... 
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException 
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand 

有沒有人使用這個API來創建一個新的裁判(分支或標記)成功,如果是的話,你可以幫我鑑定,我什麼做錯了?以下是關於API的MS文檔的鏈接,並且預先感謝您提供的任何幫助!

Git Refs : VSTS REST API Documentation

回答

3

發現了它,並在我的代碼示例校正它。需要考慮兩件事才能完成這項工作。首先,如果您使用PSObject並將其轉換爲JSON,請不要使用管道「|」轉換方法,因爲它會將1個數組的數組變爲非數組。如果請求主體不包含集合/數組(方括號),它將無法讀取請求。

$body = $requestList | ConvertTo-Json | Out-String; # Flattens one element array 
$body = ConvertTo-Json -InputObject @($requestList); # Does not flatten 

二,測試代碼時,請確保您通過JSON字符串轉換,而不是在請求主體的PSObject(這是我的一個「衛生署!」的時刻)。此示例代碼實際上用於從提交ID創建新分支,一旦您相應地替換了uri中的括號內的信息:

$uri = 'https://{account}.visualstudio.com/{project}/_apis/git/repositories/{repository}/refs?api-version=1.0'; 

[array]$requestList = @(); 
$requestObj = New-Object -TypeName psobject; 
$requestObj | Add-Member -MemberType NoteProperty -Name "name" -Value 'refs/heads/api-branch1'; 
$requestObj | Add-Member -MemberType NoteProperty -Name "oldObjectId" -Value "0000000000000000000000000000000000000000"; 
$requestObj | Add-Member -MemberType NoteProperty -Name "newObjectId" -Value "272c5f931889e5c6cc61a6fdb19ad00eeebf2d77"; 
$requestList += @($requestObj); 

$header = Get-AuthHeader; 
$body = ConvertTo-Json -InputObject @($requestList); 
Write-Host $body; 

$response = Invoke-RestMethod -Uri $uri -Headers $header -Method Post -Body $body -ContentType application/json; 

Write-Host $response;