-
Notifications
You must be signed in to change notification settings - Fork 41
/
commit-to-output.js
67 lines (58 loc) · 2.33 KB
/
commit-to-output.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
const chalk = require('chalk')
const reverts = require('./reverts')
const groups = require('./groups')
function cleanMarkdown (txt) {
// escape _~*\[]<>
return txt.replace(/([_~*\\[\]<>])/g, '\\$1')
}
function toStringSimple (data) {
let s = ''
s += '* [' + data.sha.substr(0, 10) + '] - '
s += (data.semver || []).length ? '(' + data.semver.join(', ').toUpperCase() + ') ' : ''
s += data.revert ? 'Revert "' : ''
s += data.group ? data.group + ': ' : ''
s += data.summary
s += data.revert ? '" ' : ' '
s += data.author ? '(' + data.author + ') ' : ''
s += data.pr ? data.prUrl : ''
s = s.trim()
return (data.semver && data.semver.length)
? chalk.green(chalk.bold(s))
: data.group === 'doc'
? chalk.grey(s)
: s
}
function toStringMarkdown (data) {
let s = ''
s += '* [[`' + data.sha.substr(0, 10) + '`](' + data.shaUrl + ')] - '
s += (data.semver || []).length ? '**(' + data.semver.join(', ').toUpperCase() + ')** ' : ''
s += data.revert ? '***Revert*** "' : ''
s += data.group ? '**' + data.group + '**: ' : ''
s += cleanMarkdown(data.summary)
s += data.revert ? '" ' : ' '
s += data.author ? '(' + data.author + ') ' : ''
s += data.pr ? '[' + data.pr + '](' + data.prUrl + ')' : ''
s = s.trim()
return (data.semver && data.semver.length)
? chalk.green(chalk.bold(s))
: data.group === 'doc'
? chalk.grey(s)
: s
}
function commitToOutput (commit, simple, ghId, commitUrl) {
let data = {}
let prUrlMatch = commit.prUrl && commit.prUrl.match(/^https?:\/\/.+\/([^/]+\/[^/]+)\/\w+\/\d+$/i)
let urlHash = '#' + commit.ghIssue || commit.prUrl
let ref = commit.sha.substr(0, 10)
data.sha = commit.sha
data.shaUrl = commitUrl.replace(/\{ghUser\}/g, ghId.user).replace(/\{ghRepo\}/g, ghId.repo).replace(/\{ref\}/g, ref)
data.semver = commit.labels && commit.labels.filter((l) => l.indexOf('semver') > -1)
data.revert = reverts.isRevert(commit.summary)
data.group = groups.toGroups(commit.summary)
data.summary = groups.cleanSummary(reverts.cleanSummary(commit.summary))
data.author = (commit.author && commit.author.name) || ''
data.pr = prUrlMatch && ((prUrlMatch[1] !== `${ghId.user}/${ghId.repo}` ? prUrlMatch[1] : '') + urlHash)
data.prUrl = prUrlMatch && commit.prUrl
return (simple ? toStringSimple : toStringMarkdown)(data)
}
module.exports = commitToOutput