From ec601bef93922ffa69949f634227dc68f2273390 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Sat, 22 Aug 2026 11:36:39 +0800 Subject: [PATCH] feat: append a collapsible runtime environment to bot comments Comments in which ghbot starts or completes an operation now end with a collapsed Runtime environment block (trigger, runner, goose model and version, review policy, automation flags, cache state) followed by a Created By GHBot signature linking to this repository. The template is shared by action-mode progress, recheck, completion, and PR chat comments; the workflow exports the pinned GOOSE_VERSION so the displayed version is truthful. --- .github/workflows/review-reusable.yml | 2 + src/actions/runReview.ts | 7 +- src/chat/processor.ts | 3 +- src/github/runtimeInfo.ts | 106 ++++++++++++++++++++++++++ src/review/processor.ts | 11 ++- test/runtime-info.test.ts | 76 ++++++++++++++++++ 6 files changed, 199 insertions(+), 6 deletions(-) create mode 100644 src/github/runtimeInfo.ts create mode 100644 test/runtime-info.test.ts diff --git a/.github/workflows/review-reusable.yml b/.github/workflows/review-reusable.yml index e66ce05..dec4c3c 100644 --- a/.github/workflows/review-reusable.yml +++ b/.github/workflows/review-reusable.yml @@ -241,6 +241,7 @@ jobs: GOOSE_BASE_URL: ${{ inputs.goose_base_url || inputs.opencode_base_url }} GOOSE_MODEL: ${{ inputs.goose_model || inputs.opencode_model }} GOOSE_THINKING_EFFORT: ${{ inputs.goose_thinking_effort || inputs.opencode_reasoning_effort }} + GOOSE_VERSION: v1.46.0 REVIEW_POLICY: ${{ inputs.review_policy }} REVIEW_STRICTNESS: ${{ inputs.review_strictness }} REVIEW_INSTRUCTIONS: ${{ inputs.review_instructions }} @@ -316,6 +317,7 @@ jobs: GOOSE_BASE_URL: ${{ inputs.goose_base_url || inputs.opencode_base_url }} GOOSE_MODEL: ${{ inputs.goose_model || inputs.opencode_model }} GOOSE_THINKING_EFFORT: ${{ inputs.goose_thinking_effort || inputs.opencode_reasoning_effort }} + GOOSE_VERSION: v1.46.0 REVIEW_POLICY: ${{ inputs.review_policy }} REVIEW_STRICTNESS: ${{ inputs.review_strictness }} REVIEW_INSTRUCTIONS: ${{ inputs.review_instructions }} diff --git a/src/actions/runReview.ts b/src/actions/runReview.ts index 4dc83e3..fa1a335 100644 --- a/src/actions/runReview.ts +++ b/src/actions/runReview.ts @@ -1,6 +1,7 @@ import fs from "node:fs"; import { config } from "../config.js"; import { createGitHubCredentials } from "../github/client.js"; +import { formatBotSignature, formatRuntimeEnvironmentDetails } from "../github/runtimeInfo.js"; import { logger } from "../logger.js"; import { withRetry } from "../retry.js"; import { processPullRequestChat } from "../chat/processor.js"; @@ -161,7 +162,11 @@ async function main(): Promise { owner: prPayload.repository.owner.login, repo: prPayload.repository.name, issue_number: prPayload.pull_request.number, - body: "Automated review has started. I am checking this pull request now." + body: [ + "Automated review has started. I am checking this pull request now.", + formatRuntimeEnvironmentDetails(), + formatBotSignature() + ].join("\n\n") }); }); } diff --git a/src/chat/processor.ts b/src/chat/processor.ts index f6654b6..38b8abc 100644 --- a/src/chat/processor.ts +++ b/src/chat/processor.ts @@ -4,6 +4,7 @@ import type { Octokit } from "@octokit/rest"; import { runGooseAgent } from "../ai/gooseCli.js"; import { config } from "../config.js"; import { postPermissionDeniedComment } from "../github/commandFeedback.js"; +import { formatBotSignature, formatRuntimeEnvironmentDetails } from "../github/runtimeInfo.js"; import { logger } from "../logger.js"; import { withRetry } from "../retry.js"; import { compactFilesForReview } from "../review/prompt.js"; @@ -145,7 +146,7 @@ export async function processPullRequestChat( owner: params.owner, repo: params.repo, issue_number: params.pullNumber, - body: `${marker}\n${reply}` + body: `${marker}\n${reply}\n\n${formatRuntimeEnvironmentDetails()}\n\n${formatBotSignature()}` }); }); } diff --git a/src/github/runtimeInfo.ts b/src/github/runtimeInfo.ts new file mode 100644 index 0000000..8e434c2 --- /dev/null +++ b/src/github/runtimeInfo.ts @@ -0,0 +1,106 @@ +import { config } from "../config.js"; + +export type RuntimeMode = "actions" | "webhook" | "local"; + +/** + * Where is this ghbot process running? Action-mode workflow runs set + * GITHUB_ACTIONS; the optional webhook service opts in via WEBHOOK_ENABLED. + * Anything else is a local simulation. + */ +export function detectRuntimeMode(): RuntimeMode { + if (process.env.GITHUB_ACTIONS === "true") { + return "actions"; + } + return config.webhookEnabled ? "webhook" : "local"; +} + +function gooseVersion(): string | undefined { + const version = process.env.GOOSE_VERSION?.trim(); + return version ? version : undefined; +} + +function hostLine(): string { + const os = process.env.RUNNER_OS ?? process.platform; + return `${os} / ${process.arch} · Node.js ${process.version}`; +} + +function eventLine(): string | undefined { + const eventName = process.env.GHBOT_EVENT_NAME ?? process.env.GITHUB_EVENT_NAME; + const eventAction = process.env.GHBOT_EVENT_ACTION; + if (!eventName) { + return undefined; + } + return eventAction ? `\`${eventName}\` / ${eventAction}` : `\`${eventName}\``; +} + +function r2CacheEnabled(): boolean { + return Boolean( + config.r2Endpoint && + config.r2BucketName && + config.r2AccessKeyId && + config.r2SecretAccessKey + ); +} + +/** + * Collapsed
block appended to comments in which the bot announces or + * performs an operation. The visual template is identical for every runtime + * mode (Actions, webhook service, local); only the fact lines differ. It must + * never contain credentials: only non-secret configuration and host facts. + */ +export function formatRuntimeEnvironmentDetails(mode: RuntimeMode = detectRuntimeMode()): string { + const lines: string[] = []; + + if (mode === "webhook") { + lines.push( + "- **Mode:** GitHub App webhook service (read-only chat)", + `- **Host:** ${hostLine()}` + ); + } else { + const event = eventLine(); + const modeLabel = mode === "actions" + ? event ? `GitHub Actions (${event})` : "GitHub Actions" + : event ? `local run (${event})` : "local run"; + lines.push( + `- **Mode:** ${modeLabel}`, + `- **Runner:** ${hostLine()}` + ); + } + + const gooseParts = [ + gooseVersion(), + `model \`${config.gooseModel ?? "unknown"}\``, + `thinking effort \`${config.gooseThinkingEffort ?? "high"}\`` + ].filter(Boolean); + lines.push(`- **goose:** ${gooseParts.join(" · ")}`); + + if (mode === "webhook") { + lines.push( + `- **Chat permission policy:** \`${config.webhookChatPermission}\``, + "- **Tools:** none · cannot edit files, run commands, or push commits" + ); + } else { + lines.push( + `- **Review policy:** \`${config.reviewPolicy}\` · strictness \`${config.reviewStrictness}\` · max patch ${config.maxPatchChars} chars`, + `- **Automation:** auto-merge ${config.autoMerge ? "on" : "off"} · conflict repair ${config.autoResolveConflicts ? "on" : "off"}`, + `- **Cache:** R2 ${r2CacheEnabled() ? "enabled" : "disabled"} · repository knowledge ${config.repositoryKnowledgeEnabled ? "enabled" : "disabled"}` + ); + } + + return [ + "
", + "⚙️ Runtime environment", + "", + ...lines, + "", + "
" + ].join("\n"); +} + +/** + * Attribution footer appended after the environment block on the comments in + * which the bot announces or performs an operation. + */ +export function formatBotSignature(): string { + return "🤖 Created By [**GHBot**](https://github.com/lezi-fun/ghbot)"; +} diff --git a/src/review/processor.ts b/src/review/processor.ts index d797598..9a05ede 100644 --- a/src/review/processor.ts +++ b/src/review/processor.ts @@ -2,6 +2,7 @@ import type { Octokit } from "@octokit/rest"; import { config } from "../config.js"; import { requiredChecksAreGreen } from "../github/checks.js"; import { postPermissionDeniedComment } from "../github/commandFeedback.js"; +import { formatBotSignature, formatRuntimeEnvironmentDetails } from "../github/runtimeInfo.js"; import { formatBotDisplayName } from "../github/botIdentity.js"; import { collectValidNewLines, toDiffPosition } from "../github/diff.js"; import { logger } from "../logger.js"; @@ -351,8 +352,10 @@ export async function beginCommitReviewProgress( const marker = reviewProgressMarker(pullRequest.head.sha); const body = [ marker, - `New commit \`${shortSha(pullRequest.head.sha)}\` detected. Automated review has started for this commit.` - ].join("\n"); + `New commit \`${shortSha(pullRequest.head.sha)}\` detected. Automated review has started for this commit.`, + formatRuntimeEnvironmentDetails(), + formatBotSignature() + ].join("\n\n"); const existing = await findIssueCommentByMarker(octokit, params, marker); const response = existing ? await withRetry("github.issues.updateComment.reviewProgressStarted", async () => { @@ -410,7 +413,7 @@ export async function finishCommitReviewProgress( owner: params.owner, repo: params.repo, comment_id: params.commentId, - body: [marker, message].join("\n") + body: [marker, message, formatRuntimeEnvironmentDetails(), formatBotSignature()].join("\n\n") }); }); } @@ -608,7 +611,7 @@ export async function processRecheckComment( owner: params.owner, repo: params.repo, issue_number: params.pullNumber, - body: `Recheck requested by @${params.commenterLogin}. Re-running the review with the repository's current strictness settings.` + body: `Recheck requested by @${params.commenterLogin}. Re-running the review with the repository's current strictness settings.\n\n${formatRuntimeEnvironmentDetails()}\n\n${formatBotSignature()}` }); }); diff --git a/test/runtime-info.test.ts b/test/runtime-info.test.ts new file mode 100644 index 0000000..1ad179c --- /dev/null +++ b/test/runtime-info.test.ts @@ -0,0 +1,76 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { + detectRuntimeMode, + formatBotSignature, + formatRuntimeEnvironmentDetails +} from "../src/github/runtimeInfo.js"; + +test("environment block renders as a collapsed GitHub details section", () => { + const body = formatRuntimeEnvironmentDetails("actions"); + assert.match(body, /^
\n⚙️ Runtime environment<\/summary>\n\n/); + assert.ok(body.endsWith("\n\n
")); +}); + +test("actions mode reports trigger, runner, goose, policy, automation, cache", () => { + const previousEvent = process.env.GITHUB_EVENT_NAME; + process.env.GITHUB_EVENT_NAME = "pull_request_target"; + try { + const body = formatRuntimeEnvironmentDetails("actions"); + assert.match(body, /- \*\*Mode:\*\* GitHub Actions \(\`pull_request_target\`\)/); + assert.match(body, /- \*\*Runner:\*\* .+ · Node\.js v/); + assert.match(body, /- \*\*goose:\*\* model \`[^\`]+\` · thinking effort \`[^\`]+\`/); + assert.match(body, /- \*\*Review policy:\*\* \`(allow|require_approval|reject)\` · strictness \`(normal|strict)\`/); + assert.match(body, /- \*\*Automation:\*\* auto-merge (on|off) · conflict repair (on|off)/); + assert.match(body, /- \*\*Cache:\*\* R2 (enabled|disabled) · repository knowledge (enabled|disabled)/); + } finally { + if (previousEvent === undefined) { + delete process.env.GITHUB_EVENT_NAME; + } else { + process.env.GITHUB_EVENT_NAME = previousEvent; + } + } +}); + +test("webhook mode keeps the identical template but swaps in service facts", () => { + const actionsBody = formatRuntimeEnvironmentDetails("actions"); + const webhookBody = formatRuntimeEnvironmentDetails("webhook"); + + assert.match(webhookBody, /^
\n⚙️ Runtime environment<\/summary>/); + assert.match(webhookBody, /- \*\*Mode:\*\* GitHub App webhook service \(read-only chat\)/); + assert.match(webhookBody, /- \*\*Host:\*\** .+Node\.js v/); + assert.match(webhookBody, /- \*\*Chat permission policy:\*\* \`(anyone|read|write)\`/); + assert.match(webhookBody, /- \*\*Tools:\*\* none · cannot edit files, run commands, or push commits/); + assert.doesNotMatch(webhookBody, /GitHub Actions|Review policy|Runner:/); + + const summaryOf = (body: string) => body.split("\n").slice(0, 2).join("\n"); + assert.equal(summaryOf(actionsBody), summaryOf(webhookBody)); +}); + +test("environment block never contains credentials", () => { + for (const mode of ["actions", "webhook", "local"] as const) { + const body = formatRuntimeEnvironmentDetails(mode); + assert.doesNotMatch(body, /API_KEY|SECRET_ACCESS_KEY|PRIVATE_KEY|ghp_|sk-/i); + } +}); + +test("bot signature is a single attribution footer line", () => { + assert.equal( + formatBotSignature(), + "🤖 Created By [**GHBot**](https://github.com/lezi-fun/ghbot)" + ); +}); + +test("detection prefers Actions, then the webhook opt-in flag", () => { + const previousActions = process.env.GITHUB_ACTIONS; + process.env.GITHUB_ACTIONS = "true"; + try { + assert.equal(detectRuntimeMode(), "actions"); + } finally { + if (previousActions === undefined) { + delete process.env.GITHUB_ACTIONS; + } else { + process.env.GITHUB_ACTIONS = previousActions; + } + } +});