合并到Main后是否有办法检索PR的标签/标签?

问题描述 投票:0回答:1

我正在寻求实施自动化 SemVer 流程。

假设我有一个打开的 PR,其中包含专业的标签/标签,在将其合并到主标签后是否可以检索该标签?

当 PR 合并到 main 时,我想触发一个读取标签的管道,然后自动在存储库上更改标签版本,这在 Azure DevOps 中可能吗?我知道这可以在 GitHub 中完成。

azure-devops azure-pipelines azure-repos
1个回答
0
投票

我们可以使用两个 REAT API Pull Request Labels - GetAnnotated Tags - Create 来完成该过程。

以下是我的测试步骤:

  1. 创建具有代码权限的个人访问令牌
  2. 在主分支中使用以下 yaml 创建管道。
  3. 使用管道中的个人访问令牌创建一个名为
    PAT
    的秘密变量。
  4. 管道在作业中有一个条件,因此只有当
    Build.SourceVersionMessage
    包含“Merged PR”字样时,作业才会运行。然后我们在PowerShell脚本中使用REST API从PR中获取标签值并将标签添加到分支中。如果没有,该作业将被跳过。
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
          }

测试结果:

  1. 使用标签 3.0.0 创建 PR 到主分支
  2. 管道已触发并正在运行。
  3. 管道运行成功后添加标签。
© www.soinside.com 2019 - 2024. All rights reserved.