Skip to content

Commit

Permalink
Add support for supplying multiple label files
Browse files Browse the repository at this point in the history
- Fixes #49

This change introduces support for any number of `--labels` arguments to
be supplied. The behavior is the same as before, with regards to http(s)
urls or local paths, and after they are all collected, they will be
merged. Any items with a duplicate name and different property values
will throw an error. Otherwise they will be merged and then applied.
  • Loading branch information
kenperkins authored and JakeChampion committed May 6, 2020
1 parent 7e8d12b commit ff8c3bb
Showing 1 changed file with 66 additions and 21 deletions.
87 changes: 66 additions & 21 deletions bin/github-label-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ program
.option(
'-l, --labels <path>',
'the path or URL to look for the label configuration in. Default: labels.json',
'labels.json'
getLabelFiles,
[ ]
)
.option(
'-d, --dry-run',
Expand All @@ -41,29 +42,44 @@ if (program.args.length !== 1) {
program.help();
}

function getLabelFiles(value, previous) {
return previous.concat([value]);
}

function readLabels() {
if (program.labels.indexOf('http://') === 0 || program.labels.indexOf('https://') === 0) {
const got = require('got');
if (program.labels.length === 0) {
program.labels = [ 'labels.json' ];
}

return got(program.labels, { json: true }).then((response) => response.body).catch(() => {
console.error(chalk.red(`Downloading labels from ${program.labels} failed`));
process.exit(1);
});
const files = [];

} else {
// Resolve the label configuration path
if (!/^[\/\~]/.test(program.labels)) {
program.labels = path.resolve(process.cwd(), program.labels);
}
program.labels.forEach((file) => {
if (file.indexOf('http://') === 0 || file.indexOf('https://') === 0) {
const got = require('got');

// Load the labels
try {
return Promise.resolve(require(program.labels));
} catch (error) {
console.error(chalk.red(`No labels were found in ${program.labels}`));
process.exit(1);
files.push(got(file, { json: true }).then((response) => response.body).catch(() => {
console.error(chalk.red(`Downloading labels from ${file} failed`));
process.exit(1);
}));

} else {
// Resolve the label configuration path
if (!/^[\/\~]/.test(file)) {
file = path.resolve(process.cwd(), file);
}

// Load the labels
try {
files.push(Promise.resolve(require(file)));
} catch (error) {
console.error(chalk.red(`No labels were found in ${file}`));
process.exit(1);
}
}
}

});

return Promise.all(files);
}

// Apply some log formatting
Expand All @@ -79,16 +95,45 @@ const format = {
}
};

// Merge our lists together
function merge(files) {
const data = {};
const labels = [];

files.forEach((file) => {
if (!Array.isArray(file)) {
return;
}

file.forEach((label) => {
if (data[label.name] !== null && data[label.name] !== '') {
data[label.name] = label;
} else {
if (JSON.stringify(data[label.name]) !== JSON.stringify(label)) {
console.error(chalk.red(`Conflicting label names were found: ${label.name}`));
process.exit(1);
}
}
});
});

Object.keys(data).forEach((key) => {
labels.push(data[key]);
});

return labels;
}

// Pull together all the options
function resolveOptions() {
return readLabels().then((labels) => {
return readLabels().then((files) => {
return {
accessToken: program.accessToken,
allowAddedLabels: program.allowAddedLabels,
dryRun: program.dryRun,
endpoint: program.endpoint,
format: format,
labels: labels,
labels: merge(files),
log: console,
repo: program.args[0]
};
Expand Down

0 comments on commit ff8c3bb

Please sign in to comment.