Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to import labels to Github issue page

Import GitHub labels

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

  1. Go on your labels page https://github.com/user/repo/labels
  2. Paste this script in your console.
  3. Press Enter.
[
  {
    "name": "0 - blocker",
    "color": "b60205",
    "description": "High priority, needs immediate response"
  },
  {
    "name": "1 - urgent",
    "color": "d93f0b",
    "description": "Urgent priority"
  },
  {
    "name": "2 - normal",
    "color": "e99695",
    "description": "Normal priority"
  },
  {
    "name": "3 - low",
    "color": "f9d0c4",
    "description": "Low priority"
  },
  {
    "name": "bug",
    "color": "e27461",
    "description": "Something isn't working"
  },
  {
    "name": "enhancement",
    "color": "fbca04",
    "description": "New feature or request"
  },
  {
    "name": "on hold",
    "color": "e9e9e9",
    "description": "This will be worked on when situation changes"
  },
  {
    "name": "in progress",
    "color": "5cc429",
    "description": "Someone is working on this"
  }
].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)
}

You'll get the following labels:

@fivesmallq

This comment has been minimized.

Copy link

@fivesmallq fivesmallq commented Apr 10, 2019

Great, thank you !

@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