Home > Back-end >  Azure DevOps Git: Fork into another Repo using Azure DevOps REST API
Azure DevOps Git: Fork into another Repo using Azure DevOps REST API

Time:09-28

In my Azure DevOps Project, I have a Git repository that I would like to copy to another Azure DevOps Project.

In other words, I should be able to copy the original repo into other Azure DevOps projects as needed.

I'm trying to achieve this using the Azure DevOps REST APIs, but I haven't found enough documentation. The only thing I see is the ability to create new repos using the Azure DevOps REST APIs, but nothing about copying the contents of the repo.

CodePudding user response:

I should be able to copy the original repo into other Azure DevOps projects as needed.

Fork:

We can call the enter image description here

Import Git repo from one project to another:

Another way is importing the original Git repo from one project to another. Get the clone URL from the original Git repo, create a PAT, then import. See Import a Git repo for details.

If you want to do that by calling REST API, the following PowerShell for your reference (projects within the same Azure DevOps organization):

Param(
   [string]$organization = "organization name here",
   [string]$sourceproject = "source project name",
   [string]$targetproject = "target project name",
   [string]$sourceRepoName = "source git repo name",
   [string]$targetRepoName = "target repo name",
   [string]$username = "",
   [string]$token = "PAT"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$targetUrl = "https://dev.azure.com/$organization/$targetproject/_apis"

# create endpoint
$endpoint = irm "$targetUrl/serviceendpoint/endpoints?api-version=5.0-preview" -Method:Post -ContentType "application/json" `
   -Headers @{Authorization = "Basic $base64AuthInfo"} `
   -Body ( '{{"name":"temporary-script-git-import3","type":"git","url":"https://{4}@dev.azure.com/{4}/{0}/_git/{1}","authorization":{{"parameters":{{"username":"{2}","password":"{3}"}},"scheme":"UsernamePassword"}}}}' -f $sourceproject, $sourceRepoName, $username, $token, $organization )

# import repository
$importRepo = irm "$targetUrl/git/repositories/$targetRepoName/importRequests?api-version=5.0-preview" -Method:Post -ContentType "application/json" `
   -Headers @{Authorization = "Basic $base64AuthInfo"} `
   -Body ( '{{"parameters":{{"deleteServiceEndpointAfterImportIsDone":true,"gitSource":{{"url":"https://{3}@dev.azure.com/{3}/{0}/_git/{1}","overwrite":false}},"tfvcSource":null,"serviceEndpointId":"{2}"}}}}' -f $sourceproject, $sourceRepoName, $endpoint.id, $organization )
  • Related