Skip to content

Instantly share code, notes, and snippets.

@ricardocosta
Last active July 26, 2020 11:50
Show Gist options
  • Save ricardocosta/e44f6896afb6a642b3f95e712857cde3 to your computer and use it in GitHub Desktop.
Save ricardocosta/e44f6896afb6a642b3f95e712857cde3 to your computer and use it in GitHub Desktop.
Export / Import Github labels

Export / Import Github labels

A collage from several other gists.

To export labels from a repository:

  1. Go to the repository's labels page
  2. Run the following code in the browser's console:
var labels = [];
[].slice
  .call(document.querySelectorAll(".js-label-link"))
  .forEach(function (element) {
    labels.push({
      name: element.textContent.trim(),
      description: element.getAttribute("title"),
      // using style.backgroundColor might returns "rgb(...)"
      color: element
        .getAttribute("style")
        .replace("background-color:", "")
        .replace(/color:.*/, "")
        .trim()
        // github wants hex code only without # or ;
        .replace(/^#/, "")
        .replace(/;$/, "")
        .trim(),
    });
  });

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

Source

To import the resulting labels to another repository:

  1. Go to the repository's labels page
  2. Delete all existing labels
  3. Run the following code in the browser's console:
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(".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);
}

// The labels resulting from the export operation
[
  {
    name: "wip",
    description: "Work in progress",
    color: "a8d3da",
  },
  {
    name: "bug",
    description: "Something isn't working",
    color: "ef6c57",
  },
  {
    name: "documentation",
    description: "Improvements or additions to documentation",
    color: "f4eeff",
  },
  {
    name: "duplicate",
    description: "This issue or pull request already exists",
    color: "383e56",
  },
  {
    name: "enhancement",
    description: "New feature or request",
    color: "ffe3b0",
  },
  {
    name: "help wanted",
    description: "Extra attention is needed",
    color: "aacdbe",
  },
].forEach(function (label) {
  addLabel(label);
});

Source

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