简体   繁体   English

Azure DevOps 服务 REST API 5.1 - 获取单元测试 (VSTest) 和 sonarqued

[英]Azure DevOps Services REST API 5.1 - get unit testing (VSTest) and sonarqued

I'm creating a script in ISE powershell to get information from the projects in azure devops.我正在 ISE powershell 中创建一个脚本,以从 azure devops 中的项目获取信息。 Some apis that I already use in my script are the following:我已经在我的脚本中使用的一些 api 如下:

  1. ApiUri = "$projectUri/_apis/tfvc/branches?$apiVersion" ApiUri = "$projectUri/_apis/tfvc/branches?$apiVersion"
  2. ApiUri = "$projectUri/_apis/distributedtask/deploymentgroups?$apiVersion-preview" ApiUri = "$projectUri/_apis/distributedtask/deploymentgroups?$apiVersion-preview"
  3. ApiUri = "$projectUri/_apis/pipelines?$apiVersion-preview" ApiUri = "$projectUri/_apis/pipelines?$apiVersion-preview"
  4. ApiUri = "$projectUri/_apis/git/repositories?$apiVersion" ApiUri = "$projectUri/_apis/git/repositories?$apiVersion"
  5. ApiReleasePipelinesUri = "$projectUri/_apis/release/definitions?$apiVersion" ApiReleasePipelinesUri = "$projectUri/_apis/release/definitions?$apiVersion"

An example of what I'm getting with the above apis is:我从上面的 api 得到的一个例子是:

在此处输入图像描述

In the value, if it is 1 or more, the information of that process goes.在值中,如果它是 1 或更多,则该过程的信息。

Now I want to extract the information about the unit testing (Visual studio test) and sonarqued.现在我想提取有关单元测试(Visual Studio 测试)和 sonarqued 的信息。

I would like to have something like this for the case of sonarqued and unittesting.对于声纳和单元测试,我想有这样的东西。 0 if there was never such an implementation and 1 if unit testing and sonarqued were applied.如果从未有过这样的实现,则为 0;如果应用了单元测试和声呐,则为 1。

I can't find something similar to what I need, if someone could guide me or know how to extract that information it would help me我找不到与我需要的类似的东西,如果有人可以指导我或知道如何提取该信息,它会对我有所帮助

Used this task for unit tests:将此任务用于单元测试: 在此处输入图像描述

Used this task to sonarqued:使用此任务声纳: 在此处输入图像描述

This is relatively complex task and I have done similar thing for governance purposes (eg. find out which project is using specific task in their pipelines).这是一项相对复杂的任务,我出于治理目的做过类似的事情(例如,找出哪个项目正在其管道中使用特定任务)。

This is only my view how I have resolved similar task.这只是我如何解决类似任务的看法。

For this, you will have to use undocumented API to get all available tasks in your project collection.为此,您必须使用未记录的 API 来获取项目集合中的所有可用任务。 There are bunch of data that are returned, though you need task ID for next steps.返回了一堆数据,但您需要任务 ID 才能执行后续步骤。 (change the {project_collection}) https://dev.azure.com/{project_collection}/_apis/distributedtask/tasks (更改 {project_collection}) https://dev.azure.com/{project_collection}/_apis/distributedtask/tasks

In my scripts I use this functions to return all task names with their IDs written in Powershell:在我的脚本中,我使用此函数返回所有任务名称,其 ID 写在 Powershell 中:

$ORG = "https://dev.azure.com/YOUR_PROJECT_COLLECTION/"
$ADOAH = # Define your authentication header

# Get all tasks
Function Get-Tasks {
    $tasks = Get-API -url "$($ORG)_apis/distributedtask/tasks"
    $tasks = ConvertFrom-Json $tasks -AsHashTable
    $taskarray = @{}
    foreach ($task in $tasks.value) {
        $taskarray.Set_Item($task.name, $task.id) }
    return $taskarray
}

# Search for Task ID
Function Get-TaskId {
    param (
        [parameter(mandatory)] $taskname
    )
    $tasks = Get-Tasks
    if ($taskname -match '^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$' -and $tasks.ContainsValue("$taskname")) {
        return $taskname }
    elseif ($null -eq $tasks."$taskname") {
        throw "Task name or ID does not exist!" }
    else { 
        return $tasks."$taskname" }
}

# Function to call API
Function Get-API {
    param (
        [parameter(mandatory=$true)] [string] $url
    )
    Invoke-RestMethod -Uri $url -Method get -Headers $ADOAH
}

Second, if you have only classic pipelines, then the task is relatively easy and follow Scenario A. If you have also YAML pipelines, you will have to make the script even more complex, follow Scenario B.其次,如果您只有经典管道,那么任务相对容易,按照场景 A。如果您还有 YAML 管道,则必须使脚本更加复杂,按照场景 B。

Scenario A: For the classic pipelines you need to get the URL of each pipeline.场景 A:对于经典管道,您需要获取每个管道的 URL。 The JSON will return all jobs with respective tasks. JSON 将返回所有作业以及各自的任务。 You can scan the output and match with the task ID described in step 1. Please note, that if you have multiple jobs in single pipeline, you have to scan each job individually.您可以扫描 output 并匹配步骤 1 中描述的任务 ID。请注意,如果您在单个管道中有多个作业,则必须单独扫描每个作业。

Scenario B: From JSON return of each pipeline you can determine if it is classic or YAML pipeline by reading PipelineConfiguration - Type.场景 B:从每个管道的 JSON 返回,您可以通过阅读 PipelineConfiguration - Type 来确定它是经典管道还是 YAML 管道。 https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines/get?view=azure-devops-rest-7.0#pipelineconfiguration https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines/get?view=azure-devops-rest-7.0#pipelineconfiguration

Usually, the result is YAML for YAML pipeline and designerJson for classic pipeline.通常,YAML 管道的结果为 YAML,经典管道的结果为 designerJson。 For classic pipeline follow Scenario A. For YAML pipeline you have to read the content of YAML file and search for the task name in YAML format.对于经典管道,请遵循场景 A。对于 YAML 管道,您必须阅读 YAML 文件的内容并搜索 YAML 格式的任务名称。 You are not searching now for the ID of the task.您现在不是在搜索任务的 ID。 Tricky part is, that when there are templates and extends used in pipeline, you have to cover them in your script logic and recursively scan them too.棘手的部分是,当管道中使用模板和扩展时,您必须在脚本逻辑中覆盖它们并递归扫描它们。

There might be easier way how to do it, though this worked for me.尽管这对我有用,但可能有更简单的方法。

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

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