-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (129 loc) · 4.04 KB
/
index.js
File metadata and controls
157 lines (129 loc) · 4.04 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
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
const core = require('@actions/core');
const github = require('@actions/github');
const list = require('markdown-list');
export function formatChType(type) {
const featureRegex = /(feat+)/;
if (!type) {
return 'other';
}
if (type.match(featureRegex)) {
return 'feature';
}
// Otherwise, return type passed in and
// Remove parentheses:
return type.replace(/[^a-zA-Z ]/g, '');
}
export function formatCommits(commits, chStoryUrl) {
const {
payload: {
repository: { url: repoUrl },
},
} = github.context;
return commits.reduce((acc, com) => {
const {
commit: { message },
} = com;
const regex = /(?<chType>\(\w*\))?(\s)?(?<prMsg>\w*\W*.+?)(\s+)?(\[)?(sc-)?(?<chId>\d+)?(\])?\s\(#(?<prNumber>\d+)\)/;
const matches = message.match(regex);
const chType = formatChType(matches && matches.groups.chType);
const prMsg = (matches && matches.groups.prMsg) || message;
const prLink =
(matches &&
matches.groups.prNumber &&
`[#${matches.groups.prNumber}](${repoUrl}/pull/${matches.groups.prNumber})`) ||
null;
const chLink =
(matches &&
matches.groups.chId &&
`[sc-${matches.groups.chId}](${chStoryUrl}/${matches.groups.chId})`) ||
null;
const formattedCommit = { chLink, prMsg, prLink };
return Object.assign(acc, {
[chType]: [...(acc[chType] || []), formattedCommit],
});
}, {});
}
export function generateChangelog(formattedCommits) {
return Object.keys(formattedCommits).reduce((acc, type) => {
const heading = `${type.charAt(0).toUpperCase()}${type.slice(1)}`;
const markdownCommits = formattedCommits[type].map((commit) => {
let msg = commit.prMsg;
if (commit.chLink) {
msg = `${msg} ${commit.chLink}`;
}
if (commit.prLink) {
msg = `${msg} ${commit.prLink}`;
}
return msg;
});
const listOfCommits = list(markdownCommits);
const log = `${acc}### ${heading}\n${listOfCommits}\n`;
return log;
}, '');
}
export async function run() {
try {
const previousTag = core.getInput('previousTag');
const prerelease = core.getInput('prerelease');
const isPreRelease = prerelease === 'true';
const createChangelog = core.getInput('createChangelog') === 'true';
const ghToken = core.getInput('ghToken');
const chStoryUrl = core.getInput('chStoryUrl');
const { ref } = github.context;
const tagRegex = /(?<refs>refs)\/(?<tags>tags)\/(?<tag>.*)/;
const validTag = ref.match(tagRegex);
if (!ghToken) {
return core.setFailed('Must provide ghToken');
}
core.setSecret('ghToken');
if (!validTag || !validTag.groups.tag) {
return core.setFailed('Ref must be a tag');
}
const {
groups: { tag },
} = validTag;
const octokit = github.getOctokit(ghToken);
core.info(
`Tag ${tag}: Creating a ${isPreRelease ? 'prerelease' : 'release'}...`
);
let changelog = null;
/* CREATE CHANGELOG */
if (createChangelog) {
// CH Story URL is required for changelog
if (!chStoryUrl) {
return core.setFailed('Must provide chStoryUrl');
}
core.setSecret('chStoryUrl');
// Previous Tag is required for changelog
if (!previousTag) {
return core.setFailed(
'Must provide a previousTag to create a changelog'
);
}
const {
data: { commits },
} = await octokit.repos.compareCommits({
...github.context.repo,
base: previousTag,
head: tag,
});
const formattedCommits = formatCommits(commits, chStoryUrl);
changelog = generateChangelog(formattedCommits);
core.info(changelog);
}
/* CREATE RELEASE */
return await octokit.repos.createRelease({
...github.context.repo,
name: tag,
tag_name: tag,
...(createChangelog && {
body: `Service: ${github.context.repo.repo}\n${changelog}`,
}),
prerelease: isPreRelease,
});
} catch (error) {
return core.setFailed(error.message);
}
}
run();
export default run;