azure devops 是否支持从第 3 方 Rest API 动态检索值

问题描述 投票:0回答:2

azure devops 是否支持从 REST API 检索数据并将该数据填充到工作项字段中

我尝试编写 powershell 脚本来从 REST API 检索数据并将该数据填充到 azure devops 中的工作项字段中

azure azure-devops-rest-api azure-rest-api
2个回答
0
投票

是的,您可以通过调用以下API获取

workItems
的字段:-

获取API:-

参考1

GET https://dev.azure.com/organization/_apis/work/processes/processId/workItemTypes/witRefName/fields/fieldRefName?api-version=7.1-preview.2
$PAT = "xxxxxxxxxxxxx3ceq"
$OrgName = "sid24desai0738"
$ProjectName = "AzureDevops"
$ApiVersion = "7.0"
$processId =  ""
$witRefName = ""
$fieldRefName = ""


$services = Invoke-RestMethod -Uri "https://dev.azure.com/$OrgName/_apis/work/processes/$processId/workItemTypes/$witRefName/fields/$fieldRefName?api-version=7.1-preview.2" -Method Get -Headers @{Authorization=("Basic {0}" -f [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$PAT")))}

$services.value

并通过调用以下 API 更新字段:-

更新API:-

参考2

PATCH https://dev.azure.com/organization/_apis/work/processes/processId/workItemTypes/witRefName/fields/fieldRefName?api-version=7.1-preview.2
$personalAccessToken = "YOUR-PAT-GOES-HERE"
$organization = "YOUR-ORGANIZATION-NAME"
$processId = "PROCESS-ID"
$witRefName = "WORK-ITEM-TYPE-REF-NAME"
$fieldRefName = "FIELD-REF-NAME"
$apiVersion = "7.1-preview.2"

$baseUrl = "https://dev.azure.com/$organization/_apis/work/processes/$processId/workItemTypes/$witRefName/fields/$fieldRefName?api-version=$apiVersion"

$body = @{
    defaultValue = "Blue"
} | ConvertTo-Json

$headers = @{
    Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
    "Content-Type" = "application/json"
}

try {
    $response = Invoke-RestMethod -Uri $baseUrl -Method PATCH -Headers $headers -Body $body -ErrorAction Stop
    Write-Output "Field default value updated successfully."
} 
catch {
    Write-Error $_.Exception.Message
}

还有一个 API 可以更新 WorkItem 中的特定字段

参考3:-

PATCH https://dev.azure.com/{organization}/_apis/work/processes/{processId}/workItemTypes/{witRefName}/fields/{fieldRefName}?api-version=7.1-preview.2
$PAT = "xxxxxxxxxsxq"
$OrgName = "sid24desai0738"
$ApiVersion = "7.1-preview.3"
$WorkItemId = "123"  # Replace with the actual work item ID

$uri = "https://dev.azure.com/$OrgName/_apis/wit/workitems/$WorkItemId?api-version=$ApiVersion"

$body = @(
    @{
        op = "test"
        path = "/rev"
        value = 1
    },
    @{
        op = "add"
        path = "/fields/System.AreaPath"
        value = "Fabrikam-Fiber-Git\\Website"
    },
    @{
        op = "add"
        path = "/fields/System.History"
        value = "Moving to the right area path"
    }
) | ConvertTo-Json

Invoke-RestMethod -Uri $uri -Method Patch -Headers @{Authorization=("Basic {0}" -f [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$PAT")))} -ContentType "application/json-patch+json" -Body $body


0
投票

从第 3 方 Rest api 检索值后,您可以

parse
响应来获取所需的值,然后
set
将值发送到下面的 Rest api 中的
field value
来更新工作项。

$fieldvalue
是从您的第 3 方 Rest api 获取的示例。参考文档:更新字段

$org = "orgname"
$witId = "workitemid"
$fieldvalue = "Active"     # This is the value got from 3rd party rest api.

$token = "PAT"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$url="https://dev.azure.com/$org/_apis/wit/workitems/$witId" + "?api-version=7.0"
echo $url

$body = @(
  @{
    op = "add"
    path = "/fields/System.State"        # the field path
    value = "$fieldvalue"                # the field value
   }
)

$head = @{ Authorization =" Basic $token" }
$response = Invoke-RestMethod -Uri $url -Method PATCH -Headers $head -Body (ConvertTo-Json -InputObject $body) -ContentType application/json-patch+json

请确保您的帐户(生成 PAT)具有

enough permission
来编辑面板工作项,您可以简单地将帐户设置为 DevOps 项目的
contributor
规则。

azure devops 是否支持从第 3 方 Rest API 动态检索值

dynamically
从第3方rest api检索值,您可以考虑使用DevOps管道,并且可以有以下选项:

  1. 你可能需要以某种方式监控第3方rest api的变化,这取决于你的第3方rest api,然后触发devops pipeline,在pipeline中你可以获取值并更新工作项值。

  2. 或者您可以在DevOps管道上设置计划触发器,以便管道会自动按计划运行以更新工作项。

© www.soinside.com 2019 - 2024. All rights reserved.