Last active
April 25, 2025 19:15
-
-
Save jamesperrin/c2bf6d32fbb8142682f6107e561b664d to your computer and use it in GitHub Desktop.
JavaScript file for exporting GitHub labels to a JSON object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Purpose: Export the configuration settings for GitHub Labels. | |
(c) James Perrin, CC0 1.0 Universal License, https://github.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 () { | |
// Utility function to convert RGB to Hex | |
const hex = (x) => `0${parseInt(x).toString(16)}`.slice(-2); | |
const rgba2hex = (rgba) => { | |
const rgbaMatch = rgba?.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(,\s*\d+(\.\d+)?)?\)$/); | |
return rgbaMatch ? hex(rgbaMatch[1]) + hex(rgbaMatch[2]) + hex(rgbaMatch[3]) : '000000'; // Default to black | |
}; | |
// Process to export labels into a JSON object | |
function getLabels() { | |
const jsLabels = document.querySelectorAll('.js-label-link'); | |
if (!jsLabels || jsLabels.length === 0) { | |
console.error('No labels found in the DOM. Make sure the page has loaded and the selector is correct.'); | |
return []; | |
} | |
return Array.from(jsLabels).map((jsLabel) => { | |
const nameLabel = jsLabel.dataset.name.trim(); | |
const descriptionLabel = | |
jsLabel.parentElement?.nextElementSibling?.firstElementChild?.innerText?.trim() ?? ''; | |
const colorLabel = rgba2hex(window.getComputedStyle(jsLabel).getPropertyValue('background-color')); | |
return { | |
name: nameLabel, | |
description: descriptionLabel, | |
color: colorLabel, | |
}; | |
}); | |
} | |
// Function save JSON object to a file | |
const saveJSON = (data, filename) => { | |
if (!data || data.length === 0) { | |
console.error('No data to save'); | |
return; | |
} | |
const blob = new Blob([JSON.stringify(data, null, 4)], { type: 'text/json' }); | |
const anchorElement = document.createElement('a'); | |
anchorElement.download = filename; | |
anchorElement.href = URL.createObjectURL(blob); | |
anchorElement.dataset.downloadurl = ['text/json', anchorElement.download, anchorElement.href].join(':'); | |
anchorElement.click(); | |
// Cleanup URL object | |
URL.revokeObjectURL(anchorElement.href); | |
}; | |
// Saves labels to JSON file. | |
try { | |
const labels = getLabels(); | |
if (labels.length) { | |
saveJSON(labels, 'github-labels.json'); | |
} | |
} catch (error) { | |
console.error('An error occurred:', error.message); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was able to use this successfully. Thanks for making this available!