Skip to content

Instantly share code, notes, and snippets.

@zpao

zpao/Readme.md Secret

Created February 12, 2019 21:46
Show Gist options
  • Save zpao/a44889a3dfeda9af24561c631c9f8c03 to your computer and use it in GitHub Desktop.
Save zpao/a44889a3dfeda9af24561c631c9f8c03 to your computer and use it in GitHub Desktop.

Usage

Make a new personal access token and give it permissions necessary. If this is just public repos, then public_repo is enough. Make sure you copy this token somewhere safe (eg 1Password, LastPass) since it won't be available once you complete this step.

Then run it.

TOKEN=1234 node index.js --from="org/repo" --to="neworg/repo" --number=1234
const yargs = require('yargs');
const Octokit = require('@octokit/rest');
async function main() {
const argv = yargs.options({
from: {
required: true,
type: 'string',
},
to: {
required: true,
type: 'string',
},
number: {
required: true,
type: 'number',
},
}).argv;
if (process.env.TOKEN == null) {
throw new Error('Set TOKEN environment variable to personal access token.');
}
const [oldOrg, oldRepo] = argv.from.split('/');
const [newOrg, newRepo] = argv.to.split('/');
const octokit = new Octokit({ auth: `token ${process.env.TOKEN}` });
const oldIssueResponse = await octokit.issues.get({
owner: oldOrg,
repo: oldRepo,
number: argv.number,
});
const oldIssue = oldIssueResponse.data;
const newLabels = oldIssue.labels.map(label => label.name);
const newBody = `* * *
This issue was originally created by @${oldIssue.user.login} as ${
argv.from
}#${argv.number}.
* * *
${oldIssue.body}`;
const newIssueResponse = await octokit.issues.create({
owner: newOrg,
repo: newRepo,
title: oldIssue.title,
body: newBody,
labels: newLabels,
});
const newIssue = newIssueResponse.data;
const oldIssueBody = `This issue has been moved to ${argv.to}#${
newIssue.number
}.`;
await octokit.issues.createComment({
owner: oldOrg,
repo: oldRepo,
number: argv.number,
body: oldIssueBody,
});
await octokit.issues.update({
owner: oldOrg,
repo: oldRepo,
number: argv.number,
state: 'closed',
});
}
main();
{
"name": "github-move-issue",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"@octokit/rest": "^16.15.0",
"yargs": "^13.1.0"
},
"prettier": {
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment