Skip to content

Instantly share code, notes, and snippets.

@jamesperrin
Last active July 17, 2023 21:03
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesperrin/c2bf6d32fbb8142682f6107e561b664d to your computer and use it in GitHub Desktop.
Save jamesperrin/c2bf6d32fbb8142682f6107e561b664d to your computer and use it in GitHub Desktop.
JavaScript file for exporting GitHub labels to a JSON object.
/*
Purpose: Export the configuration settings for GitHub Labels.
(c) James Perrin, MIT License, https://www.countrydawgg.com, | @jamesperrin
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.
Please visit the below link to download the import JavaScript script.
github-labels-import.js - https://gist.github.com/jamesperrin/d811fadea2bd199ecf98195d96513afd
*/
/**
* @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).forEach((jsLabel) => {
const name = `${jsLabel.dataset.name.trim()}`;
const description = jsLabel.parentElement.nextElementSibling.firstElementChild;
const color = rgba2hex(window.getComputedStyle(jsLabel).getPropertyValue('background-color'));
labels.push({
name: name ? name : '',
description: description ? description.innerText.trim() : '',
color: color ? color : '',
});
});
// 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');
})();
@jamesperrin
Copy link
Author

Updated code based on @NibrasAbuAyyash contrbutions.

View original thread on Export/import github labels

Link to code for importing GitHub labels github-labels-import.js

@mhucka
Copy link

mhucka commented Jul 13, 2023

I was able to use this successfully. Thanks for making this available!

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