Skip to content

Commit 46456f0

Browse files
committed
feat: add support for stdin JSON input
1 parent a05e0b3 commit 46456f0

File tree

3 files changed

+269
-31
lines changed

3 files changed

+269
-31
lines changed

bin/cmd.js

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,21 @@ if (parsed.version) {
6565
if (!parsed.help && !args.length) { args.push('HEAD') }
6666

6767
function load (sha, cb) {
68-
try {
69-
const parsed = new URL(sha)
70-
return loadPatch(parsed, cb)
71-
} catch (_) {
72-
exec(`git show --quiet --format=medium ${sha}`, (err, stdout, stderr) => {
73-
if (err) return cb(err)
74-
cb(null, stdout.trim())
68+
// Handle pre-parsed commit objects from stdin
69+
if (typeof sha === 'object' && sha.id && sha.message) {
70+
return process.nextTick(() => {
71+
cb(null, sha.message)
7572
})
7673
}
74+
75+
const parsed = URL.parse(sha);
76+
if (parsed != null) {
77+
return loadPatch(parsed, cb)
78+
}
79+
exec(`git show --quiet --format=medium ${sha}`, (err, stdout, stderr) => {
80+
if (err) return cb(err)
81+
cb(null, stdout.trim())
82+
})
7783
}
7884

7985
function loadPatch (uri, cb) {
@@ -121,33 +127,66 @@ if (parsed.list) {
121127
process.exit(0)
122128
}
123129

124-
if (parsed.tap) {
125-
const tap = new Tap()
126-
tap.pipe(process.stdout)
127-
if (parsed.out) tap.pipe(fs.createWriteStream(parsed.out))
128-
let count = 0
129-
const total = args.length
130-
131-
v.on('commit', (c) => {
132-
count++
133-
const test = tap.test(c.commit.sha)
134-
formatTap(test, c.commit, c.messages, v)
135-
if (count === total) {
136-
setImmediate(() => {
137-
tap.end()
138-
if (tap.status === 'fail') { process.exitCode = 1 }
139-
})
130+
// Don't start processing if reading from stdin (handled in stdin.on('end'))
131+
if (args.length === 1 && args[0] === '-') {
132+
const chunks = []
133+
process.stdin.on('data', (chunk) => chunks.push(chunk))
134+
process.stdin.on('end', () => {
135+
try {
136+
const input = Buffer.concat(chunks).toString('utf8')
137+
const commits = JSON.parse(input)
138+
139+
if (!Array.isArray(commits)) {
140+
throw new Error('Input must be an array')
141+
}
142+
143+
// Replace args with the commit data directly
144+
args.splice(0, 1, ...commits.map(commit => {
145+
if (!commit.id || !commit.message) {
146+
throw new Error('Each commit must have "id" and "message" properties')
147+
}
148+
return { sha: commit.id, ...commit }
149+
}))
150+
run()
151+
} catch (err) {
152+
console.error('Error parsing JSON input:', err.message)
153+
process.exit(1)
140154
}
141155
})
142-
143-
tapRun()
156+
process.stdin.resume()
144157
} else {
145-
v.on('commit', (c) => {
146-
pretty(c.commit, c.messages, v)
147-
commitRun()
148-
})
158+
run()
159+
}
149160

150-
commitRun()
161+
function run () {
162+
if (parsed.tap) {
163+
const tap = new Tap()
164+
tap.pipe(process.stdout)
165+
if (parsed.out) tap.pipe(fs.createWriteStream(parsed.out))
166+
let count = 0
167+
const total = args.length
168+
169+
v.on('commit', (c) => {
170+
count++
171+
const test = tap.test(c.commit.sha)
172+
formatTap(test, c.commit, c.messages, v)
173+
if (count === total) {
174+
setImmediate(() => {
175+
tap.end()
176+
if (tap.status === 'fail') { process.exitCode = 1 }
177+
})
178+
}
179+
})
180+
181+
tapRun()
182+
} else {
183+
v.on('commit', (c) => {
184+
pretty(c.commit, c.messages, v)
185+
commitRun()
186+
})
187+
188+
commitRun()
189+
}
151190
}
152191

153192
function tapRun () {

bin/usage.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
core-validate-commit - Validate the commit message for a particular commit in node core
22

3-
usage: core-validate-commit [options] [sha[, sha]]
3+
usage: core-validate-commit [options] [sha[, sha] | -]
44

55
options:
66
-h, --help show help and usage
@@ -26,3 +26,7 @@ core-validate-commit - Validate the commit message for a particular commit in no
2626
Passing a url to a specific sha on github:
2727

2828
$ core-validate-commit https://api.github.com/repos/nodejs/node/git/commits/9e9d499b8be8ffc6050db25129b042507d7b4b02
29+
30+
Passing JSON from stdin (no git required):
31+
32+
$ echo '[{"id":"287bdab","message":"doc: update README"}]' | core-validate-commit -

test/cli-test.js

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,200 @@ test('Test cli flags', (t) => {
152152
})
153153
})
154154

155+
t.test('test stdin with valid JSON', (tt) => {
156+
const validCommit = {
157+
id: 'abc123',
158+
message: 'stream: make null an invalid chunk to write in object mode\n\nthis harmonizes behavior between readable, writable, and transform\nstreams so that they all handle nulls in object mode the same way by\nconsidering them invalid chunks.\n\nPR-URL: https://github.com/nodejs/node/pull/6170\nReviewed-By: James M Snell <jasnell@gmail.com>\nReviewed-By: Matteo Collina <matteo.collina@gmail.com>'
159+
}
160+
const input = JSON.stringify([validCommit])
161+
162+
const ls = spawn('./bin/cmd.js', ['-'])
163+
let compiledData = ''
164+
let errorData = ''
165+
166+
ls.stdout.on('data', (data) => {
167+
compiledData += data
168+
})
169+
170+
ls.stderr.on('data', (data) => {
171+
errorData += data
172+
})
173+
174+
ls.stdin.write(input)
175+
ls.stdin.end()
176+
177+
ls.on('close', (code) => {
178+
tt.equal(code, 0, 'CLI exits with zero code on success')
179+
tt.match(compiledData, /abc123/, 'output contains commit id')
180+
tt.equal(errorData, '', 'no error output')
181+
tt.end()
182+
})
183+
})
184+
185+
t.test('test stdin with invalid commit (missing subsystem)', (tt) => {
186+
const invalidCommit = {
187+
id: 'def456',
188+
message: 'this is a bad commit message without subsystem\n\nPR-URL: https://github.com/nodejs/node/pull/1234\nReviewed-By: Someone <someone@example.com>'
189+
}
190+
const input = JSON.stringify([invalidCommit])
191+
192+
const ls = spawn('./bin/cmd.js', ['-'])
193+
let compiledData = ''
194+
195+
ls.stdout.on('data', (data) => {
196+
compiledData += data
197+
})
198+
199+
ls.stdin.write(input)
200+
ls.stdin.end()
201+
202+
ls.on('close', (code) => {
203+
tt.notEqual(code, 0, 'CLI exits with non-zero code on failure')
204+
tt.match(compiledData, /def456/, 'output contains commit id')
205+
tt.match(compiledData, /title-format/, 'output mentions the rule violation')
206+
tt.end()
207+
})
208+
})
209+
210+
t.test('test stdin with multiple commits', (tt) => {
211+
const commits = [
212+
{
213+
id: 'commit1',
214+
message: 'doc: update README\n\nPR-URL: https://github.com/nodejs/node/pull/1111\nReviewed-By: Someone <someone@example.com>'
215+
},
216+
{
217+
id: 'commit2',
218+
message: 'test: add new test case\n\nPR-URL: https://github.com/nodejs/node/pull/2222\nReviewed-By: Someone <someone@example.com>'
219+
}
220+
]
221+
const input = JSON.stringify(commits)
222+
223+
const ls = spawn('./bin/cmd.js', ['-'])
224+
let compiledData = ''
225+
226+
ls.stdout.on('data', (data) => {
227+
compiledData += data
228+
})
229+
230+
ls.stdin.write(input)
231+
ls.stdin.end()
232+
233+
ls.on('close', (code) => {
234+
tt.equal(code, 0, 'CLI exits with zero code on success')
235+
tt.match(compiledData, /commit1/, 'output contains first commit id')
236+
tt.match(compiledData, /commit2/, 'output contains second commit id')
237+
tt.end()
238+
})
239+
})
240+
241+
t.test('test stdin with TAP output', (tt) => {
242+
const validCommit = {
243+
id: 'tap123',
244+
message: 'doc: update documentation\n\nPR-URL: https://github.com/nodejs/node/pull/5555\nReviewed-By: Someone <someone@example.com>'
245+
}
246+
const input = JSON.stringify([validCommit])
247+
248+
const ls = spawn('./bin/cmd.js', ['--tap', '-'])
249+
let compiledData = ''
250+
251+
ls.stdout.on('data', (data) => {
252+
compiledData += data
253+
})
254+
255+
ls.stdin.write(input)
256+
ls.stdin.end()
257+
258+
ls.on('close', (code) => {
259+
tt.equal(code, 0, 'CLI exits with zero code on success')
260+
tt.match(compiledData, /TAP version 14/, 'output is in TAP format')
261+
tt.match(compiledData, /# tap123/, 'TAP output contains commit id')
262+
tt.end()
263+
})
264+
})
265+
266+
t.test('test stdin with invalid JSON', (tt) => {
267+
const input = 'this is not valid JSON'
268+
269+
const ls = spawn('./bin/cmd.js', ['-'])
270+
let errorData = ''
271+
272+
ls.stderr.on('data', (data) => {
273+
errorData += data
274+
})
275+
276+
ls.stdin.write(input)
277+
ls.stdin.end()
278+
279+
ls.on('close', (code) => {
280+
tt.equal(code, 1, 'CLI exits with non-zero code on error')
281+
tt.match(errorData, /Error parsing JSON input/, 'error message is shown')
282+
tt.end()
283+
})
284+
})
285+
286+
t.test('test stdin with non-array JSON', (tt) => {
287+
const input = JSON.stringify({ id: 'test', message: 'test' })
288+
289+
const ls = spawn('./bin/cmd.js', ['-'])
290+
let errorData = ''
291+
292+
ls.stderr.on('data', (data) => {
293+
errorData += data
294+
})
295+
296+
ls.stdin.write(input)
297+
ls.stdin.end()
298+
299+
ls.on('close', (code) => {
300+
tt.equal(code, 1, 'CLI exits with non-zero code on error')
301+
tt.match(errorData, /Input must be an array/, 'error message is shown')
302+
tt.end()
303+
})
304+
})
305+
306+
t.test('test stdin with missing properties', (tt) => {
307+
const input = JSON.stringify([{ id: 'test' }]) // missing 'message'
308+
309+
const ls = spawn('./bin/cmd.js', ['-'])
310+
let errorData = ''
311+
312+
ls.stderr.on('data', (data) => {
313+
errorData += data
314+
})
315+
316+
ls.stdin.write(input)
317+
ls.stdin.end()
318+
319+
ls.on('close', (code) => {
320+
tt.equal(code, 1, 'CLI exits with non-zero code on error')
321+
tt.match(errorData, /must have "id" and "message" properties/, 'error message is shown')
322+
tt.end()
323+
})
324+
})
325+
326+
t.test('test stdin with --no-validate-metadata', (tt) => {
327+
const commit = {
328+
id: 'novalidate',
329+
message: 'doc: update README\n\nThis commit has no PR-URL or reviewers'
330+
}
331+
const input = JSON.stringify([commit])
332+
333+
const ls = spawn('./bin/cmd.js', ['--no-validate-metadata', '-'])
334+
let compiledData = ''
335+
336+
ls.stdout.on('data', (data) => {
337+
compiledData += data
338+
})
339+
340+
ls.stdin.write(input)
341+
ls.stdin.end()
342+
343+
ls.on('close', (code) => {
344+
tt.equal(code, 0, 'CLI exits with zero code when metadata validation is disabled')
345+
tt.match(compiledData, /novalidate/, 'output contains commit id')
346+
tt.end()
347+
})
348+
})
349+
155350
t.end()
156351
})

0 commit comments

Comments
 (0)