根据MS文档,VSTS的'refs'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成功创建一个新的ref(分支或标记),如果有的话,你能帮我识别我做错了什么吗?以下是API的MS文档链接,并提前感谢您提供的任何帮助!
1 回答
找到它,并在我的代码示例中更正它 . 要做到这一点,需要考虑两件事 . 首先,如果您正在使用PSObject并将其转换为JSON,请不要使用管道“|”转换方法,因为它会将1个项目的数组展平为非数组 . 如果请求正文不包含集合/数组(方括号),则它将无法读取请求 .
其次,在测试代码时,请确保传递JSON转换的字符串而不是请求正文中的PSObject(这是我的“DOH!”时刻) . 一旦您相应地更换了uri中的括号内信息,此示例代码实际上可以从提交ID创建新分支: