Skip to content

Instantly share code, notes, and snippets.

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

This comment has been minimized.

Copy link

@hisabimbola hisabimbola commented Sep 14, 2016

Nice, works well. It is possible to add a flag to clear all existing flags before importing new ones

@mqklin

This comment has been minimized.

Copy link

@mqklin mqklin commented Oct 21, 2017

With some changes in github classnames:

[
  {
    "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('.js-new-label-name-input').value = label.name
      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-color-input').value = '#' + label.color
  document.querySelector('.js-details-target ~ .btn-primary').click()
}

function addLabel (label) {
  if (!updateLabel(label)) addNewLabel(label)
}
@Merovex

This comment has been minimized.

Copy link

@Merovex Merovex commented Dec 2, 2017

Very nice. Thanks!

@lgelfan

This comment has been minimized.

Copy link

@lgelfan lgelfan commented Jan 26, 2018

Trying it again recently, I needed to add document.querySelector('.js-details-target ~ .btn-primary').disabled = false before the click() to get it to work (using @mqklin updated code).

@NillerMedDild

This comment has been minimized.

Copy link

@NillerMedDild NillerMedDild commented Sep 24, 2018

I added two lines so the script now also works with descriptions. This includes Igelfan's fix.

// append this to the array returned from the label getter at the bottom
.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('.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)
}

Getting the labels from source repo, with descriptions:

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))
@TreONeill

This comment has been minimized.

Copy link

@TreONeill TreONeill commented Oct 10, 2018

Thanks for this! Very helpful

@priom

This comment has been minimized.

Copy link

@priom priom commented Feb 16, 2019

Amazing! Thank you!

@cdlhub

This comment has been minimized.

Copy link

@cdlhub cdlhub commented Feb 18, 2019

Thanks a lot. I have added my own labels to add priority labels and change some default GitHub label color:
https://gist.github.com/cdlhub/02f59220d842b5a32d282f58bd502c11

@dallinca

This comment has been minimized.

Copy link

@dallinca dallinca commented Sep 8, 2019

Hey all, very helpful. When I tried the updates to include the descriptions the code wasn't quite working (getting null for all descriptions). I only slightly modified the code for getting the labels from the source repo. The following code worked for me.

var labels = [];
[].slice.call(document.querySelectorAll(".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))
@guylepage3

This comment has been minimized.

Copy link

@guylepage3 guylepage3 commented Jan 13, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.