Add pr show, comment and review commands

Adds CLI commands for inspecting a pull request, posting a comment, and
submitting an APPROVE/REQUEST_CHANGES/COMMENT review. Includes API client
methods, CLI wiring, and tests.
This commit is contained in:
kimi-reviewer-andresmgsl 2026-07-22 21:19:36 +00:00
parent f30f22daf4
commit 8e6907a11e
4 changed files with 167 additions and 0 deletions

View file

@ -167,6 +167,21 @@ class ForgejoClient {
return this.post(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls`, payload);
}
async getPullRequest(owner, repo, index) {
return this.get(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${index}`);
}
async createPullRequestComment(owner, repo, index, body) {
return this.post(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/issues/${index}/comments`, { body });
}
async createPullRequestReview(owner, repo, index, event, body) {
return this.post(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${index}/reviews`, {
event,
body: body || '',
});
}
async mergePullRequest(owner, repo, index, payload) {
return this.post(`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${index}/merge`, payload);
}

View file

@ -679,6 +679,93 @@ pr
}
});
pr
.command('show')
.description('Show details of a pull request')
.requiredOption('-o, --owner <owner>', 'repository owner')
.requiredOption('-r, --repo <repo>', 'repository name')
.requiredOption('-n, --number <number>', 'pull request number', parseId)
.action(async (options) => {
try {
const config = loadConfig();
const client = ForgejoClient.fromConfig(config);
const prData = await client.getPullRequest(options.owner, options.repo, options.number);
console.log(`!${prData.number} [${prData.state}] ${prData.title}`);
console.log(`URL: ${prData.html_url}`);
console.log(`Author: ${prData.user.login}`);
console.log(`Branch: ${prData.head.ref} -> ${prData.base.ref}`);
console.log(`Mergeable: ${prData.mergeable}`);
console.log(`Created: ${prData.created_at}`);
if (prData.body) {
console.log('\n' + prData.body);
}
} catch (err) {
console.error(`Failed to show pull request: ${err.message}`);
if (err.status) console.error(`HTTP status: ${err.status}`);
process.exit(1);
}
});
pr
.command('comment')
.description('Add a comment to a pull request')
.requiredOption('-o, --owner <owner>', 'repository owner')
.requiredOption('-r, --repo <repo>', 'repository name')
.requiredOption('-n, --number <number>', 'pull request number', parseId)
.option('-b, --body <body>', 'comment body (markdown)')
.option('--body-file <path>', 'read the comment body from a file')
.action(async (options) => {
try {
const config = loadConfig();
const client = ForgejoClient.fromConfig(config);
const body = readBodyOption(options);
if (!body) {
console.error('Comment body is required. Use -b/--body or --body-file.');
process.exit(1);
}
const result = await client.createPullRequestComment(options.owner, options.repo, options.number, body);
console.log(`Comment added to !${options.number}.`);
console.log(`URL: ${result.html_url}`);
} catch (err) {
console.error(`Failed to comment on pull request: ${err.message}`);
if (err.status) console.error(`HTTP status: ${err.status}`);
process.exit(1);
}
});
pr
.command('review')
.description('Submit a review on a pull request')
.requiredOption('-o, --owner <owner>', 'repository owner')
.requiredOption('-r, --repo <repo>', 'repository name')
.requiredOption('-n, --number <number>', 'pull request number', parseId)
.requiredOption('--event <event>', 'review event: approve, request-changes, comment')
.option('-b, --body <body>', 'review body (markdown)')
.option('--body-file <path>', 'read the review body from a file')
.action(async (options) => {
try {
const config = loadConfig();
const client = ForgejoClient.fromConfig(config);
const eventMap = {
approve: 'APPROVE',
'request-changes': 'REQUEST_CHANGES',
comment: 'COMMENT',
};
const event = eventMap[options.event.toLowerCase()];
if (!event) {
console.error(`Invalid review event: ${options.event}. Must be approve, request-changes, or comment.`);
process.exit(1);
}
const body = readBodyOption(options) || '';
await client.createPullRequestReview(options.owner, options.repo, options.number, event, body);
console.log(`Review submitted on !${options.number}: ${event}.`);
} catch (err) {
console.error(`Failed to submit review: ${err.message}`);
if (err.status) console.error(`HTTP status: ${err.status}`);
process.exit(1);
}
});
const branchCmd = program
.command('branch')
.description('Manage branches');

