diff --git a/.github/workflows/review-reusable.yml b/.github/workflows/review-reusable.yml index 2ee1e76..327bc64 100644 --- a/.github/workflows/review-reusable.yml +++ b/.github/workflows/review-reusable.yml @@ -42,10 +42,6 @@ on: required: false type: string default: github-actions[bot] - lenient_approval_user: - required: false - type: string - default: lezi-fun codex_base_url: required: false type: string @@ -113,7 +109,6 @@ jobs: CODEX_MODEL: ${{ inputs.codex_model }} CODEX_REASONING_EFFORT: ${{ inputs.codex_reasoning_effort }} BOT_NAME: ${{ inputs.bot_name }} - LENIENT_APPROVAL_USER: ${{ inputs.lenient_approval_user }} AUTO_MERGE: ${{ inputs.auto_merge }} MERGE_METHOD: ${{ inputs.merge_method }} REQUIRE_CHECKS: ${{ inputs.require_checks }} diff --git a/.github/workflows/review.yml b/.github/workflows/review.yml index bbc9992..1b57fac 100644 --- a/.github/workflows/review.yml +++ b/.github/workflows/review.yml @@ -42,7 +42,6 @@ jobs: review_state: ${{ github.event.review.state || '' }} review_commit_id: ${{ github.event.review.commit_id || '' }} bot_name: github-actions[bot] - lenient_approval_user: lezi-fun codex_base_url: ${{ vars.CODEX_BASE_URL || 'https://api.openai.com/v1' }} codex_model: ${{ vars.CODEX_MODEL || 'gpt-5.4' }} codex_reasoning_effort: ${{ vars.CODEX_REASONING_EFFORT || 'high' }} diff --git a/README.md b/README.md index f7af5b9..44ff225 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,10 @@ GitHub Actions bot that reviews pull requests by calling Codex CLI, leaves inlin - Supports a lenient pass when a repository administrator comments `/lenient-check`. - If no repository administrator has commented or reviewed within the last 24 hours, the lenient trigger automatically broadens to users with `write`, `maintain`, or `admin` permission. - In lenient mode, only blocks dangerous changes, runtime-impacting issues, errors, crashes, broken builds, data-loss risks, and security risks. -- A PR that passes lenient mode is not auto-merged until `@lezi-fun` approves the current head commit. +- A PR that passes lenient mode is not auto-merged until an eligible reviewer approves the current head commit. +- Eligible reviewers for lenient merge approval are: + - repository administrators when an administrator has commented or reviewed on the PR within the last 24 hours + - otherwise any repository user with `write`, `maintain`, or `admin` permission - If the reviewer detects clearly malicious code, the bot comments with the reason and closes the pull request. - Posts inline comments only on valid added diff lines. - Approves clean pull requests. @@ -93,7 +96,6 @@ jobs: review_state: ${{ github.event.review.state || '' }} review_commit_id: ${{ github.event.review.commit_id || '' }} bot_name: github-actions[bot] - lenient_approval_user: lezi-fun codex_base_url: ${{ vars.CODEX_BASE_URL || 'https://api.openai.com/v1' }} codex_model: ${{ vars.CODEX_MODEL || 'gpt-5.4' }} codex_reasoning_effort: ${{ vars.CODEX_REASONING_EFFORT || 'high' }} @@ -133,7 +135,12 @@ That reruns the AI in lenient mode. If no administrator has commented or reviewed within the last 24 hours on that PR, the command is also accepted from users with `write` or `maintain` permission. -If lenient mode passes, the bot still requires `@lezi-fun` to approve the current head commit before merge. +If lenient mode passes, the bot still requires an eligible reviewer to approve the current head commit before merge. + +Approval eligibility follows the same rule as lenient triggering: + +- if a repository administrator has commented or reviewed on that PR within the last 24 hours, only an administrator approval can unlock merge +- otherwise any user with `write`, `maintain`, or `admin` permission can unlock merge ## Local development @@ -167,7 +174,6 @@ Important variables: - `CODEX_MODEL`: defaults to `gpt-5.4` - `CODEX_REASONING_EFFORT`: optional, one of `minimal`, `low`, `medium`, `high`, `xhigh` - `BOT_NAME`: defaults to `ghbot`, but the workflow sets it to `github-actions[bot]` -- `LENIENT_APPROVAL_USER`: defaults to `lezi-fun` - `AUTO_MERGE`: defaults to `false` - `MERGE_METHOD`: defaults to `squash` - `REQUIRE_CHECKS`: defaults to `true` diff --git a/src/config.ts b/src/config.ts index 53e652d..634703c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -36,7 +36,6 @@ const configSchema = z.object({ codexBaseUrl: optionalString, codexApiKey: z.string().min(1), botName: z.string().min(1).default("ghbot"), - lenientApprovalUser: z.string().min(1).default("lezi-fun"), autoMerge: envBoolean.default(false), mergeMethod: z.enum(["merge", "squash", "rebase"]).default("squash"), requireChecks: envBoolean.default(true), @@ -52,7 +51,6 @@ export const config = configSchema.parse({ codexBaseUrl: process.env.CODEX_BASE_URL, codexApiKey: process.env.CODEX_API_KEY, botName: process.env.BOT_NAME, - lenientApprovalUser: process.env.LENIENT_APPROVAL_USER, autoMerge: process.env.AUTO_MERGE, mergeMethod: process.env.MERGE_METHOD, requireChecks: process.env.REQUIRE_CHECKS, diff --git a/src/review/processor.ts b/src/review/processor.ts index 5fb4174..bb08900 100644 --- a/src/review/processor.ts +++ b/src/review/processor.ts @@ -98,7 +98,7 @@ export async function processPullRequestReviewApproval( commitId: string; } ): Promise { - if (params.state !== "approved" || params.reviewerLogin !== config.lenientApprovalUser) { + if (params.state !== "approved") { return; } @@ -122,6 +122,37 @@ export async function processPullRequestReviewApproval( return; } + const adminRecentlyResponded = await hasRecentAdminResponse(octokit, { + owner: params.owner, + repo: params.repo, + pullNumber: params.pullNumber + }); + + const { data: permission } = await octokit.rest.repos.getCollaboratorPermissionLevel({ + owner: params.owner, + repo: params.repo, + username: params.reviewerLogin + }); + + const allowedPermissions = adminRecentlyResponded + ? new Set(["admin"]) + : new Set(["admin", "maintain", "write"]); + + if (!allowedPermissions.has(permission.permission)) { + logger.info( + { + owner: params.owner, + repo: params.repo, + pullNumber: params.pullNumber, + reviewerLogin: params.reviewerLogin, + permission: permission.permission, + adminRecentlyResponded + }, + "Ignoring lenient approval because reviewer does not meet the current permission threshold." + ); + return; + } + await maybeMergePullRequest(octokit, { owner: params.owner, repo: params.repo, @@ -228,24 +259,34 @@ async function maybeMergePullRequest( } if (params.mode === "lenient") { - const approvedByOwner = await hasCurrentHeadApprovalFrom(octokit, { + const adminRecentlyResponded = await hasRecentAdminResponse(octokit, { + owner, + repo, + pullNumber + }); + + const approvalRequirement = adminRecentlyResponded + ? "a repository administrator" + : "a repository user with write permission or above"; + + const approvedByEligibleReviewer = await hasCurrentHeadApprovalFrom(octokit, { owner, repo, pullNumber, headSha: params.headSha, - reviewerLogin: config.lenientApprovalUser + requireAdmin: adminRecentlyResponded }); - if (!approvedByOwner) { + if (!approvedByEligibleReviewer) { await withRetry("github.issues.createComment.awaitLenientApproval", async () => { return octokit.rest.issues.createComment({ owner, repo, issue_number: pullNumber, - body: `Lenient check passed, but this PR will not be merged until @${config.lenientApprovalUser} approves the current head commit.` + body: `Lenient check passed, but this PR will not be merged until ${approvalRequirement} approves the current head commit.` }); }); - logger.info({ owner, repo, pullNumber, reviewer: config.lenientApprovalUser }, "Waiting for lenient approval before merging."); + logger.info({ owner, repo, pullNumber, adminRecentlyResponded }, "Waiting for eligible lenient approval before merging."); return; } } @@ -300,7 +341,7 @@ async function hasCurrentHeadApprovalFrom( repo: string; pullNumber: number; headSha: string; - reviewerLogin: string; + requireAdmin: boolean; } ): Promise { const reviews = await octokit.paginate(octokit.rest.pulls.listReviews, { @@ -310,13 +351,44 @@ async function hasCurrentHeadApprovalFrom( per_page: 100 }); - return reviews.some((review) => { - return ( - review.user?.login === params.reviewerLogin && - review.state === "APPROVED" && - review.commit_id === params.headSha - ); - }); + const approvedLogins = reviews + .filter((review) => review.state === "APPROVED" && review.commit_id === params.headSha) + .map((review) => review.user?.login) + .filter((login): login is string => Boolean(login)); + + for (const login of approvedLogins) { + try { + const { data: permission } = await octokit.rest.repos.getCollaboratorPermissionLevel({ + owner: params.owner, + repo: params.repo, + username: login + }); + + if (params.requireAdmin) { + if (permission.permission === "admin") { + return true; + } + continue; + } + + if (new Set(["admin", "maintain", "write"]).has(permission.permission)) { + return true; + } + } catch (error) { + logger.warn( + { + error, + owner: params.owner, + repo: params.repo, + pullNumber: params.pullNumber, + login + }, + "Failed to resolve collaborator permission while checking lenient approval eligibility." + ); + } + } + + return false; } async function listPullRequestFiles(