You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PowerShell获取Azure DevOps单个PR的所有关联数据并导出为CSV

从Azure DevOps批量导出Pull Request完整数据到CSV(含所有关联Commit和评论)

原代码仅能获取PR的单个lastMergeSourceCommit提交ID,无法满足获取所有关联Commit和评论内容的需求。以下是修改后的PowerShell脚本,实现批量拉取所有PR的完整数据:

修改后的脚本

# 配置参数
$organization = "{organization name}"
$project = "{project name}"
$repo = "{repo name}"
$pat = "<你的PAT令牌>"
$outputPath = "C:\Temp\PRFullData.csv"

# 生成认证信息
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
$headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}

# 获取所有PR列表
$prListUrl = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repo/pullrequests?api-version=7.1-preview.1"
$prList = Invoke-RestMethod -Uri $prListUrl -Headers $headers -Method GET -ContentType 'application/json'

# 初始化结果数组
$prFullData = @()

# 遍历每个PR,获取关联Commit和评论
foreach ($pr in $prList.value) {
    # 获取当前PR的所有关联Commit
    $commitsUrl = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repo/pullrequests/$($pr.pullRequestId)/commits?api-version=7.1-preview.1"
    $commits = Invoke-RestMethod -Uri $commitsUrl -Headers $headers -Method GET -ContentType 'application/json'
    $allCommitIds = $commits.value.commitId -join ";"

    # 获取当前PR的所有评论(含回复)
    $commentsUrl = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repo/pullrequests/$($pr.pullRequestId)/threads?api-version=7.1-preview.1"
    $commentThreads = Invoke-RestMethod -Uri $commentsUrl -Headers $headers -Method GET -ContentType 'application/json'
    $allComments = @()
    foreach ($thread in $commentThreads.value) {
        foreach ($comment in $thread.comments) {
            $allComments += "$($comment.author.displayName): $($comment.content)"
        }
    }
    $allCommentsStr = $allComments -join "`n"

    # 整理PR完整数据对象
    $prData = [PSCustomObject]@{
        PullRequestId = $pr.pullRequestId
        Author = $pr.authoredBy.displayName
        Status = $pr.status
        CreationDate = $pr.creationDate
        AllCommitIds = $allCommitIds
        AllComments = $allCommentsStr
    }
    $prFullData += $prData
}

# 导出到CSV文件
$prFullData | ConvertTo-Csv -NoTypeInformation -Delimiter "," | Out-File $outputPath -Encoding utf8

Write-Host "数据导出完成,文件路径:$outputPath"

关键说明

  • API版本:使用7.1-preview.1确保兼容最新的Azure DevOps接口,支持完整的Commit和评论拉取
  • Commit收集:通过每个PR的/commits端点获取所有关联提交ID,用分号分隔存储
  • 评论收集:通过/threads端点获取评论线程,遍历每个线程下的评论内容,包含评论作者和内容,用换行符分隔
  • 编码处理:导出时指定utf8编码,避免中文乱码问题

使用注意事项

  1. 替换脚本中的占位符:{organization name}、{project name}、{repo name}和<你的PAT令牌>
  2. 确保PAT拥有**Code (Read)**权限,否则无法拉取PR、Commit和评论数据
  3. 若PR数量较多,脚本执行时间会相应增加,可根据需要添加进度提示

内容的提问来源于stack exchange,提问作者Santoshkumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 18:27:52