-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathprocess-commits.js
More file actions
80 lines (69 loc) · 2.34 KB
/
Copy pathprocess-commits.js
File metadata and controls
80 lines (69 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { stripVTControlCharacters, styleText } from 'node:util'
import { commitToOutput, formatType } from './commit-to-output.js'
import { groupCommits } from './group-commits.js'
import { toGroups } from './groups.js'
import { formatMarkdown } from './format.js'
import { collectCommitLabels } from './collect-commit-labels.js'
import { findMatchingPrs } from './find-matching-prs.js'
function getFormat (argv) {
if (!argv.format) {
return formatType.SIMPLE
}
if (!Object.values(formatType).includes(argv.format)) throw new Error(`Unknown format: ${argv.format}`)
return argv.format
}
async function printCommits (list) {
const supportsColor = styleText('green', 'test') !== 'test'
for (let commit of list) {
if (!supportsColor) {
commit = stripVTControlCharacters(commit)
}
process.stdout.write(`${commit}\n`)
}
}
export async function processCommits (argv, ghId, list) {
const quiet = argv.quiet || argv.q
const reverse = argv.reverse
const commitUrl = argv['commit-url'] || 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
if (argv['find-matching-prs']) {
await findMatchingPrs(ghId, list)
}
await collectCommitLabels(list)
const format = getFormat(argv)
if (argv.group || argv.g || format === formatType.PLAINTEXT || format === formatType.MESSAGEONLY) {
list = groupCommits(list)
}
if (format === formatType.SHA) {
list = list.map((commit) => `${commit.sha.substr(0, 10)}`)
} else if (
format === formatType.PLAINTEXT ||
format === formatType.MESSAGEONLY
) {
const formatted = []
let currentGroup
for (const commit of list) {
const commitGroup = toGroups(commit.summary)
if (currentGroup !== commitGroup) {
formatted.push(commitGroup ? `${commitGroup}:` : '')
currentGroup = commitGroup
}
formatted.push(commitToOutput(commit, format, ghId, commitUrl))
}
list = formatted
} else {
list = await Promise.all(list.map(async (commit) => {
let output = commitToOutput(commit, format, ghId, commitUrl)
if (format === formatType.MARKDOWN) {
output = stripVTControlCharacters(output)
return (await formatMarkdown(output)).replace(/\n$/, '')
}
return output
}))
}
if (format !== formatType.PLAINTEXT && reverse) {
list = list.reverse()
}
if (!quiet) {
printCommits(list)
}
}