-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathreleases.js
159 lines (149 loc) · 4.49 KB
/
releases.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import semver from 'semver'
import { niceDate } from './utils'
const MERGE_COMMIT_PATTERN = /^Merge (remote-tracking )?branch '.+'/
const COMMIT_MESSAGE_PATTERN = /\n+([\S\s]+)/
const NUMERIC_PATTERN = /^\d+(\.\d+)?$/
export function parseReleases (commits, remote, latestVersion, options) {
let release = newRelease(latestVersion)
const releases = []
const sortCommits = commitSorter(options)
for (let commit of commits) {
if (commit.tag) {
if (release.tag || options.unreleased) {
releases.push({
...release,
href: getCompareLink(
`${options.tagPrefix}${commit.tag}`,
release.tag ? `${options.tagPrefix}${release.tag}` : 'HEAD',
remote
),
commits: sliceCommits(release.commits.sort(sortCommits), options, release),
major: !options.tagPattern && commit.tag && release.tag && semver.diff(commit.tag, release.tag) === 'major'
})
}
const summary = getSummary(commit.message, options.releaseSummary)
release = newRelease(commit.tag, commit.date, summary)
}
if (commit.merge) {
release.merges.push(commit.merge)
} else if (commit.fixes) {
release.fixes.push({
fixes: commit.fixes,
commit
})
} else if (filterCommit(commit, options, release)) {
release.commits.push(commit)
}
}
releases.push({
...release,
commits: sliceCommits(release.commits.sort(sortCommits), options, release)
})
return releases
}
export function sortReleases (a, b) {
const tags = {
a: inferSemver(a.tag),
b: inferSemver(b.tag)
}
if (tags.a && tags.b) {
if (semver.valid(tags.a) && semver.valid(tags.b)) {
return semver.rcompare(tags.a, tags.b)
}
if (NUMERIC_PATTERN.test(tags.a) && NUMERIC_PATTERN.test(tags.b)) {
return parseFloat(tags.a) < parseFloat(tags.b) ? 1 : -1
}
if (tags.a === tags.b) {
return 0
}
return tags.a < tags.b ? 1 : -1
}
if (tags.a) return 1
if (tags.b) return -1
return 0
}
function inferSemver (tag) {
if (/^v\d+$/.test(tag)) {
// v1 becomes v1.0.0
return `${tag}.0.0`
}
if (/^v\d+\.\d+$/.test(tag)) {
// v1.0 becomes v1.0.0
return `${tag}.0`
}
return tag
}
function newRelease (tag = null, date = new Date().toISOString(), summary = null) {
return {
commits: [],
fixes: [],
merges: [],
tag,
date,
summary,
title: tag || 'Unreleased',
niceDate: niceDate(date),
isoDate: date.slice(0, 10)
}
}
function sliceCommits (commits, { commitLimit, backfillLimit }, release) {
if (commitLimit === false) {
return commits
}
const emptyRelease = release.fixes.length === 0 && release.merges.length === 0
const limit = emptyRelease ? backfillLimit : commitLimit
const minLimit = commits.filter(c => c.breaking).length
return commits.slice(0, Math.max(minLimit, limit))
}
function filterCommit (commit, { ignoreCommitPattern }, release) {
if (commit.breaking) {
return true
}
if (ignoreCommitPattern) {
// Filter out commits that match ignoreCommitPattern
return new RegExp(ignoreCommitPattern).test(commit.subject) === false
}
if (semver.valid(commit.subject)) {
// Filter out version commits
return false
}
if (MERGE_COMMIT_PATTERN.test(commit.subject)) {
// Filter out merge commits
return false
}
if (release.merges.findIndex(m => m.message === commit.subject) !== -1) {
// Filter out commits with the same message as an existing merge
return false
}
return true
}
function getCompareLink (from, to, remote) {
if (!remote) {
return null
}
if (/bitbucket/.test(remote.hostname)) {
return `${remote.url}/compare/${to}..${from}`
}
if (/dev\.azure/.test(remote.hostname) || /visualstudio/.test(remote.hostname)) {
return `${remote.url}/branches?baseVersion=GT${to}&targetVersion=GT${from}&_a=commits`
}
return `${remote.url}/compare/${from}...${to}`
}
function getSummary (message, releaseSummary) {
if (!message || !releaseSummary) {
return null
}
if (COMMIT_MESSAGE_PATTERN.test(message)) {
return message.match(COMMIT_MESSAGE_PATTERN)[1]
}
return null
}
function commitSorter ({ sortCommits }) {
return (a, b) => {
if (!a.breaking && b.breaking) return 1
if (a.breaking && !b.breaking) return -1
if (sortCommits === 'date') return new Date(a.date) - new Date(b.date)
if (sortCommits === 'date-desc') return new Date(b.date) - new Date(a.date)
return (b.insertions + b.deletions) - (a.insertions + a.deletions)
}
}