Last active
December 25, 2024 13:44
-
-
Save davorpa/8201479803ff2a9bcc93a76a8fe71e43 to your computer and use it in GitHub Desktop.
Export-import Github repo labels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Go on you labels page: | |
// eg.: https://github.com/EbookFoundation/free-programming-books/labels | |
// | |
// 2. Paste this script in your console / save as browser bookmarklet, and then execute it | |
// 3. Copy the output / download files and now you can import it using https://github.com/popomore/github-labels ! | |
// | |
// How to bookmark: https://gist.github.com/caseywatts/c0cec1f89ccdb8b469b1 | |
(function(undefined) { | |
var reposlug; | |
try { | |
window.location.hostname.indexOf("github.com") != -1 && (reposlug = window.location.pathname.match(/([^\/]+\/[^\/]+)\/labels/i)[1].toLowerCase().replace(/\//ig, "__")); | |
} catch(e) { } | |
if (!reposlug) { | |
throw "It seems that you are not in a github.com repo labels page: " + window.location; | |
} | |
var labels = [].slice.call(document.querySelectorAll(".js-label-link")).map(function(el) { | |
var styles = window.getComputedStyle(el), | |
form = el.closest('.js-labels-list-item').querySelector('.js-label-form'); | |
return { | |
name: (el.textContent || el.innerText).trim(), /* required */ | |
description: el.getAttribute("title") || el.getAttribute("aria-label") || form['label[description]'].value || null, /* optional */ | |
color: rgba2hex(styles.getPropertyValue("background-color")), /* required */ | |
}; | |
}); | |
var json = JSON.stringify(labels, null, 2), yaml = labels2yml(labels); | |
var exts = (window.prompt("Choice download formats to save " +labels.length+ " labels.\n\n Options: json,yml,yaml\n\nCancel or leave empty to ignore.", "json,yml")||"").split(/\s*,\s*/); | |
(exts.includes("json")) && save(reposlug+"__labels.json", "application/json", json); | |
(exts.includes("yaml") || exts.includes("yml")) && save(reposlug+"__labels.yml", "application/yaml", yaml); | |
return { | |
labels: labels, | |
jsonText: json, | |
yamlText: yaml, | |
}; | |
function hex(x) { | |
return ("0" + parseInt(x).toString(16)).slice(-2); | |
} | |
function rgba2hex(rgba) { | |
rgba = rgba.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(,\s*\d+\.*\d+)?\)$/); | |
return rgba.slice(1,4).reduce(function(s, x){ return s + hex(x); }, ""); | |
} | |
function save(filename, type, content) { | |
const blob = new Blob([content], { type: type }); | |
const e = document.createEvent("MouseEvents"); | |
const a = document.createElement("a"); | |
a.download = filename; | |
a.href = window.URL.createObjectURL(blob); | |
a.dataset.downloadurl = [type, a.download, a.href].join(":"); | |
e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); | |
a.dispatchEvent(e); | |
} | |
function labels2yml(labels) { | |
return labels.reduce(function(s, l){ | |
return s +" - name: \"" +(l.name)+ "\"\n description: \"" +(l.description||"")+ "\"\n color: \"" +(l.color)+ "\"\n"; | |
}, "labels:\n"); | |
} | |
}()); |
tl;dr gh label clone src-org/src_repo --repo dest-org/dest_org
If you're coming by this thread in 2024 or later, I highly recommend seeing the official GitHub CLI [1].
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to bookmark a script: https://gist.github.com/caseywatts/c0cec1f89ccdb8b469b1