View file

@ -141,3 +141,32 @@ test('createIssue and createPullRequest hit the expected endpoints', async () =>
assert.equal(calls[0].opts.method, 'POST');
assert.equal(JSON.parse(calls[1].opts.body).head, 'h');
});
test('getPullRequest fetches a single pull request', async () => {
const calls = mockFetch(() => jsonResponse({ number: 7, title: 'Fix' }));
const client = new ForgejoClient('https://forge.test', 'tok');
const pr = await client.getPullRequest('owner', 'repo', 7);
assert.equal(pr.number, 7);
assert.equal(calls[0].url, 'https://forge.test/api/v1/repos/owner/repo/pulls/7');
assert.equal(calls[0].opts.method, 'GET');
});
test('createPullRequestComment posts to the issue comments endpoint', async () => {
const calls = mockFetch(() => jsonResponse({ id: 99, html_url: 'https://forge.test/comment/99' }));
const client = new ForgejoClient('https://forge.test', 'tok');
await client.createPullRequestComment('owner', 'repo', 7, 'Looks good.');
assert.equal(calls[0].url, 'https://forge.test/api/v1/repos/owner/repo/issues/7/comments');
assert.equal(calls[0].opts.method, 'POST');
assert.equal(JSON.parse(calls[0].opts.body).body, 'Looks good.');
});
test('createPullRequestReview posts the review event and body', async () => {
const calls = mockFetch(() => jsonResponse({ id: 88 }));
const client = new ForgejoClient('https://forge.test', 'tok');
await client.createPullRequestReview('owner', 'repo', 7, 'APPROVE', 'Ship it.');
assert.equal(calls[0].url, 'https://forge.test/api/v1/repos/owner/repo/pulls/7/reviews');
assert.equal(calls[0].opts.method, 'POST');
const body = JSON.parse(calls[0].opts.body);
assert.equal(body.event, 'APPROVE');
assert.equal(body.body, 'Ship it.');
});

View file

@ -75,3 +75,39 @@ test('issue create --body-file reports unreadable files cleanly', () => {
fs.unlinkSync(cfg);
}
});
test('pr show validates --number before any network call', () => {
const cfg = path.join(os.tmpdir(), `stoke-cfg-${process.pid}.json`);
fs.writeFileSync(cfg, JSON.stringify({ url: 'https://forge.test', token: 'tok' }));
try {
const res = run(['pr', 'show', '-o', 'o', '-r', 'r', '-n', 'zero'], { STOKE_CONFIG_FILE: cfg });
assert.equal(res.status, 1);
assert.match(res.stderr, /Id must be a positive integer/);
} finally {
fs.unlinkSync(cfg);
}
});
test('pr comment rejects a missing body before any network call', () => {
const cfg = path.join(os.tmpdir(), `stoke-cfg-${process.pid}.json`);
fs.writeFileSync(cfg, JSON.stringify({ url: 'https://forge.test', token: 'tok' }));
try {
const res = run(['pr', 'comment', '-o', 'o', '-r', 'r', '-n', '1'], { STOKE_CONFIG_FILE: cfg });
assert.equal(res.status, 1);
assert.match(res.stderr, /Comment body is required/);
} finally {
fs.unlinkSync(cfg);
}
});
test('pr review rejects an invalid event before any network call', () => {
const cfg = path.join(os.tmpdir(), `stoke-cfg-${process.pid}.json`);
fs.writeFileSync(cfg, JSON.stringify({ url: 'https://forge.test', token: 'tok' }));
try {
const res = run(['pr', 'review', '-o', 'o', '-r', 'r', '-n', '1', '--event', 'nope'], { STOKE_CONFIG_FILE: cfg });
assert.equal(res.status, 1);
assert.match(res.stderr, /Invalid review event/);
} finally {
fs.unlinkSync(cfg);
}
});