Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bot: require admin or maintain role for users #1890

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions .github/actions/bot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ async function bot(core, github, context, uuid) {
}
console.log("Comment found in payload");

// user's org membership must be public for the author_association to be MEMBER
// go to the org's member page, find yourself, and set the visibility to public
const author = payload.comment.user.login;
const authorized = ["OWNER", "MEMBER"].includes(payload.comment.author_association);
const authorized = await isUserAuthorized(github, payload);
if (!authorized) {
console.log(`Comment author is not authorized: ${author}`);
return;
Expand Down Expand Up @@ -52,6 +49,31 @@ async function bot(core, github, context, uuid) {
}
}

/**
* @returns true if the author of this payload's comment has both:
* - an OWNER or MEMBER of the repository's organization
* - the admin or maintain roles in the repository
*/
async function isUserAuthorized(github, payload) {
// user's org membership must be public for the author_association to be MEMBER
// go to the org's member page, find yourself, and set the visibility to public
const author = payload.comment.user.login;
if (!["OWNER", "MEMBER"].includes(payload.comment.author_association)) {
console.log(`Comment author association is not OWNER or MEMBER: ${author}`);
return false;
}
const authorPermissionLevel = await github.rest.repos.getCollaboratorPermissionLevel({
owner: payload.repository.owner.login,
repo: payload.repository.name,
username: author
});
if (!['admin', 'maintain'].includes(authorPermissionLevel.data.role_name)) {
console.log(`Comment author does not have the admin or maintain role for the repository: ${author}`);
return false;
}
return true;
}

// replyToCommand creates a comment on the same PR that triggered this workflow
function replyToCommand(github, payload, reply) {
github.rest.issues.createComment({
Expand Down
Loading