Skip to content

Commit

Permalink
chore: bot check authorized users
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Jan 27, 2021
1 parent 1a6f32c commit 5809a03
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions .github/bot-scripts/checkAuthorized.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ async function main(param) {
// console.log(`context.payload.issue:`);
// console.dir(context.payload.issue);

let isAuthorized;
const user = context.payload.comment.user.login;

if (context.payload.issue.html_url.includes("/pull/")) {
console.log("Comment appears in a PR, retrieving PR info...");
// Only the pull request author and authorized users may execute this command
Expand All @@ -25,33 +28,35 @@ async function main(param) {
});

const allowed = [...authorizedUsers, pull.user.login];
const commenting = context.payload.comment.user.login;
console.log(`Authorized users: ${allowed.join(", ")}`);
console.log(`Commenting user: ${commenting}`);
const isAuthorized = allowed.includes(commenting);
console.log(`Commenting user: ${user}`);
isAuthorized = allowed.includes(user);
console.log(`Is authorized: ${isAuthorized}`);

if (!isAuthorized) return false;
} else {
// In issues, only the authorized users may execute any commands
console.log("Comment appears in an issue");

const commenting = context.payload.comment.user.login;
console.log(`Authorized users: ${authorizedUsers.join(", ")}`);
console.log(`Commenting user: ${commenting}`);
const isAuthorized = authorizedUsers.includes(commenting);
console.log(`Commenting user: ${user}`);
isAuthorized = authorizedUsers.includes(user);
console.log(`Is authorized: ${isAuthorized}`);

if (!isAuthorized) return false;
}

// Let the user know we're working on it
await github.reactions.createForIssueComment({
...options,
comment_id: context.payload.comment.id,
content: "rocket",
});

return true;
if (isAuthorized) {
// Let the user know we're working on it
await github.reactions.createForIssueComment({
...options,
comment_id: context.payload.comment.id,
content: "rocket",
});
} else {
// Let the user know he can't do that
await github.issues.createComment({
...options,
issue_number: context.payload.issue.number,
body: `Sorry ${user}, you're not authorized to do that 🙁!`,
});
}
return isAuthorized;
}
module.exports = main;
module.exports = main;

0 comments on commit 5809a03

Please sign in to comment.