Last active
December 18, 2015 09:37
-
-
Save alreva/bb28f6f4e92f1e1f8f66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name TFS Work Items Scaffolding | |
// @namespace http://tfs.it.volvo.net:8080/ | |
// @version 0.1 | |
// @description nothing | |
// @author alreva | |
// @match http://tfs.it.volvo.net:8080/tfs/Global3/SEGOT-eCom-CORE/* | |
// @grant none | |
// ==/UserScript== | |
(function ($) { | |
function buildWorkItemDescriptionUpdateQuery(parentWorkItem, rev) { | |
var query = { | |
"id": 123123, | |
"rev": 3, | |
"projectId": "b0474275-272e-48c2-a8bc-db80005ba20e", | |
"isDirty": true, | |
"fields": { | |
"52": "description", | |
"-4": { | |
"type": 1, | |
"value": "\/Date(1425940913427)\/" | |
} | |
} | |
}; | |
query.id = parentWorkItem.id; | |
query.rev = rev || parentWorkItem.rev; | |
query.fields[52] = parentWorkItem.description + "\n<div>Scaffolded by 9CC901FAFBD54A1A97F63DAE62B1624E</div>"; | |
return query; | |
} | |
function buildTaskCreateQuery(parentId, taskName, taskDescription, areaId, iterationId, assignedTo, tempId) { | |
var query = { | |
"id": 0, | |
"rev": 0, | |
"projectId": "b0474275-272e-48c2-a8bc-db80005ba20e", | |
"isDirty": true, | |
"tempId": -1, | |
"fields": { | |
"1": "Test", // task name | |
"2": "To Do", | |
"9": "John Doe", | |
"22": "New task", | |
"24": "John Doe", // assigned to | |
"25": "Task", | |
"32": { | |
"type": 1 | |
}, | |
"33": "John Doe", | |
"52": "description", | |
"54": "Scaffolded by 9CC901FAFBD54A1A97F63DAE62B1624E", | |
"-2": 1231, // area ID | |
"-104": 2342, // iteration ID | |
"-4": { | |
"type": 1 | |
} | |
}, | |
"links": { | |
"addedLinks": [ | |
{ | |
"ID": 123123, | |
"LinkType": -2, | |
"Comment": "", | |
"FldID": 37, | |
"Changed Date": "\/Date(253370764800000)\/", | |
"Revised Date": "\/Date(253370764800000)\/" | |
} | |
] | |
} | |
}; | |
var ctx = window.TFS.Host.TfsContext.getDefault(); | |
var userName = ctx.currentUser; | |
query.fields["9"] = userName; | |
query.fields["24"] = assignedTo || userName; | |
query.fields["33"] = userName; | |
query.fields["1"] = taskName; | |
query.fields["52"] = taskDescription; | |
query.fields["-2"] = areaId; | |
query.fields["-104"] = iterationId; | |
query.tempId = tempId; | |
query.links.addedLinks[0].ID = parentId; | |
return query; | |
} | |
function buildPbiQueries(pbi, queries) { | |
return buildQueries( | |
pbi, | |
queries, | |
[ | |
"Requirements analysis and clarification", | |
"Technical design", | |
"Technical design review", | |
"Unit tests development", | |
"Development", | |
"Code review", | |
"UI tests", | |
"Manual testing", | |
"Documentation update", | |
"Documentation review" | |
]); | |
} | |
function buildBugQueries(bug, queries) { | |
return buildQueries( | |
bug, | |
queries, | |
[ | |
"Development", | |
"Code review", | |
"Manual testing" | |
]); | |
} | |
function buildQueries(workItem, queries, taskNames) { | |
var result = queries || []; | |
$.each(taskNames, function (_, task) { | |
result.push( | |
buildTaskCreateQuery( | |
workItem.id, | |
workItem.title + " - " + task, | |
workItem.description, | |
workItem.areaId, | |
workItem.iterationId, | |
workItem.assignedTo, | |
-(result.length + 1))); | |
}); | |
result.push(buildWorkItemDescriptionUpdateQuery(workItem, workItem.rev)); | |
return result; | |
} | |
function createWorkItems() { | |
if (!confirm("You are going to create a plenty of the child work items. Are you sure you want to proceed?")) { | |
return false; | |
} | |
var verificationToken = $("input[name='__RequestVerificationToken']").val(); | |
var allQueriesUrl = "http://tfs.it.volvo.net:8080/tfs/Global3/SEGOT-eCom-CORE/_api/_wit/queries?__v=5&includeQueryTexts=true"; | |
$.get(allQueriesUrl, function (data) { | |
try { | |
var query = getQuery(data); | |
} catch (e) { | |
alert(e); | |
return; | |
} | |
var execQueryUrl = "http://tfs.it.volvo.net:8080/tfs/Global3/SEGOT-eCom-CORE/_api/_wit/query?__v=5"; | |
$.post(execQueryUrl, { | |
wiql: query.query, | |
runQuery: true, | |
__RequestVerificationToken: verificationToken, | |
persistenceId: query.id | |
}, function (data) { | |
var workItems = getWorkItems(data); | |
var queries = []; | |
$.each(workItems, function (_, workItem) { | |
var workItemType = workItem.workItemType; | |
if (workItemType == "Product Backlog Item") { | |
buildPbiQueries(workItem, queries); | |
} | |
if (workItemType == "Bug") { | |
buildBugQueries(workItem, queries); | |
} | |
}); | |
var updateUrl = "http://tfs.it.volvo.net:8080/tfs/Global3/_api/_wit/updateWorkItems?__v=5"; | |
$.post(updateUrl, { | |
"updatePackage": JSON.stringify(queries), | |
"__RequestVerificationToken": verificationToken | |
}, function () { | |
console.log("Successfully created scaffolding for the work item(s) " + workItems.map(function (workItem) { return workItem.id; })); | |
if ($(".icon-refresh").length > 0) { | |
$(".icon-refresh").click(); | |
} else { | |
location.reload(true); | |
} | |
}) | |
.fail(function () { | |
var msg = "Failed to create scaffolding for the work item(s) " + workItems.map(function (workItem) { return workItem.id; }); | |
console.log(msg); | |
alert(msg); | |
}); | |
}); | |
}); | |
} | |
function getQuery(payload) { | |
var queries = payload.publicQueries.filter(function(val) { | |
return val.name == "No Child Tasks" | |
}); | |
if (queries.length > 1) { | |
throw 'There are many public queries named "No Child Tasks" in the team project. Please ensure to have only one such query. Execution will stop now.'; | |
} | |
return queries[0]; | |
} | |
function getWorkItems(data) { | |
var workItems = []; | |
$.each(data.payload.rows, function (index, value) { | |
workItems.push({ | |
id: value[0], | |
workItemType: value[1], | |
title: value[2], | |
assignedTo: value[3], | |
description: value[6], | |
areaId: value[7], | |
iterationId: value[8], | |
rev: value[9] | |
}); | |
}); | |
return workItems; | |
} | |
function buildGenerateButtons() { | |
if ($(".hub-pivot").length < 1) { | |
setTimeout(buildGenerateButtons, 1000); | |
return; | |
} | |
var $cmd = $("#genTasks"); | |
if ($cmd.length < 1) { | |
$cmd = $("<div />") | |
.attr({ 'title': 'Generate Tasks', 'id': 'genTasks' }) | |
.addClass('pivot-view') | |
.click(function () { | |
createWorkItems(); | |
}) | |
.appendTo($(".hub-pivot")); | |
} | |
$cmd.html('<a>Generate Tasks</a>'); | |
setTimeout(buildGenerateButtons, 1000); | |
} | |
buildGenerateButtons(); | |
console.log("TFS Work Items Scaffolding loaded."); | |
})(jQuery); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment