Skip to content

Instantly share code, notes, and snippets.

@bastienrobert
Last active Aug 23, 2021
Embed
What would you like to do?
Export GitHub labels from web
// paste this code inside the JS console
// use the output with import.js
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])
}
const labels = [].slice
.call(document.querySelectorAll('.js-label-link'))
.map((element) => {
return {
name: element.textContent.trim(),
description: element.getAttribute('title'),
color: rgba2hex(
window.getComputedStyle(element).getPropertyValue('background-color')
),
}
})
// paste the export.js output in a "labels" const
// run this function
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 addLabel(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();
}
labels.forEach(label => {
if (!updateLabel(label)) addLabel(label)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment