-4

我需要使用来自 vsp-node-api 包的 Wiql 查询 VSTS 工作项,如果可能,请提供任何示例。

4

2 回答 2

4

详情参考以下代码:

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';
import * as core from 'vso-node-api/interfaces/CoreInterfaces';

var collectionUrl = "https://xxxxxx.visualstudio.com";

let token: string = "PersonalAccessToekn";

let creds = vm.getPersonalAccessTokenHandler(token);

var connection = new vm.WebApi(collectionUrl, creds); 

let vstsWI: wa.IWorkItemTrackingApi = connection.getWorkItemTrackingApi();

async function WIQLquery() {
    let teamC: core.TeamContext = {project: "", projectId: "", team: "", teamId: "" };
    let wiqls: wi.Wiql = { query: "Select [System.Id] From WorkItems Where [System.WorkItemType] = 'Task' And [System.TeamProject] = 'Project'"};
    let queryResult: wi.WorkItemQueryResult = await vstsWI.queryByWiql(wiqls, teamC);
    queryResult.workItems.forEach(s=>console.log(s.url));
}

WIQLquery();
于 2016-09-27T02:36:17.973 回答
0

这是我的做法,使用 Javascript 而不是 Typescript。向 Eddie Chen 大喊,因为他带领我朝着正确的方向前进。

// file - models/witModel.js

var azdev = require("azure-devops-node-api");

var Model = function(){};

Model.prototype.getWiqlQuery = function(wiqlQuery, teamName){
    return new Promise(function(resolve, reject){   

        const orgUrl = process.env.ADOURI; // ex.  https://dev.azure.com/<your org>
        const token = process.env.ADOPAT;   // Your personal access token
        const teamProject = process.env.ADOPROJ;// Team Project     

        let authHandler = azdev.getPersonalAccessTokenHandler(token); 
        let connection = new azdev.WebApi(orgUrl, authHandler);

        connection.getWorkItemTrackingApi().then(function(witAPI){
            var teamContext = {project: teamProject, team: teamName };
            witAPI.queryByWiql(wiqlQuery, teamContext).then(function(queryResult){
                resolve(queryResult);
            }).catch(function(err){reject(err)});
        }).catch(function(err){
            reject(err);
        });

    });
};


module.exports = new Model();

这就是我使用它的方式。

// usage - the above code was saved in a module called witModel.js
// feel free to put the module where you need to.
var witModel = require("./models/witModel.js");

// form query and set the value of the teame to query
var query = {query: "your wiql query"};
var team = "team name in Azure DEvops";

// call the promise and handle resolve/reject - then/catch
witModel.getWiqlQueryResuults(query,team).then(function(data){
    console.log(data);
}).catch(function(err){
    console.log(err)
});
于 2020-09-11T20:13:52.250 回答