Skip to content

Instantly share code, notes, and snippets.

@AHilyard
Last active April 19, 2023 12:56
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AHilyard/a5b9376d0326fd658a8064d5569791a4 to your computer and use it in GitHub Desktop.
Save AHilyard/a5b9376d0326fd658a8064d5569791a4 to your computer and use it in GitHub Desktop.
Export Github Labels via console commands
/*
This script will export Github labels to an array.
This array can then be imported using the label importer script.
Instructions:
Go to the labels page for the repo you'd like to export from (https://github.com/user/repo/labels)
Paste this script in your console
Press Enter
Copy the resultant array into the importer script. (https://gist.github.com/AHilyard/5babebe06c30a48e07d949053e00bd5c)
*/
function hslToHex(h, s, l) {
l /= 100;
const a = s * Math.min(l, 1 - l) / 100;
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)}`;
}
var labels = [];
[].slice.call(document.querySelectorAll(".js-label-link"))
.forEach(function(element) {
labels.push({
name: element.textContent.trim(),
description: element.getAttribute("title"),
color: hslToHex(getComputedStyle(element).getPropertyValue("--label-h"),
getComputedStyle(element).getPropertyValue("--label-s"),
getComputedStyle(element).getPropertyValue("--label-l"))
.trim(),
})
});
console.log(JSON.stringify(labels, null, 2));
@zrezke
Copy link

zrezke commented Apr 19, 2023

I updated the script to set the data-name attribute as the title, emojies weren't being copied for me otherwise

/*
  This script will export Github labels to an array.  
  This array can then be imported using the label importer script.
  
  Instructions:
  Go to the labels page for the repo you'd like to export from (https://github.com/user/repo/labels)
  Paste this script in your console
  Press Enter
  Copy the resultant array into the importer script. (https://gist.github.com/AHilyard/5babebe06c30a48e07d949053e00bd5c)
*/

function hslToHex(h, s, l) {
  l /= 100;
  const a = s * Math.min(l, 1 - l) / 100;
  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)}`;
}

var labels = [];
[].slice.call(document.querySelectorAll(".js-label-link"))
.forEach(function(element) {
  labels.push({
    name: element.getAttribute("data-name"),
    description: element.getAttribute("title"),
    color: hslToHex(getComputedStyle(element).getPropertyValue("--label-h"),
                    getComputedStyle(element).getPropertyValue("--label-s"),
                    getComputedStyle(element).getPropertyValue("--label-l"))
      .trim(),
  })
});
console.log(JSON.stringify(labels, null, 2));

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