问题
I am writing a VSTS dashboard widget used for Work Item Tracking
However I am running into a problem when using the getWorkItem() function. I want to get the ids of all the Features under a given Epic (I already know the epic ID). I am confident that if I set the expand paremeter of getWorkItem() to "All" I will get a list of all the Features and their respective ids. Unfortunately I do not know how to define the "type" of expand parameter and how to properly pass it as a value to the getWorkItem() function.
Here is my code:
VSS.require(["VSS/Service", "TFS/Dashboards/WidgetHelpers", "TFS/WorkItemTracking/RestClient"],
function (VSS_Service, WidgetHelpers, TFS_Wit_WebApi) {
WidgetHelpers.IncludeWidgetStyles();
VSS.register("myapp", function () {
var fetchData = function (widgetSettings) {
const epicID = 123456;
// Get a WIT client to make REST calls to VSTS
return VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient).getWorkItem(123456, null, null, All).
then(
//Successful retrieval of workItems
function (workItems) {
$('#myText').text(JSON.stringify(workItems));
console.log(workItems);
// Use the widget helper and return success as Widget Status
return WidgetHelpers.WidgetStatusHelper.Success();
},
function (error) {
// Use the widget helper and return failure as Widget Status
return WidgetHelpers.WidgetStatusHelper.Failure(error.message);
});
}
Here is the VSTS reference for expand It explains what the values can be, but doesn't say how to pass it into the getWorkItem() function.
I would like to set the optional expand parameter of the function to "All" but don't know its type and how to properly define and use it.
回答1:
Based on the source code, it is the enum, so you can specify the integer (e.g. 4) in getWorkItem function.
export enum WorkItemExpand {
None = 0,
Relations = 1,
Fields = 2,
Links = 3,
All = 4,
}
回答2:
Using the enum is good, but you can also pass in the value from the 'TFS/WorkItemTracking/Contracts' module. You can find the reference here (shows the module path, the 'class', and the enums):
- https://docs.microsoft.com/en-us/vsts/extend/reference/client/api/tfs/workitemtracking/contracts/workitemexpand?view=vsts
The link above comes from the TFS WorkItemTracking API Reference, which can be found here:
- https://docs.microsoft.com/en-us/vsts/extend/reference/client/api/tfs/workitemtracking/restclient/workitemtrackinghttpclient2_2?view=vsts#getworkitem()
Here's how you can add it in your code:
- Declare the 'TFS/WorkItemTracking/Contracts' module
- Pass the module into the callback ('_Contracts' in the example below)
- Use '_Contracts' as needed
Here is your code, updated to use the Contracts module:
VSS.require([
"VSS/Service",
"TFS/Dashboards/WidgetHelpers",
"TFS/WorkItemTracking/RestClient",
"TFS/WorkItemTracking/Contracts"],
function (VSS_Service, WidgetHelpers, TFS_Wit_WebApi, _Contracts) {
WidgetHelpers.IncludeWidgetStyles();
VSS.register("myapp", function () {
var fetchData = function (widgetSettings) {
const epicID = 123456;
// Get a WIT client to make REST calls to VSTS
return VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient).
getWorkItem(123456, null, null, _Contracts.WorkItemExpand.All).
then(
//Successful retrieval of workItems
function (workItems) {
$('#myText').text(JSON.stringify(workItems));
console.log(workItems);
// Use the widget helper and return success as Widget Status
return WidgetHelpers.WidgetStatusHelper.Success();
},
function (error) {
// Use the widget helper and return failure as Widget Status
return WidgetHelpers.WidgetStatusHelper.Failure(error.message);
});
}
});
});
Hope that helps!
来源:https://stackoverflow.com/questions/50275661/vsts-dashboard-widget-getworkitem-optional-parameter-expand