Use admin-or-writer lenient approval

This commit is contained in:
lezi-fun committed 2026-05-17 10:13:30 +08:00
1 parent 298baea998
commit de5c10115b
5 files changed
+96 -26

No files matched your search

-5
View File
@@ -42,10 +42,6 @@ on:
required: false required: false
type: string type: string
default: github-actions[bot] default: github-actions[bot]
lenient_approval_user:
required: false
type: string
default: lezi-fun
codex_base_url: codex_base_url:
required: false required: false
type: string type: string
@@ -113,7 +109,6 @@ jobs:
CODEX_MODEL: ${{ inputs.codex_model }} CODEX_MODEL: ${{ inputs.codex_model }}
CODEX_REASONING_EFFORT: ${{ inputs.codex_reasoning_effort }} CODEX_REASONING_EFFORT: ${{ inputs.codex_reasoning_effort }}
BOT_NAME: ${{ inputs.bot_name }} BOT_NAME: ${{ inputs.bot_name }}
LENIENT_APPROVAL_USER: ${{ inputs.lenient_approval_user }}
AUTO_MERGE: ${{ inputs.auto_merge }} AUTO_MERGE: ${{ inputs.auto_merge }}
MERGE_METHOD: ${{ inputs.merge_method }} MERGE_METHOD: ${{ inputs.merge_method }}
REQUIRE_CHECKS: ${{ inputs.require_checks }} REQUIRE_CHECKS: ${{ inputs.require_checks }}
-1
View File
@@ -42,7 +42,6 @@ jobs:
review_state: ${{ github.event.review.state || '' }} review_state: ${{ github.event.review.state || '' }}
review_commit_id: ${{ github.event.review.commit_id || '' }} review_commit_id: ${{ github.event.review.commit_id || '' }}
bot_name: github-actions[bot] bot_name: github-actions[bot]
lenient_approval_user: lezi-fun
codex_base_url: ${{ vars.CODEX_BASE_URL || 'https://api.openai.com/v1' }} codex_base_url: ${{ vars.CODEX_BASE_URL || 'https://api.openai.com/v1' }}
codex_model: ${{ vars.CODEX_MODEL || 'gpt-5.4' }} codex_model: ${{ vars.CODEX_MODEL || 'gpt-5.4' }}
codex_reasoning_effort: ${{ vars.CODEX_REASONING_EFFORT || 'high' }} codex_reasoning_effort: ${{ vars.CODEX_REASONING_EFFORT || 'high' }}
+10 -4
View File
@@ -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`. - 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. - 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. - 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. - 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. - Posts inline comments only on valid added diff lines.
- Approves clean pull requests. - Approves clean pull requests.
@@ -93,7 +96,6 @@ jobs:
review_state: ${{ github.event.review.state || '' }} review_state: ${{ github.event.review.state || '' }}
review_commit_id: ${{ github.event.review.commit_id || '' }} review_commit_id: ${{ github.event.review.commit_id || '' }}
bot_name: github-actions[bot] bot_name: github-actions[bot]
lenient_approval_user: lezi-fun
codex_base_url: ${{ vars.CODEX_BASE_URL || 'https://api.openai.com/v1' }} codex_base_url: ${{ vars.CODEX_BASE_URL || 'https://api.openai.com/v1' }}
codex_model: ${{ vars.CODEX_MODEL || 'gpt-5.4' }} codex_model: ${{ vars.CODEX_MODEL || 'gpt-5.4' }}
codex_reasoning_effort: ${{ vars.CODEX_REASONING_EFFORT || 'high' }} 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 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 ## Local development
@@ -167,7 +174,6 @@ Important variables:
- `CODEX_MODEL`: defaults to `gpt-5.4` - `CODEX_MODEL`: defaults to `gpt-5.4`
- `CODEX_REASONING_EFFORT`: optional, one of `minimal`, `low`, `medium`, `high`, `xhigh` - `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]` - `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` - `AUTO_MERGE`: defaults to `false`
- `MERGE_METHOD`: defaults to `squash` - `MERGE_METHOD`: defaults to `squash`
- `REQUIRE_CHECKS`: defaults to `true` - `REQUIRE_CHECKS`: defaults to `true`
-2
View File
@@ -36,7 +36,6 @@ const configSchema = z.object({
codexBaseUrl: optionalString, codexBaseUrl: optionalString,
codexApiKey: z.string().min(1), codexApiKey: z.string().min(1),
botName: z.string().min(1).default("ghbot"), botName: z.string().min(1).default("ghbot"),
lenientApprovalUser: z.string().min(1).default("lezi-fun"),
autoMerge: envBoolean.default(false), autoMerge: envBoolean.default(false),
mergeMethod: z.enum(["merge", "squash", "rebase"]).default("squash"), mergeMethod: z.enum(["merge", "squash", "rebase"]).default("squash"),
requireChecks: envBoolean.default(true), requireChecks: envBoolean.default(true),
@@ -52,7 +51,6 @@ export const config = configSchema.parse({
codexBaseUrl: process.env.CODEX_BASE_URL, codexBaseUrl: process.env.CODEX_BASE_URL,
codexApiKey: process.env.CODEX_API_KEY, codexApiKey: process.env.CODEX_API_KEY,
botName: process.env.BOT_NAME, botName: process.env.BOT_NAME,
lenientApprovalUser: process.env.LENIENT_APPROVAL_USER,
autoMerge: process.env.AUTO_MERGE, autoMerge: process.env.AUTO_MERGE,
mergeMethod: process.env.MERGE_METHOD, mergeMethod: process.env.MERGE_METHOD,
requireChecks: process.env.REQUIRE_CHECKS, requireChecks: process.env.REQUIRE_CHECKS,
+86 -14
View File
@@ -98,7 +98,7 @@ export async function processPullRequestReviewApproval(
commitId: string; commitId: string;
} }
): Promise<void> { ): Promise<void> {
if (params.state !== "approved" || params.reviewerLogin !== config.lenientApprovalUser) { if (params.state !== "approved") {
return; return;
} }
@@ -122,6 +122,37 @@ export async function processPullRequestReviewApproval(
return; 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, { await maybeMergePullRequest(octokit, {
owner: params.owner, owner: params.owner,
repo: params.repo, repo: params.repo,
@@ -228,24 +259,34 @@ async function maybeMergePullRequest(
} }
if (params.mode === "lenient") { 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, owner,
repo, repo,
pullNumber, pullNumber,
headSha: params.headSha, headSha: params.headSha,
reviewerLogin: config.lenientApprovalUser requireAdmin: adminRecentlyResponded
}); });
if (!approvedByOwner) { if (!approvedByEligibleReviewer) {
await withRetry("github.issues.createComment.awaitLenientApproval", async () => { await withRetry("github.issues.createComment.awaitLenientApproval", async () => {
return octokit.rest.issues.createComment({ return octokit.rest.issues.createComment({
owner, owner,
repo, repo,
issue_number: pullNumber, 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; return;
} }
} }
@@ -300,7 +341,7 @@ async function hasCurrentHeadApprovalFrom(
repo: string; repo: string;
pullNumber: number; pullNumber: number;
headSha: string; headSha: string;
reviewerLogin: string; requireAdmin: boolean;
} }
): Promise<boolean> { ): Promise<boolean> {
const reviews = await octokit.paginate(octokit.rest.pulls.listReviews, { const reviews = await octokit.paginate(octokit.rest.pulls.listReviews, {
@@ -310,13 +351,44 @@ async function hasCurrentHeadApprovalFrom(
per_page: 100 per_page: 100
}); });
return reviews.some((review) => { const approvedLogins = reviews
return ( .filter((review) => review.state === "APPROVED" && review.commit_id === params.headSha)
review.user?.login === params.reviewerLogin && .map((review) => review.user?.login)
review.state === "APPROVED" && .filter((login): login is string => Boolean(login));
review.commit_id === params.headSha
); 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( async function listPullRequestFiles(