Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanmiehe committed Jan 15, 2020
1 parent 1a06f1c commit 6910855
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,11 @@ function run() {
// Load the existing labels the issue has
const labels = getLabels(client, issue_number);
// Load our regex rules from the configuration path
const labelGlobs = yield getLabelGlobs(client, configPath);
const labelRegexes = yield getLabelRegexes(client, configPath);
const addLabel = [];
const removeLabelItems = [];
for (const [label, globs] of labelGlobs.entries()) {
console.debug(`processing ${label}`);
console.debug(`for globs ${globs[0]}`);
if (checkGlobs(issue_body, globs)) {
console.log(`Queue label for addition: ${label}`);
for (const [label, globs] of labelRegexes.entries()) {
if (checkRegexes(issue_body, globs)) {
addLabel.push(label);
}
else {
Expand Down Expand Up @@ -80,13 +77,13 @@ function getIssueBody() {
}
return issue.body;
}
function getLabelGlobs(client, configurationPath) {
function getLabelRegexes(client, configurationPath) {
return __awaiter(this, void 0, void 0, function* () {
const configurationContent = yield fetchContent(client, configurationPath);
// loads (hopefully) a `{[label:string]: string | string[]}`, but is `any`:
const configObject = yaml.safeLoad(configurationContent);
// transform `any` => `Map<string,string[]>` or throw if yaml is malformed:
return getLabelGlobMapFromObject(configObject);
return getLabelRegexesMapFromObject(configObject);
});
}
// Load the configuration file
Expand All @@ -106,32 +103,28 @@ function fetchContent(client, repoPath) {
return Buffer.from(data.content, 'base64').toString('utf8');
});
}
function getLabelGlobMapFromObject(configObject) {
const labelGlobs = new Map();
function getLabelRegexesMapFromObject(configObject) {
const labelRegexes = new Map();
for (const label in configObject) {
if (typeof configObject[label] === 'string') {
labelGlobs.set(label, [configObject[label]]);
labelRegexes.set(label, [configObject[label]]);
}
else if (Array.isArray(configObject[label])) {
labelGlobs.set(label, configObject[label]);
labelRegexes.set(label, configObject[label]);
}
else {
throw Error(`found unexpected type for label ${label} (should be string or array of globs)`);
throw Error(`found unexpected type for label ${label} (should be string or array of regex)`);
}
}
return labelGlobs;
return labelRegexes;
}
function checkGlobs(issue_body, globs) {
console.log(issue_body);
function checkRegexes(issue_body, regexes) {
// If several regex entries are provided we require all of them to match for the label to be applied.
for (const glob of globs) {
console.log(` checking pattern ${glob}`);
const found = issue_body.match(glob);
for (const regEx of regexes) {
const found = issue_body.match(regEx);
if (!found) {
console.log(`Didn't find pattern`);
return false;
}
console.log(`Found patterns ${found}`);
}
return true;
}
Expand All @@ -148,12 +141,9 @@ function getLabels(client, issue_number) {
process.exit(1);
}
const labels = [];
console.log(data);
for (let i = 0; i < Object.keys(data).length; i++) {
console.log(`label is ${data[i].name}`);
labels.push(data[i].name);
}
console.log("done");
return labels;
});
}
Expand Down

0 comments on commit 6910855

Please sign in to comment.