【问题标题】:How to create work items using Visual Studio Team Services Client for Node.js (vso-node-api)?如何使用 Visual Studio Team Services Client for Node.js (vso-node-api) 创建工作项?
【发布时间】:2017-01-30 14:25:18
【问题描述】:

我需要使用 Visual Studio Team Services Client for Node.js (vso-node-api) 创建 VSTS 工作项,请提供任何示例?

【问题讨论】:

    标签: node.js azure-devops tfs-sdk azure-pipelines azure-pipelines-build-task


    【解决方案1】:

    我创建了一个简单的代码示例来获取和创建工作项供您参考,请参阅以下部分了解详细信息:

    /// <reference path="typings/index.d.ts" />
    
    import * as vm from 'vso-node-api/WebApi';
    import * as wa from 'vso-node-api/WorkItemTrackingApi';
    import * as wi from 'vso-node-api/interfaces/WorkItemTrackingInterfaces';
    import * as vss from 'vso-node-api/interfaces/Common/VSSInterfaces';
    
    var collectionUrl = "https://xxxxxx.visualstudio.com";
    
    let token: string = "Yourpersonalaccesstoken";
    
    let creds = vm.getPersonalAccessTokenHandler(token);
    
    var connection = new vm.WebApi(collectionUrl, creds); 
    
    let vstsWI: wa.IWorkItemTrackingApi = connection.getWorkItemTrackingApi();
    
    async function getWI() {
        let wiid: number = 1;
        let workitem: wi.WorkItem = await vstsWI.getWorkItem(wiid);
    
        console.log(workitem.url);
    }
    
    getWI();
    
    async function createWI() {
        let wijson: vss.JsonPatchDocument = [{ "op": "add", "path": "/fields/System.Title", "value": "Task created from Node JS" }];
        let project: string = "Project";
        let witype: string = "Task";
        let cWI: wi.WorkItem = await vstsWI.createWorkItem(null, wijson, project, witype);
        console.log(cWI.id);
    }
    
    createWI();
    

    【讨论】:

    • 非常感谢 Eddie 的回复,我的项目使用的是 ES5 版本,所以当我添加 vso-node-api 节点包并构建到 ES5 时出现错误,似乎“承诺”是在 ES5 中不可用,您知道任何解决方法吗?
    • @BandR 不,我不知道任何解决方法。对不起。
    • 即使输入了有效的凭据,我也会收到未经授权的 401 错误,知道为什么吗? “错误:请求失败:未经授权 (401) - 在 processResponse (c:\vso-test\node_modules\vso-node-api\RestClient.js:59:18)\n 在 c:\vso-test\node_modules \vso-node-api\RestClient.js:147:13\n 在 HttpClient.request.callback (c:\vso-test\node_modules\vso-node-a...
    • @BandR 你使用了什么凭证?
    • 我使用了下面的令牌,它起作用了 'let auth = tl.getEndpointAuthorization("SYSTEMVSSCONNECTION", false);' 'var token = auth.parameters["AccessToken"];'
    【解决方案2】:

    使用通过 vsts-task-lib 检索到的令牌

    import tl = require('vsts-task-lib/task');
    let auth = tl.getEndpointAuthorization("SYSTEMVSSCONNECTION", false);
    var token = auth.parameters["AccessToken"];
    

    【讨论】:

      猜你喜欢
      • 2016-12-05
      • 1970-01-01
      • 2014-11-11
      • 2019-02-08
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      相关资源
      最近更新 更多