首先我要说的是,我是自动化方面的新手,所以如有任何错误,请原谅。 我们目前使用 Azure DevOps 存储库(从 TFS 迁移)。在 DevOps 中,我们有一个“主要”存储库。典型的工作是,开发人员创建自己的分支,一旦准备就绪,他们将其分支推送到远程 DevOps 存储库(例如 git push -u origin itsp2187)。 一旦进入 DevOps,开发人员将创建一个 PR,一旦获得批准,PR 就完成了(合并到远程主分支)。
我想做的是当 PR 合并到 DevOPs 主分支时能够做一些事情。我需要做的具体操作是使用合并的 PR 中的信息更新外部数据库(例如执行 PR 合并的用户、发生这种情况的日期和时间、PR 标题以及任何描述(如果可能))。
我应该使用什么样的软件来完成此任务?这可能吗?我听说 Github 中使用了 Actions,我听说 Azure 中使用了管道和 Webhook。这些是一样的吗?或者这些软件或其他软件中的任何一个最适合这种需求吗? 谢谢你
合并 PR 时,提交历史记录中会有类似
Merged PR xx: The pull request title
的 Commit 消息。
基于此消息,我们可以创建一个管道,仅当 Commit 消息 (
$(Build.SourceVersionMessage)
) 包含关键字 Merged PR
时才会运行。在管道中,我们在 PowerShell 任务中使用 REST API Get Pull Request By Id。
以下是我的测试步骤:
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 information
$organization = "your organization name"
$project = "your project name"
$repositoryId = "your repo name" # repo name or repo id
$token="$(PAT)"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$token)))
# get pullRequestId from the $(Build.SourceVersionMessage)
$string = "$(Build.SourceVersionMessage)"
$splitString = $string.Split(" ")
$pullRequestId = $splitString[2].Replace(":", "")
Write-Host "pullRequestId is $pullRequestId"
# Invoke the REST API to get the pull request
$uri = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repositoryId/pullRequests/$pullRequestId"+"?api-version=7.1-preview.1"
$uri
try {
$Response = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$Response | ConvertTO-Json
} catch {
Write-Host "Error retrieving infornation of Pull Request $pullRequestId : $_"
exit 1
}
Merged PR
,则作业将被跳过。