@agentic-sdlc-kit/cli 1.0.1 → 1.2.0
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/dist/application/agent/gateway.d.ts +15 -1
- package/dist/application/agent/gateway.d.ts.map +1 -1
- package/dist/application/agent/gateway.js +60 -4
- package/dist/application/agent/gateway.js.map +1 -1
- package/dist/application/dispatcher.d.ts.map +1 -1
- package/dist/application/dispatcher.js +3 -0
- package/dist/application/dispatcher.js.map +1 -1
- package/dist/application/improvement/capture.d.ts +25 -0
- package/dist/application/improvement/capture.d.ts.map +1 -0
- package/dist/application/improvement/capture.js +45 -0
- package/dist/application/improvement/capture.js.map +1 -0
- package/dist/application/improvement/github-api.d.ts +38 -0
- package/dist/application/improvement/github-api.d.ts.map +1 -0
- package/dist/application/improvement/github-api.js +132 -0
- package/dist/application/improvement/github-api.js.map +1 -0
- package/dist/application/improvement/github-context.d.ts +19 -0
- package/dist/application/improvement/github-context.d.ts.map +1 -0
- package/dist/application/improvement/github-context.js +39 -0
- package/dist/application/improvement/github-context.js.map +1 -0
- package/dist/application/improvement/scrub-terms.d.ts +12 -0
- package/dist/application/improvement/scrub-terms.d.ts.map +1 -0
- package/dist/application/improvement/scrub-terms.js +66 -0
- package/dist/application/improvement/scrub-terms.js.map +1 -0
- package/dist/application/improvement/upstream.d.ts +18 -0
- package/dist/application/improvement/upstream.d.ts.map +1 -0
- package/dist/application/improvement/upstream.js +32 -0
- package/dist/application/improvement/upstream.js.map +1 -0
- package/dist/commands/improve-kit.d.ts +10 -0
- package/dist/commands/improve-kit.d.ts.map +1 -0
- package/dist/commands/improve-kit.js +79 -0
- package/dist/commands/improve-kit.js.map +1 -0
- package/dist/commands/improve-project.d.ts +18 -0
- package/dist/commands/improve-project.d.ts.map +1 -0
- package/dist/commands/improve-project.js +133 -0
- package/dist/commands/improve-project.js.map +1 -0
- package/dist/commands/improve-review.d.ts +12 -0
- package/dist/commands/improve-review.d.ts.map +1 -0
- package/dist/commands/improve-review.js +56 -0
- package/dist/commands/improve-review.js.map +1 -0
- package/dist/commands/improve.d.ts +12 -0
- package/dist/commands/improve.d.ts.map +1 -0
- package/dist/commands/improve.js +185 -0
- package/dist/commands/improve.js.map +1 -0
- package/dist/index.js +19 -2
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/application/agent/gateway.test.ts +80 -3
- package/src/application/agent/gateway.ts +61 -4
- package/src/application/dispatcher.ts +2 -0
- package/src/application/improvement/capture.test.ts +80 -0
- package/src/application/improvement/capture.ts +72 -0
- package/src/application/improvement/github-api.test.ts +191 -0
- package/src/application/improvement/github-api.ts +214 -0
- package/src/application/improvement/github-context.test.ts +55 -0
- package/src/application/improvement/github-context.ts +67 -0
- package/src/application/improvement/scrub-terms.ts +76 -0
- package/src/application/improvement/upstream.test.ts +70 -0
- package/src/application/improvement/upstream.ts +43 -0
- package/src/commands/evolve.test.ts +153 -0
- package/src/commands/improve-kit.test.ts +126 -0
- package/src/commands/improve-kit.ts +93 -0
- package/src/commands/improve-project.test.ts +194 -0
- package/src/commands/improve-project.ts +192 -0
- package/src/commands/improve-review.test.ts +126 -0
- package/src/commands/improve-review.ts +75 -0
- package/src/commands/improve.test.ts +121 -0
- package/src/commands/improve.ts +215 -0
- package/src/commands/journal.test.ts +150 -0
- package/src/commands/memory.test.ts +146 -0
- package/src/commands/optimize.test.ts +53 -0
- package/src/commands/refactor.test.ts +57 -0
- package/src/index.ts +18 -2
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// packages/cli/src/commands/improve-project.ts
|
|
2
|
+
//
|
|
3
|
+
// `kit improve issues --target project` (ADR-0028 P2.3). Findings about the user's own repository
|
|
4
|
+
// become issues in that same repository, never upstream. The default is a dry run that needs no
|
|
5
|
+
// token and makes no GitHub call; --sync writes. Writes stop at the first failure and are never
|
|
6
|
+
// retried, so a re-run finds anything already written by its key instead of filing it twice.
|
|
7
|
+
// Exit codes: 0 done, 1 some issue was withheld by the privacy gate, 2 usage or runtime error.
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { improvement } from '@agentic-sdlc-kit/core';
|
|
10
|
+
import type { CommandContext, CommandResult } from './types.js';
|
|
11
|
+
import {
|
|
12
|
+
createGitHubApi,
|
|
13
|
+
type GitHubApi,
|
|
14
|
+
type GitHubApiOptions,
|
|
15
|
+
} from '../application/improvement/github-api.js';
|
|
16
|
+
import { connectGitHub } from '../application/improvement/github-context.js';
|
|
17
|
+
import type { GhRunner } from '../application/improvement/upstream.js';
|
|
18
|
+
|
|
19
|
+
export interface ProjectIssueOptions {
|
|
20
|
+
readonly sync: boolean;
|
|
21
|
+
readonly format: 'json' | 'md';
|
|
22
|
+
readonly repo?: string;
|
|
23
|
+
readonly ignoreFile?: string;
|
|
24
|
+
readonly extraIgnores: ReadonlyArray<string>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ProjectIssueDeps {
|
|
28
|
+
readonly gh: GhRunner;
|
|
29
|
+
readonly env: NodeJS.ProcessEnv;
|
|
30
|
+
readonly github: (opts: GitHubApiOptions) => GitHubApi;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const defaultGitHub = (opts: GitHubApiOptions): GitHubApi => createGitHubApi(opts);
|
|
34
|
+
|
|
35
|
+
interface Entry {
|
|
36
|
+
readonly kind: string;
|
|
37
|
+
readonly key: string;
|
|
38
|
+
readonly title: string;
|
|
39
|
+
readonly issue?: number;
|
|
40
|
+
readonly url?: string;
|
|
41
|
+
readonly rules?: ReadonlyArray<string>;
|
|
42
|
+
readonly labels?: ReadonlyArray<string>;
|
|
43
|
+
readonly body?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface Outcome {
|
|
47
|
+
readonly entries: Entry[];
|
|
48
|
+
readonly error?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function draftsFor(root: string, opts: ProjectIssueOptions) {
|
|
52
|
+
const report = await improvement.scanRepo({
|
|
53
|
+
root,
|
|
54
|
+
...(opts.ignoreFile !== undefined ? { ignoreFile: opts.ignoreFile } : {}),
|
|
55
|
+
extraIgnores: opts.extraIgnores,
|
|
56
|
+
});
|
|
57
|
+
return improvement.groupFindings(report.findings).map((g) => improvement.renderProjectIssue(g));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A dry run shows each draft as written; it cannot know which issues already exist. */
|
|
61
|
+
function dryRunEntry(a: improvement.SyncAction): Entry {
|
|
62
|
+
if (a.kind !== 'create') return a;
|
|
63
|
+
return { kind: 'draft', key: a.key, title: a.title, labels: a.draft.labels, body: a.draft.body };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function linkedEntry(a: improvement.SyncAction): Entry {
|
|
67
|
+
if (a.kind === 'blocked') return a;
|
|
68
|
+
if (a.kind === 'create' || a.kind === 'update')
|
|
69
|
+
return { kind: a.kind, key: a.key, title: a.title };
|
|
70
|
+
return { kind: a.kind, key: a.key, title: a.title, issue: a.issue, url: a.url };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function apply(
|
|
74
|
+
api: GitHubApi,
|
|
75
|
+
plan: ReadonlyArray<improvement.SyncAction>,
|
|
76
|
+
): Promise<Outcome> {
|
|
77
|
+
const entries: Entry[] = [];
|
|
78
|
+
for (const a of plan) {
|
|
79
|
+
try {
|
|
80
|
+
if (a.kind === 'create') {
|
|
81
|
+
const i = await api.createIssue({
|
|
82
|
+
title: a.draft.title,
|
|
83
|
+
body: a.draft.body,
|
|
84
|
+
labels: a.draft.labels,
|
|
85
|
+
});
|
|
86
|
+
entries.push({ kind: 'created', key: a.key, title: a.title, issue: i.number, url: i.url });
|
|
87
|
+
} else if (a.kind === 'update') {
|
|
88
|
+
await api.updateIssueBody(a.issue, a.draft.body);
|
|
89
|
+
entries.push({ kind: 'updated', key: a.key, title: a.title, issue: a.issue, url: a.url });
|
|
90
|
+
} else entries.push(linkedEntry(a));
|
|
91
|
+
} catch (err) {
|
|
92
|
+
return { entries, error: `sync stopped: ${(err as Error).message}` };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return { entries };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function summary(entries: ReadonlyArray<Entry>): Record<string, number> {
|
|
99
|
+
const counts: Record<string, number> = {};
|
|
100
|
+
for (const e of entries) counts[e.kind] = (counts[e.kind] ?? 0) + 1;
|
|
101
|
+
return counts;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function markdown(mode: string, repo: string | undefined, entries: ReadonlyArray<Entry>): string {
|
|
105
|
+
const head = `kit improve issues --target project (${mode}${repo ? `, ${repo}` : ''})`;
|
|
106
|
+
const lines = entries.map((e) => {
|
|
107
|
+
const ref = e.issue !== undefined ? ` #${e.issue}` : '';
|
|
108
|
+
const why = e.rules ? ` (blocked by ${e.rules.join(', ')})` : '';
|
|
109
|
+
return `- ${e.kind}${ref}: ${e.title}${why}`;
|
|
110
|
+
});
|
|
111
|
+
const counts = Object.entries(summary(entries)).map(([k, n]) => `${n} ${k}`);
|
|
112
|
+
return [
|
|
113
|
+
head,
|
|
114
|
+
'',
|
|
115
|
+
...(lines.length > 0 ? lines : ['- nothing to report']),
|
|
116
|
+
'',
|
|
117
|
+
`Summary: ${counts.join(', ') || 'none'}`,
|
|
118
|
+
'',
|
|
119
|
+
].join('\n');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function emit(
|
|
123
|
+
ctx: CommandContext,
|
|
124
|
+
opts: ProjectIssueOptions,
|
|
125
|
+
mode: string,
|
|
126
|
+
repo: string | undefined,
|
|
127
|
+
entries: Entry[],
|
|
128
|
+
): void {
|
|
129
|
+
if (opts.format === 'md') ctx.stdout(markdown(mode, repo, entries));
|
|
130
|
+
else
|
|
131
|
+
ctx.stdout(
|
|
132
|
+
`${JSON.stringify({ mode, ...(repo ? { repo } : {}), summary: summary(entries), entries }, null, 2)}\n`,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function finish(ctx: CommandContext, entries: ReadonlyArray<Entry>, error?: string): CommandResult {
|
|
137
|
+
if (error !== undefined) {
|
|
138
|
+
ctx.stderr(`kit improve: ${error}\n`);
|
|
139
|
+
return { exitCode: 2, message: error };
|
|
140
|
+
}
|
|
141
|
+
const blocked = entries.filter((e) => e.kind === 'blocked').length;
|
|
142
|
+
if (blocked > 0)
|
|
143
|
+
ctx.stderr(
|
|
144
|
+
`kit improve: ${blocked} issue(s) withheld by the privacy gate; see the blocked entries.\n`,
|
|
145
|
+
);
|
|
146
|
+
return { exitCode: blocked > 0 ? 1 : 0 };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
type Drafts = ReturnType<typeof improvement.renderProjectIssue>[];
|
|
150
|
+
|
|
151
|
+
/** Drafts, or the error line to report. Error class and first line only: never file contents. */
|
|
152
|
+
async function tryDrafts(root: string, opts: ProjectIssueOptions): Promise<Drafts | string> {
|
|
153
|
+
try {
|
|
154
|
+
return await draftsFor(path.resolve(root), opts);
|
|
155
|
+
} catch (err) {
|
|
156
|
+
const e = err as NodeJS.ErrnoException;
|
|
157
|
+
return `scan failed: ${e.code ?? e.name}: ${(e.message ?? '').split('\n')[0] ?? ''}`;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function sync(
|
|
162
|
+
ctx: CommandContext,
|
|
163
|
+
opts: ProjectIssueOptions,
|
|
164
|
+
deps: ProjectIssueDeps,
|
|
165
|
+
drafts: Drafts,
|
|
166
|
+
) {
|
|
167
|
+
const conn = await connectGitHub(ctx.cwd, opts.repo, deps);
|
|
168
|
+
if (typeof conn === 'string') return finish(ctx, [], conn);
|
|
169
|
+
let existing;
|
|
170
|
+
try {
|
|
171
|
+
existing = await conn.api.listIssuesByLabel(improvement.DISCOVERED_LABEL);
|
|
172
|
+
} catch (err) {
|
|
173
|
+
return finish(ctx, [], `sync stopped: ${(err as Error).message}`);
|
|
174
|
+
}
|
|
175
|
+
const outcome = await apply(conn.api, improvement.planIssueSync(drafts, existing));
|
|
176
|
+
emit(ctx, opts, 'sync', conn.slug, outcome.entries);
|
|
177
|
+
return finish(ctx, outcome.entries, outcome.error);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export async function projectIssues(
|
|
181
|
+
ctx: CommandContext,
|
|
182
|
+
root: string,
|
|
183
|
+
opts: ProjectIssueOptions,
|
|
184
|
+
deps: ProjectIssueDeps,
|
|
185
|
+
): Promise<CommandResult> {
|
|
186
|
+
const drafts = await tryDrafts(root, opts);
|
|
187
|
+
if (typeof drafts === 'string') return finish(ctx, [], drafts);
|
|
188
|
+
if (opts.sync) return sync(ctx, opts, deps, drafts);
|
|
189
|
+
const entries = improvement.planIssueSync(drafts, []).map(dryRunEntry);
|
|
190
|
+
emit(ctx, opts, 'dry-run', undefined, entries);
|
|
191
|
+
return finish(ctx, entries);
|
|
192
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
import { REVIEW_KINDS } from '@agentic-sdlc-kit/contracts';
|
|
6
|
+
import { createImproveCommand } from './improve.js';
|
|
7
|
+
import type { CommandContext } from './types.js';
|
|
8
|
+
import { GitHubApiError, type GitHubApi } from '../application/improvement/github-api.js';
|
|
9
|
+
import type { GhRunner } from '../application/improvement/upstream.js';
|
|
10
|
+
|
|
11
|
+
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../..');
|
|
12
|
+
const ENV = { GITHUB_TOKEN: 'test-token', GITHUB_REPOSITORY: 'acme/app' };
|
|
13
|
+
const noGh: GhRunner = async () => ({ code: 127, stdout: '', stderr: '' });
|
|
14
|
+
|
|
15
|
+
interface Dispatch {
|
|
16
|
+
readonly file: string;
|
|
17
|
+
readonly ref: string;
|
|
18
|
+
readonly inputs: Readonly<Record<string, string>>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function fakeGitHub(fail?: GitHubApiError): { api: GitHubApi; dispatched: Dispatch[] } {
|
|
22
|
+
const dispatched: Dispatch[] = [];
|
|
23
|
+
const unused = async (): Promise<never> => {
|
|
24
|
+
throw new Error('not used by review');
|
|
25
|
+
};
|
|
26
|
+
const api: GitHubApi = {
|
|
27
|
+
listIssuesByLabel: unused,
|
|
28
|
+
createIssue: unused,
|
|
29
|
+
updateIssueBody: unused,
|
|
30
|
+
permissionOf: unused,
|
|
31
|
+
defaultBranch: async () => 'trunk',
|
|
32
|
+
dispatchWorkflow: async (file, ref, inputs) => {
|
|
33
|
+
if (fail) throw fail;
|
|
34
|
+
dispatched.push({ file, ref, inputs });
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
return { api, dispatched };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function review(argv: string[], fake = fakeGitHub()) {
|
|
41
|
+
let out = '';
|
|
42
|
+
let err = '';
|
|
43
|
+
const ctx: CommandContext = {
|
|
44
|
+
argv: ['review', ...argv],
|
|
45
|
+
cwd: ROOT,
|
|
46
|
+
stdout: (s) => (out += s),
|
|
47
|
+
stderr: (s) => (err += s),
|
|
48
|
+
};
|
|
49
|
+
const cmd = createImproveCommand({ gh: noGh, env: ENV, github: () => fake.api });
|
|
50
|
+
const { exitCode } = await cmd.run(ctx);
|
|
51
|
+
return { exitCode, out, err, dispatched: fake.dispatched };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
describe('kit improve review', () => {
|
|
55
|
+
it('shows the request and sends nothing without --yes, because reviews cost money', async () => {
|
|
56
|
+
const r = await review(['--pr', '12', '--kind', 'security']);
|
|
57
|
+
expect(r.exitCode).toBe(0);
|
|
58
|
+
expect(r.out).toContain('a security review of PR #12 on acme/app');
|
|
59
|
+
expect(r.out).toContain('--yes');
|
|
60
|
+
expect(r.dispatched).toEqual([]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('dispatches the gated review workflow on the default branch with --yes', async () => {
|
|
64
|
+
const r = await review(['--pr', '12', '--yes']);
|
|
65
|
+
expect(r.exitCode).toBe(0);
|
|
66
|
+
expect(r.dispatched).toEqual([
|
|
67
|
+
{ file: 'ai-review.yml', ref: 'trunk', inputs: { pr_number: '12', kind: 'correctness' } },
|
|
68
|
+
]);
|
|
69
|
+
expect(r.out).toContain('review gate');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('passes the instruction for a custom review only', async () => {
|
|
73
|
+
const r = await review([
|
|
74
|
+
'--pr',
|
|
75
|
+
'3',
|
|
76
|
+
'--kind',
|
|
77
|
+
'custom',
|
|
78
|
+
'--instruction',
|
|
79
|
+
'check retry limits',
|
|
80
|
+
'--yes',
|
|
81
|
+
]);
|
|
82
|
+
expect(r.dispatched[0]?.inputs).toEqual({
|
|
83
|
+
pr_number: '3',
|
|
84
|
+
kind: 'custom',
|
|
85
|
+
instruction: 'check retry limits',
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('rejects an invalid request before any GitHub call', async () => {
|
|
90
|
+
for (const argv of [
|
|
91
|
+
[],
|
|
92
|
+
['--pr', 'abc'],
|
|
93
|
+
['--pr', '3', '--kind', 'style'],
|
|
94
|
+
['--pr', '3', '--kind', 'custom'],
|
|
95
|
+
['--pr', '3', '--kind', 'security', '--instruction', 'x'],
|
|
96
|
+
]) {
|
|
97
|
+
const r = await review([...argv, '--yes']);
|
|
98
|
+
expect(r.exitCode).toBe(2);
|
|
99
|
+
expect(r.dispatched).toEqual([]);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('reports a refused dispatch with exit 2', async () => {
|
|
104
|
+
const fake = fakeGitHub(
|
|
105
|
+
new GitHubApiError(
|
|
106
|
+
403,
|
|
107
|
+
'GitHub refused POST (403): the token lacks the permission it needs.',
|
|
108
|
+
),
|
|
109
|
+
);
|
|
110
|
+
const r = await review(['--pr', '12', '--yes'], fake);
|
|
111
|
+
expect(r.exitCode).toBe(2);
|
|
112
|
+
expect(r.err).toMatch(/403/);
|
|
113
|
+
expect(r.err).toMatch(/actions: write/);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('review kinds stay in step', () => {
|
|
118
|
+
it('matches ci-agent and the ai-review workflow choices', async () => {
|
|
119
|
+
const agent = await readFile(path.join(ROOT, 'tools/ci-agent/src/review/request.ts'), 'utf8');
|
|
120
|
+
const block = /REVIEW_KINDS = \[([^\]]*)\]/.exec(agent)?.[1] ?? '';
|
|
121
|
+
expect([...block.matchAll(/'([a-z]+)'/g)].map((m) => m[1])).toEqual([...REVIEW_KINDS]);
|
|
122
|
+
const workflow = await readFile(path.join(ROOT, '.github/workflows/ai-review.yml'), 'utf8');
|
|
123
|
+
const options = /options: \[([^\]]*)\]/.exec(workflow)?.[1] ?? '';
|
|
124
|
+
expect(options.split(',').map((s) => s.trim())).toEqual([...REVIEW_KINDS]);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// packages/cli/src/commands/improve-review.ts
|
|
2
|
+
//
|
|
3
|
+
// `kit improve review` (ADR-0028 §4, P2.4). The kit has no review engine: this asks ci-agent's
|
|
4
|
+
// `ai-review.yml` workflow for one focused review. That workflow runs the review gate (open, not a
|
|
5
|
+
// draft, same repository, every other check green, kind not yet reviewed at this head), so a
|
|
6
|
+
// request can never bypass it. Reviews cost money, so nothing is sent without --yes. Review output
|
|
7
|
+
// stays a PR comment; nothing here turns model output into an issue.
|
|
8
|
+
import { ReviewRequestSchema, type ReviewRequest } from '@agentic-sdlc-kit/contracts';
|
|
9
|
+
import type { CommandContext, CommandResult } from './types.js';
|
|
10
|
+
import { connectGitHub } from '../application/improvement/github-context.js';
|
|
11
|
+
import type { ProjectIssueDeps } from './improve-project.js';
|
|
12
|
+
|
|
13
|
+
export const REVIEW_WORKFLOW = 'ai-review.yml';
|
|
14
|
+
|
|
15
|
+
export interface ReviewOptions {
|
|
16
|
+
readonly pr?: string;
|
|
17
|
+
readonly kind?: string;
|
|
18
|
+
readonly instruction?: string;
|
|
19
|
+
readonly repo?: string;
|
|
20
|
+
readonly yes: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function parseRequest(opts: ReviewOptions): ReviewRequest | string {
|
|
24
|
+
if (opts.pr === undefined) return 'review needs --pr <number>';
|
|
25
|
+
const parsed = ReviewRequestSchema.safeParse({
|
|
26
|
+
pr: /^\d+$/.test(opts.pr) ? Number(opts.pr) : Number.NaN,
|
|
27
|
+
kind: opts.kind ?? 'correctness',
|
|
28
|
+
...(opts.instruction !== undefined ? { instruction: opts.instruction } : {}),
|
|
29
|
+
});
|
|
30
|
+
if (parsed.success) return parsed.data;
|
|
31
|
+
const issue = parsed.error.issues[0];
|
|
32
|
+
return `invalid review request: ${issue?.path.join('.') || 'request'}: ${issue?.message ?? 'invalid'}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function inputsOf(r: ReviewRequest): Record<string, string> {
|
|
36
|
+
const inputs: Record<string, string> = { pr_number: String(r.pr), kind: r.kind };
|
|
37
|
+
if (r.instruction !== undefined) inputs.instruction = r.instruction;
|
|
38
|
+
return inputs;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function refuse(ctx: CommandContext, message: string): CommandResult {
|
|
42
|
+
ctx.stderr(`kit improve: ${message}\n`);
|
|
43
|
+
return { exitCode: 2, message };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function requestReview(
|
|
47
|
+
ctx: CommandContext,
|
|
48
|
+
opts: ReviewOptions,
|
|
49
|
+
deps: ProjectIssueDeps,
|
|
50
|
+
): Promise<CommandResult> {
|
|
51
|
+
const request = parseRequest(opts);
|
|
52
|
+
if (typeof request === 'string') return refuse(ctx, request);
|
|
53
|
+
const conn = await connectGitHub(ctx.cwd, opts.repo, deps);
|
|
54
|
+
if (typeof conn === 'string') return refuse(ctx, conn);
|
|
55
|
+
const what = `a ${request.kind} review of PR #${request.pr} on ${conn.slug}`;
|
|
56
|
+
if (!opts.yes) {
|
|
57
|
+
ctx.stdout(
|
|
58
|
+
`This would request ${what}.\nAI reviews cost money. Run again with --yes to send it.\n`,
|
|
59
|
+
);
|
|
60
|
+
return { exitCode: 0 };
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
const ref = await conn.api.defaultBranch();
|
|
64
|
+
await conn.api.dispatchWorkflow(REVIEW_WORKFLOW, ref, inputsOf(request));
|
|
65
|
+
} catch (err) {
|
|
66
|
+
return refuse(
|
|
67
|
+
ctx,
|
|
68
|
+
`${(err as Error).message} Dispatching ${REVIEW_WORKFLOW} needs a token with actions: write.`,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
ctx.stdout(
|
|
72
|
+
`Requested ${what}.\nIt runs through the review gate; if the gate skips it, the run summary says why.\n`,
|
|
73
|
+
);
|
|
74
|
+
return { exitCode: 0 };
|
|
75
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
7
|
+
import { ScanReportSchema } from '@agentic-sdlc-kit/contracts';
|
|
8
|
+
import { improveCommand } from './improve.js';
|
|
9
|
+
import type { CommandContext } from './types.js';
|
|
10
|
+
|
|
11
|
+
const run = promisify(execFile);
|
|
12
|
+
const SECRET = 'ABCPE1234F';
|
|
13
|
+
|
|
14
|
+
let dir: string;
|
|
15
|
+
|
|
16
|
+
async function makeRepo(files: Record<string, string>): Promise<void> {
|
|
17
|
+
await run('git', ['-C', dir, 'init', '-q']);
|
|
18
|
+
for (const [rel, text] of Object.entries(files)) {
|
|
19
|
+
await mkdir(path.dirname(path.join(dir, rel)), { recursive: true });
|
|
20
|
+
await writeFile(path.join(dir, rel), text);
|
|
21
|
+
}
|
|
22
|
+
await run('git', ['-C', dir, 'add', '-A']);
|
|
23
|
+
await run('git', [
|
|
24
|
+
'-C',
|
|
25
|
+
dir,
|
|
26
|
+
'-c',
|
|
27
|
+
'user.email=t@example.test',
|
|
28
|
+
'-c',
|
|
29
|
+
'user.name=t',
|
|
30
|
+
'commit',
|
|
31
|
+
'-q',
|
|
32
|
+
'-m',
|
|
33
|
+
'init',
|
|
34
|
+
]);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function invoke(argv: string[]): Promise<{ exitCode: number; out: string; err: string }> {
|
|
38
|
+
let out = '';
|
|
39
|
+
let err = '';
|
|
40
|
+
const ctx: CommandContext = {
|
|
41
|
+
argv,
|
|
42
|
+
cwd: dir,
|
|
43
|
+
stdout: (s) => (out += s),
|
|
44
|
+
stderr: (s) => (err += s),
|
|
45
|
+
};
|
|
46
|
+
const result = await improveCommand.run(ctx);
|
|
47
|
+
return { exitCode: result.exitCode, out, err };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
beforeEach(async () => {
|
|
51
|
+
dir = await mkdtemp(path.join(os.tmpdir(), 'kit-improve-'));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
afterEach(async () => {
|
|
55
|
+
await rm(dir, { recursive: true, force: true });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('kit improve scan', () => {
|
|
59
|
+
it('prints a schema-valid JSON report and exits 0 without --fail-on-findings', async () => {
|
|
60
|
+
await makeRepo({ 'a.txt': `pan ${SECRET}\n` });
|
|
61
|
+
const r = await invoke(['scan']);
|
|
62
|
+
expect(r.exitCode).toBe(0);
|
|
63
|
+
const report = JSON.parse(r.out);
|
|
64
|
+
expect(ScanReportSchema.safeParse(report).success).toBe(true);
|
|
65
|
+
expect(report.findings).toHaveLength(1);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('exits 1 with --fail-on-findings when a finding is reported', async () => {
|
|
69
|
+
await makeRepo({ 'a.txt': `pan ${SECRET}\n` });
|
|
70
|
+
const r = await invoke(['scan', '--fail-on-findings']);
|
|
71
|
+
expect(r.exitCode).toBe(1);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('never prints the matched value in any format', async () => {
|
|
75
|
+
await makeRepo({ 'a.txt': `pan ${SECRET}\n` });
|
|
76
|
+
const json = await invoke(['scan', '--format', 'json']);
|
|
77
|
+
const md = await invoke(['scan', '--format', 'md']);
|
|
78
|
+
expect(json.out.includes(SECRET)).toBe(false);
|
|
79
|
+
expect(md.out.includes(SECRET)).toBe(false);
|
|
80
|
+
expect(md.out).toMatch(/Findings: \*\*1\*\*/);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('says "No findings" for a clean repository', async () => {
|
|
84
|
+
await makeRepo({ 'a.txt': 'clean\n' });
|
|
85
|
+
const r = await invoke(['scan', '--format', 'md']);
|
|
86
|
+
expect(r.exitCode).toBe(0);
|
|
87
|
+
expect(r.out).toMatch(/No findings/);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('honours --ignore globs given on the command line', async () => {
|
|
91
|
+
await makeRepo({ 'docs/x.md': `pan ${SECRET}\n` });
|
|
92
|
+
const r = await invoke(['scan', '--ignore', 'docs/**', '--fail-on-findings']);
|
|
93
|
+
expect(r.exitCode).toBe(0);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('rejects an unknown --format with exit 2 and a message', async () => {
|
|
97
|
+
await makeRepo({ 'a.txt': 'clean\n' });
|
|
98
|
+
const r = await invoke(['scan', '--format', 'xml']);
|
|
99
|
+
expect(r.exitCode).toBe(2);
|
|
100
|
+
expect(r.err).toMatch(/unknown --format/);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('reports a git failure as exit 2 with no values', async () => {
|
|
104
|
+
const r = await invoke(['scan', '--changed-only', 'no-such-ref']);
|
|
105
|
+
expect(r.exitCode).toBe(2);
|
|
106
|
+
expect(r.err).toMatch(/scan failed/);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('kit improve dispatch', () => {
|
|
111
|
+
it('prints help with exit 0 when asked', async () => {
|
|
112
|
+
const r = await invoke(['--help']);
|
|
113
|
+
expect(r.exitCode).toBe(0);
|
|
114
|
+
expect(r.out).toMatch(/kit improve scan/);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('exits 2 with no subcommand, and 2 for an unknown subcommand', async () => {
|
|
118
|
+
expect((await invoke([])).exitCode).toBe(2);
|
|
119
|
+
expect((await invoke(['frobnicate'])).exitCode).toBe(2);
|
|
120
|
+
});
|
|
121
|
+
});
|