@eduardo191labs/opencode-actions 0.0.0-stage → 0.1.1
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/LICENSE +22 -0
- package/README.md +311 -2
- package/action.yml +251 -0
- package/dist/actions.d.ts +351 -0
- package/dist/actions.js +561 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +180 -0
- package/dist/commands.d.ts +23 -0
- package/dist/commands.js +120 -0
- package/dist/compile.d.ts +49 -0
- package/dist/compile.js +217 -0
- package/dist/config.d.ts +306 -0
- package/dist/config.js +133 -0
- package/dist/detect.d.ts +55 -0
- package/dist/detect.js +215 -0
- package/dist/format.d.ts +79 -0
- package/dist/format.js +168 -0
- package/dist/git.d.ts +38 -0
- package/dist/git.js +125 -0
- package/dist/github.d.ts +213 -0
- package/dist/github.js +227 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/install.d.ts +2 -0
- package/dist/install.js +299 -0
- package/dist/loader.d.ts +54 -0
- package/dist/loader.js +306 -0
- package/dist/mcp-server.d.ts +16 -0
- package/dist/mcp-server.js +402 -0
- package/dist/prompts.d.ts +70 -0
- package/dist/prompts.js +260 -0
- package/dist/runner.d.ts +108 -0
- package/dist/runner.js +318 -0
- package/dist/summary.d.ts +2 -0
- package/dist/summary.js +99 -0
- package/package.json +57 -4
- package/schemas/opencode-actions.schema.json +527 -0
package/dist/detect.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { repoFromEnv } from "./github.js";
|
|
2
|
+
/**
|
|
3
|
+
* Lexically sniff the repository root. This is what makes the bot work in any
|
|
4
|
+
* repository without being told the language: the prompt and the default
|
|
5
|
+
* commands come from what is actually there.
|
|
6
|
+
*/
|
|
7
|
+
export function detectStack(rootFiles) {
|
|
8
|
+
const has = (...names) => names.some((name) => rootFiles.includes(name));
|
|
9
|
+
const files = rootFiles;
|
|
10
|
+
// Node
|
|
11
|
+
if (has("package.json")) {
|
|
12
|
+
let packageManager = "npm";
|
|
13
|
+
if (has("bun.lock", "bun.lockb"))
|
|
14
|
+
packageManager = "bun";
|
|
15
|
+
else if (has("pnpm-lock.yaml"))
|
|
16
|
+
packageManager = "pnpm";
|
|
17
|
+
else if (has("yarn.lock"))
|
|
18
|
+
packageManager = "yarn";
|
|
19
|
+
const run = packageManager === "npm" ? "npm run" : packageManager === "bun" ? "bun run" : packageManager === "pnpm" ? "pnpm run" : "yarn";
|
|
20
|
+
return {
|
|
21
|
+
language: "typescript/javascript",
|
|
22
|
+
packageManager,
|
|
23
|
+
testCommand: `${run} test`,
|
|
24
|
+
buildCommand: `${run} build`,
|
|
25
|
+
lintCommand: `${run} lint`,
|
|
26
|
+
files,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
// Rust
|
|
30
|
+
if (has("Cargo.toml")) {
|
|
31
|
+
return { language: "rust", packageManager: "cargo", testCommand: "cargo test", buildCommand: "cargo build", lintCommand: "cargo clippy", files };
|
|
32
|
+
}
|
|
33
|
+
// Go
|
|
34
|
+
if (has("go.mod")) {
|
|
35
|
+
return { language: "go", packageManager: "go", testCommand: "go test ./...", buildCommand: "go build ./...", lintCommand: "go vet ./...", files };
|
|
36
|
+
}
|
|
37
|
+
// Python
|
|
38
|
+
if (has("pyproject.toml", "setup.py", "requirements.txt", "Pipfile")) {
|
|
39
|
+
const manager = has("poetry.lock") ? "poetry" : has("uv.lock") ? "uv" : has("Pipfile") ? "pipenv" : "pip";
|
|
40
|
+
const test = manager === "poetry" ? "poetry run pytest" : manager === "uv" ? "uv run pytest" : manager === "pipenv" ? "pipenv run pytest" : "pytest";
|
|
41
|
+
return { language: "python", packageManager: manager, testCommand: test, lintCommand: "ruff check .", files };
|
|
42
|
+
}
|
|
43
|
+
// JVM
|
|
44
|
+
if (has("pom.xml"))
|
|
45
|
+
return { language: "java", packageManager: "maven", testCommand: "mvn test", buildCommand: "mvn package", files };
|
|
46
|
+
if (has("build.gradle", "build.gradle.kts", "settings.gradle")) {
|
|
47
|
+
return { language: "java/kotlin", packageManager: "gradle", testCommand: "./gradlew test", buildCommand: "./gradlew build", files };
|
|
48
|
+
}
|
|
49
|
+
// Ruby
|
|
50
|
+
if (has("Gemfile"))
|
|
51
|
+
return { language: "ruby", packageManager: "bundler", testCommand: "bundle exec rspec", files };
|
|
52
|
+
// PHP
|
|
53
|
+
if (has("composer.json"))
|
|
54
|
+
return { language: "php", packageManager: "composer", testCommand: "composer test", files };
|
|
55
|
+
// C/C++
|
|
56
|
+
if (has("CMakeLists.txt", "Makefile", "meson.build")) {
|
|
57
|
+
return { language: "c/c++", packageManager: "cmake", testCommand: "ctest", buildCommand: "cmake --build build", files };
|
|
58
|
+
}
|
|
59
|
+
// .NET
|
|
60
|
+
if (rootFiles.some((file) => file.endsWith(".sln") || file.endsWith(".csproj"))) {
|
|
61
|
+
return { language: "c#", packageManager: "dotnet", testCommand: "dotnet test", buildCommand: "dotnet build", files };
|
|
62
|
+
}
|
|
63
|
+
// Swift
|
|
64
|
+
if (has("Package.swift"))
|
|
65
|
+
return { language: "swift", packageManager: "swift", testCommand: "swift test", files };
|
|
66
|
+
// Dart / Flutter
|
|
67
|
+
if (has("pubspec.yaml"))
|
|
68
|
+
return { language: "dart", packageManager: "pub", testCommand: "dart test", files };
|
|
69
|
+
// Docs-only or unknown
|
|
70
|
+
if (has("README.md") && rootFiles.length <= 3) {
|
|
71
|
+
return { language: "documentation", files };
|
|
72
|
+
}
|
|
73
|
+
return { language: "unknown", files };
|
|
74
|
+
}
|
|
75
|
+
export async function fetchRepoInfo(github, ref) {
|
|
76
|
+
const data = await github.get(`/repos/${ref.owner}/${ref.repo}`);
|
|
77
|
+
return {
|
|
78
|
+
ref,
|
|
79
|
+
defaultBranch: data.default_branch,
|
|
80
|
+
private: data.private,
|
|
81
|
+
archived: data.archived,
|
|
82
|
+
fork: data.fork,
|
|
83
|
+
disabled: data.disabled,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** Detect the stack and the workflows already in a repository. */
|
|
87
|
+
export async function detect(github, ref) {
|
|
88
|
+
const info = await fetchRepoInfo(github, ref);
|
|
89
|
+
const root = await github.listDir(ref, "");
|
|
90
|
+
const stack = detectStack(root.filter((entry) => entry.type === "file").map((entry) => entry.name));
|
|
91
|
+
let workflows = [];
|
|
92
|
+
try {
|
|
93
|
+
const entries = await github.listDir(ref, ".github/workflows");
|
|
94
|
+
workflows = entries
|
|
95
|
+
.filter((entry) => entry.type === "file" && /\.ya?ml$/.test(entry.name))
|
|
96
|
+
.map((entry) => ({ name: entry.name, path: entry.path }));
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
workflows = [];
|
|
100
|
+
}
|
|
101
|
+
const installed = workflows.some((file) => file.name === "opencode-review.yml" || file.name === "opencode.yml");
|
|
102
|
+
return { repo: info, stack, workflows, installed };
|
|
103
|
+
}
|
|
104
|
+
export function workflowTemplates(options) {
|
|
105
|
+
const actionRef = options.actionRef ?? "v0";
|
|
106
|
+
const configRef = options.configRef ?? "main";
|
|
107
|
+
const model = options.model ?? "free-pi/deepseek-v4.1-flash#max";
|
|
108
|
+
const plugins = options.plugins ?? "@eduardo191labs/opencode-free-pi@latest";
|
|
109
|
+
const common = ` config-repo: ${options.configRepo}
|
|
110
|
+
config-ref: ${configRef}
|
|
111
|
+
plugins: '${plugins}'`;
|
|
112
|
+
const watchdog = `concurrency:
|
|
113
|
+
group: opencode-\${{ github.workflow }}-\${{ github.ref }}
|
|
114
|
+
cancel-in-progress: true
|
|
115
|
+
`;
|
|
116
|
+
return [
|
|
117
|
+
{
|
|
118
|
+
path: ".github/workflows/opencode-review.yml",
|
|
119
|
+
content: `name: OpenCode Review
|
|
120
|
+
on:
|
|
121
|
+
pull_request:
|
|
122
|
+
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
|
+
issue_comment:
|
|
153
|
+
types: [created]
|
|
154
|
+
|
|
155
|
+
permissions:
|
|
156
|
+
contents: write
|
|
157
|
+
pull-requests: write
|
|
158
|
+
issues: write
|
|
159
|
+
|
|
160
|
+
${watchdog}
|
|
161
|
+
jobs:
|
|
162
|
+
command:
|
|
163
|
+
if: |
|
|
164
|
+
startsWith(github.event.comment.body, '/bot') ||
|
|
165
|
+
contains(github.event.comment.body, '\\n/bot')
|
|
166
|
+
runs-on: ubuntu-latest
|
|
167
|
+
steps:
|
|
168
|
+
- uses: actions/checkout@v4
|
|
169
|
+
with:
|
|
170
|
+
ref: \${{ github.event.issue.pull_request && format('refs/pull/{0}/head', github.event.issue.number) || github.event.repository.default_branch }}
|
|
171
|
+
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
|
+
|
|
188
|
+
permissions:
|
|
189
|
+
contents: write
|
|
190
|
+
pull-requests: write
|
|
191
|
+
issues: write
|
|
192
|
+
|
|
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
|
+
- uses: eduardo191-star/opencode-actions@${actionRef}
|
|
202
|
+
with:
|
|
203
|
+
event: issues
|
|
204
|
+
mode: issues
|
|
205
|
+
github-token: \${{ secrets.GITHUB_TOKEN }}
|
|
206
|
+
${common}
|
|
207
|
+
env:
|
|
208
|
+
FREEPI_JWT: \${{ secrets.FREEPI_JWT }}
|
|
209
|
+
`,
|
|
210
|
+
},
|
|
211
|
+
];
|
|
212
|
+
}
|
|
213
|
+
export function repoFromEnvOr(o) {
|
|
214
|
+
return o ?? repoFromEnv();
|
|
215
|
+
}
|
package/dist/format.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { BotConfig } from "./config.js";
|
|
2
|
+
declare const SEVERITY_ORDER: readonly ["critical", "high", "medium", "low", "info"];
|
|
3
|
+
export type Severity = (typeof SEVERITY_ORDER)[number];
|
|
4
|
+
export declare function severityRank(severity: string): number;
|
|
5
|
+
export interface Finding {
|
|
6
|
+
path: string;
|
|
7
|
+
line: number;
|
|
8
|
+
severity: Severity;
|
|
9
|
+
title: string;
|
|
10
|
+
body: string;
|
|
11
|
+
suggestion?: string | null;
|
|
12
|
+
}
|
|
13
|
+
export interface ReviewResult {
|
|
14
|
+
summary: string;
|
|
15
|
+
verdict: "APPROVE" | "COMMENT" | "REQUEST_CHANGES";
|
|
16
|
+
findings: Finding[];
|
|
17
|
+
strengths?: string[];
|
|
18
|
+
}
|
|
19
|
+
export declare function header(config: BotConfig, subtitle?: string): string;
|
|
20
|
+
export declare function badge(severity: Severity): "🟥 **critical**" | "🟧 **high**" | "🟨 **medium**" | "🟦 **low**" | "⬜ **info**";
|
|
21
|
+
export declare function verdictLine(verdict: ReviewResult["verdict"]): "✅ **Approved** — no blocking issues found." | "🚧 **Changes requested** — see the findings below." | "💬 **Review complete** — comments below are non-blocking.";
|
|
22
|
+
export declare function reviewSummaryBody(config: BotConfig, review: ReviewResult, meta: {
|
|
23
|
+
model?: string;
|
|
24
|
+
cost?: number;
|
|
25
|
+
}): string;
|
|
26
|
+
export declare function footer(config: BotConfig, meta: {
|
|
27
|
+
model?: string;
|
|
28
|
+
cost?: number;
|
|
29
|
+
durationMs?: number;
|
|
30
|
+
runUrl?: string;
|
|
31
|
+
branch?: string;
|
|
32
|
+
actor?: string;
|
|
33
|
+
command?: string;
|
|
34
|
+
}): string;
|
|
35
|
+
/** Inline comment body for a single finding. */
|
|
36
|
+
export declare function findingBody(config: BotConfig, finding: Finding): string;
|
|
37
|
+
export declare function issueTriageBody(config: BotConfig, result: {
|
|
38
|
+
summary: string;
|
|
39
|
+
severity: string;
|
|
40
|
+
labels: string[];
|
|
41
|
+
answer: string;
|
|
42
|
+
plan: string[];
|
|
43
|
+
files: string[];
|
|
44
|
+
reproducible: string;
|
|
45
|
+
needs_info: string | null;
|
|
46
|
+
}, meta: {
|
|
47
|
+
model?: string;
|
|
48
|
+
cost?: number;
|
|
49
|
+
}): string;
|
|
50
|
+
export declare function autofixBody(config: BotConfig, result: {
|
|
51
|
+
diagnosis: string;
|
|
52
|
+
fixable: boolean;
|
|
53
|
+
summary: string;
|
|
54
|
+
explanation: string;
|
|
55
|
+
commit_message: string;
|
|
56
|
+
}, meta: {
|
|
57
|
+
runUrl: string;
|
|
58
|
+
branch: string;
|
|
59
|
+
}): string;
|
|
60
|
+
export declare function commandReplyBody(config: BotConfig, text: string, meta: {
|
|
61
|
+
actor: string;
|
|
62
|
+
command: string;
|
|
63
|
+
}): string;
|
|
64
|
+
export declare function errorBody(config: BotConfig, message: string, hint?: string): string;
|
|
65
|
+
/** Collapse a huge diff into a digest that fits a prompt. */
|
|
66
|
+
export declare function diffDigest(files: {
|
|
67
|
+
filename: string;
|
|
68
|
+
patch?: string;
|
|
69
|
+
additions: number;
|
|
70
|
+
deletions: number;
|
|
71
|
+
status: string;
|
|
72
|
+
}[], limit?: number): string;
|
|
73
|
+
export declare function filesSummary(files: {
|
|
74
|
+
filename: string;
|
|
75
|
+
additions: number;
|
|
76
|
+
deletions: number;
|
|
77
|
+
status: string;
|
|
78
|
+
}[]): string;
|
|
79
|
+
export {};
|
package/dist/format.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { bot } from "./config.js";
|
|
2
|
+
const SEVERITY_ORDER = ["critical", "high", "medium", "low", "info"];
|
|
3
|
+
export function severityRank(severity) {
|
|
4
|
+
const index = SEVERITY_ORDER.indexOf(severity);
|
|
5
|
+
return index === -1 ? SEVERITY_ORDER.length : index;
|
|
6
|
+
}
|
|
7
|
+
export function header(config, subtitle) {
|
|
8
|
+
const name = bot(config).name ?? "opencode";
|
|
9
|
+
const emoji = bot(config).emoji ?? "🤖";
|
|
10
|
+
return subtitle ? `### ${emoji} ${name} — ${subtitle}` : `### ${emoji} ${name}`;
|
|
11
|
+
}
|
|
12
|
+
export function badge(severity) {
|
|
13
|
+
switch (severity) {
|
|
14
|
+
case "critical":
|
|
15
|
+
return "🟥 **critical**";
|
|
16
|
+
case "high":
|
|
17
|
+
return "🟧 **high**";
|
|
18
|
+
case "medium":
|
|
19
|
+
return "🟨 **medium**";
|
|
20
|
+
case "low":
|
|
21
|
+
return "🟦 **low**";
|
|
22
|
+
default:
|
|
23
|
+
return "⬜ **info**";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export function verdictLine(verdict) {
|
|
27
|
+
switch (verdict) {
|
|
28
|
+
case "APPROVE":
|
|
29
|
+
return "✅ **Approved** — no blocking issues found.";
|
|
30
|
+
case "REQUEST_CHANGES":
|
|
31
|
+
return "🚧 **Changes requested** — see the findings below.";
|
|
32
|
+
default:
|
|
33
|
+
return "💬 **Review complete** — comments below are non-blocking.";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export function reviewSummaryBody(config, review, meta) {
|
|
37
|
+
const counts = new Map();
|
|
38
|
+
for (const finding of review.findings)
|
|
39
|
+
counts.set(finding.severity, (counts.get(finding.severity) ?? 0) + 1);
|
|
40
|
+
const parts = [];
|
|
41
|
+
for (const severity of SEVERITY_ORDER) {
|
|
42
|
+
const count = counts.get(severity);
|
|
43
|
+
if (count)
|
|
44
|
+
parts.push(`${count} ${severity}`);
|
|
45
|
+
}
|
|
46
|
+
const lines = [
|
|
47
|
+
header(config, "review"),
|
|
48
|
+
"",
|
|
49
|
+
verdictLine(review.verdict),
|
|
50
|
+
"",
|
|
51
|
+
review.summary.trim(),
|
|
52
|
+
];
|
|
53
|
+
if (parts.length) {
|
|
54
|
+
lines.push("", `**Findings:** ${parts.join(" · ")}`);
|
|
55
|
+
}
|
|
56
|
+
if (review.findings.length) {
|
|
57
|
+
lines.push("", "<details><summary>All findings</summary>", "");
|
|
58
|
+
for (const finding of review.findings) {
|
|
59
|
+
const title = `${badge(finding.severity)} \`${finding.path}:${finding.line}\` — ${finding.title}`;
|
|
60
|
+
lines.push(`- ${title}`);
|
|
61
|
+
}
|
|
62
|
+
lines.push("", "</details>");
|
|
63
|
+
}
|
|
64
|
+
if (review.strengths?.length) {
|
|
65
|
+
lines.push("", "<details><summary>What works well</summary>", "");
|
|
66
|
+
for (const strength of review.strengths)
|
|
67
|
+
lines.push(`- ${strength}`);
|
|
68
|
+
lines.push("", "</details>");
|
|
69
|
+
}
|
|
70
|
+
lines.push("", footer(config, meta));
|
|
71
|
+
return lines.join("\n");
|
|
72
|
+
}
|
|
73
|
+
export function footer(config, meta) {
|
|
74
|
+
const bits = [];
|
|
75
|
+
if (meta.model)
|
|
76
|
+
bits.push(`\`${meta.model}\``);
|
|
77
|
+
if (meta.cost !== undefined)
|
|
78
|
+
bits.push(`$${meta.cost.toFixed(3)}`);
|
|
79
|
+
if (meta.durationMs !== undefined)
|
|
80
|
+
bits.push(`${(meta.durationMs / 1000).toFixed(0)}s`);
|
|
81
|
+
const name = bot(config).name ?? "opencode";
|
|
82
|
+
return `<sub>${bits.length ? bits.join(" · ") + " · " : ""}${name} · [commands](${commandHelpUrl(config)}) · re-run with \`${bot(config).commands?.prefix ?? "/bot"} review\`</sub>`;
|
|
83
|
+
}
|
|
84
|
+
function commandHelpUrl(_config) {
|
|
85
|
+
return "https://github.com/eduardo191-star/opencode-actions#commands";
|
|
86
|
+
}
|
|
87
|
+
/** Inline comment body for a single finding. */
|
|
88
|
+
export function findingBody(config, finding) {
|
|
89
|
+
const lines = [`${badge(finding.severity)} ${finding.title}`, "", finding.body.trim()];
|
|
90
|
+
if (finding.suggestion) {
|
|
91
|
+
lines.push("", "```suggestion", finding.suggestion.replace(/\n$/, ""), "```");
|
|
92
|
+
}
|
|
93
|
+
lines.push("", `<sub>${bot(config).name ?? "opencode"}</sub>`);
|
|
94
|
+
return lines.join("\n");
|
|
95
|
+
}
|
|
96
|
+
export function issueTriageBody(config, result, meta) {
|
|
97
|
+
const lines = [header(config, "triage"), "", result.summary.trim()];
|
|
98
|
+
lines.push("", `**Severity:** ${badge(result.severity)} · **Reproducible:** ${result.reproducible}`);
|
|
99
|
+
if (result.plan.length) {
|
|
100
|
+
lines.push("", "**Plan**", "");
|
|
101
|
+
result.plan.forEach((step, index) => lines.push(`${index + 1}. ${step}`));
|
|
102
|
+
}
|
|
103
|
+
if (result.files.length) {
|
|
104
|
+
lines.push("", "**Files involved**", "");
|
|
105
|
+
for (const file of result.files)
|
|
106
|
+
lines.push(`- \`${file}\``);
|
|
107
|
+
}
|
|
108
|
+
if (result.answer.trim())
|
|
109
|
+
lines.push("", "---", "", result.answer.trim());
|
|
110
|
+
if (result.needs_info)
|
|
111
|
+
lines.push("", `> **Needs info:** ${result.needs_info}`);
|
|
112
|
+
if (result.labels.length)
|
|
113
|
+
lines.push("", `<sub>Suggested labels: ${result.labels.map((l) => `\`${l}\``).join(" ")}</sub>`);
|
|
114
|
+
lines.push("", footer(config, meta));
|
|
115
|
+
return lines.join("\n");
|
|
116
|
+
}
|
|
117
|
+
export function autofixBody(config, result, meta) {
|
|
118
|
+
const lines = [
|
|
119
|
+
header(config, "workflow autofix"),
|
|
120
|
+
"",
|
|
121
|
+
`Run: ${meta.runUrl}`,
|
|
122
|
+
`Branch: \`${meta.branch}\``,
|
|
123
|
+
"",
|
|
124
|
+
"**Diagnosis**",
|
|
125
|
+
"",
|
|
126
|
+
result.diagnosis.trim(),
|
|
127
|
+
];
|
|
128
|
+
if (result.fixable) {
|
|
129
|
+
lines.push("", "**Fix**", "", result.explanation.trim(), "", `Commit: \`${result.commit_message}\``);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
lines.push("", "> This failure cannot be fixed automatically. A manual change is required.");
|
|
133
|
+
}
|
|
134
|
+
lines.push("", footer(config, meta));
|
|
135
|
+
return lines.join("\n");
|
|
136
|
+
}
|
|
137
|
+
export function commandReplyBody(config, text, meta) {
|
|
138
|
+
const lines = [header(config, `\`${meta.command}\``), "", text.trim(), "", footer(config, meta)];
|
|
139
|
+
return lines.join("\n");
|
|
140
|
+
}
|
|
141
|
+
export function errorBody(config, message, hint) {
|
|
142
|
+
const lines = [header(config, "run failed"), "", "```", message.trim().slice(0, 3000), "```"];
|
|
143
|
+
if (hint)
|
|
144
|
+
lines.push("", `> ${hint}`);
|
|
145
|
+
return lines.join("\n");
|
|
146
|
+
}
|
|
147
|
+
/** Collapse a huge diff into a digest that fits a prompt. */
|
|
148
|
+
export function diffDigest(files, limit = 60_000) {
|
|
149
|
+
const chunks = [];
|
|
150
|
+
let total = 0;
|
|
151
|
+
for (const file of files) {
|
|
152
|
+
if (!file.patch)
|
|
153
|
+
continue;
|
|
154
|
+
const block = `diff --git a/${file.filename} b/${file.filename}\n${file.patch}\n`;
|
|
155
|
+
if (total + block.length > limit) {
|
|
156
|
+
chunks.push(`... (${files.length - chunks.length} more files omitted from this digest; read them with git)`);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
chunks.push(block);
|
|
160
|
+
total += block.length;
|
|
161
|
+
}
|
|
162
|
+
return chunks.join("\n");
|
|
163
|
+
}
|
|
164
|
+
export function filesSummary(files) {
|
|
165
|
+
return files
|
|
166
|
+
.map((file) => `- \`${file.filename}\` (${file.status}, +${file.additions}/-${file.deletions})`)
|
|
167
|
+
.join("\n");
|
|
168
|
+
}
|
package/dist/git.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { BotConfig } from "./config.js";
|
|
2
|
+
export interface GitResult {
|
|
3
|
+
stdout: string;
|
|
4
|
+
stderr: string;
|
|
5
|
+
code: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function git(cwd: string, args: string[]): Promise<GitResult>;
|
|
8
|
+
export interface GitState {
|
|
9
|
+
branch: string;
|
|
10
|
+
sha: string;
|
|
11
|
+
dirty: boolean;
|
|
12
|
+
changedFiles: string[];
|
|
13
|
+
ahead: number;
|
|
14
|
+
}
|
|
15
|
+
export declare function state(cwd: string): Promise<GitState>;
|
|
16
|
+
export declare class PermissionError extends Error {
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
}
|
|
19
|
+
/** Refuse to write workflow files unless the config allows it. */
|
|
20
|
+
export declare function assertWorkflowWrites(config: BotConfig, files: string[]): void;
|
|
21
|
+
/** Refuse to push to a protected or default branch unless allowed. */
|
|
22
|
+
export declare function assertBranchWrites(config: BotConfig, branch: string): void;
|
|
23
|
+
export declare function assertFileCount(config: BotConfig, files: string[], limit?: number): void;
|
|
24
|
+
export declare function configureIdentity(cwd: string, config: BotConfig): Promise<void>;
|
|
25
|
+
export declare function commitAll(cwd: string, message: string, config: BotConfig): Promise<{
|
|
26
|
+
sha: string;
|
|
27
|
+
files: string[];
|
|
28
|
+
} | null>;
|
|
29
|
+
/**
|
|
30
|
+
* Push with the token embedded in the remote URL. Actions exposes the token
|
|
31
|
+
* through an env var so it never lands in the process list.
|
|
32
|
+
*/
|
|
33
|
+
export declare function push(cwd: string, branch: string, config: BotConfig, options?: {
|
|
34
|
+
force?: boolean;
|
|
35
|
+
}): Promise<boolean>;
|
|
36
|
+
export declare function sanitize(text: string, secret: string): string;
|
|
37
|
+
export declare function diffForPR(cwd: string, base: string): Promise<string>;
|
|
38
|
+
export declare function changedFilesSince(cwd: string, base: string): Promise<string[]>;
|
package/dist/git.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
import { bot, permission } from "./config.js";
|
|
4
|
+
const exec = promisify(execFile);
|
|
5
|
+
export async function git(cwd, args) {
|
|
6
|
+
try {
|
|
7
|
+
const { stdout, stderr } = await exec("git", args, { cwd, maxBuffer: 64 * 1024 * 1024 });
|
|
8
|
+
return { stdout, stderr, code: 0 };
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
return { stdout: error.stdout ?? "", stderr: error.stderr ?? String(error), code: error.code ?? 1 };
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export async function state(cwd) {
|
|
15
|
+
const [branch, sha, status] = await Promise.all([
|
|
16
|
+
git(cwd, ["rev-parse", "--abbrev-ref", "HEAD"]),
|
|
17
|
+
git(cwd, ["rev-parse", "HEAD"]),
|
|
18
|
+
git(cwd, ["status", "--porcelain"]),
|
|
19
|
+
]);
|
|
20
|
+
const changedFiles = status.stdout
|
|
21
|
+
.split("\n")
|
|
22
|
+
.filter(Boolean)
|
|
23
|
+
.map((line) => line.slice(3).trim());
|
|
24
|
+
return {
|
|
25
|
+
branch: branch.stdout.trim(),
|
|
26
|
+
sha: sha.stdout.trim(),
|
|
27
|
+
dirty: changedFiles.length > 0,
|
|
28
|
+
changedFiles,
|
|
29
|
+
ahead: 0,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export class PermissionError extends Error {
|
|
33
|
+
constructor(message) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "PermissionError";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** Refuse to write workflow files unless the config allows it. */
|
|
39
|
+
export function assertWorkflowWrites(config, files) {
|
|
40
|
+
const allowed = permission(config, "edit_workflows") === "allow";
|
|
41
|
+
if (allowed)
|
|
42
|
+
return;
|
|
43
|
+
const workflows = files.filter((file) => file.startsWith(".github/workflows/") || /\/.github\/workflows\//.test(file));
|
|
44
|
+
if (workflows.length) {
|
|
45
|
+
throw new PermissionError(`Refusing to modify workflow files (${workflows.join(", ")}): set bot.permissions.edit_workflows to "allow".`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Refuse to push to a protected or default branch unless allowed. */
|
|
49
|
+
export function assertBranchWrites(config, branch) {
|
|
50
|
+
const cfg = bot(config);
|
|
51
|
+
const protectedPatterns = cfg.protected_branches ?? ["main", "master"];
|
|
52
|
+
const isProtected = protectedPatterns.some((pattern) => globMatch(pattern, branch));
|
|
53
|
+
const isDefault = branch === (cfg.base_branch ?? "main");
|
|
54
|
+
if (isProtected || isDefault) {
|
|
55
|
+
if (permission(config, "push_default_branch") !== "allow") {
|
|
56
|
+
throw new PermissionError(`Refusing to write to protected branch "${branch}": set bot.permissions.push_default_branch to "allow".`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export function assertFileCount(config, files, limit) {
|
|
61
|
+
const max = limit ?? bot(config).autofix?.max_files ?? 10;
|
|
62
|
+
if (files.length > max) {
|
|
63
|
+
throw new PermissionError(`Refusing to change ${files.length} files (limit ${max}); narrow the task.`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function globMatch(pattern, value) {
|
|
67
|
+
const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
|
|
68
|
+
return new RegExp(`^${escaped}$`).test(value);
|
|
69
|
+
}
|
|
70
|
+
export async function configureIdentity(cwd, config) {
|
|
71
|
+
const cfg = bot(config);
|
|
72
|
+
const name = cfg.name ?? "opencode";
|
|
73
|
+
await git(cwd, ["config", "user.name", `${name}[bot]`]);
|
|
74
|
+
await git(cwd, ["config", "user.email", `${name}-bot@users.noreply.github.com`]);
|
|
75
|
+
}
|
|
76
|
+
export async function commitAll(cwd, message, config) {
|
|
77
|
+
const owner = await state(cwd);
|
|
78
|
+
if (!owner.dirty)
|
|
79
|
+
return null;
|
|
80
|
+
assertWorkflowWrites(config, owner.changedFiles);
|
|
81
|
+
await configureIdentity(cwd, config);
|
|
82
|
+
const trailer = bot(config).commit_trailer;
|
|
83
|
+
const full = trailer ? `${message}\n\n${trailer}` : message;
|
|
84
|
+
await git(cwd, ["add", "-A"]);
|
|
85
|
+
const result = await git(cwd, ["commit", "-m", full]);
|
|
86
|
+
if (result.code !== 0)
|
|
87
|
+
throw new Error(`git commit failed: ${result.stderr}`);
|
|
88
|
+
const sha = await git(cwd, ["rev-parse", "HEAD"]);
|
|
89
|
+
return { sha: sha.stdout.trim(), files: owner.changedFiles };
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Push with the token embedded in the remote URL. Actions exposes the token
|
|
93
|
+
* through an env var so it never lands in the process list.
|
|
94
|
+
*/
|
|
95
|
+
export async function push(cwd, branch, config, options = {}) {
|
|
96
|
+
assertBranchWrites(config, branch);
|
|
97
|
+
if (options.force && permission(config, "force_push") !== "allow") {
|
|
98
|
+
throw new PermissionError(`Refusing to force-push: set bot.permissions.force_push to "allow".`);
|
|
99
|
+
}
|
|
100
|
+
const token = process.env.GITHUB_TOKEN;
|
|
101
|
+
const repo = process.env.GITHUB_REPOSITORY;
|
|
102
|
+
if (!token || !repo)
|
|
103
|
+
throw new Error("GITHUB_TOKEN and GITHUB_REPOSITORY are required to push");
|
|
104
|
+
const url = `https://x-access-token:${token}@github.com/${repo}.git`;
|
|
105
|
+
const args = ["push", url, `HEAD:refs/heads/${branch}`];
|
|
106
|
+
if (options.force)
|
|
107
|
+
args.splice(1, 0, "--force-with-lease");
|
|
108
|
+
const result = await git(cwd, args);
|
|
109
|
+
if (result.code !== 0) {
|
|
110
|
+
// Never echo the URL, which contains the token.
|
|
111
|
+
throw new Error(`git push failed: ${sanitize(result.stderr, token)}`);
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
export function sanitize(text, secret) {
|
|
116
|
+
return text.split(secret).join("***");
|
|
117
|
+
}
|
|
118
|
+
export async function diffForPR(cwd, base) {
|
|
119
|
+
const result = await git(cwd, ["diff", "--stat", `${base}...HEAD`]);
|
|
120
|
+
return result.stdout;
|
|
121
|
+
}
|
|
122
|
+
export async function changedFilesSince(cwd, base) {
|
|
123
|
+
const result = await git(cwd, ["diff", "--name-only", `${base}...HEAD`]);
|
|
124
|
+
return result.stdout.split("\n").filter(Boolean);
|
|
125
|
+
}
|