如何在Azure DevOps扩展中用VSS工作项客户端绕过规则更新只读字段
如何通过VSS工作项客户端绕过规则更新只读字段
一、使用VSS Work Item Client绕过规则
VSS提供的WorkItemTrackingHttpClient的updateWorkItem方法支持通过可选参数传递查询参数,直接加入bypassRules=true即可绕过Process Rule限制,同时无需硬编码PAT——扩展会自动使用当前用户的身份进行认证。
示例代码:
var document = [{ "op": "add", "path": "/fields/Custom.MyField", "value": "10001" }]; // 在updateWorkItem的最后一个参数中传入options,设置bypassRules为true witClient.updateWorkItem(document, 1, null, null, null, { bypassRules: true }) .then(function (response) { console.log(response); }, function (error) { console.log(error); });
参数说明:updateWorkItem的第6个参数是可选的请求选项对象,可直接添加需要的查询参数,这里传入{ bypassRules: true }就会自动附加到API请求的查询字符串中。
二、其他实现方案:利用VSS认证服务调用原生API
如果不想依赖Work Item Client,也可以直接调用Azure DevOps REST API,但通过VSS的认证服务获取当前用户的授权头,避免嵌入PAT:
VSS.require(["VSS/Authentication/Services"], function(AuthService) { // 获取当前扩展上下文的授权头 AuthService.getAuthorizationHeader().then(function(authHeader) { fetch("https://dev.azure.com/aherrick/_apis/wit/workitems/1?api-version=7.0&bypassRules=true", { method: "PATCH", headers: { "Content-Type": "application/json-patch+json", "Authorization": authHeader }, body: JSON.stringify([{ "op": "add", "path": "/fields/Custom.MyField", "value": "100" }]) }); }); });
这种方式既保留了直接调用API的灵活性,又借助VSS的身份管理机制实现安全认证,无需手动处理PAT。
内容的提问来源于stack exchange,提问作者aherrick
相关产品推荐
相关产品推荐

