diff --git a/README.md b/README.md index 979387d..4bbc089 100644 --- a/README.md +++ b/README.md @@ -483,7 +483,7 @@ Options: -o, --owner repository owner (required) -r, --repo repository name (required) -n, --number pull request number (required) - --event review event: approve, request-changes, comment (required) + --event review event: approve, request-changes|request_changes, comment (required) -b, --body review body (markdown) --body-file read the review body from a file ``` diff --git a/src/cli.js b/src/cli.js index 36497e6..954f0ae 100755 --- a/src/cli.js +++ b/src/cli.js @@ -718,12 +718,12 @@ pr try { const config = loadConfig(); const client = ForgejoClient.fromConfig(config); - const body = readBodyOption(options); - if (!body) { + const rawBody = readBodyOption(options) || ''; + if (rawBody.trim().length === 0) { 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); + const result = await client.createPullRequestComment(options.owner, options.repo, options.number, rawBody); console.log(`Comment added to !${options.number}.`); console.log(`URL: ${result.html_url}`); } catch (err) { @@ -739,7 +739,7 @@ pr .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') + .requiredOption('--event ', 'review event: approve, request-changes|request_changes, comment') .option('-b, --body ', 'review body (markdown)') .option('--body-file ', 'read the review body from a file') .action(async (options) => { @@ -749,11 +749,12 @@ pr const eventMap = { approve: 'APPROVED', 'request-changes': 'REQUEST_CHANGES', + 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.`); + console.error(`Invalid review event: ${options.event}. Must be approve, request-changes (or request_changes), or comment.`); process.exit(1); } const rawBody = readBodyOption(options) || ''; diff --git a/test/cli.test.js b/test/cli.test.js index 28a3aff..3de1b83 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -1,7 +1,8 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); -const { execFileSync, spawnSync } = require('node:child_process'); +const { execFileSync, spawn, spawnSync } = require('node:child_process'); const fs = require('node:fs'); +const http = require('node:http'); const os = require('node:os'); const path = require('node:path'); @@ -148,3 +149,66 @@ test('pr review approve allows an empty body before any network call', () => { fs.unlinkSync(cfg); } }); + +function spawnAsync(args, env = {}) { + return new Promise((resolve, reject) => { + const child = spawn(process.execPath, [CLI, ...args], { + env: { ...process.env, ...env }, + }); + let stdout = ''; + let stderr = ''; + child.stdout.setEncoding('utf8'); + child.stderr.setEncoding('utf8'); + child.stdout.on('data', (chunk) => { stdout += chunk; }); + child.stderr.on('data', (chunk) => { stderr += chunk; }); + child.on('error', reject); + child.on('close', (status) => resolve({ status, stdout, stderr })); + }); +} + +test('pr review preserves exact body-file whitespace through the CLI boundary', async () => { + const cfg = path.join(os.tmpdir(), `stoke-cfg-${process.pid}.json`); + const bodyFile = path.join(os.tmpdir(), `stoke-body-${process.pid}.md`); + const rawBody = ' leading spaces\nline\ntrailing newline\n'; + + fs.writeFileSync(bodyFile, rawBody, 'utf8'); + + const captured = await new Promise((resolve, reject) => { + const server = http.createServer((req, res) => { + let data = ''; + req.setEncoding('utf8'); + req.on('data', (chunk) => { data += chunk; }); + req.on('end', () => { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ id: 99 })); + server.close(() => resolve({ url: req.url, body: data })); + }); + }); + + server.listen(0, '127.0.0.1', async () => { + const { port } = server.address(); + fs.writeFileSync(cfg, JSON.stringify({ url: `http://127.0.0.1:${port}`, token: 'tok' })); + try { + const res = await spawnAsync( + ['pr', 'review', '-o', 'o', '-r', 'r', '-n', '7', '--event', 'request-changes', '--body-file', bodyFile], + { STOKE_CONFIG_FILE: cfg }, + ); + if (res.status !== 0) { + server.close(() => reject(new Error(`CLI failed: ${res.stderr}`))); + } + } catch (err) { + server.close(() => reject(err)); + } + }); + }); + + try { + assert.equal(captured.url, '/api/v1/repos/o/r/pulls/7/reviews'); + const json = JSON.parse(captured.body); + assert.equal(json.event, 'REQUEST_CHANGES'); + assert.equal(json.body, rawBody); + } finally { + fs.unlinkSync(cfg); + fs.unlinkSync(bodyFile); + } +});