Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaimramlan/88eaa5eb3836d9e1574a56957ab2bba5 to your computer and use it in GitHub Desktop.
Save zaimramlan/88eaa5eb3836d9e1574a56957ab2bba5 to your computer and use it in GitHub Desktop.
Export, Import, Update, Delete GitHub labels via browser console command

Usage

  • This script allows export, import, update and delete of GitHub Labels
  • Adopted from Isaddo's gist with some tweaks for 2020
  • Included a delete script too

Export Labels

  1. Go to the source repository's labels page
  2. Open up the browser console
  3. Copy and paste the following script & copy its output
    var labels = [];
    []
    .slice
    .call(document.querySelectorAll(".js-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))

Delete Labels

  1. Go to destination repository's labels page
  2. Open browser console
  3. Copy and paste the following script
    (Note: This script will delete all labels)
    []
    .slice
    .call(document.querySelectorAll(".js-labels-list-item"))
    .forEach(function(element) {
      var button = element.querySelector('.js-delete-label').querySelector('button')
      button.removeAttribute('data-confirm')
      button.click()
    })

Import or Update Labels

  1. Go the destination repository's labels page
  2. Open up the browser console
  3. Paste the output from the previous section
  4. Copy and paste the following script to the same last line of the previus step
    (The script below will import new labels or automatically update pre-existing labels)
    .forEach(function(label) {
      addLabel(label)
    })
    
    function updateLabel (label) {
      var flag = false;
      []
      .slice
      .call(document.querySelectorAll(".js-labels-list-item"))
      .forEach(function(element) {
        if (element.querySelector('.js-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)
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment