supermemo-wiki / SuperMemoAssistant.Wiki

Documentation resources
MIT License
3 stars 7 forks source link

Project management: Updating & synchronizing GitHub labels #24

Open alexis- opened 4 years ago

alexis- commented 4 years ago

1) Generate the desired label list: navigate to a repo's Labels, or to the default organization Labels* and run:

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

2) Go the repositories that require synchronization, and run:

[
  {
    "name": "bug",
    "description": "Something isn't working",
    "color": "d73a4a"
  },
  ...
  (More labels)
  ...
  {
    "name": "wontfix",
    "description": "This will not be worked on",
    "color": "ffffff"
  }
].forEach(function(label) {
  addLabel(label)
})

function updateLabel (label) {
  var flag = false;
  [].slice.call(document.querySelectorAll(".js-label-list"))
  .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)
}

Scripts modified from the original versions: (1) https://gist.github.com/MoOx/93c2853fee760f42d97f (2) https://gist.github.com/Isaddo/7efebcb673a0957b9c6f07cd14826ea4