-
-
Save Isaddo/7efebcb673a0957b9c6f07cd14826ea4 to your computer and use it in GitHub Desktop.
/* | |
Go on your labels page (https://github.com/user/repo/labels) | |
Edit the following label array | |
or | |
Use this snippet to export github labels (https://gist.github.com/MoOx/93c2853fee760f42d97f) | |
and replace it | |
Paste this script in your console | |
Press Enter!! | |
*/ | |
[ | |
{ | |
"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) | |
} |
With some changes in github classnames:
[
{
"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('.js-new-label-name-input').value = label.name
element.querySelector('.js-new-label-color-input').value = '#' + label.color
element.querySelector('.js-edit-label-cancel ~ .btn-primary').click()
}
})
return flag
}
function addNewLabel (label) {
document.querySelector('.js-new-label-name-input').value = label.name
document.querySelector('.js-new-label-color-input').value = '#' + label.color
document.querySelector('.js-details-target ~ .btn-primary').click()
}
function addLabel (label) {
if (!updateLabel(label)) addNewLabel(label)
}
Very nice. Thanks!
Trying it again recently, I needed to add document.querySelector('.js-details-target ~ .btn-primary').disabled = false
before the click()
to get it to work (using @mqklin updated code).
I added two lines so the script now also works with descriptions. This includes Igelfan's fix.
// append this to the array returned from the label getter at the bottom
.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('.js-new-label-name-input').value = label.name
element.querySelector('.js-new-label-description-input').value = label.description
element.querySelector('.js-new-label-color-input').value = '#' + label.color
element.querySelector('.js-edit-label-cancel ~ .btn-primary').click()
}
})
return flag
}
function addNewLabel (label) {
document.querySelector('.js-new-label-name-input').value = label.name
document.querySelector('.js-new-label-description-input').value = label.description
document.querySelector('.js-new-label-color-input').value = '#' + label.color
document.querySelector('.js-details-target ~ .btn-primary').disabled = false
document.querySelector('.js-details-target ~ .btn-primary').click()
}
function addLabel (label) {
if (!updateLabel(label)) addNewLabel(label)
}
Getting the labels from source repo, with descriptions:
var labels = [];
[].slice.call(document.querySelectorAll(".label-link"))
.forEach(function(element) {
labels.push({
name: element.textContent.trim(),
description: element.getAttribute("aria-label"),
// 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))
Thanks for this! Very helpful
Amazing! Thank you!
Thanks a lot. I have added my own labels to add priority labels and change some default GitHub label color:
https://gist.github.com/cdlhub/02f59220d842b5a32d282f58bd502c11
Hey all, very helpful. When I tried the updates to include the descriptions the code wasn't quite working (getting null for all descriptions). I only slightly modified the code for getting the labels from the source repo. The following code worked for me.
var labels = [];
[].slice.call(document.querySelectorAll(".label-link"))
.forEach(function(element) {
labels.push({
name: element.textContent.trim(),
description: element.getAttribute("title"),
// 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))
This is great. It would be nice to have the script replace colors and descriptions of custom labels as well.
Thank you all! This is so handy - using @NillerMedDild's combo solution currently
Thanks, very helpful.
This is the code with updated GitHub classes (js-
prefix), tested and works:
function updateLabel(label) {
var flag = false;
[].slice
.call(document.querySelectorAll(".js-labels-list-item"))
.forEach(function (element) {
if (
element.querySelector(".js-label-link").textContent.trim() === label.name
) {
flag = true;
element.querySelector(".js-edit-label").click();
element.querySelector(".js-new-label-name-input").value = label.name;
element.querySelector(".js-new-label-description-input").value = label.description;
element.querySelector(".js-new-label-color-input").value = "#" + label.color;
element.querySelector(".js-edit-label-cancel ~ .btn-primary").click();
}
});
return flag;
}
function addNewLabel(label) {
document.querySelector(".js-new-label-name-input").value = label.name;
document.querySelector(".js-new-label-description-input").value = label.description;
document.querySelector(".js-new-label-color-input").value = "#" + label.color;
document.querySelector(".js-details-target ~ .btn-primary").disabled = false;
document.querySelector(".js-details-target ~ .btn-primary").click();
}
function addLabel(label) {
if (!updateLabel(label)) addNewLabel(label);
}
// Your labels
[]
.forEach(function (label) {
addLabel(label);
});
I've cleaned up and updated this code and the exporter to work with Github's new classes and style variables, and added a new "remove existing" option to the label exporter script. You can find them at these gists:
@AHilyard I tried the importer, it works like a charm!
Thanks!!
@AHilyard Thanks !!
I have updated code in 3 files:
- export-github-labels.js
- import-github-labels.js
- delete-github-labels.js
You find them here
Nice, works well. It is possible to add a flag to clear all existing flags before importing new ones