-1

Any Automated way to create Azuredevops Repos Dynamically from Work item inputs ?

We are looking for self service based automated repo creation for Application teams.

Scenario Explanation:- The Appteam can raise a Workitem with the details such as "RepoName, BranchPoicy, PR approvers and Security group details in the Workitem as input

Once the certain Approver validated the given details and changed the WorkItem Status to "Approved", In backend the corresponding repo should be created with the required configurations.

Is there any such automation is possible using AzureDevops?

Vowneee
  • 956
  • 10
  • 33

1 Answers1

1

Currently, there is no build-in feature to achieve this automation.

Using REST API is a good approach for your scenario.

You could use Repo-Create and Push-Create (Branch) for the repo creation automation.

Sample PowerShell Script:

$user = "{User}"
$token = "{PAT}"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$orgUrl = "https://dev.azure.com/{org}"
$teamProject = "{ProjectName}"
$repoName = "NEW_REPO"

$restApiInitialCommit = "$orgUrl/$teamProject/_apis/git/repositories/$repoName/pushes?api-version=6.1-preview.2"
$restApiCreateRepo = "$orgUrl/$teamProject/_apis/git/repositories?api-version=7.1-preview.1"

$repoBody = "{`"name`": `"$repoName`"}"

$commitBody = @"
{
    "refUpdates": [
      {
        "name": "refs/heads/main",
        "oldObjectId": "0000000000000000000000000000000000000000"
      }
    ],
    "commits": [
      {
        "comment": "Updates file",
        "changes": [
          {
            "changeType": "add",
            "item": {
              "path": "/README0102.md"
            },
            "newContent": {
              "content": "{newFileContentToUpdate}",
              "contentType": "rawtext"
            }
          }
        ]
      }
    ]
  }
"@

function InvokePostRequest ($PostUrl, $body)
{   
    return Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
}

InvokePostRequest $restApiCreateRepo $repoBody

$fileContent = Get-Content -Path "C:/README0102.md" -Raw

$fileContentToCommit = $fileContent.Replace("\", "\\")
$fileContentToCommit = $fileContentToCommit.Replace("`"", "\`"")

$updateBody = $commitBody.Replace("{newFileContentToUpdate}", $fileContentToCommit);

InvokePostRequest $restApiInitialCommit $updateBody

enter image description here

To add branch policy API: Configuration-Create

To add PR approvers: sample

For the automation, you could consider the below workflow:

1.Create a webhook with the event to track the wit status change.

2.Create a pipeline and set the webhook trigger (Pipeline could use the PowerShell task to run the API)

3.Whenever the wit status is changed to "Approved", the webhook triggers the pipeline.

If you would like a direct setting to achieve this automation by WIT, try creating a suggestion via this link.

Kim Xu-MSFT
  • 1,819
  • 1
  • 2
  • 4
  • Great.. good to know that it is achievable. We are using Linux agents only and is there any bash script available for the same? Also how we can we get the Repo name and other inputs from the WIT directly – Vowneee Jan 02 '23 at 06:58