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

如何通过Azure REST API实现继承流程工作项的可编辑配置?

问题

通过REST API创建继承流程后,尝试为工作项添加状态或字段时出现错误——系统使用的是原始工作项的引用名(例如Microsoft.VSTS.WorkItemTypes.Bug),而非该继承流程自身的引用名(例如processName.Bug)。

创建继承流程的PowerShell代码如下:

function newProcess {
param (
    [Parameter(Mandatory)] [string]     $organization,
    [Parameter(Mandatory)] [string]     $process,
    [Parameter(Mandatory)] [string]     $description,
    [Parameter(Mandatory)] [string]     $parent,
    [Parameter(Mandatory)] [hashtable]  $header
)

[string] $uri = "https://dev.azure.com/{0}/_apis/work/processes?{1}" -f $organization, $apiVersion
[string] $body = @{ name = $process; parentProcessTypeId = (getProcessId -organization $organization -process $parent -header $header); description = $description } | ConvertTo-Json

try {
    [string] $id = (Invoke-RestMethod -Uri $uri -Method Post -ContentType application/json -Body $body -Headers $header).typeId

    Start-Sleep -Seconds 1.0
    if ( $id -eq (getProcessId -organization $organization -process $process -header $header) ) {
        Write-Host " - Process '$process' successfully created in organization $organization !"
        return ($id)
    } else {
        throw
    }

} catch {
    Write-Host "##[error] An error occurred while creating the Process $process in the organization $organization at $uri"
    Get-Error
    throw
}
}

手动在Azure中更新状态或字段时,引用名会自动修正,请问如何通过Azure REST API实现该操作的自动化?

解决方案

核心思路

继承流程创建后,工作项类型的引用名仍沿用父流程的前缀,需通过Azure DevOps的工作项类型更新API将其替换为新流程的前缀,实现和手动操作一致的引用名修正。

步骤1:获取新流程的工作项类型列表

先调用API获取目标继承流程下所有工作项类型的当前引用名:

function getProcessWorkItemTypes {
    param(
        [Parameter(Mandatory)][string]$organization,
        [Parameter(Mandatory)][string]$processId,
        [Parameter(Mandatory)][hashtable]$header
    )
    $uri = "https://dev.azure.com/$organization/_apis/work/processes/$processId/workitemtypes?api-version=7.1-preview.2"
    return Invoke-RestMethod -Uri $uri -Method Get -Headers $header
}

# 调用示例:$newProcessId为newProcess函数返回的流程ID
$witTypes = getProcessWorkItemTypes -organization $organization -processId $newProcessId -header $header

步骤2:批量更新工作项类型引用名

遍历每个工作项类型,将引用名的前缀从父流程(如Microsoft.VSTS.WorkItemTypes)替换为新流程名称:

function updateWitReferenceName {
    param(
        [Parameter(Mandatory)][string]$organization,
        [Parameter(Mandatory)][string]$processId,
        [Parameter(Mandatory)][string]$oldRefName,
        [Parameter(Mandatory)][string]$newRefName,
        [Parameter(Mandatory)][hashtable]$header
    )
    # 对引用名中的斜杠进行URL编码
    $encodedOldRefName = $oldRefName -replace '/','%2F'
    $uri = "https://dev.azure.com/$organization/_apis/work/processes/$processId/workitemtypes/$encodedOldRefName?api-version=7.1-preview.2"
    $body = @{ referenceName = $newRefName } | ConvertTo-Json
    
    try {
        Invoke-RestMethod -Uri $uri -Method Patch -ContentType application/json -Body $body -Headers $header
        Write-Host "已更新引用名:$oldRefName → $newRefName"
    } catch {
        Write-Host "##[error] 更新失败 $oldRefName : $_"
        Get-Error
    }
}

# 执行批量更新
$newProcessName = "YourNewProcessName" # 替换为你的继承流程名称
foreach ($wit in $witTypes.value) {
    $oldRefName = $wit.referenceName
    # 提取工作项类型名称(如从Microsoft.VSTS.WorkItemTypes.Bug中提取Bug)
    $witTypeName = $oldRefName.Split('.')[-1]
    $newRefName = "$newProcessName.$witTypeName"
    
    updateWitReferenceName -organization $organization -processId $newProcessId -oldRefName $oldRefName -newRefName $newRefName -header $header
}

关键注意事项

  1. API版本选择:必须使用预览版API(如7.1-preview.2),引用名更新功能暂未正式发布。
  2. 流程状态校验:原代码中的Start-Sleep 1秒可能不足以等待流程完全初始化,建议添加循环检查流程的isEnabled状态为true后再执行更新:
    function waitForProcessReady {
        param(
            [Parameter(Mandatory)][string]$organization,
            [Parameter(Mandatory)][string]$processId,
            [Parameter(Mandatory)][hashtable]$header
        )
        $uri = "https://dev.azure.com/$organization/_apis/work/processes/$processId?api-version=7.1-preview.2"
        do {
            Start-Sleep -Seconds 2
            $processStatus = Invoke-RestMethod -Uri $uri -Method Get -Headers $header
        } while (-not $processStatus.isEnabled)
        Write-Host "流程已就绪"
    }
    
  3. 字段引用名更新:若需更新继承字段的引用名,可使用字段更新API,逻辑与工作项类型一致,API路径为:
    PATCH https://dev.azure.com/{organization}/_apis/work/processes/{processId}/workitemtypes/{witRefName}/fields/{fieldRefName}?api-version=7.1-preview.2
    

内容的提问来源于stack exchange,提问作者Takadox et compagnie

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 11:22:34