Skip to content

Instantly share code, notes, and snippets.

@elstgav
Last active March 11, 2025 01:13
Show Gist options
  • Save elstgav/977b1cc86c4fa7a809a56e77196709db to your computer and use it in GitHub Desktop.
Save elstgav/977b1cc86c4fa7a809a56e77196709db to your computer and use it in GitHub Desktop.
Bulk create labels from the GitHub Issues page.
/**
* 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()
}

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'],
    ])

Note

The script is idempotent and will not overwrite existing labels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment