Skip to content

Instantly share code, notes, and snippets.

@MoOx
Last active February 9, 2024 22:44
Show Gist options
  • Save MoOx/93c2853fee760f42d97f to your computer and use it in GitHub Desktop.
Save MoOx/93c2853fee760f42d97f to your computer and use it in GitHub Desktop.
Export/import github labels
// go on you labels pages
// eg https://github.com/cssnext/cssnext/labels
// paste this script in your console
// copy the output and now you can import it using https://github.com/popomore/github-labels !
var labels = [];
[].slice.call(document.querySelectorAll(".label-link"))
.forEach(function(element) {
labels.push({
name: element.textContent.trim(),
// 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))
@DeathGOD7
Copy link

Also to easily import and export things.
Use @davorpa code mentioned here. (direct link)

And for importing just install "Settings" app (link) by probot and make this file :
.github/settings.yml

# Labels: define labels for Issues and Pull Requests
labels:
- name: api
  description: This issue or pull request is related to API
  color: b60205
- name: not related
  description: not related to us or our plugins/addons
  color: d93f0b
- name: awaiting response
  description: Waiting for response

Happy importing. And thanks to @davorpa for both json and yml / yaml file saver.

@jamesperrin
Copy link

jamesperrin commented May 10, 2022

Here are my updates for exporting and importing issues labels.
It should be cross browser compatible except for IE which is pretty much dead.

github-labels-export.js
https://gist.github.com/jamesperrin/c2bf6d32fbb8142682f6107e561b664d

/*

Purpose: Export the configuration settings for GitHub Labels.
(c) James Perrin, MIT License, https://www.countrydawgg.com, | @jamesperrin

Exporting Instructions:

1. Open a web browser.
2. Navigate to the desired GitHub repository.
3. Navigate to Issues tab.
4. Navigate to Labels link.
5. Open the web browser's Developer Tools
6. Navigate to the Console
7. Copy and Paste below code into the Console.
8. Save github-labels.json to a desired computer folder location.

Please visit the below link to download the import JavaScript script.
github-labels-import.js - https://gist.github.com/jamesperrin/d811fadea2bd199ecf98195d96513afd

*/

/**
 * @description Exports GitHub repository Issues labels to a JSON file
 *
 */
(function () {
  function hex(x) {
    return ('0' + parseInt(x).toString(16)).slice(-2);
  }

  function rgba2hex(rgba) {
    rgba = rgba.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(,\s*\d+\.*\d+)?\)$/);
    return hex(rgba[1]) + hex(rgba[2]) + hex(rgba[3]);
  }

  // Process to export labels into a JSON object
  function getLabels() {
    const jsLabels = document.querySelectorAll('.js-label-link');

    if (!jsLabels || jsLabels.length < 1) {
      console.error('Unable to find GitHub labels');

      return;
    }

    const labels = [];

    [].slice.call(jsLabels).forEach((jsLabel) => {
      const name = `${jsLabel.dataset.name.trim()}`;
      const description = jsLabel.parentElement.nextElementSibling.firstElementChild;
      const color = rgba2hex(window.getComputedStyle(jsLabel).getPropertyValue('background-color'));

      labels.push({
        name: name ? name : '',
        description: description ? description.innerText.trim() : '',
        color: color ? color : '',
      });
    });

    // Outputs labels to the Console
    console.log(JSON.stringify(labels, null, 2));

    return labels;
  }

  // Function save JSON object to a file
  function saveJSON(data, filename) {
    if (!data || data.length < 1) {
      console.error('No data');

      return;
    }

    const blob = new Blob([JSON.stringify(data, undefined, 4)], { type: 'text/json' });

    const anchorElement = document.createElement('a');

    const mouseEventOptions = {
      bubbles: true,
      cancelable: false,
      screenX: 0,
      screenY: 0,
      clientX: 0,
      clientY: 0,
      ctrlKey: false,
      altKey: false,
      shiftKey: false,
      metaKey: false,
      button: 0,
      buttons: 0,
      relatedTarget: null,
      region: null,
    };

    const mouseEvent = new MouseEvent('click', mouseEventOptions);

    anchorElement.download = filename;
    anchorElement.href = window.URL.createObjectURL(blob);
    anchorElement.dataset.downloadurl = ['text/json', anchorElement.download, anchorElement.href].join(':');
    anchorElement.dispatchEvent(mouseEvent);
  }

  // Saves labels to JSON file.
  saveJSON(getLabels(), 'github-labels.json');
})();

github-labels-import.js
https://gist.github.com/jamesperrin/d811fadea2bd199ecf98195d96513afd

/*

Purpose: Import settings for GitHub Labels.
(c) James Perrin, MIT License, https://www.countrydawgg.com, | @jamesperrin

Importing Instructions:

1. Update the labels JSON object.
2. Open a web browser.
3. Navigate to the desired GitHub repository.
4. Navigate to Issues tab.
5. Navigate to Labels link.
6. Open the web browswer's Developer Tools
7. Navigate to the Console window.
8. Copy and Paste the below code snippets into the Console window.

Please visit the below link to download the export JavaScript script.
github-labels-export.js - https://gist.github.com/jamesperrin/c2bf6d32fbb8142682f6107e561b664d

*/

const labels = [
  {
    name: 'bug',
    description: "Something isn't working",
    color: 'ee0701',
  },
  {
    name: 'duplicate',
    description: 'This issue or pull request already exists',
    color: 'cccccc',
  },
  {
    name: 'enhancement',
    description: 'New feature or request',
    color: '84b6eb',
  },
  {
    name: 'good first issue',
    description: 'Good for newcomers',
    color: '7057ff',
  },
  {
    name: 'help wanted',
    description: 'Extra attention is needed',
    color: '33aa3f',
  },
  {
    name: 'invalid :notebook:',
    description: "This doesn't seem right",
    color: 'e6e6e6',
  },
  {
    name: 'question',
    description: 'Further information is requested',
    color: 'cc317c',
  },
  {
    name: 'wontfix',
    description: 'This will not be worked on',
    color: 'ffffff',
  },
];

// Function to update an existing label
function updateLabel(label) {
  let flag = false;

  [].slice.call(document.querySelectorAll('.labels-list-item')).forEach((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 update or add a new label
function addLabel(label) {
  if (!updateLabel(label)) {
    addNewLabel(label);
  }
}

labels.map((label) => {
  addLabel(label);
});

@jakobe
Copy link

jakobe commented Jun 10, 2022

Update June 2022: As far as I can see the description no longer exists as title on the label element itself.
Though it might be brittle to rely on the DOM structure of the labels page, this is my fix getting the description from the column after the label itself:

...
description: jsLabel.parentElement.nextElementSibling.firstElementChild.innerText.trim(),
...

@jamesperrin
Copy link

@jakobe I updated my code based on your comments. At the moment, looks like a good option.

@jakobe
Copy link

jakobe commented Jun 10, 2022

@jakobe I updated my code based on your comments. At the moment, looks like a good option.

Awesome 👍

@Brend-Smits
Copy link

The official GitHub CLI now includes functionality that allows you to clone labels easily from one repo to another.
Example syntax:
gh label clone org-name/repo-to-clone-from --repo org-name/repo-to-clone-to

See the documentation for more information.

@sambacha
Copy link

gh label clone org-name/repo-to-clone-from --repo org-name/repo-to-clone-to

bet

@jeffwidman
Copy link

The GitHub CLI docs also illustrate listing the labels using custom JSON output templates: https://cli.github.com/manual/gh_label_list

@adevinwild
Copy link

The official GitHub CLI now includes functionality that allows you to clone labels easily from one repo to another. Example syntax: gh label clone org-name/repo-to-clone-from --repo org-name/repo-to-clone-to

See the documentation for more information.

Thanks works well!🎉

@deffcolony
Copy link

deffcolony commented Jul 13, 2023

@jamesperrin I get the following error when i try to paste the export code in the console browser

3188:30 Uncaught TypeError: Cannot read properties of null (reading 'innerText')
    at <anonymous>:30:77
    at Array.map (<anonymous>)
    at getLabels (<anonymous>:27:27)
    at <anonymous>:79:11
    at <anonymous>:80:3
(anonymous) @ VM3188:30
getLabels @ VM3188:27
(anonymous) @ VM3188:79
(anonymous) @ VM3188:80

Do you have a fix for this?

@jamesperrin
Copy link

@jamesperrin I get the following error when i try to paste the export code in the console browser

3188:30 Uncaught TypeError: Cannot read properties of null (reading 'innerText')
    at <anonymous>:30:77
    at Array.map (<anonymous>)
    at getLabels (<anonymous>:27:27)
    at <anonymous>:79:11
    at <anonymous>:80:3
(anonymous) @ VM3188:30
getLabels @ VM3188:27
(anonymous) @ VM3188:79
(anonymous) @ VM3188:80

Do you have a fix for this?

@deffcolony I need a little more information. What is the URL for GitHub repository you tried to run the script against?

@deffcolony
Copy link

@jamesperrin I get the following error when i try to paste the export code in the console browser

3188:30 Uncaught TypeError: Cannot read properties of null (reading 'innerText')
    at <anonymous>:30:77
    at Array.map (<anonymous>)
    at getLabels (<anonymous>:27:27)
    at <anonymous>:79:11
    at <anonymous>:80:3
(anonymous) @ VM3188:30
getLabels @ VM3188:27
(anonymous) @ VM3188:79
(anonymous) @ VM3188:80

Do you have a fix for this?

@deffcolony I need a little more information. What is the URL for GitHub repository you tried to run the script against?

@jamesperrin The URL is https://github.com/deffcolony/HP-Witchcraft-and-Wizardry/labels

@jamesperrin
Copy link

jamesperrin commented Jul 17, 2023

@jamesperrin I get the following error when i try to paste the export code in the console browser

3188:30 Uncaught TypeError: Cannot read properties of null (reading 'innerText')
    at <anonymous>:30:77
    at Array.map (<anonymous>)
    at getLabels (<anonymous>:27:27)
    at <anonymous>:79:11
    at <anonymous>:80:3
(anonymous) @ VM3188:30
getLabels @ VM3188:27
(anonymous) @ VM3188:79
(anonymous) @ VM3188:80

Do you have a fix for this?

@deffcolony I need a little more information. What is the URL for GitHub repository you tried to run the script against?

@jamesperrin The URL is https://github.com/deffcolony/HP-Witchcraft-and-Wizardry/labels

@deffcolony I added validations checks for all the label properties. The issue was some label may not have a description which caused the error. You should be able to run the script without issue.

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