@eduardo191labs/opencode-actions 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +259 -5
- package/action.yml +71 -9
- package/dist/actions.d.ts +337 -65
- package/dist/actions.js +271 -27
- package/dist/app-auth.d.ts +70 -0
- package/dist/app-auth.js +137 -0
- package/dist/cli.js +35 -4
- package/dist/commands.d.ts +3 -1
- package/dist/commands.js +59 -19
- package/dist/compile.js +15 -11
- package/dist/config.d.ts +5 -1
- package/dist/config.js +1 -1
- package/dist/detect.d.ts +37 -4
- package/dist/detect.js +103 -78
- package/dist/format.d.ts +4 -1
- package/dist/format.js +5 -2
- package/dist/git.d.ts +16 -0
- package/dist/git.js +23 -1
- package/dist/github.d.ts +9 -1
- package/dist/implement.d.ts +116 -0
- package/dist/implement.js +192 -0
- package/dist/install.js +195 -20
- package/dist/review-thread.d.ts +63 -0
- package/dist/review-thread.js +70 -0
- package/dist/runner.js +1 -1
- package/dist/signals.d.ts +134 -0
- package/dist/signals.js +117 -0
- package/dist/summary.js +2 -0
- package/package.json +4 -2
- package/scripts/check-marketplace.js +111 -0
- package/scripts/install-opencode.sh +74 -0
package/dist/cli.js
CHANGED
|
@@ -2,6 +2,7 @@ import { readFileSync } from "node:fs";
|
|
|
2
2
|
import { readFile } from "node:fs/promises";
|
|
3
3
|
import { existsSync } from "node:fs";
|
|
4
4
|
import { GitHub, repoFromEnv, tokenFromEnv } from "./github.js";
|
|
5
|
+
import { appClient, appCredentialsFromEnv, findInstallation, installationToken } from "./app-auth.js";
|
|
5
6
|
import { loadConfig, resolveSecretEnv, providerAuthPlan } from "./loader.js";
|
|
6
7
|
import { compileConfig, describeCapabilities } from "./compile.js";
|
|
7
8
|
import { createRenderer } from "./runner.js";
|
|
@@ -10,9 +11,26 @@ import { helpText } from "./commands.js";
|
|
|
10
11
|
async function main() {
|
|
11
12
|
const options = parseArgs(process.argv.slice(2));
|
|
12
13
|
const workspace = options.workspace ?? process.env.GITHUB_WORKSPACE ?? process.cwd();
|
|
13
|
-
const token = tokenFromEnv();
|
|
14
|
-
const github = new GitHub({ token });
|
|
15
14
|
const repo = repoFromEnv();
|
|
15
|
+
// A GitHub App installation token is preferred when the workflow carries the
|
|
16
|
+
// app credentials: it is scoped to the installed repositories and to the
|
|
17
|
+
// permissions the owner granted, so it cannot write anywhere else. The token
|
|
18
|
+
// is also exported as GITHUB_TOKEN because `git push` reads it from there.
|
|
19
|
+
const app = appCredentialsFromEnv();
|
|
20
|
+
let token;
|
|
21
|
+
if (app) {
|
|
22
|
+
const installation = await findInstallation(appClient(app), repo.owner, repo.repo);
|
|
23
|
+
if (!installation) {
|
|
24
|
+
const slug = process.env.OPENCODE_APP_SLUG ?? "opencode-actions-bot-new";
|
|
25
|
+
throw new Error(`the GitHub App is not installed on ${repo.owner}/${repo.repo}; install it at https://github.com/apps/${slug}/installations/new`);
|
|
26
|
+
}
|
|
27
|
+
token = (await installationToken(app, installation.id)).token;
|
|
28
|
+
process.env.GITHUB_TOKEN = token;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
token = tokenFromEnv();
|
|
32
|
+
}
|
|
33
|
+
const github = new GitHub({ token });
|
|
16
34
|
const { config, sources } = await loadConfig({
|
|
17
35
|
workspace,
|
|
18
36
|
github,
|
|
@@ -80,11 +98,22 @@ async function main() {
|
|
|
80
98
|
const result = await dispatch(ctx, options.event, payload);
|
|
81
99
|
process.stdout.write(JSON.stringify(result ?? { ok: true }) + "\n");
|
|
82
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Read the event payload.
|
|
103
|
+
*
|
|
104
|
+
* Two shapes reach this function: GitHub's native payload, and the one the
|
|
105
|
+
* Cloudflare worker forwards, which is `{ event, payload }`. Unwrapping here
|
|
106
|
+
* means the workflow can pass the file as-is and no step needs a JSON parser of
|
|
107
|
+
* its own: one parser, one schema, one place to fix a bug.
|
|
108
|
+
*/
|
|
83
109
|
async function readPayload(path) {
|
|
84
110
|
const file = path ?? process.env.GITHUB_EVENT_PATH;
|
|
85
111
|
if (!file || !existsSync(file))
|
|
86
112
|
return null;
|
|
87
|
-
|
|
113
|
+
const data = JSON.parse(await readFile(file, "utf8"));
|
|
114
|
+
if (data && typeof data === "object" && "payload" in data && "event" in data)
|
|
115
|
+
return data.payload;
|
|
116
|
+
return data;
|
|
88
117
|
}
|
|
89
118
|
function parseArgs(argv) {
|
|
90
119
|
const options = {
|
|
@@ -154,7 +183,7 @@ function usage() {
|
|
|
154
183
|
Usage:
|
|
155
184
|
opencode-actions --event <event> [flags]
|
|
156
185
|
|
|
157
|
-
|
|
186
|
+
pull_request, pull_request_review_comment, issue_comment, issues, workflow_run, push, workflow_dispatch
|
|
158
187
|
|
|
159
188
|
Flags:
|
|
160
189
|
--payload <file> Event payload (default: $GITHUB_EVENT_PATH)
|
|
@@ -167,6 +196,8 @@ Flags:
|
|
|
167
196
|
|
|
168
197
|
Environment:
|
|
169
198
|
GITHUB_TOKEN Required
|
|
199
|
+
OPENCODE_APP_ID GitHub App id (preferred over GITHUB_TOKEN)
|
|
200
|
+
OPENCODE_APP_PRIVATE_KEY GitHub App private key (PEM)
|
|
170
201
|
GITHUB_REPOSITORY Required, "owner/repo"
|
|
171
202
|
GITHUB_EVENT_PATH Event payload path
|
|
172
203
|
OPENCODE_BOT_MODEL Override the model
|
package/dist/commands.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { BotConfig, CommandInvocation } from "./config.js";
|
|
2
|
-
export declare const KNOWN_COMMANDS: readonly ["review", "fix", "explain", "test", "docs", "describe", "triage", "answer", "merge", "close", "reopen", "label", "assign", "implement", "help", "stop"];
|
|
2
|
+
export declare const KNOWN_COMMANDS: readonly ["review", "fix", "explain", "test", "docs", "describe", "triage", "answer", "merge", "close", "reopen", "label", "assign", "implement", "continue", "rebase", "sync", "help", "stop"];
|
|
3
3
|
export type KnownCommand = (typeof KNOWN_COMMANDS)[number];
|
|
4
4
|
export declare function parseCommand(body: string, config: BotConfig): CommandInvocation | null;
|
|
5
|
+
/** Every way the bot answers to, longest first so `@opencode-actions-bot-new` wins over `@opencode`. */
|
|
6
|
+
export declare function botHandles(config: BotConfig): string[];
|
|
5
7
|
export declare function parseInvocation(body: string, config: BotConfig): CommandInvocation | null;
|
|
6
8
|
export interface ActorContext {
|
|
7
9
|
login: string;
|
package/dist/commands.js
CHANGED
|
@@ -14,6 +14,9 @@ export const KNOWN_COMMANDS = [
|
|
|
14
14
|
"label",
|
|
15
15
|
"assign",
|
|
16
16
|
"implement",
|
|
17
|
+
"continue",
|
|
18
|
+
"rebase",
|
|
19
|
+
"sync",
|
|
17
20
|
"help",
|
|
18
21
|
"stop",
|
|
19
22
|
];
|
|
@@ -26,7 +29,10 @@ const COMMAND_HELP = {
|
|
|
26
29
|
describe: "Rewrite the PR description from the actual diff.",
|
|
27
30
|
triage: "Classify an issue: labels, severity and a plan.",
|
|
28
31
|
answer: "Answer an issue question with repository context.",
|
|
29
|
-
merge: "Merge the pull request (respects the merge permission).",
|
|
32
|
+
merge: "Merge the pull request, resolving conflicts first if needed (respects the merge permission).",
|
|
33
|
+
continue: "Another pass on the working branch: fix what the last run left, amend the PR.",
|
|
34
|
+
rebase: "Bring the branch up to date with its base and settle any conflict.",
|
|
35
|
+
sync: "Rewrite the PR body and title from the current diff.",
|
|
30
36
|
close: "Close an issue or pull request.",
|
|
31
37
|
reopen: "Reopen an issue or pull request.",
|
|
32
38
|
label: "Add or remove labels: `label +bug -docs`.",
|
|
@@ -42,31 +48,65 @@ function stripFences(body) {
|
|
|
42
48
|
export function parseCommand(body, config) {
|
|
43
49
|
return parseInvocation(body, config);
|
|
44
50
|
}
|
|
51
|
+
/** Every way the bot answers to, longest first so `@opencode-actions-bot-new` wins over `@opencode`. */
|
|
52
|
+
export function botHandles(config) {
|
|
53
|
+
const layer = botLayer(config);
|
|
54
|
+
const handles = new Set();
|
|
55
|
+
handles.add(`@${(layer.name ?? "opencode-actions-bot-new").toLowerCase()}`);
|
|
56
|
+
for (const alias of layer.aliases ?? [])
|
|
57
|
+
handles.add(alias.startsWith("@") ? alias.toLowerCase() : `@${alias.toLowerCase()}`);
|
|
58
|
+
return [...handles].sort((a, b) => b.length - a.length);
|
|
59
|
+
}
|
|
45
60
|
export function parseInvocation(body, config) {
|
|
46
61
|
if (!body)
|
|
47
62
|
return null;
|
|
48
|
-
const
|
|
63
|
+
const layer = botLayer(config);
|
|
64
|
+
const prefix = (layer.commands?.prefix ?? "/bot").toLowerCase();
|
|
49
65
|
const cleaned = stripFences(body);
|
|
50
|
-
const enabled = new Set(
|
|
66
|
+
const enabled = new Set(layer.commands?.enabled ?? KNOWN_COMMANDS);
|
|
67
|
+
const handles = botHandles(config);
|
|
68
|
+
const invocation = (command, args, raw) => enabled.has(command) ? { command, args, raw, actor: "", source: "issue_comment" } : null;
|
|
51
69
|
for (const line of cleaned.split(/\r?\n/)) {
|
|
52
70
|
const trimmed = line.trim();
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
71
|
+
const lower = trimmed.toLowerCase();
|
|
72
|
+
// `/bot review` and `/oc review`.
|
|
73
|
+
if (lower.startsWith(prefix)) {
|
|
74
|
+
const [head = "", ...args] = trimmed.slice(prefix.length).trim().split(/\s+/);
|
|
75
|
+
if (!head)
|
|
76
|
+
continue;
|
|
77
|
+
const found = invocation(head.replace(/^@/, "").toLowerCase(), args, trimmed);
|
|
78
|
+
if (found)
|
|
79
|
+
return found;
|
|
57
80
|
continue;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
81
|
+
}
|
|
82
|
+
// `@opencode-actions-bot-new review`. A mention with no command is still a
|
|
83
|
+
// mention, so it falls back to `answer`: "how does this work?" is the most
|
|
84
|
+
// common thing anyone types at a bot, and it needs no keyword.
|
|
85
|
+
for (const handle of handles) {
|
|
86
|
+
const at = lower.indexOf(handle);
|
|
87
|
+
if (at === -1)
|
|
88
|
+
continue;
|
|
89
|
+
// Not `foo@bar.com` or `user@opencode-actions-bot`: the mention must be a
|
|
90
|
+
// token of its own, or every email address would trigger a run.
|
|
91
|
+
const before = lower.slice(Math.max(0, at - 1), at);
|
|
92
|
+
const after = lower[at + handle.length] ?? " ";
|
|
93
|
+
if (before && !/[\s([{>|,;:!?"'`*_~-]/.test(before))
|
|
94
|
+
continue;
|
|
95
|
+
if (!/[\s,;:!?.()\[\]{}]/.test(after))
|
|
96
|
+
continue;
|
|
97
|
+
const rest = trimmed.slice(at + handle.length).trim().replace(/^[,;:!?.]+/, "").trim();
|
|
98
|
+
const [head = "", ...args] = rest.split(/\s+/);
|
|
99
|
+
const command = head.replace(/^@/, "").toLowerCase();
|
|
100
|
+
if (command) {
|
|
101
|
+
const found = invocation(command, args, trimmed);
|
|
102
|
+
if (found)
|
|
103
|
+
return found;
|
|
104
|
+
}
|
|
105
|
+
// A bare mention, or a mention of something that is not a command.
|
|
106
|
+
const answer = invocation("answer", rest ? [rest] : [], trimmed);
|
|
107
|
+
if (answer)
|
|
108
|
+
return answer;
|
|
109
|
+
}
|
|
70
110
|
}
|
|
71
111
|
return null;
|
|
72
112
|
}
|
package/dist/compile.js
CHANGED
|
@@ -25,17 +25,23 @@ export function compileConfig(config, options = { mode: "review" }) {
|
|
|
25
25
|
warnings.push("No model configured; OpenCode will use its default.");
|
|
26
26
|
// ------------------------------------------------------- permissions rules
|
|
27
27
|
const rules = buildPermissionRules(config, options.mode);
|
|
28
|
-
//
|
|
29
|
-
const
|
|
30
|
-
oc.permissions = [...
|
|
28
|
+
// The ruleset is the V2 shape. User rules come first so their denies win.
|
|
29
|
+
const existingRules = Array.isArray(oc.permissions) ? oc.permissions : [];
|
|
30
|
+
oc.permissions = [...existingRules, ...rules];
|
|
31
|
+
// A `permission` key belongs to V1 and V2 rejects it, so drop a leftover.
|
|
32
|
+
if ("permission" in oc) {
|
|
33
|
+
delete oc.permission;
|
|
34
|
+
warnings.push("removed the V1 `permission` key; V2 uses the `permissions` ruleset.");
|
|
35
|
+
}
|
|
31
36
|
// ---------------------------------------------------------------- the agent
|
|
32
37
|
const agents = { ...(oc.agents ?? {}) };
|
|
33
38
|
const agentName = "opencode-bot";
|
|
34
39
|
const existingAgent = agents[agentName] ?? {};
|
|
40
|
+
const { permissions: _ignored, ...agentRest } = existingAgent;
|
|
35
41
|
agents[agentName] = {
|
|
36
42
|
description: "Autonomous GitHub Actions agent for opencode-actions.",
|
|
37
43
|
mode: "primary",
|
|
38
|
-
...
|
|
44
|
+
...agentRest,
|
|
39
45
|
permissions: [
|
|
40
46
|
...(Array.isArray(existingAgent.permissions) ? existingAgent.permissions : []),
|
|
41
47
|
...rules,
|
|
@@ -57,13 +63,15 @@ export function compileConfig(config, options = { mode: "review" }) {
|
|
|
57
63
|
oc.websearch = false;
|
|
58
64
|
// A CI run has nobody to answer a permission prompt; anything left as "ask"
|
|
59
65
|
// is a bug in the configuration, so make it explicit instead of hanging.
|
|
60
|
-
const asks =
|
|
66
|
+
const asks = rules.filter((rule) => rule.effect === "ask");
|
|
61
67
|
for (const rule of asks) {
|
|
62
|
-
|
|
63
|
-
warnings.push(`permission "${rule.action}" is "ask"; the run will reject it in CI.`);
|
|
68
|
+
warnings.push(`permission "${rule.action}" is "ask"; the run will reject it in CI.`);
|
|
64
69
|
}
|
|
65
70
|
return { content: oc, warnings, model };
|
|
66
71
|
}
|
|
72
|
+
function isPlainObject(value) {
|
|
73
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
74
|
+
}
|
|
67
75
|
function pickTaskModel(config, mode) {
|
|
68
76
|
const tasks = config.bot?.model?.tasks;
|
|
69
77
|
if (!tasks)
|
|
@@ -89,10 +97,6 @@ function isMcpShape(value) {
|
|
|
89
97
|
const record = value;
|
|
90
98
|
return "servers" in record || "timeout" in record;
|
|
91
99
|
}
|
|
92
|
-
function isInteractiveCapable(_rule) {
|
|
93
|
-
// In CI nothing can be asked; a run that hits "ask" is rejected by the runner.
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
100
|
/**
|
|
97
101
|
* Map GitHub capabilities onto OpenCode tool rules.
|
|
98
102
|
*
|
package/dist/config.d.ts
CHANGED
|
@@ -165,12 +165,16 @@ export interface DescribeConfig {
|
|
|
165
165
|
}
|
|
166
166
|
/** GitHub-side behaviour. Never forwarded to OpenCode. */
|
|
167
167
|
export interface BotLayer {
|
|
168
|
-
/** Display name used in comments and commit trailers
|
|
168
|
+
/** Display name used in comments and commit trailers, and the `@mention`. */
|
|
169
169
|
name?: string;
|
|
170
|
+
/** Extra `@handles` the bot answers to, e.g. the app slug and a short form. */
|
|
171
|
+
aliases?: string[];
|
|
170
172
|
/** Emoji used in the comment header. */
|
|
171
173
|
emoji?: string;
|
|
172
174
|
/** Base branch for autofix PRs and issue implementations. */
|
|
173
175
|
base_branch?: string;
|
|
176
|
+
/** Prefix for the branch that carries an issue implementation. */
|
|
177
|
+
work_branch_prefix?: string;
|
|
174
178
|
/** Master switch for any write operation. */
|
|
175
179
|
write?: boolean;
|
|
176
180
|
/** Branches that count as protected for `merge_protected`. */
|
package/dist/config.js
CHANGED
|
@@ -126,7 +126,7 @@ export function bot(config) {
|
|
|
126
126
|
return config.bot ?? {};
|
|
127
127
|
}
|
|
128
128
|
export function feature(config, key) {
|
|
129
|
-
return config.bot?.features?.[key]
|
|
129
|
+
return config.bot?.features?.[key] === true;
|
|
130
130
|
}
|
|
131
131
|
export function permission(config, key) {
|
|
132
132
|
return config.bot?.permissions?.[key] ?? "deny";
|
package/dist/detect.d.ts
CHANGED
|
@@ -36,9 +36,43 @@ export declare function fetchRepoInfo(github: GitHub, ref: RepoRef): Promise<Rep
|
|
|
36
36
|
/** Detect the stack and the workflows already in a repository. */
|
|
37
37
|
export declare function detect(github: GitHub, ref: RepoRef): Promise<Detection>;
|
|
38
38
|
/**
|
|
39
|
-
* The
|
|
40
|
-
*
|
|
41
|
-
*
|
|
39
|
+
* The whole point of the app is that the bot acts as itself, not as the
|
|
40
|
+
* repository's `GITHUB_TOKEN`. A `permissions:` block also *caps*
|
|
41
|
+
* `GITHUB_TOKEN`, which is a separate credential, so both exist side by side:
|
|
42
|
+
* the app token does the writing, and `GITHUB_TOKEN` keeps a narrow read scope
|
|
43
|
+
* for anything else a step might touch.
|
|
44
|
+
*
|
|
45
|
+
* The action mints the installation token on every run, so what travels in the
|
|
46
|
+
* repository secrets is the app id and private key, never a token that would
|
|
47
|
+
* expire within the hour.
|
|
48
|
+
*/
|
|
49
|
+
/**
|
|
50
|
+
* The `permissions:` block written into every workflow.
|
|
51
|
+
*
|
|
52
|
+
* This caps `GITHUB_TOKEN`, a credential separate from the app's installation
|
|
53
|
+
* token. The app token does the writing; this block exists so that anything a
|
|
54
|
+
* step does with the default token — checkout, a CLI that reads Actions, a
|
|
55
|
+
* fallback path — has what it needs instead of failing with a 403 that looks
|
|
56
|
+
* like a bot bug.
|
|
57
|
+
*
|
|
58
|
+
* `contents` varies: a review must never be able to push, so its workflow is
|
|
59
|
+
* read-only on purpose. That is defence in depth behind the bot's own guard.
|
|
60
|
+
*/
|
|
61
|
+
export declare function workflowPermissions(options: {
|
|
62
|
+
contents: "read" | "write";
|
|
63
|
+
}): string;
|
|
64
|
+
export declare const APP_CREDENTIAL_WITH: string;
|
|
65
|
+
/**
|
|
66
|
+
* The one workflow the installer writes into a repository.
|
|
67
|
+
*
|
|
68
|
+
* A single file, because every event shares the same runner, the same checkout
|
|
69
|
+
* and the same action: five files meant five copies of that boilerplate, five
|
|
70
|
+
* places to update, and five chances to disagree. GitHub bills per job, not per
|
|
71
|
+
* file, so one workflow with one job and one event matrix spends exactly the
|
|
72
|
+
* same minutes and is far easier to reason about.
|
|
73
|
+
*
|
|
74
|
+
* Every trigger is on the default branch or on a pull request that targets it.
|
|
75
|
+
* Work on a side branch costs nothing until it becomes a pull request.
|
|
42
76
|
*/
|
|
43
77
|
export interface WorkflowFile {
|
|
44
78
|
path: string;
|
|
@@ -52,4 +86,3 @@ export declare function workflowTemplates(options: {
|
|
|
52
86
|
actionRef?: string;
|
|
53
87
|
stack?: Stack;
|
|
54
88
|
}): WorkflowFile[];
|
|
55
|
-
export declare function repoFromEnvOr(o?: RepoRef): RepoRef;
|
package/dist/detect.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { repoFromEnv } from "./github.js";
|
|
2
1
|
/**
|
|
3
2
|
* Lexically sniff the repository root. This is what makes the bot work in any
|
|
4
3
|
* repository without being told the language: the prompt and the default
|
|
@@ -101,115 +100,141 @@ export async function detect(github, ref) {
|
|
|
101
100
|
const installed = workflows.some((file) => file.name === "opencode-review.yml" || file.name === "opencode.yml");
|
|
102
101
|
return { repo: info, stack, workflows, installed };
|
|
103
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* The whole point of the app is that the bot acts as itself, not as the
|
|
105
|
+
* repository's `GITHUB_TOKEN`. A `permissions:` block also *caps*
|
|
106
|
+
* `GITHUB_TOKEN`, which is a separate credential, so both exist side by side:
|
|
107
|
+
* the app token does the writing, and `GITHUB_TOKEN` keeps a narrow read scope
|
|
108
|
+
* for anything else a step might touch.
|
|
109
|
+
*
|
|
110
|
+
* The action mints the installation token on every run, so what travels in the
|
|
111
|
+
* repository secrets is the app id and private key, never a token that would
|
|
112
|
+
* expire within the hour.
|
|
113
|
+
*/
|
|
114
|
+
/**
|
|
115
|
+
* The `permissions:` block written into every workflow.
|
|
116
|
+
*
|
|
117
|
+
* This caps `GITHUB_TOKEN`, a credential separate from the app's installation
|
|
118
|
+
* token. The app token does the writing; this block exists so that anything a
|
|
119
|
+
* step does with the default token — checkout, a CLI that reads Actions, a
|
|
120
|
+
* fallback path — has what it needs instead of failing with a 403 that looks
|
|
121
|
+
* like a bot bug.
|
|
122
|
+
*
|
|
123
|
+
* `contents` varies: a review must never be able to push, so its workflow is
|
|
124
|
+
* read-only on purpose. That is defence in depth behind the bot's own guard.
|
|
125
|
+
*/
|
|
126
|
+
export function workflowPermissions(options) {
|
|
127
|
+
return [
|
|
128
|
+
` contents: ${options.contents}`,
|
|
129
|
+
// Every scope the bot has ever needed, plus the ones GitHub requires for the
|
|
130
|
+
// related operations. A missing scope is not a graceful failure: the run
|
|
131
|
+
// dies with a 403 that looks like a bug in the bot, and the cause is a line
|
|
132
|
+
// in a generated workflow nobody thinks to check.
|
|
133
|
+
" pull-requests: write",
|
|
134
|
+
" issues: write",
|
|
135
|
+
" checks: write",
|
|
136
|
+
" statuses: write",
|
|
137
|
+
" deployments: write",
|
|
138
|
+
" discussions: write",
|
|
139
|
+
" packages: write",
|
|
140
|
+
" actions: write",
|
|
141
|
+
" attestations: write",
|
|
142
|
+
" id-token: write",
|
|
143
|
+
" pages: write",
|
|
144
|
+
" security-events: write",
|
|
145
|
+
" models: read",
|
|
146
|
+
].join("\n");
|
|
147
|
+
}
|
|
148
|
+
export const APP_CREDENTIAL_WITH = [
|
|
149
|
+
` app-id: \${{ secrets.OPENCODE_APP_ID }}`,
|
|
150
|
+
` app-private-key: \${{ secrets.OPENCODE_APP_PRIVATE_KEY }}`,
|
|
151
|
+
].join("\n");
|
|
104
152
|
export function workflowTemplates(options) {
|
|
105
153
|
const actionRef = options.actionRef ?? "v0";
|
|
106
154
|
const configRef = options.configRef ?? "main";
|
|
107
|
-
const model = options.model ?? "free-pi/deepseek-v4.1-flash#max";
|
|
108
155
|
const plugins = options.plugins ?? "@eduardo191labs/opencode-free-pi@latest";
|
|
109
156
|
const common = ` config-repo: ${options.configRepo}
|
|
110
157
|
config-ref: ${configRef}
|
|
111
158
|
plugins: '${plugins}'`;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
159
|
+
// Two ways in, one job.
|
|
160
|
+
//
|
|
161
|
+
// - `repository_dispatch` of type `bot-event`: the Cloudflare Worker in
|
|
162
|
+
// `webhook/` already received the event, decided it was worth a runner and
|
|
163
|
+
// forwarded the payload. This path exists so the decision happens before a
|
|
164
|
+
// runner is allocated.
|
|
165
|
+
// - The native triggers remain for a deployment without the worker. Each is
|
|
166
|
+
// limited to the default branch or a pull request targeting it, so
|
|
167
|
+
// side-branch work costs nothing.
|
|
168
|
+
//
|
|
169
|
+
// There is exactly one job, so one runner per accepted event, and no step that
|
|
170
|
+
// re-implements what the CLI already does: the payload file is handed straight
|
|
171
|
+
// to the action, which unwraps the forwarded shape.
|
|
116
172
|
return [
|
|
117
173
|
{
|
|
118
|
-
path: ".github/workflows/opencode
|
|
119
|
-
content: `name: OpenCode
|
|
174
|
+
path: ".github/workflows/opencode.yml",
|
|
175
|
+
content: `name: OpenCode
|
|
120
176
|
on:
|
|
177
|
+
repository_dispatch:
|
|
178
|
+
types: [bot-event]
|
|
121
179
|
pull_request:
|
|
180
|
+
branches: [main, master]
|
|
122
181
|
types: [opened, synchronize, reopened, ready_for_review]
|
|
123
|
-
|
|
124
|
-
permissions:
|
|
125
|
-
contents: read
|
|
126
|
-
pull-requests: write
|
|
127
|
-
issues: write
|
|
128
|
-
|
|
129
|
-
${watchdog}
|
|
130
|
-
jobs:
|
|
131
|
-
review:
|
|
132
|
-
if: github.event.pull_request.draft == false
|
|
133
|
-
runs-on: ubuntu-latest
|
|
134
|
-
steps:
|
|
135
|
-
- uses: actions/checkout@v4
|
|
136
|
-
with:
|
|
137
|
-
fetch-depth: 0
|
|
138
|
-
ref: \${{ github.event.pull_request.head.sha }}
|
|
139
|
-
- uses: eduardo191-star/opencode-actions@${actionRef}
|
|
140
|
-
with:
|
|
141
|
-
event: pull_request
|
|
142
|
-
github-token: \${{ secrets.GITHUB_TOKEN }}
|
|
143
|
-
${common}
|
|
144
|
-
env:
|
|
145
|
-
FREEPI_JWT: \${{ secrets.FREEPI_JWT }}
|
|
146
|
-
`,
|
|
147
|
-
},
|
|
148
|
-
{
|
|
149
|
-
path: ".github/workflows/opencode-commands.yml",
|
|
150
|
-
content: `name: OpenCode Commands
|
|
151
|
-
on:
|
|
152
182
|
issue_comment:
|
|
153
183
|
types: [created]
|
|
184
|
+
pull_request_review_comment:
|
|
185
|
+
types: [created]
|
|
186
|
+
issues:
|
|
187
|
+
types: [opened]
|
|
188
|
+
workflow_run:
|
|
189
|
+
workflows: ["*"]
|
|
190
|
+
types: [completed]
|
|
154
191
|
|
|
155
192
|
permissions:
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
193
|
+
${workflowPermissions({ contents: "write" })}
|
|
194
|
+
|
|
195
|
+
concurrency:
|
|
196
|
+
# One run per pull request or issue thread; a newer one replaces the older.
|
|
197
|
+
group: opencode-\${{ github.event.client_payload.payload.pull_request.number || github.event.pull_request.number || github.event.issue.number || github.run_id }}
|
|
198
|
+
cancel-in-progress: true
|
|
159
199
|
|
|
160
|
-
${watchdog}
|
|
161
200
|
jobs:
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
startsWith(github.event.comment.body, '/bot') ||
|
|
165
|
-
contains(github.event.comment.body, '\\n/bot')
|
|
201
|
+
bot:
|
|
202
|
+
name: \${{ github.event.client_payload.event || github.event_name }}
|
|
166
203
|
runs-on: ubuntu-latest
|
|
204
|
+
# A draft is not ready, a bot comment is not a request, a successful run
|
|
205
|
+
# needs no autofix. The worker filters most of this; repeating it keeps a
|
|
206
|
+
# deployment without the worker behaving the same.
|
|
207
|
+
if: |
|
|
208
|
+
github.event_name == 'repository_dispatch' ||
|
|
209
|
+
(github.event_name == 'pull_request' && github.event.pull_request.draft == false) ||
|
|
210
|
+
(github.event_name == 'issue_comment' && github.event.comment.user.type != 'Bot') ||
|
|
211
|
+
(github.event_name == 'pull_request_review_comment' && github.event.comment.user.type != 'Bot') ||
|
|
212
|
+
github.event_name == 'issues' ||
|
|
213
|
+
(github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'failure' && github.event.workflow_run.name != 'OpenCode')
|
|
167
214
|
steps:
|
|
168
215
|
- uses: actions/checkout@v4
|
|
169
216
|
with:
|
|
170
|
-
ref: \${{ github.event.issue.pull_request && format('refs/pull/{0}/head', github.event.issue.number) || github.event.repository.default_branch }}
|
|
217
|
+
ref: \${{ github.event.client_payload.payload.pull_request.head.sha || github.event.client_payload.payload.workflow_run.head_sha || github.event.pull_request.head.sha || github.event.issue.pull_request && format('refs/pull/{0}/head', github.event.issue.number) || github.event.workflow_run.head_sha || github.event.repository.default_branch }}
|
|
171
218
|
fetch-depth: 0
|
|
172
|
-
- uses: eduardo191-star/opencode-actions@${actionRef}
|
|
173
|
-
with:
|
|
174
|
-
event: issue_comment
|
|
175
|
-
github-token: \${{ secrets.GITHUB_TOKEN }}
|
|
176
|
-
${common}
|
|
177
|
-
env:
|
|
178
|
-
FREEPI_JWT: \${{ secrets.FREEPI_JWT }}
|
|
179
|
-
`,
|
|
180
|
-
},
|
|
181
|
-
{
|
|
182
|
-
path: ".github/workflows/opencode-issues.yml",
|
|
183
|
-
content: `name: OpenCode Issues
|
|
184
|
-
on:
|
|
185
|
-
issues:
|
|
186
|
-
types: [opened, reopened]
|
|
187
219
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
220
|
+
# The forwarded payload is written to a file so the CLI reads one shape
|
|
221
|
+
# from one path. It unwraps \`{ event, payload }\` itself.
|
|
222
|
+
- name: Write the forwarded payload
|
|
223
|
+
if: github.event_name == 'repository_dispatch'
|
|
224
|
+
env:
|
|
225
|
+
FORWARDED: \${{ toJSON(github.event.client_payload) }}
|
|
226
|
+
run: printf '%s' "$FORWARDED" > "$RUNNER_TEMP/event.json"
|
|
192
227
|
|
|
193
|
-
jobs:
|
|
194
|
-
triage:
|
|
195
|
-
if: github.event.issue.author_association != 'NONE'
|
|
196
|
-
runs-on: ubuntu-latest
|
|
197
|
-
steps:
|
|
198
|
-
- uses: actions/checkout@v4
|
|
199
|
-
with:
|
|
200
|
-
fetch-depth: 0
|
|
201
228
|
- uses: eduardo191-star/opencode-actions@${actionRef}
|
|
202
229
|
with:
|
|
203
|
-
event:
|
|
204
|
-
mode: issues
|
|
230
|
+
event: \${{ github.event.client_payload.event || github.event_name }}
|
|
205
231
|
github-token: \${{ secrets.GITHUB_TOKEN }}
|
|
232
|
+
payload: \${{ github.event_name == 'repository_dispatch' && format('{0}/event.json', runner.temp) || github.event_path }}
|
|
206
233
|
${common}
|
|
234
|
+
${APP_CREDENTIAL_WITH}
|
|
207
235
|
env:
|
|
208
236
|
FREEPI_JWT: \${{ secrets.FREEPI_JWT }}
|
|
209
237
|
`,
|
|
210
238
|
},
|
|
211
239
|
];
|
|
212
240
|
}
|
|
213
|
-
export function repoFromEnvOr(o) {
|
|
214
|
-
return o ?? repoFromEnv();
|
|
215
|
-
}
|
package/dist/format.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { BotConfig } from "./config.js";
|
|
2
2
|
declare const SEVERITY_ORDER: readonly ["critical", "high", "medium", "low", "info"];
|
|
3
3
|
export type Severity = (typeof SEVERITY_ORDER)[number];
|
|
4
|
+
/** The plain-language name for the scale: critical, high, medium, low, info. */
|
|
5
|
+
export type SemanticSeverity = Severity;
|
|
4
6
|
export declare function severityRank(severity: string): number;
|
|
5
7
|
export interface Finding {
|
|
6
8
|
path: string;
|
|
@@ -18,10 +20,11 @@ export interface ReviewResult {
|
|
|
18
20
|
}
|
|
19
21
|
export declare function header(config: BotConfig, subtitle?: string): string;
|
|
20
22
|
export declare function badge(severity: Severity): "🟥 **critical**" | "🟧 **high**" | "🟨 **medium**" | "🟦 **low**" | "⬜ **info**";
|
|
21
|
-
export declare function verdictLine(verdict: ReviewResult["verdict"]): "✅ **
|
|
23
|
+
export declare function verdictLine(verdict: ReviewResult["verdict"]): "✅ **Nothing blocking** — no findings worth holding the merge for." | "🚧 **Worth fixing before merge** — suggestions are inline, nothing is blocked." | "💬 **Review complete** — comments below are non-blocking.";
|
|
22
24
|
export declare function reviewSummaryBody(config: BotConfig, review: ReviewResult, meta: {
|
|
23
25
|
model?: string;
|
|
24
26
|
cost?: number;
|
|
27
|
+
durationMs?: number;
|
|
25
28
|
}): string;
|
|
26
29
|
export declare function footer(config: BotConfig, meta: {
|
|
27
30
|
model?: string;
|
package/dist/format.js
CHANGED
|
@@ -26,9 +26,12 @@ export function badge(severity) {
|
|
|
26
26
|
export function verdictLine(verdict) {
|
|
27
27
|
switch (verdict) {
|
|
28
28
|
case "APPROVE":
|
|
29
|
-
return "✅ **
|
|
29
|
+
return "✅ **Nothing blocking** — no findings worth holding the merge for.";
|
|
30
30
|
case "REQUEST_CHANGES":
|
|
31
|
-
|
|
31
|
+
// The review is always posted as COMMENT, so nothing here blocks a merge.
|
|
32
|
+
// Saying "changes requested" would describe a state the pull request is
|
|
33
|
+
// not in, and read as a gate the bot does not have.
|
|
34
|
+
return "🚧 **Worth fixing before merge** — suggestions are inline, nothing is blocked.";
|
|
32
35
|
default:
|
|
33
36
|
return "💬 **Review complete** — comments below are non-blocking.";
|
|
34
37
|
}
|
package/dist/git.d.ts
CHANGED
|
@@ -22,6 +22,22 @@ export declare function assertWorkflowWrites(config: BotConfig, files: string[])
|
|
|
22
22
|
export declare function assertBranchWrites(config: BotConfig, branch: string): void;
|
|
23
23
|
export declare function assertFileCount(config: BotConfig, files: string[], limit?: number): void;
|
|
24
24
|
export declare function configureIdentity(cwd: string, config: BotConfig): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* The marker that keeps a bot commit from triggering the bot.
|
|
27
|
+
*
|
|
28
|
+
* Every commit made from a workflow is pushed with the workflow's token, and
|
|
29
|
+
* GitHub does not start a new workflow run for an `GITHUB_TOKEN` push — but it
|
|
30
|
+
* *does* start one when the run is dispatched by the app, and it always starts
|
|
31
|
+
* one for a pull request branch. Without a marker, each commit the bot makes can
|
|
32
|
+
* start the workflow that made it, and the repository spins.
|
|
33
|
+
*
|
|
34
|
+
* `[skip ci]` is GitHub's own opt-out and is respected by every workflow, which
|
|
35
|
+
* is why it is used instead of a private convention: a repository with its own
|
|
36
|
+
* CI gets the same protection without being told.
|
|
37
|
+
*/
|
|
38
|
+
export declare const SKIP_CI = "[skip ci]";
|
|
39
|
+
/** True when a commit is one the bot made, and so must not re-trigger it. */
|
|
40
|
+
export declare function isBotCommit(message: string): boolean;
|
|
25
41
|
export declare function commitAll(cwd: string, message: string, config: BotConfig): Promise<{
|
|
26
42
|
sha: string;
|
|
27
43
|
files: string[];
|