Forked from jamesperrin/github-labels-import.js
Last active
November 29, 2023 09:13
-
-
Save rossmacarthur/e4a1fc53354c8d089ff47136e3665bd7 to your computer and use it in GitHub Desktop.
JavaScript file for importing GitHub labels from 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
/** | |
* 1. Update the labels JSON object. | |
* 2. Open web browsers | |
* 3. Navigate to desired GitHub repository. | |
* 4. Navigate to Issues tab. | |
* 5. Navigate to Labels link. | |
* 6. Open web browswer Developer Tools | |
* 7. Navigate to the Console window. | |
* 8. Copy and Paste the below code snippets into the Console window. | |
*/ | |
const labels = [ | |
{ | |
"name": "bug", | |
"description": "Something isn't working", | |
"color": "ff7a86" | |
}, | |
{ | |
"name": "enhancement", | |
"description": "Internal improvements", | |
"color": "a3c3ff" | |
}, | |
{ | |
"name": "feature", | |
"description": "New feature or request", | |
"color": "58de8f" | |
}, | |
{ | |
"name": "question", | |
"description": "Further information is requested", | |
"color": "f470ed" | |
} | |
]; | |
// Function to update an existing label | |
function updateLabel(label) { | |
let 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 to add a new label | |
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 to add a new label | |
function addLabel(label) { | |
if (!updateLabel(label)) { | |
addNewLabel(label); | |
} | |
} | |
// Importing labels from JSON. | |
// This one is not available natively in IE, but there are polyfills available. | |
Promise.resolve().then(function(){ | |
labels.forEach(function(label) { | |
addLabel(label); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Extract existing labels: