Is There Anyway To Retrieve The Label/Tag of a PR After Merged to Main?

42 Views Asked by At

I am looking to implement a automated SemVer process.

Lets say I have a PR open which has the label/tag of major, is there anyway to retrieve this tag AFTER it has been merged to main?

When the PR gets merged to main, I want to then trigger a pipeline which reads the label and then automatically bumps the tag version on the repo, is this possible in Azure DevOps? I know this can be done in GitHub.

1

There are 1 best solutions below

0
Miao Tian-MSFT On

We can use the two REAT API Pull Request Labels - Get and Annotated Tags - Create to complete the process.

The following is my test steps:

  1. Create a Personal Access Token with Code permisson.
  2. Create a pipeline with the following yaml in the main branch.
  3. Create a secret Variable named PAT with the Personal Access Token in the pipeline.enter image description here
trigger:
- main

pool:
  vmImage: Windows-latest

jobs:
  - job: A
    condition: eq(contains(variables['Build.SourceVersionMessage'], 'Merged PR'), True)
    steps:
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          # Set your Azure DevOps organization, project, repository
          $organization = ""
          $project = ""
          $repositoryId = ""  #repo name or repo id

          $token="$(PAT)"
          $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$token)))

          $string = "$(Build.SourceVersionMessage)"
          $splitString = $string.Split(" ")
          $pullRequestId = $splitString[2].Replace(":", "")

          Write-Host "pullRequestId is $pullRequestId"
          
          # Invoke the REST API to get the label for the pull request
          $uri = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repositoryId/pullRequests/$pullRequestId/labels?api-version=7.1-preview.1"
          try {
              $labelResponse = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
              $labelName = $labelResponse.value[0].name
              Write-Host "Label for Pull Request $pullRequestId : $labelName"
          } catch {
              Write-Host "Error retrieving label for Pull Request $pullRequestId : $_"
              exit 1
          }
          

          $tagName = $labelName 
          $tagMessage = "Release $tagName"  
          $commitId = "$(Build.SourceVersion)"  

          # Invoke the REST API to create the annotated tag
          $uri = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repositoryId/annotatedtags?api-version=7.1-preview.1"
          $tagBody = @{
              name = $tagName
              taggedObject = @{
                  objectId = $commitId
              }
              message = $tagMessage
          }
          try {
              $tagResponse = Invoke-RestMethod -Uri $uri -Method Post -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body ($tagBody | ConvertTo-Json) -ContentType 'application/json'                   
              # Extract the tag URL
              $tagUrl = $tagResponse.url
              Write-Host "Annotated tag created: $tagUrl"
          } catch {
              Write-Host "Error creating annotated tag: $_"
              exit 1
          }

The pipeline has a condition in the job, so the job will only run when the Build.SourceVersionMessage contains the words "Merged PR". If not, the job will be skipped. enter image description here

Then we use the REST API in the PowerShell script to get the tag value from the PR and add tag to the branch.

Test Result:

  1. Create a PR to main branch with tag 3.0.0 enter image description here
  2. The pipeline is triggered and running. enter image description here
  3. The tag is added after the pipeline runs successfully. enter image description here enter image description here