Add reusable review workflow

This commit is contained in:
lezi-fun committed 2026-05-17 09:56:11 +08:00
1 parent 3b7ca153f9
commit 298baea998
4 files changed
+305 -35

No files matched your search

+131
View File
@@ -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 }}
+23 -27
View File
@@ -23,35 +23,31 @@ permissions:
jobs: jobs:
review: review:
name: bot-review
if: | if: |
github.event_name == 'pull_request_target' || github.event_name == 'pull_request_target' ||
(github.event_name == 'issue_comment' && github.event.issue.pull_request != null) || (github.event_name == 'issue_comment' && github.event.issue.pull_request != null) ||
github.event_name == 'pull_request_review' github.event_name == 'pull_request_review'
runs-on: ubuntu-latest uses: ./.github/workflows/review-reusable.yml
steps: secrets:
- 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_API_KEY: ${{ secrets.CODEX_API_KEY }}
CODEX_BASE_URL: ${{ vars.CODEX_BASE_URL || 'https://api.openai.com/v1' }} with:
CODEX_MODEL: ${{ vars.CODEX_MODEL || 'gpt-5.4' }} event_name: ${{ github.event_name }}
CODEX_REASONING_EFFORT: ${{ vars.CODEX_REASONING_EFFORT || 'high' }} event_action: ${{ github.event.action }}
BOT_NAME: github-actions[bot] repository_owner: ${{ github.repository_owner }}
LENIENT_APPROVAL_USER: lezi-fun repository_name: ${{ github.event.repository.name }}
AUTO_MERGE: ${{ vars.AUTO_MERGE || 'false' }} pull_number: ${{ github.event.pull_request.number || github.event.issue.number }}
MERGE_METHOD: ${{ vars.MERGE_METHOD || 'squash' }} commenter_login: ${{ github.event.comment.user.login || '' }}
REQUIRE_CHECKS: ${{ vars.REQUIRE_CHECKS || 'true' }} comment_body: ${{ github.event.comment.body || '' }}
MAX_PATCH_CHARS: ${{ vars.MAX_PATCH_CHARS || '120000' }} reviewer_login: ${{ github.event.review.user.login || '' }}
LOG_LEVEL: info 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
+65
View File
@@ -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. 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`. 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 ## Required workflow permissions
Repository Actions permissions must allow the workflow token to: Repository Actions permissions must allow the workflow token to:
+86 -8
View File
@@ -59,15 +59,16 @@ type PullRequestReviewPayload = {
}; };
async function main(): Promise<void> { async function main(): Promise<void> {
const eventName = process.env.GITHUB_EVENT_NAME; const workflowCallEventName = process.env.GHBOT_EVENT_NAME;
const eventPath = process.env.GITHUB_EVENT_PATH; const payload = workflowCallEventName
? buildPayloadFromWorkflowCallEnv(workflowCallEventName)
if (!eventName || !eventPath) { : readPayloadFromGitHubEventPath();
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 octokit = createGitHubClient(); 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."); logger.info({ eventName }, "Handling GitHub Actions review event.");
@@ -135,6 +136,83 @@ async function main(): Promise<void> {
logger.warn({ eventName }, "Unhandled GitHub Actions event."); 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) => { main().catch((error) => {
logger.error({ error, botName: config.botName }, "GitHub Actions review run failed."); logger.error({ error, botName: config.botName }, "GitHub Actions review run failed.");
process.exitCode = 1; process.exitCode = 1;