想要启动从项目A到项目B的VSTS git repo导入。编写以下脚本以通过REST API创建导入请求。如下所述,它给出了错误的请求400。有线索吗?
param(
<parameter(mandatory=$true)>
[string] $token,
<parameter(mandatory=$true)>
[string] $collectionUri,
<parameter(mandatory=$true)>
[string] $TargetTeamProject,
<parameter(mandatory=$true)>
[string] $TargetGitRepoName,
<parameter(mandatory=$true)>
[string] $SourceGitRepoUrl
)
$User=""
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$token)));
$header = @{Authorization=("Basic {0}" -f $base64AuthInfo)};
$Uri = $collectionUri + $TargetTeamProject + '/_apis/git/repositories/' + $TargetGitRepoName + '/importRequests?api-version=4.1-preview.1'
$ImportRequestData = '{"parameters": {"gitSource": {"url": "' + $SourceGitRepoUrl + '"}}}'
write-host 'calling:' $Uri
write-host 'with data:' $ImportRequestData
$ImportResponse = Invoke-RestMethod -Method Post -ContentType application/json -Uri $Uri -Body $ImportRequestData -Headers $header
$ImportResponse
用
调用.\CreateImportRequest.ps1 -token '*************************************' -collectionUri 'https://myvsts.visualstudio.com/DefaultCollection/' -TargetTeamProject 'HasiTempJava' -TargetGitRepoName 'impfromhasiJava' -SourceGitRepoUrl 'https://myvsts.visualstudio.com/DefaultCollection/_git/HasiJava'
当传递现有的空仓库时,它会抛出错误的请求异常
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At C:\Users\chamindac\Desktop\CreateImportRequest.ps1:33 char:19
+ ... tResponse = Invoke-RestMethod -Method Post -ContentType application/j ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand</parameter(mandatory=$true)></parameter(mandatory=$true)></parameter(mandatory=$true)></parameter(mandatory=$true)></parameter(mandatory=$true)>
When a new repo name used it is giving below error which is correct I believe since url refers to a non existing repo
<parameter(mandatory=$true)><parameter(mandatory=$true)><parameter(mandatory=$true)><parameter(mandatory=$true)><parameter(mandatory=$true) class="">Invoke-RestMethod : {"$id":"1","innerException":null,"message":"TF401019: The Git repository with name or identifier impfromhasiJavaNew does not exist or you do not have permissions for the operation you are
attempting.","typeName":"Microsoft.TeamFoundation.Git.Server.GitRepositoryNotFoundException, Microsoft.TeamFoundation.Git.Server","typeKey":"GitRepositoryNotFoundException","errorCode":0,"eventId":3000}
At C:\Users\chamindac\Desktop\CreateImportRequest.ps1:33 char:19
+ ... tResponse = Invoke-RestMethod -Method Post -ContentType application/j ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand </parameter(mandatory=$true)></parameter(mandatory=$true)></parameter(mandatory=$true)></parameter(mandatory=$true)></parameter(mandatory=$true)>
As per documentation (https://docs.microsoft.com/en-us/rest/api/vsts/git/import%20requests/create) only required parameter is the source git repo url. Is there any additional parameter requirements?
发布网址
https://myvsts.visualstudio.com/DefaultCollection/HasiTempJava/_apis/git/repositories/impfromhasiJava/importRequests?api-version=4.1-preview.1
请求正文
{"parameters": {"gitSource": {"url": "https://myvsts.visualstudio.com/DefaultCollection/_git/HasiJava"}}}
答案 0 :(得分:1)
您需要将源仓库(来自项目A)添加为目标项目的外部Git端点(项目B),然后提供参数 serviceEndpointId
< / strong> for create import requests REST API。
详细步骤广告如下:
在目标项目中创建外部Git端点
在项目B中 - &gt;服务中心(https://account.visualstudio.com/projectB/_admin/_services) - &gt;新服务端点 - &gt;外部Git - &gt;从项目A添加源代码作为服务器URL(https://account.visualstudio.com/projectA/_git/sourcerepo) - &gt;行。
获取端点ID
使用REST API,如下所示:
GET https://marinaliu.visualstudio.com/GitTest/_apis/serviceendpoint/endpoints/?api-version=4.1-preview.1
然后从响应中获取端点ID(假设它是c534772b-bf52-442f-abd0-544d6bf76ed9
)。
将git repo导入目标项目
然后将源回购从项目A导入项目B:
POST https://account.visualstudio.com/projectB/_apis/git/repositories/targetrepo/importRequests?api-version=4.1-preview.1
应用/ JSON:
{
"parameters": {
"gitSource": {
"url": "https://marinaliu.visualstudio.com/projectA/_git/sourcerepo"
},
"serviceEndpointId": "c534772b-bf52-442f-abd0-544d6bf76ed9"
}
}
更多详细信息,您可以参考博客Import a Git Project with REST API between VSTS Team Projects。