Skip to content

Commit 3200730

Browse files
feat: add support for stdin JSON input (#135)
Co-authored-by: Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com>
1 parent 47e1d6d commit 3200730

File tree

3 files changed

+266
-16
lines changed

3 files changed

+266
-16
lines changed

bin/cmd.js

Lines changed: 55 additions & 15 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') {
70+
return process.nextTick(() => {
71+
cb(null, sha)
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,7 +127,48 @@ if (parsed.list) {
121127
process.exit(0)
122128
}
123129

124-
if (parsed.tap) {
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+
for (let i = 0; i < commits.length; i++) {
145+
const commit = commits[i]
146+
if (!commit.id || !commit.message) {
147+
throw new Error('Each commit must have "id" and "message" properties')
148+
}
149+
args[i] = { sha: commit.id, ...commit }
150+
}
151+
run()
152+
} catch (err) {
153+
console.error('Error parsing JSON input:', err.message)
154+
process.exit(1)
155+
}
156+
})
157+
process.stdin.resume()
158+
} else {
159+
run()
160+
}
161+
162+
function run () {
163+
if (!parsed.tap) {
164+
v.on('commit', (c) => {
165+
pretty(c.commit, c.messages, v)
166+
commitRun()
167+
})
168+
169+
commitRun()
170+
return
171+
}
125172
const tap = new Tap()
126173
tap.pipe(process.stdout)
127174
if (parsed.out) tap.pipe(fs.createWriteStream(parsed.out))
@@ -141,13 +188,6 @@ if (parsed.tap) {
141188
})
142189

143190
tapRun()
144-
} else {
145-
v.on('commit', (c) => {
146-
pretty(c.commit, c.messages, v)
147-
commitRun()
148-
})
149-
150-
commitRun()
151191
}
152192

153193
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: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,211 @@ test('Test cli flags', (t) => {
152152
})
153153
})
154154

155+
t.test('test stdin with valid JSON', (tt) => {
156+
const validCommit = {
157+
id: '2b98d02b52',
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, /[^0-9a-f]2b98d02b52[^0-9a-f]/, '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: '69435db261',
244+
message: 'chore: update tested node release lines (#94)'
245+
}
246+
const input = JSON.stringify([validCommit])
247+
248+
const ls = spawn('./bin/cmd.js', ['--no-validate-metadata', '--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+
const output = compiledData.trim()
260+
tt.match(output,
261+
/# 69435db261/,
262+
'TAP output contains the sha of the commit being linted')
263+
tt.match(output,
264+
/not ok \d+ subsystem: Invalid subsystem: "chore" \(chore: update tested node release lines \(#94\)\)/,
265+
'TAP output contains failure for subsystem')
266+
tt.match(output,
267+
/# fail\s+\d+/,
268+
'TAP output contains total failures')
269+
tt.match(output,
270+
/# Please review the commit message guidelines:\s# https:\/\/github.com\/nodejs\/node\/blob\/HEAD\/doc\/contributing\/pull-requests.md#commit-message-guidelines/,
271+
'TAP output contains pointer to commit message guidelines')
272+
tt.equal(code, 1, 'CLI exits with non-zero code on failure')
273+
tt.end()
274+
})
275+
})
276+
277+
t.test('test stdin with invalid JSON', (tt) => {
278+
const input = 'this is not valid JSON'
279+
280+
const ls = spawn('./bin/cmd.js', ['-'])
281+
let errorData = ''
282+
283+
ls.stderr.on('data', (data) => {
284+
errorData += data
285+
})
286+
287+
ls.stdin.write(input)
288+
ls.stdin.end()
289+
290+
ls.on('close', (code) => {
291+
tt.equal(code, 1, 'CLI exits with non-zero code on error')
292+
tt.match(errorData, /Error parsing JSON input/, 'error message is shown')
293+
tt.end()
294+
})
295+
})
296+
297+
t.test('test stdin with non-array JSON', (tt) => {
298+
const input = JSON.stringify({ id: 'test', message: 'test' })
299+
300+
const ls = spawn('./bin/cmd.js', ['-'])
301+
let errorData = ''
302+
303+
ls.stderr.on('data', (data) => {
304+
errorData += data
305+
})
306+
307+
ls.stdin.write(input)
308+
ls.stdin.end()
309+
310+
ls.on('close', (code) => {
311+
tt.equal(code, 1, 'CLI exits with non-zero code on error')
312+
tt.match(errorData, /Input must be an array/, 'error message is shown')
313+
tt.end()
314+
})
315+
})
316+
317+
t.test('test stdin with missing properties', (tt) => {
318+
const input = JSON.stringify([{ id: 'test' }]) // missing 'message'
319+
320+
const ls = spawn('./bin/cmd.js', ['-'])
321+
let errorData = ''
322+
323+
ls.stderr.on('data', (data) => {
324+
errorData += data
325+
})
326+
327+
ls.stdin.write(input)
328+
ls.stdin.end()
329+
330+
ls.on('close', (code) => {
331+
tt.equal(code, 1, 'CLI exits with non-zero code on error')
332+
tt.match(errorData, /must have "id" and "message" properties/, 'error message is shown')
333+
tt.end()
334+
})
335+
})
336+
337+
t.test('test stdin with --no-validate-metadata', (tt) => {
338+
const commit = {
339+
id: 'novalidate',
340+
message: 'doc: update README\n\nThis commit has no PR-URL or reviewers'
341+
}
342+
const input = JSON.stringify([commit])
343+
344+
const ls = spawn('./bin/cmd.js', ['--no-validate-metadata', '-'])
345+
let compiledData = ''
346+
347+
ls.stdout.on('data', (data) => {
348+
compiledData += data
349+
})
350+
351+
ls.stdin.write(input)
352+
ls.stdin.end()
353+
354+
ls.on('close', (code) => {
355+
tt.equal(code, 0, 'CLI exits with zero code when metadata validation is disabled')
356+
tt.match(compiledData, /novalidate/, 'output contains commit id')
357+
tt.end()
358+
})
359+
})
360+
155361
t.end()
156362
})

0 commit comments

Comments
 (0)