Skip to content

Instantly share code, notes, and snippets.

@scarf005
Forked from AHilyard/export-github-labels.js
Last active November 25, 2022 07:23
Show Gist options
  • Save scarf005/df796bfd6521f504e7f4b103d95e65ad to your computer and use it in GitHub Desktop.
Save scarf005/df796bfd6521f504e7f4b103d95e65ad to your computer and use it in GitHub Desktop.
Export Github Labels via console commands
/*
Copy this script into console and run.
*/
{
/**
* Convert HSL colors to HEX
* @param {number} h Hue
* @param {number} s Saturation
* @param {number} l Lightness
* @returns {string} HEX color
*/
const hslToHex = (h, s, l) => {
l /= 100
const a = (s * Math.min(l, 1 - l)) / 100
/** @param {number} n */
const f = n => {
const k = (n + h / 30) % 12
const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1)
return Math.round(255 * color)
.toString(16)
.padStart(2, '0') // convert to Hex and prefix "0" if needed
}
return `${f(0)}${f(8)}${f(4)}`.trim()
}
const labels = Array.from(document.querySelectorAll('.js-label-link')).map(
e => {
const style = getComputedStyle(e)
const hsl = ['h', 's', 'l'].map(
c => +style.getPropertyValue(`--label-${c}`)
)
return {
name: e.textContent?.trim(),
description: e.getAttribute('title'),
color: hslToHex(...hsl),
}
}
)
const text = JSON.stringify(labels, null, 2)
copy(text)
console.log(text)
}
/*
Copy this script into console and run. Paste previous result from import_label.js.
*/
{
/**
* @typedef {Object} Label
* @property {string} name
* @property {string | null} description
* @property {string} color
*/
/** @param {Label} label */
const getPreviousLabel = label =>
previousLabels.find(
e => e.querySelector('.js-label-link')?.textContent?.trim() === label.name
)
/**
* @param {Label} prev
* @param {Label} label
*/
const updateLabel = (prev, label) => {
prev.querySelector('.js-edit-label').click()
prev.querySelector('.js-new-label-name-input').value = label.name
prev.querySelector('.js-new-label-description-input').value =
label.description
prev.querySelector('.js-new-label-color-input').value = `#${label.color}`
prev.querySelector('.js-edit-label-cancel ~ .btn-primary')?.click()
}
/** @param {Label} label */
const addNewLabel = label => {
document.querySelector('.js-details-target ~ .btn-primary').click()
document.querySelector('.js-new-label-name-input').value = label.name
document.querySelector('.js-new-label-description-input').value =
label.description
document.querySelector(
'.js-new-label-color-input'
).value = `#${label.color}`
document.querySelector('.js-details-target ~ .btn-primary').disabled = false
}
const previousLabels = Array.from(
document.querySelectorAll('.js-labels-list-item')
)
/** @type {Label[]} */
const labels = JSON.parse(prompt('Paste your labels here'))
labels.forEach(label => {
const prev = getPreviousLabel(label)
if (prev) {
updateLabel(prev, label)
} else {
addNewLabel(label)
}
})
}
Array.from(document.querySelectorAll('.js-delete-label'))
.map(e => e.querySelector('.btn-link'))
.forEach(button => {
button.removeAttribute('data-confirm')
button.click()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment