Skip to content

Instantly share code, notes, and snippets.

@Aschen
Created July 29, 2022 15:07
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 Aschen/56e750b1ee17879238c9484d57e28434 to your computer and use it in GitHub Desktop.
Save Aschen/56e750b1ee17879238c9484d57e28434 to your computer and use it in GitHub Desktop.
Copy labels from one Github repository to another

Copy labels from one Github repository to another

Install dependencies npm i @octokit/rest

Generate a Github Personnal Token (starts with ghp_) and insert it in the script.

Choose the source repository and insert it in the script (e.g. kuzzleio/kuzzle)

Run the script: node copy-labels.js kuzzleio/koncorde

This will copy all labels from kuzzleio/kuzzle to kuzzleio/koncorde.

const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({
auth: 'ghp_..', // Your Personnal Token here
});
const sourceRepository = 'kuzzleio/kuzzle';
const destRepository = process.argv[2];
console.log(`Copy labels from ${sourceRepository} to ${destRepository}`);
(async () => {
const [sourceOwner, sourceRepo] = sourceRepository.split('/');
const [destOwner, destRepo] = destRepository.split('/');
const res = await octokit.issues.listLabelsForRepo({ owner: sourceOwner, repo: sourceRepo })
const labels = res.data;
console.log(`Found ${labels.length} labels`);
const promises = [];
const errors = [];
for (const label of labels) {
promises.push(
octokit.issues.createLabel({
owner: destOwner,
repo: destRepo,
name: label.name,
description: label.description,
color: label.color,
})
.then(() => console.log(`"${label.name}" copied`))
.catch(error => errors.push(`${label.name}: ${error.message}`))
);
}
await Promise.all(promises);
console.log(`${labels.length - errors.length} labels copied`);
if (errors) {
console.log('Errors: ', errors);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment