Skip to content

Commit

Permalink
Match commands against each line (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey authored Sep 7, 2023
1 parent 9d564a4 commit 5a177e0
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions GithubCommentReader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,13 @@ const commands = (/** @type {Map<RegExp, CommentAction>} */(new Map()))
await triggerGHActionWithComment(request, "run-twoslash-repros", { number: issueNumber || prNumber || undefined }, `run the code sample repros`);
}, undefined, false));


const botCall = "@typescript-bot";
for (const [key, value] of [...commands.entries()]) {
commands.delete(key);
commands.set(new RegExp(`${botCall} ${key.source}`, "i"), value);
}

/**
* @param {*} context
* @param {*} data
Expand Down Expand Up @@ -565,6 +572,11 @@ function matchesCommand(context, body, isPr, authorAssociation) {
if (!body) {
return undefined;
}

if (!body.includes(botCall)) {
return undefined;
}

const applicableActions = Array.from(commands.entries()).filter(e => {
if (!isPr && e[1].prOnly) {
return false;
Expand All @@ -574,20 +586,22 @@ function matchesCommand(context, body, isPr, authorAssociation) {
if (!applicableActions.length) {
return undefined;
}
const botCall = "@typescript-bot";
if (body.indexOf(botCall) !== -1) {
context.log(`Bot reference detected in '${body}'`);
}

/** @type {((req: any) => Promise<void>)[]} */
let results = [];
for (const [key, action] of applicableActions) {
const fullRe = new RegExp(`${botCall} ${key.source}`, "i");
if (fullRe.test(body)) {
const match = fullRe.exec(body);
assert(match);
results.push(r => action.task(r, s => context.log(s), match));

const lines = new Set(body.split("\n").map(s => s.trim()).filter(s => s));
for (const line of lines) {
for (const [key, action] of applicableActions) {
if (key.test(line)) {
const match = key.exec(line);
assert(match);
results.push(r => action.task(r, s => context.log(s), match));
break;
}
}
}

if (!results.length) {
return undefined;
}
Expand Down

0 comments on commit 5a177e0

Please sign in to comment.