Kamil
Kamil

Reputation: 306

Get github repository using Azure devops REST API

I am using postman and Azure Devops REST API to create some repositories (https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=6.0) After that I need to transfer or clone a repo from another projet to the new one but it is not possible with the API. The only way is to get an .git file so I put the repo in a private github but I don't know how to pass the credentials into PostMan. I tried with a public one, it worked.

Upvotes: 0

Views: 1939

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35109

For using Rest API to import Private Repo to new repo:

You need to create an "other git" service connection first, then you could use the Rest API to import the Github Private Repo to New Repo.

Please refer to the following steps:

Step1: Create an Other Git Service connection.

You could use Rest API to create it.

For example:

Post https://dev.azure.com/{Organization Name}/_apis/serviceendpoint/endpoints?api-version=6.0-preview.4

Request Body sample:

{
    "authorization":{"scheme":"UsernamePassword","parameters":{"username":"{User name}","password":"{Password}"}},
    "data":{"accessExternalGitServer":"true"},
    "name":"{name}",
    "serviceEndpointProjectReferences":[{"description":"","name":"{Service connection name}","projectReference":{"id":"{Project Id}","name":"{Project Name}"}}],
    "type":"git",
    "url":"{Target Git URL}",
    "isShared":false,
    "owner":"library"
}

Or you could create it in Project Settings -> Service Conntions -> New Service Connection -> Other Git

Then you could get the ServiceEndPointId, and you could use it in the Import Repo Rest API.

For example:

URL

Post https://dev.azure.com/{Organization Name}/{Project Name}/_apis/git/repositories/{Repo Name}/importRequests?api-version=5.0-preview.1

Request Body:

{
  "parameters": {
    "gitSource": {
      "url": "Git URL"
    },
    "serviceEndpointId": "{Service EndPoint Id}",
    "deleteServiceEndpointAfterImportIsDone": true
    
  }
}

Upvotes: 2

Related Questions