Skip to content

Instantly share code, notes, and snippets.

@ndiego
Last active April 12, 2023 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndiego/90e9232466ef2f858ed57db0142fd5fd to your computer and use it in GitHub Desktop.
Save ndiego/90e9232466ef2f858ed57db0142fd5fd to your computer and use it in GitHub Desktop.
Export Github Labels
/*
Purpose: Export the configuration settings for GitHub Labels.
(c) James Perrin, MIT License, https://www.countrydawgg.com, | @jamesperrin
Modified to pull additional label info from the Gutenberg repo by @ndiego
Exporting Instructions:
1. Open a web browser.
2. Navigate to the desired GitHub repository.
3. Navigate to Issues tab.
4. Navigate to Labels link.
5. Open the web browser's Developer Tools
6. Navigate to the Console
7. Copy and Paste below code into the Console.
8. Save github-labels.json to a desired computer folder location.
*/
/**
* @description Exports GitHub repository Issues labels to a JSON file
*/
(function () {
function hex(x) {
return ('0' + parseInt(x).toString(16)).slice(-2);
}
function rgba2hex(rgba) {
rgba = rgba.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(,\s*\d+\.*\d+)?\)$/);
return hex(rgba[1]) + hex(rgba[2]) + hex(rgba[3]);
}
// Process to export labels into a JSON object
function getLabels() {
const jsLabels = document.querySelectorAll('.js-label-link');
if (!jsLabels || jsLabels.length < 1) {
console.error('Unable to find GitHub labels');
return;
}
const labels = [];
[].slice.call(jsLabels).map((jsLabel) => {
labels.push({
name: `${jsLabel.dataset.name.trim()}`,
description: jsLabel.parentElement.nextElementSibling.innerText.trim(),
count: jsLabel.parentElement.nextElementSibling.nextElementSibling.innerText.trim(),
color: "#" + rgba2hex(window.getComputedStyle(jsLabel).getPropertyValue('background-color')),
link: 'https://www.github.com' + jsLabel.getAttribute('href')
});
});
// Outputs labels to the Console
console.log(JSON.stringify(labels, null, 2));
return labels;
}
// Function save JSON object to a file
function saveJSON(data, filename) {
if (!data || data.length < 1) {
console.error('No data');
return;
}
const blob = new Blob([JSON.stringify(data, undefined, 4)], { type: 'text/json' });
const anchorElement = document.createElement('a');
const mouseEventOptions = {
bubbles: true,
cancelable: false,
screenX: 0,
screenY: 0,
clientX: 0,
clientY: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
button: 0,
buttons: 0,
relatedTarget: null,
region: null,
};
const mouseEvent = new MouseEvent('click', mouseEventOptions);
anchorElement.download = filename;
anchorElement.href = window.URL.createObjectURL(blob);
anchorElement.dataset.downloadurl = ['text/json', anchorElement.download, anchorElement.href].join(':');
anchorElement.dispatchEvent(mouseEvent);
}
// Saves labels to JSON file.
saveJSON(getLabels(), 'github-labels.json');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment