Jira example: Find and delete unused components
This fragment lists the components of a space that no work item uses, and deletes the ones you select. Use it to tidy up a space's components in one go.
Applies to: Script Master for Jira Β· Cloud
Before you beginβ
- Jira permission: Jira admin, to create the fragment. The people who use it must be admins of the space, because only they can manage its components. The calls run as the viewer.
- App permission: an active Script Master license.
Set it upβ
-
Create a fragment with Location set to Space settings page. Only space admins can open a space's settings, so nobody else sees the fragment.
-
In Spaces, select the spaces to tidy up, or leave it empty for every space.
-
Paste the code below into Frontend HTML + JavaScript.
-
In the preview, pick a space and check the list. Don't select Delete all selected unless you mean it.
warningThe preview runs the deletes for real once you select the button. A deleted component can't be restored.
-
Turn on Enable and select Save.
Result: In the space settings, Custom Settings lists the fragment. It shows each unused component with a checked box and a Delete all selected button. A component that Jira refuses to delete stays in the list, and an error flag says why.
The codeβ
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@atlaskit/css-reset@3.0.1/dist/bundle.css" />
<p>Empty components:</p>
<p id="loading">Loading...</p>
<div id="components"></div>
<button id="delete-components" style="display: none;">Delete all selected</button>
<script type="module">
const createCheckbox = (id, name) => {
const paragraph = document.createElement('p');
const label = document.createElement('label');
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.name = name;
checkbox.value = id;
checkbox.checked = true;
label.append(checkbox, document.createTextNode(` ${name}`));
paragraph.append(label);
return paragraph;
};
// Reads every page of the space's components
const getComponents = async (projectKey) => {
const components = [];
let startAt = 0;
while (true) {
const response = await requestJira(`/rest/api/3/project/${projectKey}/component?startAt=${startAt}&maxResults=50`, {
headers: { 'Accept': 'application/json' }
});
const page = await response.json();
components.push(...page.values);
if (page.isLast) return components;
startAt += page.values.length;
}
};
const context = await view.getContext();
const projectKey = context.extension.project.key;
const components = await getComponents(projectKey);
for (const component of components) {
if (component.issueCount === 0) {
document.getElementById('components').append(createCheckbox(component.id, component.name));
}
}
document.getElementById('loading').style.display = 'none';
document.getElementById('delete-components').style.display = 'block';
document.getElementById('delete-components').addEventListener('click', async () => {
for (const checkbox of document.querySelectorAll('input:checked')) {
const response = await requestJira(`/rest/api/3/component/${checkbox.value}`, { method: 'DELETE' });
if (!response.ok) {
showFlag({
id: `not-deleted-${checkbox.value}`,
title: `${checkbox.name} not deleted: Jira answered ${response.status}`,
type: 'error',
isAutoDismiss: true,
});
continue;
}
checkbox.closest('p').remove();
showFlag({
id: `deleted-${checkbox.value}`,
title: `${checkbox.name} deleted`,
type: 'info',
isAutoDismiss: true,
});
}
});
</script>
Relatedβ
- Write fragment code: the context fields a space settings page gets.
- Preview your code: how the preview holds writes until you select something.
- Fragments: every location and how people open it.