Skip to content

Instantly share code, notes, and snippets.

@turtlepod
Last active April 8, 2020 15:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save turtlepod/353ee764592d0dd357cb86f7830e8096 to your computer and use it in GitHub Desktop.
Save turtlepod/353ee764592d0dd357cb86f7830e8096 to your computer and use it in GitHub Desktop.
Export-Import GitHub Labels
/**
* Export Github Labels
****************************************************
*
* FIREFOX STEP BY STEP:
* 1. Open the labels manage page e.g github.com/user/repo/lebels
* 2. Open Scratch Pad (SHIFT + F4)
* 3. Paste the code below and run
* 4. Inspect Element > Console ( To read console log)
* 5. Copy it the console.log results
*
* @link https://gist.github.com/turtlepod/353ee764592d0dd357cb86f7830e8096/
*
* @link https://gist.github.com/MoOx/93c2853fee760f42d97f
* @link https://gist.github.com/Isaddo/7efebcb673a0957b9c6f07cd14826ea4
**/
var labels = [];
[].slice.call(document.querySelectorAll(".label-link"))
.forEach(function(element) {
labels.push({
name: element.textContent.trim(),
// using style.backgroundColor might returns "rgb(...)"
color: element.getAttribute("style")
.replace("background-color:", "")
.replace(/color:.*/,"")
.trim()
// github wants hex code only without # or ;
.replace(/^#/, "")
.replace(/;$/, "")
.trim(),
})
})
console.log(JSON.stringify(labels, null, 2))
/**
* Import Github Labels
****************************************************
*
* FIREFOX STEP BY STEP:
* 1. Open the labels manage page e.g github.com/user/repo/lebels
* 2. Open Scratch Pad (SHIFT + F4)
* 3. Paste the code below
* 4. Replace the labels array with your own labels data from export.js
* 5. Run it, and see the new labels
*
* @link https://gist.github.com/turtlepod/353ee764592d0dd357cb86f7830e8096/
*
* @link https://gist.github.com/MoOx/93c2853fee760f42d97f
* @link https://gist.github.com/Isaddo/7efebcb673a0957b9c6f07cd14826ea4
**/
[
{
"name": "bugfix",
"color": "eb6420"
},
{
"name": "feature",
"color": "0e8a16"
},
{
"name": "hotfix",
"color": "e11d21"
}
]
.forEach(function(label) {
addLabel(label)
})
function updateLabel (label) {
var flag = false;
[].slice.call(document.querySelectorAll(".labels-list-item"))
.forEach(function(element) {
if (element.querySelector('.label-link').textContent.trim() === label.name) {
flag = true
element.querySelector('.js-edit-label').click()
element.querySelector('.label-edit-name').value = label.name
element.querySelector('.color-editor-input').value = '#' + label.color
element.querySelector('.new-label-actions .btn-primary').click()
}
})
return flag
}
function addNewLabel (label) {
document.querySelector('.new-label input#label-').value = label.name
document.querySelector('.new-label input#edit-label-color-new').value = '#' + label.color
document.querySelector('.new-label-actions .btn-primary').click()
}
function addLabel (label) {
if (!updateLabel(label)) addNewLabel(label)
}
[
{
"name": "bug",
"color": "ee0701"
},
{
"name": "duplicate",
"color": "cccccc"
},
{
"name": "enhancement",
"color": "84b6eb"
},
{
"name": "help wanted",
"color": "128A0C"
},
{
"name": "invalid",
"color": "e6e6e6"
},
{
"name": "question",
"color": "cc317c"
},
{
"name": "wontfix",
"color": "ffffff"
}
]
@turtlepod
Copy link
Author

Thanks to @MoOx and @Isaddo for the code :)

@GuimDev
Copy link

GuimDev commented Dec 14, 2018

You forgot the description : description: element.getAttribute("aria-label"), : https://gist.github.com/GuimDev/b29673641c85ed83526163f6aa290008

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