如何使用 Wiql 查询 VSTS 工作项

How to query VSTS Work Items with Wiql

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

详情请参考以下代码:

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();

这是我的做法,使用 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)
});