繁体   English   中英

使用 powershell 将所有项目的 Azure DevOps rest api 测试计划转换为 csv 文件

[英]Convert Azure DevOps rest api test plans for all projects to csv file using powershell

我正在尝试为 azure DevOps 组织中可用的所有测试计划生成报告。 这是我的脚本,它生成项目列表并遍历每个项目以查找其中可用的测试计划。 我希望所有这些数据都保存在 csv 文件中。 我能够获得json文件。 有什么办法可以将这些数据保存在 csv 文件中,并且每个项目都针对测试计划进行迭代?

$connectionToken = ""
$BaseUrl = "https://dev.azure.com/{organization_name}/_apis/projects?  
api-versions=5.0"

$base64AuthInfo= 
 [System.Convert]::ToBase64String([System.Text.Encoding]
 '::ASCII.GetBytes(":$($connectionToken)"))

$ProjectInfo = Invoke-RestMethod -Uri $BaseUrl -Headers     
@{authorization = "Basic $base64AuthInfo"} - 
Method Get

$ProjectDetails = $ProjectInfo | ConvertTo-Json -Depth 100

$Projectname = $ProjectInfo.value.name 

ForEach ($project in $Projectname){

$TestPlanApi = "https://dev.azure.com/{org}/$project/_apis/test/plans?   
api-version=5.0"

$TestplanInfo = Invoke-RestMethod -Uri $TestPlanApi -Headers   
@{authorization = "Basic $base64AuthInfo"} -Method Get

  if (-NOT ($TestplanInfo.count -eq 0)){   
   
   $info = $TestplanInfo | ConvertTo-Json -Depth 100

   $info

 }
 }

这给了我以下 json 文件,我想转换为 csv。 由于每个项目测试计划值都以值开始 Json 结果,我无法扩展它并保存到 csv

 {
   "value": [
     {
       "id": 134,
        "name": "sprint1",
        "url": "https://dev.azure.com/fabrikam/fabrikam-fiber- 
         tfvc/_apis/test/Plans/1",
         "project": {
            "id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
             "name": "Fabrikam-Fiber-TFVC",
             "url":    
             "https://dev.azure.com/fabrikam/_apis/projects/Fabrikam-                         
             Fiber- 
                   TFVC"
           },
          "area": {
            "id": "343",
            "name": "Fabrikam-Fiber-TFVC"
           },
          "iteration": "Fabrikam-Fiber-TFVC\\Release 1\\Sprint 1",
          "state": "Active",
          "rootSuite": {
            "id": "1"
          },
          "clientUrl": "mtms://fabrikam.visualstudio.com:443/DefaultCollection/p:Fabrikam- 
          Fiber-TFVC/Testing/testplan/connect?id=1"
       }
     ],
     "count": 1
    }
    {
      "value": [
        {
          "id": 567,
          "name": "sprint1",
          "url": "https://dev.azure.com/fabrikam/fabrikam-fiber- 
           tfvc/_apis/test/Plans/1",
          "project": {
             "id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
             "name": "Fabrikam-Fiber-TFVC",
             "url": "https://dev.azure.com/fabrikam/_apis/projects/Fabrikam-Fiber- 
                    TFVC"
            },
            "area": {
               "id": "343",
               "name": "Fabrikam-Fiber-TFVC"
             },
             "iteration": "Fabrikam-Fiber-TFVC\\Release 1\\Sprint 1",
             "state": "Active",
             "rootSuite": {
                "id": "1"
             },
             "clientUrl":"mtms://fabrikam.visualstudio.com:443/DefaultCollection/p:Fabrikam- 
             Fiber-TFVC/Testing/testplan/connect?id=1"
          },
          {
          "id": 678,
          "name": "sprint1",
          "url": "https://dev.azure.com/fabrikam/fabrikam-fiber- 
             tfvc/_apis/test/Plans/1",
          "project": {
            "id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
            "name": "Fabrikam-Fiber-TFVC",
            "url": "https://dev.azure.com/fabrikam/_apis/projects/Fabrikam-Fiber- 
                 TFVC"
           },
           "area": {
             "id": "343",
             "name": "Fabrikam-Fiber-TFVC"
           },
           "iteration": "Fabrikam-Fiber-TFVC\\Release 1\\Sprint 1",
           "state": "Active",
           "rootSuite": {
             "id": "1"
           },
           "clientUrl": 
           "mtms://fabrikam.visualstudio.com:443/DefaultCollection/p:Fabrikam- 
            Fiber-TFVC/Testing/testplan/connect?id=1"
         }
        ],
        "count": 2
       }

这些是不同项目测试运行的值,有些项目有 count = 1 然后显示一个 id ,有些项目有 count =3 然后显示所有 3 个 id 等等

我想要这个带有列的 csv 文件中的 json 文件 -

id、name、url、project.name、project.id、project.url、area.id、area.name、迭代、所有者、修订、状态、rootsuite.id、clienturl

如何扩展 csv 文件中的所有值? 我试过

选择对象展开属性值,但无法展开json数据中的所有值

作为一种解决方法,我们可以保存响应正文,然后将 json 文件转换为 csv 文件。

我们无法将所有信息保存在一个 csv 文件中,最新的信息会覆盖旧数据,因此我们需要将测试计划信息保存在不同的 csv 文件中。

样本:

$connectionToken = "{PAT}"
$BaseUrl = "https://dev.azure.com/{Org}/_apis/projects?api-version=6.1-preview.4"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$ProjectInfo = Invoke-RestMethod -Uri $BaseUrl -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get

$ProjectDetails = $ProjectInfo | ConvertTo-Json -Depth 100

$Projectname = $ProjectInfo.value.name 

ForEach ($project in $Projectname){

$TestPlanApi = "https://dev.azure.com/{Org}/$project/_apis/test/plans?api-version=5.0"

$TestplanInfo = Invoke-RestMethod -Uri $TestPlanApi -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get 
if (-NOT ($TestplanInfo.count -eq 0)){       
   
   #Save the test plan info to json file
   $info = $TestplanInfo | ConvertTo-Json -Depth 100 | out-file E:\test\$project.json
   #convert the json file to csv file 
   Get-Content -Path E:\test\$project.json  | ConvertFrom-Json | Select-Object -expand value | ConvertTo-Csv -NoTypeInformation | Set-Content E:\test\$project.csv
   #delete json file
   Remove-Item E:\test\$project.json
 }
 }

结果:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM