TFS 2017 Rest API-批准发布后获得发布的批准

时间:2019-05-07 20:29:33

标签: tfs azure-devops azure-pipelines tfs2017

我希望能够在发布运行时记录谁批准了发布。我们有审核要求,以证明谁是环境的看门人。这是事件的顺序:

  1. 一个版本被触发并坐下来等待批准。
  2. 其他用户批准发布
  3. 发行完成。

随着发布的进行,我希望能够记录批准者。我们发现,如果您使用Approvals API,则可以获取ApprovalID。问题是只能在步骤1和步骤2之间执行此操作。发布被批准后,您将无法再获得其ApprovalID,为空。

然后我们查看了特定版本的信息,并且“批准者”区域中没有任何数据。即使在文档样本响应中,您也会看到数据不存在。我在2017年能做些什么来获取此信息?

https://docs.microsoft.com/en-us/azure/devops/integrate/previous-apis/rm/releases?view=tfs-2017

$RecentReleaseUrl = $TFSCollectionPath + $TFSProjectName + "/_apis/Release/approvals?releaseIdsFilter=$RecentRealease&api-version=3.2-preview.2"

$RecentReleasedefinition = Invoke-RestMethod -Uri $RecentReleaseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

$ApprovalID = $RecentReleasedefinition.value.id  

1 个答案:

答案 0 :(得分:1)

@Amit Baranes让我们接近了,我已经用解决方案更新了他的答案。

function DefinitionHelper.GetReleaseApprover
{
    $Local = "False"

    #If running locally then use local values, when running on the build server use the env variables
    if ($Local -eq "True")
    {
        $ReleaseID = "16734"
        $Environment = "TEST"
    }
    else
    {
        $ReleaseID = $env:RELEASE_RELEASEID
        $Environment = $env:RELEASE_ENVIRONMENTNAME
    }

    # Base64-encodes the Personal Access Token (PAT) appropriately
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username,$TFSToken)))

    #Url to get details for Release
    $ReleaseUrl = $TFSCollectionPath + $TFSProjectName +"/_apis/Release/releases/"+$ReleaseID+"?api-version=3.2-preview.2"
    Write-Host "ReleaseUrl :"$ReleaseUrl
    #Rest-API call to get details for Release
    $ReleaseDetails = Invoke-RestMethod -Method Get -ContentType application/json -Uri $ReleaseUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    #Get the current environment
    $EnvironmentDetails = $ReleaseDetails.environments | where name -eq $Environment
    #Get the approval
    $ApproverDetails = $EnvironmentDetails.preDeployApprovals.approvedBy   

}