【问题标题】:Importing a git repo from one vsts team project to another team project using vsts rest api使用 vsts rest api 将 git repo 从一个 vsts 团队项目导入另一个团队项目
【发布时间】:2018-04-20 11:05:31
【问题描述】:

想要启动从项目 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"}}}

【问题讨论】:

    标签: azure-devops azure-devops-rest-api


    【解决方案1】:

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

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

      在项目 B -> Services Hub (https://account.visualstudio.com/projectB/_admin/_services) -> 新服务端点 -> 外部 Git -> 添加来自项目 A 的源代码库作为服务器 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 中的源 repo 导入到项目 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 2013-06-14
      • 2016-02-10
      • 1970-01-01
      相关资源
      最近更新 更多