Skip to content

Instantly share code, notes, and snippets.

@Isaddo
Last active February 9, 2024 22:44
  • Star 55 You must be signed in to star a gist
  • Fork 19 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Isaddo/7efebcb673a0957b9c6f07cd14826ea4 to your computer and use it in GitHub Desktop.
import github labels via console command
/*
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)
}
@guylepage3
Copy link

This is great. It would be nice to have the script replace colors and descriptions of custom labels as well.

@hannakim91
Copy link

Thank you all! This is so handy - using @NillerMedDild's combo solution currently

@dademaru
Copy link

dademaru commented Jan 28, 2021

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);
});

@AHilyard
Copy link

AHilyard commented Jan 6, 2022

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:

Label exporter
Label importer

@KINGSABRI
Copy link

@AHilyard I tried the importer, it works like a charm!
Thanks!!

@InEase
Copy link

InEase commented Feb 23, 2022

@AHilyard Thanks !!

@guylepage3
Copy link

Ahh.. Looks like this is receiving a 422 Error.

image

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