-
Notifications
You must be signed in to change notification settings - Fork 41
/
process-commits.js
87 lines (76 loc) · 2.47 KB
/
process-commits.js
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
81
82
83
84
85
86
87
import stripAnsi from 'strip-ansi'
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 { supportsColor } from 'chalk'
import { collectCommitLabels } from './collect-commit-labels.js'
import { findMatchingPrs } from './find-matching-prs.js'
function getFormat (argv) {
if (argv.format && Object.values(formatType).includes(argv.format)) {
return argv.format
} else if (argv.sha) {
return formatType.SHA
} else if (argv.plaintext || argv.p) {
return formatType.PLAINTEXT
} else if (argv.markdown || argv.md) {
return formatType.MARKDOWN
} else if (argv.messageonly || argv.mo) {
return formatType.MESSAGEONLY
}
return formatType.SIMPLE
}
async function printCommits (list) {
for (let commit of list) {
if (!supportsColor) {
commit = stripAnsi(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 = stripAnsi(output)
return (await formatMarkdown(output)).replace(/\n$/, '')
}
return output
}))
}
if (format !== formatType.PLAINTEXT && reverse) {
list = list.reverse()
}
if (!quiet) {
printCommits(list)
}
}