使用 vsts rest api 将 git 存储库从一个 vsts 团队项目导入到另一个团队项目

Importing a git repo from one vsts team project to another team project using vsts rest api

想要启动从项目 A 到项目 B 的 VSTS git 存储库导入。编写以下脚本以通过 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'

当传递现有的空 repo 时,它会抛出错误的请求异常

    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?

Post url 使用

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"}}}

您需要将源代码库(来自项目 A)添加为目标项目(项目 B)的外部 Git 端点,然后提供参数serviceEndpointId create import requests REST API。 详细步骤如下:

  1. 在目标项目中创建外部Git端点

    在项目 B -> 服务中心 (https://account.visualstudio.com/projectB/_admin/_services) -> New Service Endpoint -> External Git -> add source repo from project A as the server URL (https://account.visualstudio.com/projectA/_git/sourcerepo) -> 确定。

  2. 获取端点id

    使用REST API如下:

    GET https://marinaliu.visualstudio.com/GitTest/_apis/serviceendpoint/endpoints/?api-version=4.1-preview.1
    

    然后从响应中获取端点 ID(假设它是 c534772b-bf52-442f-abd0-544d6bf76ed9)。

  3. 导入git repo到目标项目

    然后将源代码库从项目A导入到项目B:

    POST https://account.visualstudio.com/projectB/_apis/git/repositories/targetrepo/importRequests?api-version=4.1-preview.1
    

    application/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