Skip to content

Instantly share code, notes, and snippets.

@bitst0rm
Created September 6, 2024 00:24
Show Gist options
  • Save bitst0rm/ba0e5b0add473b4a99304fe529ed67d0 to your computer and use it in GitHub Desktop.
Save bitst0rm/ba0e5b0add473b4a99304fe529ed67d0 to your computer and use it in GitHub Desktop.
Export Github Labels via console commands
/* Export GitHub Labels
* @ref: https://gist.github.com/Isaddo/7efebcb673a0957b9c6f07cd14826ea4
*
* @see: https://gist.github.com/bitst0rm
* export-github-labels.js
* import-github-labels.js
* delete-github-labels.js
*
* To use:
* 1. Go to the labels page of your repository (e.g., https://github.com/user/repo/labels).
* 2. Paste this script into your browser's JavaScript console.
* 3. Press Enter!
*
* This script will:
* - convert RGB or HSL values to HEX color.
* - print the JSON output to the console.
* - automatically download it as a file.
*/
(function() {
function rgbToHex(r, g, b) {
function toHex(value) {
var hex = value.toString(16);
return hex.length === 1 ? "0" + hex : hex;
}
return "#" + toHex(r) + toHex(g) + toHex(b);
}
function hslToHex(h, s, l) {
s /= 100;
l /= 100;
var c = (1 - Math.abs(2 * l - 1)) * s;
var x = c * (1 - Math.abs(((h / 60) % 2) - 1));
var m = l - c / 2;
function getRgb(h) {
if (h < 60) return [c, x, 0];
if (h < 120) return [x, c, 0];
if (h < 180) return [0, c, x];
if (h < 240) return [0, x, c];
if (h < 300) return [x, 0, c];
return [c, 0, x];
}
var rgb = getRgb(h);
var r = Math.round((rgb[0] + m) * 255);
var g = Math.round((rgb[1] + m) * 255);
var b = Math.round((rgb[2] + m) * 255);
return rgbToHex(r, g, b);
}
function parseLabels() {
var labels = document.querySelectorAll(".js-label-preview-container");
var result = [];
labels.forEach(function(label) {
var labelElement = label.querySelector("a");
if (!labelElement) return; // Skip if <a> tag doesn"t exist
var style = window.getComputedStyle(labelElement);
// Extract RGB and HSL values
var r = parseFloat(style.getPropertyValue("--label-r")) || 0;
var g = parseFloat(style.getPropertyValue("--label-g")) || 0;
var b = parseFloat(style.getPropertyValue("--label-b")) || 0;
var h = parseFloat(style.getPropertyValue("--label-h")) || 0;
var s = parseFloat(style.getPropertyValue("--label-s")) || 0;
var l = parseFloat(style.getPropertyValue("--label-l")) || 0;
// Determine the color format to use
var color;
if (!isNaN(r) && !isNaN(g) && !isNaN(b)) {
// Use RGB to HEX conversion if RGB values are present
color = rgbToHex(r, g, b);
} else if (!isNaN(h) && !isNaN(s) && !isNaN(l)) {
// Use HSL to HEX conversion if HSL values are present
color = hslToHex(h, s, l);
} else {
// Fallback color
color = "#000000"; // Default to black if no valid color is found
}
var name = labelElement.textContent.trim();
var descriptionElement = label.querySelector(".js-hide-on-label-edit span");
var description = descriptionElement ? descriptionElement.innerText.trim() : "";
result.push({
name: name,
description: description,
color: color
});
});
return result;
}
function downloadJSON(labels) {
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(labels, null, 4));
var downloadAnchor = document.createElement("a");
downloadAnchor.setAttribute("href", dataStr);
downloadAnchor.setAttribute("download", "github_labels.json");
document.body.appendChild(downloadAnchor);
downloadAnchor.click();
downloadAnchor.remove();
}
var labels = parseLabels();
console.log(JSON.stringify(labels, null, 4));
downloadJSON(labels);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment