Gle*_*len 1 azure-devops-rest-api
根据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;
Run Code Online (Sandbox Code Playgroud)
如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
Run Code Online (Sandbox Code Playgroud)
是否有人使用此API成功创建了新的引用(分支或标签)?以下是指向该API的MS文档的链接,在此先感谢您提供的任何帮助!
找到了它,并在我的代码示例中对其进行了更正。使这项工作需要考虑两件事。首先,如果您使用的是PSObject并将其转换为JSON,请不要使用管道“ |” 转换方法,因为它将1项的数组展平为非数组。如果请求主体不包含集合/数组(方括号),则它将无法读取请求。
$body = $requestList | ConvertTo-Json | Out-String; # Flattens one element array
$body = ConvertTo-Json -InputObject @($requestList); # Does not flatten
Run Code Online (Sandbox Code Playgroud)
其次,在测试代码时,请确保在请求正文中传递的是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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1692 次 |
| 最近记录: |