2017-07-14 28 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;