Skip to content

Instantly share code, notes, and snippets.

@rossmacarthur
Forked from jamesperrin/github-labels-import.js
Last active November 29, 2023 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rossmacarthur/e4a1fc53354c8d089ff47136e3665bd7 to your computer and use it in GitHub Desktop.
Save rossmacarthur/e4a1fc53354c8d089ff47136e3665bd7 to your computer and use it in GitHub Desktop.
JavaScript file for importing GitHub labels from a JSON object.
/**
* 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);
});
});
@rossmacarthur
Copy link
Author

Extract existing labels:

let labels = [];
document.querySelectorAll('.js-label-list > div').forEach(labelDiv => {
    let name = labelDiv.querySelector('a').getAttribute('data-name');
    let descriptionElement = labelDiv.querySelector('.js-hide-on-label-edit span');
    let description = descriptionElement ? descriptionElement.innerText : '';
    let colorId = labelDiv.querySelector('span.IssueLabel').getAttribute('id');
    let color = colorId.slice(6); // Removes the "label-" prefix

    labels.push({ name, description, color });
});

console.log(JSON.stringify(labels, null, 4));

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