Home > Mobile >  Create fork repository in Azure Devops by Powershell script
Create fork repository in Azure Devops by Powershell script

Time:07-20

I am writting logic that adds contant of nuget package to repository. I want to automate process of creating commits and Pull Request to original repository. Potentially it will be run in Pipeline. So, does anybody know how to create fork by powershell script? I can create Pull Request from fork by using Rest API:

$pullRequestUri = "$orgUri/_apis/git/repositories/$repositoryId/pullrequests?api-version=6.0"
$body = @{
"sourceRefName" = "refs/heads/main"
"targetRefName" = "refs/heads/main"
"forkSource" = @{
    "repository" = @{
        "id" = $forkRepositoryId
    }
}
"title" = "Add $PackageName package"
"isDraft" = $True

CodePudding user response:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic <PAT>")
$headers.Add("Content-Type", "application/json")

$body = "{
`n  `"name`": `"<Dict fork repo Name>`",
`n  `"project`": {
`n    `"id`": `"<Dict Project ID>`"
`n  },
`n  `"parentRepository`": {
`n    `"name`": `"<Source repo name>`",
`n    `"id`": `"<Source repo ID>`",
`n    `"project`": {
`n      `"id`": `"<Source project ID>`"
`n    }
`n  }
`n}"

$response = Invoke-RestMethod 'https://dev.azure.com/<Organization Name>/_apis/git/repositories?api-version=6.0' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

Success on my side:

enter image description here

How to get the projects' IDs:

https://docs.microsoft.com/en-us/rest/api/azure/devops/core/projects/list?view=azure-devops-rest-6.0

How to get the repositories' IDs:

https://docs.microsoft.com/en-us/rest/api/azure/devops/git/repositories/list?view=azure-devops-rest-6.0

  • Related