|
/** |
|
* Create GitHub labels in bulk from the Web Inspector console |
|
* |
|
* To use: |
|
* |
|
* 1. Go your GitHub repo’s issues page (e.g. `github.com/my/repo/issues/labels`) |
|
* 2. Open the Web Inspector console |
|
* 3. Copy-paste the script below |
|
* 4. Call `createLabels` with the labels you’d like to add: |
|
* |
|
* createLabels([ |
|
* ['bug', 'Something isn’t working', '#0366d6'], |
|
* ['wontfix', 'We won’t implement this', '#B60205'], |
|
* ]) |
|
* |
|
* The script is idempotent and will not overwrite existing labels. |
|
*/ |
|
|
|
const NEW_LABEL_BUTTON_TEXT = 'New label' |
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) |
|
|
|
const findVisiblNewLabelButton = () => { |
|
const newLabelButtons = document.evaluate( |
|
`//button[contains(text(), '${NEW_LABEL_BUTTON_TEXT}')]`, |
|
document, |
|
null, |
|
XPathResult.UNORDERED_NODE_ITERATOR_TYPE, |
|
null, |
|
) |
|
let visibleNewLabelButton = newLabelButtons.iterateNext() |
|
while (visibleNewLabelButton && !visibleNewLabelButton.offsetWidth) { |
|
visibleNewLabelButton = newLabelButtons.iterateNext() |
|
} |
|
|
|
if (!visibleNewLabelButton) { |
|
throw new Error(`Could not find visible “${NEW_LABEL_BUTTON_TEXT}” button`) |
|
} |
|
return visibleNewLabelButton |
|
} |
|
|
|
const createLabels = (labels = []) => { |
|
newLabels = labels.filter( |
|
([name]) => !document.querySelector(`a.IssueLabel[data-name="${name}"]`), |
|
) |
|
if (!newLabels.length) return |
|
|
|
const newLabelButton = findVisiblNewLabelButton() |
|
|
|
const createLabel = async (index = 0) => { |
|
const [name, description, color] = newLabels[index] |
|
newLabelButton.click() |
|
await sleep(10) |
|
|
|
const nameInput = document.querySelector('input[name="label[name]"]') |
|
const form = nameInput.closest('form') |
|
const descriptionInput = form.querySelector( |
|
'input[name="label[description]"]', |
|
) |
|
const colorButton = form.querySelector('button#new-label-color') |
|
const colorInput = form.querySelector('input[name="label[color]"]') |
|
|
|
if ( |
|
!newLabelButton || |
|
!nameInput || |
|
!descriptionInput || |
|
!colorButton || |
|
!colorInput |
|
) { |
|
throw new Error('Could not find inputs', { |
|
newLabelButton, |
|
nameInput, |
|
descriptionInput, |
|
colorButton, |
|
colorInput, |
|
}) |
|
} |
|
|
|
nameInput.value = name |
|
descriptionInput.value = description |
|
colorButton.click() // Trigger form validation |
|
await sleep(10) |
|
colorInput.value = color |
|
|
|
form.querySelector('button[type="submit"]').click() |
|
|
|
await sleep(200) |
|
|
|
if (index < newLabels.length - 1) createLabel(index + 1) |
|
} |
|
|
|
createLabel() |
|
} |