/
commit-transform.js
75 lines (63 loc) · 1.78 KB
/
commit-transform.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
import { types } from '../types.js';
const COMMIT_HASH_LENGTH = 7;
/**
* Transform a parsed commit to render the changelog.
*
* @param {Object} commit commit parsed with `conventional-changelog-parser`.
* @param {Object} context `conventional-changelog` context.
* @return {Object} the transformed commit.
*/
export default (commit, context) => {
if (commit.notes) {
commit.notes.forEach((note) => {
note.title = 'Breaking changes';
});
}
if (
types[commit.type] &&
(types[commit.type].changelog || (commit.notes && commit.notes.length > 0))
) {
commit.groupType = `${
types[commit.type].emoji ? `${types[commit.type].emoji} ` : ''
}${types[commit.type].title}`;
} else {
return null;
}
if (commit.scope === '*') {
commit.scope = '';
}
if (typeof commit.hash === 'string') {
commit.shortHash = commit.hash.slice(0, COMMIT_HASH_LENGTH);
}
const references = [];
if (typeof commit.subject === 'string') {
let url = context.repository
? `${context.host}/${context.owner}/${context.repository}`
: context.repoUrl;
if (url) {
url += '/issues/';
// Issue URLs.
commit.subject = commit.subject.replace(/#(\d+)/g, (_, issue) => {
references.push(issue);
return `[#${issue}](${url}${issue})`;
});
}
if (context.host) {
// User URLs.
commit.subject = commit.subject.replace(
/\B@([a-z0-9](?:-?[a-z0-9]){0,38})/g,
`[@$1](${context.host}/$1)`
);
}
}
if (commit.references) {
// Remove references that already appear in the subject
commit.references = commit.references.filter((reference) => {
if (!references.includes(reference.issue)) {
return true;
}
return false;
});
}
return commit;
};