From 298baea9987ef6559755624cb67fad00c1248299 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Sun, 17 May 2026 09:56:11 +0800 Subject: [PATCH] Add reusable review workflow --- .github/workflows/review-reusable.yml | 131 ++++++++++++++++++++++++++ .github/workflows/review.yml | 52 +++++----- README.md | 65 +++++++++++++ src/actions/runReview.ts | 94 ++++++++++++++++-- 4 files changed, 306 insertions(+), 36 deletions(-) create mode 100644 .github/workflows/review-reusable.yml diff --git a/.github/workflows/review-reusable.yml b/.github/workflows/review-reusable.yml new file mode 100644 index 0000000..2ee1e76 --- /dev/null +++ b/.github/workflows/review-reusable.yml @@ -0,0 +1,131 @@ +name: Reusable PR Review Bot + +on: + workflow_call: + inputs: + event_name: + required: true + type: string + event_action: + required: true + type: string + repository_owner: + required: true + type: string + repository_name: + required: true + type: string + pull_number: + required: true + type: string + commenter_login: + required: false + type: string + default: "" + comment_body: + required: false + type: string + default: "" + reviewer_login: + required: false + type: string + default: "" + review_state: + required: false + type: string + default: "" + review_commit_id: + required: false + type: string + default: "" + bot_name: + 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 + default: https://api.openai.com/v1 + codex_model: + required: false + type: string + default: gpt-5.4 + codex_reasoning_effort: + required: false + type: string + default: high + auto_merge: + required: false + type: string + default: "false" + merge_method: + required: false + type: string + default: squash + require_checks: + required: false + type: string + default: "true" + max_patch_chars: + required: false + type: string + default: "120000" + log_level: + required: false + type: string + default: info + secrets: + CODEX_API_KEY: + required: true + +permissions: + contents: write + pull-requests: write + checks: write + statuses: read + issues: write + +jobs: + review: + name: bot-review + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 24 + cache: npm + - name: Install Codex CLI + run: npm install -g @openai/codex + - run: npm ci + - run: npm run build + - name: Remove stale review result + run: rm -f .review-result.json + - run: node dist/src/actions/runReview.js + env: + GITHUB_TOKEN: ${{ github.token }} + CODEX_API_KEY: ${{ secrets.CODEX_API_KEY }} + CODEX_BASE_URL: ${{ inputs.codex_base_url }} + 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 }} + MAX_PATCH_CHARS: ${{ inputs.max_patch_chars }} + LOG_LEVEL: ${{ inputs.log_level }} + GHBOT_EVENT_NAME: ${{ inputs.event_name }} + GHBOT_EVENT_ACTION: ${{ inputs.event_action }} + GHBOT_REPOSITORY_OWNER: ${{ inputs.repository_owner }} + GHBOT_REPOSITORY_NAME: ${{ inputs.repository_name }} + GHBOT_PULL_NUMBER: ${{ inputs.pull_number }} + GHBOT_COMMENTER_LOGIN: ${{ inputs.commenter_login }} + GHBOT_COMMENT_BODY: ${{ inputs.comment_body }} + GHBOT_REVIEWER_LOGIN: ${{ inputs.reviewer_login }} + GHBOT_REVIEW_STATE: ${{ inputs.review_state }} + GHBOT_REVIEW_COMMIT_ID: ${{ inputs.review_commit_id }} diff --git a/.github/workflows/review.yml b/.github/workflows/review.yml index b48d86c..bbc9992 100644 --- a/.github/workflows/review.yml +++ b/.github/workflows/review.yml @@ -23,35 +23,31 @@ permissions: jobs: review: - name: bot-review if: | github.event_name == 'pull_request_target' || (github.event_name == 'issue_comment' && github.event.issue.pull_request != null) || github.event_name == 'pull_request_review' - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 - with: - node-version: 24 - cache: npm - - name: Install Codex CLI - run: npm install -g @openai/codex - - run: npm ci - - run: npm run build - - name: Remove stale review result - run: rm -f .review-result.json - - run: node dist/src/actions/runReview.js - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - CODEX_API_KEY: ${{ secrets.CODEX_API_KEY }} - 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' }} - BOT_NAME: github-actions[bot] - LENIENT_APPROVAL_USER: lezi-fun - AUTO_MERGE: ${{ vars.AUTO_MERGE || 'false' }} - MERGE_METHOD: ${{ vars.MERGE_METHOD || 'squash' }} - REQUIRE_CHECKS: ${{ vars.REQUIRE_CHECKS || 'true' }} - MAX_PATCH_CHARS: ${{ vars.MAX_PATCH_CHARS || '120000' }} - LOG_LEVEL: info + uses: ./.github/workflows/review-reusable.yml + secrets: + CODEX_API_KEY: ${{ secrets.CODEX_API_KEY }} + with: + event_name: ${{ github.event_name }} + event_action: ${{ github.event.action }} + repository_owner: ${{ github.repository_owner }} + repository_name: ${{ github.event.repository.name }} + pull_number: ${{ github.event.pull_request.number || github.event.issue.number }} + commenter_login: ${{ github.event.comment.user.login || '' }} + comment_body: ${{ github.event.comment.body || '' }} + reviewer_login: ${{ github.event.review.user.login || '' }} + 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' }} + auto_merge: ${{ vars.AUTO_MERGE || 'false' }} + merge_method: ${{ vars.MERGE_METHOD || 'squash' }} + require_checks: ${{ vars.REQUIRE_CHECKS || 'true' }} + max_patch_chars: ${{ vars.MAX_PATCH_CHARS || '120000' }} + log_level: info diff --git a/README.md b/README.md index 8c89aca..f7af5b9 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,71 @@ Optional repository variables: The workflow uses the repository `GITHUB_TOKEN`, so you do not need a GitHub App, webhook endpoint, Vercel deployment, or a self-hosted listener. It installs Codex CLI inside the GitHub Actions runner and uses that CLI for the review itself. The workflow writes an isolated Codex `config.toml` at runtime with the configured model, provider base URL, and reasoning setting, then asks Codex CLI to persist its final review result into `.review-result.json`. +## Reuse from another repository + +This repository now exposes a reusable workflow at: + +```text +lezi-fun/ghbot/.github/workflows/review-reusable.yml@main +``` + +In another repository, create a thin wrapper workflow such as: + +```yaml +name: PR Review Bot + +on: + pull_request_target: + types: [opened, synchronize, reopened, ready_for_review] + issue_comment: + types: [created] + pull_request_review: + types: [submitted] + +permissions: + contents: write + pull-requests: write + checks: write + statuses: read + issues: write + +jobs: + review: + if: | + github.event_name == 'pull_request_target' || + (github.event_name == 'issue_comment' && github.event.issue.pull_request != null) || + github.event_name == 'pull_request_review' + uses: lezi-fun/ghbot/.github/workflows/review-reusable.yml@main + secrets: + CODEX_API_KEY: ${{ secrets.CODEX_API_KEY }} + with: + event_name: ${{ github.event_name }} + event_action: ${{ github.event.action }} + repository_owner: ${{ github.repository_owner }} + repository_name: ${{ github.event.repository.name }} + pull_number: ${{ github.event.pull_request.number || github.event.issue.number }} + commenter_login: ${{ github.event.comment.user.login || '' }} + comment_body: ${{ github.event.comment.body || '' }} + reviewer_login: ${{ github.event.review.user.login || '' }} + 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' }} + auto_merge: ${{ vars.AUTO_MERGE || 'false' }} + merge_method: ${{ vars.MERGE_METHOD || 'squash' }} + require_checks: ${{ vars.REQUIRE_CHECKS || 'true' }} + max_patch_chars: ${{ vars.MAX_PATCH_CHARS || '120000' }} + log_level: info +``` + +The caller repository still needs to configure: + +- `CODEX_API_KEY` as a secret +- `CODEX_BASE_URL`, `CODEX_MODEL`, `CODEX_REASONING_EFFORT`, `AUTO_MERGE`, `MERGE_METHOD`, `REQUIRE_CHECKS`, and `MAX_PATCH_CHARS` as repository variables as needed + ## Required workflow permissions Repository Actions permissions must allow the workflow token to: diff --git a/src/actions/runReview.ts b/src/actions/runReview.ts index 67ec0d9..b7e41e5 100644 --- a/src/actions/runReview.ts +++ b/src/actions/runReview.ts @@ -59,15 +59,16 @@ type PullRequestReviewPayload = { }; async function main(): Promise { - const eventName = process.env.GITHUB_EVENT_NAME; - const eventPath = process.env.GITHUB_EVENT_PATH; - - if (!eventName || !eventPath) { - throw new Error("GITHUB_EVENT_NAME and GITHUB_EVENT_PATH are required."); - } - - const payload = JSON.parse(fs.readFileSync(eventPath, "utf8")) as PullRequestPayload | IssueCommentPayload | PullRequestReviewPayload; + const workflowCallEventName = process.env.GHBOT_EVENT_NAME; + const payload = workflowCallEventName + ? buildPayloadFromWorkflowCallEnv(workflowCallEventName) + : readPayloadFromGitHubEventPath(); const octokit = createGitHubClient(); + const eventName = workflowCallEventName ?? process.env.GITHUB_EVENT_NAME; + + if (!eventName) { + throw new Error("GITHUB_EVENT_NAME is required."); + } logger.info({ eventName }, "Handling GitHub Actions review event."); @@ -135,6 +136,83 @@ async function main(): Promise { logger.warn({ eventName }, "Unhandled GitHub Actions event."); } +function readPayloadFromGitHubEventPath(): PullRequestPayload | IssueCommentPayload | PullRequestReviewPayload { + const eventPath = process.env.GITHUB_EVENT_PATH; + if (!eventPath) { + throw new Error("GITHUB_EVENT_PATH is required when workflow_call inputs are not provided."); + } + + return JSON.parse(fs.readFileSync(eventPath, "utf8")) as PullRequestPayload | IssueCommentPayload | PullRequestReviewPayload; +} + +function buildPayloadFromWorkflowCallEnv(eventName: string): PullRequestPayload | IssueCommentPayload | PullRequestReviewPayload { + const action = process.env.GHBOT_EVENT_ACTION; + const owner = process.env.GHBOT_REPOSITORY_OWNER; + const repo = process.env.GHBOT_REPOSITORY_NAME; + const pullNumber = Number(process.env.GHBOT_PULL_NUMBER); + + if (!action || !owner || !repo || !Number.isInteger(pullNumber) || pullNumber <= 0) { + throw new Error("Missing required GHBOT_* workflow_call inputs."); + } + + const repository = { + name: repo, + owner: { + login: owner + }, + full_name: `${owner}/${repo}` + }; + + if (eventName === "pull_request_target") { + return { + action, + pull_request: { + number: pullNumber, + draft: false + }, + repository + }; + } + + if (eventName === "issue_comment") { + return { + action, + issue: { + number: pullNumber, + pull_request: { + url: `https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}` + } + }, + comment: { + body: process.env.GHBOT_COMMENT_BODY ?? "", + user: { + login: process.env.GHBOT_COMMENTER_LOGIN ?? "" + } + }, + repository + }; + } + + if (eventName === "pull_request_review") { + return { + action, + review: { + state: process.env.GHBOT_REVIEW_STATE ?? "", + commit_id: process.env.GHBOT_REVIEW_COMMIT_ID ?? "", + user: { + login: process.env.GHBOT_REVIEWER_LOGIN ?? "" + } + }, + pull_request: { + number: pullNumber + }, + repository + }; + } + + throw new Error(`Unsupported GHBOT_EVENT_NAME: ${eventName}`); +} + main().catch((error) => { logger.error({ error, botName: config.botName }, "GitHub Actions review run failed."); process.exitCode = 1;