Skip to content

Instantly share code, notes, and snippets.

@victorzinho
Last active August 20, 2017 17:21
Show Gist options
  • Save victorzinho/dcb3fe39cfc4202139817258bf14f836 to your computer and use it in GitHub Desktop.
Save victorzinho/dcb3fe39cfc4202139817258bf14f836 to your computer and use it in GitHub Desktop.
Create Github labels from a JSON file
const https = require('https');
const fs = require('fs');
const LABELS_FILE = 'labels.json';
if (!fs.existsSync(LABELS_FILE)) {
console.log(LABELS_FILE + ' does not exist!');
return 1;
} else if (process.argv.length !== 5) {
console.log('Usage:' + process.argv[0] + ' ' + process.argv[1] + ' <owner> <repo> <token>');
return 2;
}
var options = {
hostname: 'api.github.com',
path: '/repos/' + process.argv[2] + '/' + process.argv[3] + '/labels',
method: 'POST',
headers: {
'Authorization': 'token ' + process.argv[4],
'User-Agent': 'node script'
}
};
function create(name, color) {
var req = https.request(options, r => console.log(`Creating label ${name}...${r.statusCode}`));
req.write(JSON.stringify({ name: name, color: color }));
req.end();
}
var labels = JSON.parse(fs.readFileSync(LABELS_FILE, 'utf8'));
for(name in labels) {
var color = labels[name];
if (typeof color !== 'string' || color.trim().length === 0) continue;
create(name, color);
}
{
"Priority: Critical": "ee0701",
"Priority: High": "ff9900",
"Priority: Low": "009900",
"Status: Ready": "c2e0c6",
"Status: Blocked": "b60205",
"Status: In Progress": "0052cc",
"Status: Review Needed": "eecc00",
"Type: Bug": "ee0701",
"Type: Feature": "84b6eb",
"Type: Question": "cc317c"
}
@victorzinho
Copy link
Author

victorzinho commented Aug 17, 2017

OWNER=owner
REPO=your_repo
TOKEN=your_token

To delete all labels:

for i in `curl https://api.github.com/repos/$OWNER/$REPO/labels | jq -r '.[].url'`; do 
  curl -H "Authorization: token $TOKEN" -X DELETE "$i";
done

To add new labels:

wget https://gist.github.com/victorzinho/dcb3fe39cfc4202139817258bf14f836/raw/create-labels.js
wget https://gist.github.com/victorzinho/dcb3fe39cfc4202139817258bf14f836/raw/labels.json
node create-labels.js $OWNER $REPO $TOKEN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment