Instantly share code, notes, and snippets.

@MoOx /index.js
Last active Dec 1, 2018

Embed
What would you like to do?
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))
@apotek

This comment has been minimized.

apotek commented Feb 5, 2016

Useful! Thank you.

@vincentaudebert

This comment has been minimized.

vincentaudebert commented Apr 15, 2016

Awesome! Saved me a lot of time :)

@Isaddo

This comment has been minimized.

Isaddo commented May 6, 2016

Another approach to import github labels via console command

https://gist.github.com/Isaddo/7efebcb673a0957b9c6f07cd14826ea4

@wo0dyn

This comment has been minimized.

wo0dyn commented Oct 13, 2016

@MoOx: Thank you so much mate! ❤️

@jpike88

This comment has been minimized.

jpike88 commented Jun 21, 2017

This script doesn't work on Safari

@oguennec

This comment has been minimized.

oguennec commented Jul 4, 2017

Great, thank you !

@ntwb

This comment has been minimized.

ntwb commented Jul 12, 2017

I just used the following which was quick and easy https://github.com/jvandemo/copy-github-labels-cli

(I was linked to this after the fact)

@martindafonte

This comment has been minimized.

martindafonte commented Jun 22, 2018

Hi, you can add one line and also get the description:

var labels = [];
[].slice.call(document.querySelectorAll(".label-link"))
.forEach(function(element) {
  labels.push({
    name: element.textContent.trim(),
    description: element.getAttribute("aria-label"),
    // 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))
@micalevisk

This comment has been minimized.

micalevisk commented Jul 27, 2018

and this function to download labels as file directly

function saveJSON(data, filename) {
  const blob = new Blob([JSON.stringify(data, undefined, 4)], { type: 'text/json' });
  const e = document.createEvent('MouseEvents');
  const a = document.createElement('a');

  a.download = filename;
  a.href = window.URL.createObjectURL(blob);
  a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
  e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  a.dispatchEvent(e);
}

example

saveJSON(labels, 'labels.json')
@TreONeill

This comment has been minimized.

TreONeill commented Oct 10, 2018

Thanks for this! Very helpful

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