VSTS REST为GIT分支创建引用API

时间:2017-07-14 13:28:39

标签: azure-devops-rest-api

根据MS文档,' refs' VSTS的API应该允许您从特定的提交创建一个新的分支,但我似乎无法让它工作。这是我的POC代码(在PowerShell中):

$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;

正如Write-Host语句所报告的那样,请求正文的格式正确,并且我已验证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

1 个答案:

答案 0 :(得分:5)

找到它,并在我的代码示例中进行了更正。要做到这一点,需要考虑两件事。首先,如果您正在使用PSObject并将其转换为JSON,请不要使用管道“|”转换方法,因为它会将1个项目的数组展平为非数组。如果请求正文不包含集合/数组(方括号),则它将无法读取请求。

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

其次,在测试代码时,请确保传递JSON转换后的字符串而不是请求正文中的PSObject(这是我的“DOH!”时刻)。一旦你相应地更换了uri中的括号信息,这个示例代码实际上可以从提交ID创建一个新的分支:

$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;