-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
210 lines (183 loc) · 6.18 KB
/
index.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const { exec } = require('child_process');
const { Octokit } = require('@octokit/rest');
const core = require('@actions/core')
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
const GITHUB_URL = 'https://github.com'
const TITLE_GROUP_RELEASE = {
feat: 'Feature 🚀',
fix: 'Bug Fix 🐛',
chore: 'Chore 💎',
docs: 'Document 🗎'
};
function getInputGithub() {
return {
owner: core.getInput('owner') || undefined,
repository: core.getInput('repository') || undefined,
token: core.getInput('token') || undefined,
}
}
const octokit = new Octokit({
auth: getInputGithub().token, request: {
fetch: fetch,
},
});
const date = new Date().toLocaleDateString();
function getListTag() {
return new Promise((resolve, reject) => {
exec(`git for-each-ref --sort=-creatordate --format="%(refname:short)" "refs/tags/v*"`, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
if (stderr) {
reject(stderr);
return;
}
const tag = stdout.trim().split(/\s+/);
resolve(tag);
});
});
};
function convertGroupedCommitsToString(groupedCommits, previousTag, lastTag) {
let result = '';
let titleRelease = `## ${lastTag} (${date}) \n`;
if (previousTag) {
titleRelease = `## [${lastTag}](${GITHUB_URL}/${getInputGithub().owner}/${getInputGithub().repository}/compare/${previousTag}...${lastTag}) (${date}) \n`
}
result += titleRelease
const commitTypeOrder = ["feat", "fix", "chore", "docs", "others"];
for (let type of commitTypeOrder) {
if (groupedCommits[type]) {
const displayTitle = TITLE_GROUP_RELEASE[type] || capitalizeFirstLetter(type);
result += `### ${displayTitle} :\n`;
for (let commit of groupedCommits[type]) {
result += `- ${commit.message} \n`;
}
result += '\n';
}
}
return result;
};
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};
function formatCommitMessage(message) {
return message.replace(/\(#(\d+)\)/, (_, prNumber) => {
return `[#${prNumber}](${GITHUB_URL}/${getInputGithub().owner}/${getInputGithub().repository}/pull/${prNumber})`;
});
};
function grouppingCommit(message, acc, type, username) {
const messageFormated = formatCommitMessage(message)
if (!acc[type]) {
acc[type] = [];
}
acc[type].push({ message: formatCommitMessage(messageFormated) + ' by ' + username });
}
function createReleaseNotes(previousTag, lastTag) {
return new Promise((resolve, reject) => {
const compareTag = previousTag ? `${previousTag}..${lastTag}` : `${lastTag}`
exec(`git log ${compareTag} --pretty=format:"%ae: %s"`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
reject(error);
}
if (stderr) {
reject(stderr);
return;
}
const commits = stdout.split('\n').filter(commit => commit);
const groupedCommits = commits.reduce((acc, commit) => {
let [username, message] = commit.split(/: (.+)/);
const spitUsername = username.match(/\+(.*?)@users.noreply.github.com/);
const usernameFormated = spitUsername ? `[${spitUsername[1]}](https://github.com/${spitUsername[1]})` : null;
/**use for checking commit eg: feat(FE): */
const match = message.trim().match(/^(feat|fix|chore|docs|style|refactor|perf|test)\(([^)]+)\): (.+)$/);
if (match) {
const message = `**${match[2]}**: ${match[3]}`;
const type = match[1]
grouppingCommit(message, acc, type, usernameFormated)
} else {
/**use for checking commit eg: feat: */
const matchPrefix = message.trim().match(/^(feat|fix|chore|docs|style|refactor|perf|test): (.+)$/);
if (matchPrefix) {
const message = matchPrefix[2];
const type = matchPrefix[1]
grouppingCommit(message, acc, type, usernameFormated)
} else {
if (!acc['others']) {
acc['others'] = [];
}
acc['others'].push({ message: formatCommitMessage(message.trim()) + ' by ' + usernameFormated });
}
}
return acc;
}, {});
const formattedOutput = convertGroupedCommitsToString(groupedCommits, previousTag, lastTag);
resolve(formattedOutput)
})
})
};
async function preRelease(tags) {
if (tags[0].includes("-rc.")) {
try {
const result = await createReleaseNotes(tags[1], tags[0]);
createReleaseNote(result, tags[0])
} catch (error) {
console.error(`An error occurred: ${error}`);
return error
}
} else {
const latestRcTag = tags.find(tag => tag.includes("-rc."));
try {
const result = await createReleaseNotes(tags[0], latestRcTag);
createReleaseNote(result, latestRcTag)
} catch (error) {
console.error(`An error occurred: ${error}`);
return error
}
}
};
async function release(tags) {
const releaseTags = tags.filter(tag => !tag.includes("-rc."));
try {
const result = await createReleaseNotes(releaseTags[1], releaseTags[0]);
createReleaseNote(result, releaseTags[0])
} catch (error) {
console.error(`An error occurred: ${error}`);
return error
}
};
async function createReleaseNote(body, tag) {
try {
await octokit.repos.createRelease({
owner: getInputGithub().owner,
repo: getInputGithub().repository,
tag_name: tag,
name: `Release ${tag}`,
body,
draft: false,
prerelease: tag.includes("-rc."),
});
} catch (err) {
console.error("Error creating release:", err);
return err
}
};
async function main() {
try {
const listTag = await getListTag();
if (listTag[0] === "") {
core.setFailed('There are no tag, please create tag before')
return;
}
if (listTag[0].includes("-rc.")) {
preRelease(listTag)
} else {
release(listTag)
};
} catch (error) {
core.setFailed(error)
return error
}
};
main();