Last active
October 2, 2024 14:36
-
-
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, 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 === 0) { | |
console.error('Unable to find GitHub labels!!'); | |
return; | |
} | |
const labels = [].slice.call(jsLabels).map((el) => { | |
const name = el.textContent || el.innerText; | |
const description = el.parentElement.nextElementSibling.firstElementChild; | |
const color = rgba2hex(window.getComputedStyle(el).getPropertyValue('background-color')); | |
return { | |
name: name ? name.trim() : '', | |
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 === 0) { | |
console.error('Missing GitHub labels data!!'); | |
return; | |
} | |
const blob = new Blob([JSON.stringify(data, undefined, 4)], { type: 'text/json' }); | |
const anchorElement = document.createElement('a'); | |
anchorElement.download = filename; | |
anchorElement.href = window.URL.createObjectURL(blob); | |
anchorElement.dataset.downloadurl = ['text/json', anchorElement.download, anchorElement.href].join(':'); | |
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.dispatchEvent(mouseEvent); | |
} | |
// Saves labels to JSON file. | |
saveJSON(getLabels(), 'github-labels.json'); | |
})(); |
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
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