简体   繁体   中英

Import repository from one azure devops organization to another through the API

I am trying to automate some processes in our organization and part of that includes being able to transfer a repository in one of our azure devops organizations to another (think of it as a dev organization and a test organization so we are pushing from dev to test)

Through the API https://docs.microsoft.com/en-us/rest/api/azure/devops/git/import%20requests/create?view=azure-devops-rest-6.0 I am trying to create an import request.

Here's where I'm stumped and the documentation doesn't say much about.

If I use a request body like in the example:

{
  "parameters": {
    "gitSource": {
      "url": "https://github.com/Microsoft/vsts-agent.git"
    }
  }
}

The import request works fine as long as I'm importing to an empty repository. However I need to be able to sync an existing repository. [There's a property in the documentation called overwrite that seems to be for this purpose][1]: https://i.stack.imgur.com/ezCB3.png

The only problem is when I add this to the request body and set to true

{
  "parameters": {
    "gitSource": {
      "url": "https://github.com/Microsoft/vsts-agent.git",
      "overwrite": true
    }
  }
}

I get bad request message saying invalid combination of parameters If I set this to false it works if I'm trying to import to an empty repository.If the repository isn't empty I get an error saying I can only import into an empty repo.

So it seems this property is meant for exactly what I'm doing, however it seems there are more parameters needed when that property is set to true in order to make the request succeed, but the documentation is lacking in this area.

Any help would be much appreciated

I suspect that overwrite = true is giving you error because there may be some permission issues or Auth problem in your latter repo.

Alternatively, you can fork your parent repository and sync only the provided refs :

CURL:

POST https://dev.azure.com/{organization}/_apis/git/repositories?sourceRef=users/heads/master&api-version=6.0

Request Body:

{
  "name": "forkRepositoryWithOnlySourceRef",
  "project": {
    "id": "3b046b6a-d070-4cd5-ad59-2eace5d05b90"
  },
  "parentRepository": {
    "id": "76b510af-7910-4a96-9902-b978d6226bee"
  }
}

Sample response (HTTP 201):

{
  "id": "29230c30-9125-459b-a3f6-ffab329053bd",
  "name": "forkRepositoryWithOnlySourceRef",
  "url": "https://dev.azure.com/fabrikam/MyFirstProject/_apis/git/repositories/29230c30-9125-459b-a3f6-ffab329053bd",
  "project": {
    "id": "3b046b6a-d070-4cd5-ad59-2eace5d05b90",
    "name": "MyFirstProject",
    "url": "https://dev.azure.com/fabrikam/_apis/projects/3b046b6a-d070-4cd5-ad59-2eace5d05b90",
    "state": "wellFormed",
    "revision": 12,
    "visibility": "private",
    "defaultTeamImageUrl": null
  },
  "size": 0,
  "remoteUrl": "https://dev.azure.com/fabrikam/MyFirstProject/_git/forkRepositoryWithOnlySourceRef",
  "sshUrl": "git@ssh.dev.azure.com:v3/fabrikam/MyFirstProject/forkRepositoryWithOnlySourceRef",
  "isFork": true,
  "_links": {
    "forkSyncOperation": {
      "href": "https://dev.azure.com/fabrikam/_apis/git/repositories/29230c30-9125-459b-a3f6-ffab329053bd/forkSyncRequests/7"
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM