Skip to content

Instantly share code, notes, and snippets.

@andrey-skl
Created March 20, 2017 09:17
Show Gist options
  • Save andrey-skl/f9819e3c637003c7880fadc638b68fde to your computer and use it in GitHub Desktop.
Save andrey-skl/f9819e3c637003c7880fadc638b68fde to your computer and use it in GitHub Desktop.
JetBrains Hub dashboards cleaning snippet
/*
Run this script in Chrome > 50. See guide here https://developers.google.com/web/tools/chrome-devtools/snippets
Backup your dashboards before running it.
Script provided "as is", there is no warranty that it won't remove wrong dashboard.
*/
(function() {
/*
Leave empty if Hub is hosted on the same URL and does not have context. (e.q. standalone Hub)
Fill with '/hub/' if running on youtrack cloud instance.
*/
const HUB_URL = '/hub/';
const NAME_TO_REMOVE = 'My Dashboard';
const authParams = JSON.parse(localStorage.getItem('0-0-0-0-0-token'));
const authString = `Bearer ${authParams.access_token}`
console.log(`
=====Dashboards removal script=====
Hub URL = ${HUB_URL};
Dashboards with name equal to "${NAME_TO_REMOVE}" will be removed.
===================================
`);
function removeDashboard(dashboard) {
console.info('Removing dashboard', dashboard.id, dashboard.name);
return fetch(`${HUB_URL}api/rest/dashboards/${dashboard.id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json, text/plain, */*',
Authorization: authString
}
})
.then(res => {
if (res.status > 300) {
console.warn('Failed to remove dashboard', dashboard.id, dashboard.name)
} else {
console.log('Successfully removed', dashboard.id, dashboard.name)
}
})
.catch(err => console.log('Error', err));
}
fetch(HUB_URL + 'api/rest/dashboards', {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json, text/plain, */*',
Authorization: authString
}
})
.then(res => res.json())
.then(dashboardRes => {
if (dashboardRes.status >= 300) {
throw dashboardRes;
}
console.info('Current list of dashboards:', dashboardRes.items.slice());
const toRemove = dashboardRes.items
.filter(dashboard => dashboard.name === NAME_TO_REMOVE);
console.log('Dashboards to remove:', toRemove);
const removalPromises = toRemove.map(dashboard => removeDashboard(dashboard));
return Promise.all(removalPromises);
})
.then(res => {
console.log('All done! Now refresh the tab')
})
.catch(err => console.error('Error', err));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment