git Azure DevOps -识别触发构建的人员

km0tfn4u  于 2023-06-28  发布在  Git
关注(0)|答案(1)|浏览(140)

我们有Azure DevOps架构,开发人员在DEV分支中提交代码。我们有一个中间分支,开发人员提供需要从DEV分支移动到生产分支的文件列表。
基于该列表,我们的自动化管道将触发并从DEV分支获取文件,并将所选文件推送到功能分支中,并且将从新创建的功能分支自动创建Pull Request(PR)。我们有服务帐户来执行所有这些操作。
一旦审批人审查并批准上述请购单,文件将合并到生产分支,并触发生产管道。
现在,我们需要向用户(提供要部署到生产分支的文件列表的用户)发送通知电子邮件,但不确定如何识别触发构建的用户。
我们是否可以使用Azure DevOps REST API回溯跟踪活动,并获取触发构建的用户?
我无法识别实际触发生成的用户

lndjwyie

lndjwyie1#

为了获得批准PR的用户列表,您可以在本地PowerShell或Cloud Shell中使用Azure CLI和Rest API的以下命令:
Reference:-

az extension add --name azure-devops
az login
az devops configure --defaults organization=https://dev.azure.com/contoso project=ContosoWebApp

我引用了这个博客中的代码来检索我的PR的审阅者。

来自博客的脚本:-

# Need the UserEmail $UserEmail = 'sid@test.com' # Put the email address for the User $ProjectName = 'AzureDevops' # use this otherwise
default project is picked  
# generate a list of User & Groups names to search for in the PR $IDs = New-Object -TypeName System.Collections.ArrayList $UserObject = az devops user show --user $UserEmail | ConvertFrom-Json
$IDs.Add($UserObject.Id)  
# Fetch all the teams and generate a list of teams a user is member of $Teams = az devops team list --project $ProjectName | ConvertFrom-Json
foreach ($team in $Teams) {
    $TeamMembers = (az devops team list-member --team $team.Name --project $ProjectName |
        ConvertFrom-Json).identity
    if ($UserEmail -in $TeamMembers.uniqueName)
    {
        #$TeamObject = az devops team show --
        $null = $IDs.Add($team.Id)
    } }

# Fetch the PRs in a Project $PRs = az repos pr list --project $ProjectName | ConvertFrom-Json  
# Iterate over each PR to find the reviewers and check if the user or groups is present $PrsAssignedToUser = foreach ($pr in $PRs) {
    $reviewersId = @($pr.reviewers.Id)
    if ($IDs.Where( { $PSItem -in $reviewersId })) {
        $pr
    } }

Rest API调用脚本:-

Rest API to get the list of PR reviewers:-

GET
https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/reviewers?api-version=7.0
$PAT = "xxxxxxxxxxmzpx5gjccwp4xxgukniyrhga"
$OrgName = "sid24desai0738"
$ProjectName = "AzureDevops"
$ApiVersion = "7.0"

$services = Invoke-RestMethod -Uri "https://dev.azure.com/$OrgName/$ProjectName/_apis/git/repositories/7419fe97-d348-4c7e-8c69-5665884b1880/pullRequests/11/reviewers?api-version=7.0" -Method Get -Headers @{Authorization=("Basic {0}" -f [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$PAT")))}

$services.value

Rest API获取特定拉取请求的PR审查者

GET
https://dev.azure.com/{organization}/{project}/_apis/git/reposit>ories/{repositoryId}/pullRequests/{pullRequestId}/reviewers/{re>viewerId}?api-version=7.0

Azure CLI命令:-

az repos pr reviewer list --id 11

相关问题