Skip to content

Instantly share code, notes, and snippets.

@mathomp4
Forked from cdlhub/import-github-labels.md
Last active November 27, 2019 13:35
Show Gist options
  • Save mathomp4/e2dd00e2d49fd3e9d031ce3e7360a8db to your computer and use it in GitHub Desktop.
Save mathomp4/e2dd00e2d49fd3e9d031ce3e7360a8db to your computer and use it in GitHub Desktop.
Script to import labels to Github issue page

Import GitHub labels

Based on: https://gist.github.com/cdlhub/02f59220d842b5a32d282f58bd502c11

Which was 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 diff structural",
    "color": "6c9b1a",
    "description": "Structural changes to repository that are zero-diff"
  },
  {
    "name": "0 diff trivial",
    "color": "9dff66",
    "description": "The changes in this pull request are trivially zero-diff (documentation, build failure, &c.)"
  },
  {
    "name": "0 diff",
    "color": "00ff00",
    "description": "The changes in this pull request have verified to be zero-diff with the target branch."
  },
  {
    "name": "Contingent - DNA",
    "color": "c14807",
    "description": "Do Not Approve (DNA). These changes are contingent on other PRs"
  },
  {
    "name": "Needs Lead Approval",
    "color": "d30e5d",
    "description": "Change requires science lead approval"
  },
  {
    "name": "Non 0-diff",
    "color": "ed1cbf",
    "description": "The changes in this pull request are non-zero-diff"
  }
].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:

@tclune
Copy link

tclune commented Sep 20, 2019

Nice. Probably should spell out DNA in the description.

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