Created
October 2, 2018 05:04
-
-
Save gyng/a4e7884554973788362ab21278c9e498 to your computer and use it in GitHub Desktop.
GitHub issues in-browser sync script
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
// Copy and paste this entire file into the browser console and run it to load the script. | |
// The console can be opened with F12 (Firefox), or Ctrl+Shift+I (Chrome) | |
// | |
// Then, to sync the current page (eg, at https://github.com/org/repo/labels) | |
// | |
// sync(LABELS); | |
const LABELS = [ | |
{ | |
name: "todo", | |
description: "This task is a TODO in the code", | |
color: "#f473af" | |
}, | |
{ | |
name: "bug", | |
description: "Something isn't working", | |
color: "#d73a4a" | |
}, | |
{ | |
name: "duplicate", | |
description: "This issue or pull request already exists", | |
color: "#cfd3d7" | |
}, | |
{ | |
name: "security", | |
description: "Something is dangerous", | |
color: "#ff8700" | |
}, | |
{ | |
name: "optimisation", | |
description: "Something is slow", | |
color: "#0030ff" | |
}, | |
{ | |
name: "critical", | |
description: "Something is really really bad", | |
color: "#000000" | |
}, | |
{ | |
name: "wontfix", | |
description: "This will not be worked on", | |
color: "#ffffff" | |
} | |
]; | |
const filterExistingLabels = labels => { | |
const existing = [].slice.call(document.querySelectorAll(".label-name")); | |
const add = labels.filter(l => !existing.find(e => e.textContent === l.name)); | |
const dupes = labels.filter(l => | |
existing.find(e => e.textContent === l.name) | |
); | |
const remove = existing | |
.filter(e => !dupes.find(d => d.name === e.textContent)) | |
.map(span => ({ name: span.textContent })); | |
return { | |
add, | |
noop: dupes, | |
remove | |
}; | |
}; | |
const removeLabels = toRemove => { | |
const rows = [].slice.call( | |
document.querySelectorAll(".js-label-list .table-list-cell") | |
); | |
rows.forEach(row => { | |
const currentRow = row.querySelector(".label-name").textContent; | |
if (toRemove.find(l => l.name === currentRow)) { | |
console.log("Clearing unused label...", currentRow); | |
row.querySelector(".js-toggle-label-delete").click(); | |
row.querySelector(".btn.btn-danger").click(); | |
} | |
}); | |
}; | |
const createLabel = label => { | |
console.log("Creating label...", label); | |
const newLabelBtn = document.querySelector(".js-details-target-new-label"); | |
newLabelBtn.click(); | |
const name = document.querySelector("#label-name-"); | |
const description = document.querySelector("#label-description-"); | |
const color = document.querySelector("#label-color-"); | |
name.value = label.name; | |
description.value = label.description; | |
color.value = label.color; | |
window.setTimeout(() => { | |
const createLabelBtn = document.querySelector( | |
'#new_label button[type="submit"]' | |
); | |
createLabelBtn.disabled = false; | |
createLabelBtn.click(); | |
}, 100); | |
}; | |
const createLabels = toAdd => { | |
toAdd.forEach((l, i) => { | |
window.setTimeout(() => { | |
createLabel(l); | |
}, i * 300); | |
}); | |
}; | |
const sync = labels => { | |
const { add, noop, remove } = filterExistingLabels(labels); | |
console.log("Deleting", remove); | |
console.log("Adding", add); | |
console.log("No change", noop); | |
removeLabels(remove); | |
window.setTimeout(() => createLabels(add), labels.length * 350); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment