Skip to content

Instantly share code, notes, and snippets.

@camjocotem
Last active November 18, 2020 17:05
Show Gist options
  • Select an option

  • Save camjocotem/6d62b831b2d00914ca83298becdb4a29 to your computer and use it in GitHub Desktop.

Select an option

Save camjocotem/6d62b831b2d00914ca83298becdb4a29 to your computer and use it in GitHub Desktop.
Get all tagged TFS work items since last live release
//This script makes some assumptions
//1. You tag the work items in your commits e.g. #1234 - Fixed bug
//2. That all 'live' environments will have the same name
//https://docs.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-6.1#api-and-tfs-version-mapping
const apiVersion = "4.1-preview"
const serverUrl = "http://serverurlhere:8080" //
const projectList = ["Some_Frontend_Project","Some_Backend_Project"];
const liveEnvironmentName = "Live"
async function _TFSFetch(url){
return await(await fetch(url, {
method: 'GET',
headers:{
'Accept': 'application/json',
'Content-Type': 'application/json'
},
credentials: 'include'
})).json()
}
/**
*
* @param {number} from BuildId
* @param {number} to BuildId
*/
async function getTFSWorkItems(projectName, from, to){
return await _TFSFetch(`${serverUrl}/tfs/Projects/${projectName}/_apis/build/workitems?fromBuildId=${from}&toBuildId=${to}&api-version=${apiVersion}`);
}
/**
*
* @param {number} from BuildId
* @param {number} to BuildId
*/
async function getTFSReleases(projectName){
return await _TFSFetch(`${serverUrl}/tfs/Projects/${projectName}/_apis/release/releases?api-version=${apiVersion}&$expand=environments,artifacts`);
}
/**
* @param {Array<Object>} Params Any number of parameters, each being an array of work items from different projects
* @returns {Array}
*/
async function parseWorkItems(){
let itemsRes = [].concat(...arguments);
let results = [];
for(const item of itemsRes){
let result = await (await fetch(item.url)).json();
results.push({
id: item.id,
value: result.fields["System.Title"]
})
}
return results;
}
let workItemsList = []
for(const project of projectList){
var releases = (await getTFSReleases(project)).value;
let lastRelease = releases[0].artifacts[0].definitionReference.version.id;
let lastLiveRelease = releases.filter(release => release.environments.find(env => env.name === liveEnvironmentName).status === "succeeded")[0].artifacts[0].definitionReference.version.id
let results = (await getTFSWorkItems(project,lastLiveRelease,lastRelease)).value
workItemsList.push(results);
}
let results = await parseWorkItems(...workItemsList);
results = new Set(results.map(a => a.value))
console.log(results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment