我可以将存储库从VSTS导入到VSTS

时间:2016-10-03 15:39:36

标签: git azure-devops

我一直在探索VSTS API并尝试将存储库从一个项目复制到另一个项目。我收到错误this remote has never connected。我通过网站尝试了这个,我得到了同样的错误,所以我猜我的代码是正确的。

我想知道是否有人之前已经这样做过,或者有人会在他们的一个VSTS实例上尝试这一点。 如果您需要代码示例,请告诉我。除了现有的here之外,它并不多。

更新 这是代码。 (归结为控制台应用程序)

  

Models.cs

public class ImportProjectRequest
{
    public Parameters parameters { get; set; }
}

public class Parameters
{
    public Gitsource gitSource { get; set; }
    public string serviceEndpointId { get; set; }
    public bool deleteServiceEndpointAfterImportIsDone { get; set; }
}

public class Gitsource
{
    public string url { get; set; }
}

public class ImportProjectCommand
{
    public Uri baseUri { get; set; }
    public string accessToken { get; set; }
    public string TeamProjectName { get; set; }
    public string CollectionName { get; set; }
    public string RepositoryId { get; set; }
    public ImportProjectRequest RequestObject { get; set; }
}
  

Program.cs的

class Program
{
    static void Main(string[] args)
    {
        HttpClient client = new HttpClient();

        ImportProjectCommand command = new ImportProjectCommand()
        {
            baseUri = new Uri("https://instance-name.visualstudio.com"),
            accessToken = "<permanent-access-token>",
            CollectionName = "DefaultCollection",
            TeamProjectName = "Test Project 3",
            RepositoryId = "Test Project 3",
            RequestObject = new ImportProjectRequest()
            {
                parameters = new Parameters()
                {
                    gitSource = new Gitsource()
                    {
                        url = "https://instance-name.visualstudio.com/_git/Test%20Project"//Server URL from Extrenal Git configured in the serivces tab of Test Project 3
                    },
                    serviceEndpointId = "0f3c8fd4-38bb-4f0f-95fc-8942dd8f53a3", //Retrieved ID from https://instance-name.visualstudio.com/Test%20Project%203/_apis/distributedtask/serviceendpoints?api-version=3.0-preview.1
                    deleteServiceEndpointAfterImportIsDone = false

                }

            }

        };

        string version = "?api-version=3.0-preview";
        var parameters = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", string.Empty, command.accessToken)));
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", parameters);
        var url = string.Format("{0}{1}/{2}/_apis/git/repositories/{3}/importRequests",
            command.baseUri,
            string.IsNullOrEmpty(command.CollectionName) ? "DefaultCollection" : command.CollectionName,
            command.TeamProjectName,
            command.RepositoryId);
        HttpContent result;
        using (HttpResponseMessage response = client.PostAsJsonAsync(url + version, command.RequestObject).Result)
        {
            response.EnsureSuccessStatusCode();
            result = response.Content;
        }
    }
}

更新2: 感谢@starain我能够部分解决错误。导致该错误是因为我的项目和存储库名称中有空格。 代码仍然无效,但我现在可以通过门户网站导入存储库。

我现在陷入了错误,VSTS给了我一条消息说

Service Endpoint not accessible to user defd6044-4747-4926-9726-4d19fa5d2d26

注意:上面的GUID是服务enpoint的GUID,而不是用户。

我的身份验证是正确的,因为我成功通过REST API创建了一个新的服务端点。

2 个答案:

答案 0 :(得分:4)

根据我的测试,我成功将存储库从VSTS导入另一个VSTS。您可以参考这些步骤来导入存储库。

  1. 点击您的姓名=&gt;安全性=&gt;选择备用身份验证凭据=&gt;创建备用身份验证凭据
  2. 转到目标VSTS,单击团队项目管理页面中的服务选项卡,然后单击新服务端点=&gt;外部Git =&gt;键入连接名称,服务器URL(源VSTS中的git存储库),用户名和密码(备用帐户,步骤1)
  3. Get a list of service endpoint通过REST API(XXX)(例如https://[target VSTS名称] .visualstudio.com / [team project] / _ apis / distributedtask / serviceendpoints?api-version = 3.0-preview.1]之后,您可以获得该服务端点的服务端点ID(步骤2)
  4. Create a request to import a repository,例如: 地址(发布请求):https://[target vsts name] .visualstudio.com / [team project] / _ apis / git / repositories / [repository name] /importRequests?api-version=3.0-preview
  5. JSON:

    {
      "parameters":
        {
          "gitSource":
            {
              "url": "[git repository address in source VSTS]"
            },
          "serviceEndpointId": "[service endpoint id, step 3]",
          "deleteServiceEndpointAfterImportIsDone": false
        }
      } 
    

    注意:存储库名称和团队项目名称不能包含空格。

答案 1 :(得分:1)

我可以确认我能够使用starain描述的相同步骤导入存储库,唯一的区别是我使用API​​创建了端点并使用PAT令牌创建端点而不是依赖于替代凭证(但这不应该改变任何东西)

一切顺利,是复制错误的唯一方法

Service Endpoint not accessible to user defd6044-4747-4926-9726-4d19fa5d2d26

是指定端点的错误ID(不存在的端点)或在端点定义中使用错误的令牌。这表明您创建的端点存在问题。

最简单的选择是使用带有POST调用的api创建端点 https://xxxxx.VisualStudio.com/TEAMPROJECT_WITH_TARGET_GIT_REPO/_apis/distributedtask/serviceendpoints?api-version=3.0-preview.1

使用url

指定源存储库
{
  "name": "TestImport",
  "type": "git",
  "url": "https://yyyyyyyy.visualstudio.com/DefaultCollection/TargetGitProject/_git/ElasticSearchExamples",
  "authorization": {
    "scheme": "UsernamePassword",
    "parameters": {
      "username": PAT_TOKEN,
      "password": ""
    }
  }
}

这将创建端点并立即为您提供ID

{
  "data": {},
  "id": "**df12f2e3-7c40-4885-8dbd-310f1781369a**",
  "name": "TestImport",
  "type": "git",

这应该可以保证您使用有效端点的ID。