如何获取PR标题并将其存储在变量中 [英] How to obtain the PR title and store it in a variable

查看:0
本文介绍了如何获取PR标题并将其存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够获取PR消息并将其作为变量存储在Azure DevOps中,以便我可以将标题更改为** SKIP TEST **并在管道中扫描该消息,而不是手动通过变量UI进行扫描。

我想我只需要执行某种REST API调用,因为我已经有了PR ID和其他识别信息。然后使用读取输出并将值存储到变量的工具。

推荐答案

在Yaml管道(而不是经典)上执行此操作

steps:
  - bash: |
      PR_TITLE="$(curl --silent -u azdo:$SYSTEM_ACCESSTOKEN 
       $(System.CollectionUri)_apis/git/repositories/$(Build.Repository.ID)/pullRequests/$(System.PullRequest.PullRequestId)?api-version=5.1 
       | jq -r .title)"
      echo "##vso[task.setvariable variable=Pr.Title]$PR_TITLE"
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    displayName: Extract pull request title
    condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))

  - bash: |
      echo "running test"
    displayName: Run test
    condition: and(succeeded(), not(startsWith(variables['Pr.Title'], '**SKIPTEST**')))

下面是一个示例,所有内容都在一行中,但我更喜欢上面的一个,因为它更容易让新手阅读

steps:
  - bash: |
      echo "##vso[task.setvariable variable=Pr.Title]"$(curl --silent -u azdo:$SYSTEM_ACCESSTOKEN $(System.CollectionUri)_apis/git/repositories/$(Build.Repository.ID)/pullRequests/$(System.PullRequest.PullRequestId)?api-version=5.1 | jq -r .title)
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    displayName: Extract pull request title
    condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))

  - bash: |
      echo "running test"
    displayName: Run test
    condition: and(succeeded(), not(startsWith(variables['Pr.Title'], '**SKIPTEST**')))

标签

此外,如果您更喜欢"标签"而不是更改标题(我自己更喜欢标题,因为它随处可见),可以使用以下脚本提取标签

steps:
- bash: |
    LABELS=$(curl --silent -u azdo:$SYSTEM_ACCESSTOKEN $(System.CollectionUri)_apis/git/repositories/$(Build.Repository.ID)/pullRequests/$(System.PullRequest.PullRequestId)/labels?api-version=5.1-preview.1 | jq .value[].name)
      echo "##vso[task.setvariable variable=Pr.Labels]$LABELS"
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)
  displayName: Get Pull Request Labels
  condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))
- bash: |
    echo "running test"
  displayName: Run test
  condition: and(succeeded(), not(contains(variables['Pr.Labels'], '"skip-test"')))

注意:我没有在JQ上使用-r,以便将值引起来。这允许对字符串进行更显式的检查,而不是匹配可能将文本作为子字符串包含的标签。

这篇关于如何获取PR标题并将其存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