Skip to content

Instantly share code, notes, and snippets.

@constanzaccg
Last active February 23, 2021 18:18
Show Gist options
  • Save constanzaccg/1b7aef383aa5bfcf6720944dd5eec3ba to your computer and use it in GitHub Desktop.
Save constanzaccg/1b7aef383aa5bfcf6720944dd5eec3ba to your computer and use it in GitHub Desktop.
Export labels from one repository and import them to another
/**
* EXPORT LABELS
* 1. Copy and paste the following code into the repository browser console that you want to get the labels
* 2. Copy the result
*/
let labels = [];
[].slice.call(document.querySelectorAll(".js-label-link"))
.forEach(function(element) {
labels.push({
name: element.textContent.trim(),
description: element.getAttribute( 'title' ),
color: element.getAttribute("style")
.replace("background-color:", "")
.replace(/color:.*/,"")
.trim()
.replace(/^#/, "")
.replace(/;$/, "")
.trim(),
})
})
console.log(JSON.stringify(labels, null, 2))
/**
* IMPORT LABELS
* 1. Change the JSON result to the one returned in the previous function
* 2. Copy and paste the following code into the repository console that you want to save the labels
*/
[
{
"name": "example1",
"description": "example 1",
"color": "d73a4a"
},
{
"name": "example2",
"description": "example 2",
"color": "dd2111"
}
].forEach(function(label) {
addLabel(label)
})
function updateLabel (label) {
let 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-color-input').value = '#' + label.color
element.querySelector('.js-update-label .btn-primary').click()
}
})
return flag
}
function addNewLabel (label) {
document.querySelector('.js-details-target-new-label').click()
document.querySelector('#new_label input#label-name-').value = label.name
document.querySelector('#new_label input#label-description-').value = label.description
document.querySelector('#new_label input#label-color-').value = '#' + label.color
document.querySelector('#new_label .btn-primary').disabled = false
document.querySelector('#new_label .btn-primary').click()
}
function addLabel (label) {
if (!updateLabel(label)) addNewLabel(label)
}
@jla25
Copy link

jla25 commented Oct 25, 2019

Nice code, thanks Constanza

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