diff --git a/src/api.js b/src/api.js index 75137e5..ea45bc2 100644 --- a/src/api.js +++ b/src/api.js @@ -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); } diff --git a/src/cli.js b/src/cli.js index 5522461..bfd9a2b 100755 --- a/src/cli.js +++ b/src/cli.js @@ -679,6 +679,93 @@ pr } }); +pr + .command('show') + .description('Show details of a pull request') + .requiredOption('-o, --owner ', 'repository owner') + .requiredOption('-r, --repo ', 'repository name') + .requiredOption('-n, --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 ', 'repository owner') + .requiredOption('-r, --repo ', 'repository name') + .requiredOption('-n, --number ', 'pull request number', parseId) + .option('-b, --body ', 'comment body (markdown)') + .option('--body-file ', '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 ', 'repository owner') + .requiredOption('-r, --repo ', 'repository name') + .requiredOption('-n, --number ', 'pull request number', parseId) + .requiredOption('--event ', 'review event: approve, request-changes, comment') + .option('-b, --body ', 'review body (markdown)') + .option('--body-file ', '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'); diff --git a/test/api.test.js b/test/api.test.js index 52eb911..8eb0656 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -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.'); +}); diff --git a/test/cli.test.js b/test/cli.test.js index b3b4a48..77f9826 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -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); + } +});