-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
108 lines (92 loc) · 2.68 KB
/
utils.ts
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
import { ParsedCommit } from "./typings";
export const getShortSHA = (sha: string): string => {
const coreAbbrev = 7;
return sha.substring(0, coreAbbrev);
};
export enum ConventionalCommitTypes {
feat = "Features",
fix = "Bug Fixes",
docs = "Documentation",
style = "Styles",
refactor = "Code Refactoring",
perf = "Performance Improvements",
test = "Tests",
build = "Builds",
ci = "Continuous Integration",
chore = "Chores",
revert = "Reverts",
breaking = "Breaking Changes",
}
const getFormattedChangelogEntry = (parsedCommit: ParsedCommit): string => {
let entry = "";
const url = parsedCommit.commit.html_url;
const sha = getShortSHA(parsedCommit.commit.sha);
const author = parsedCommit.commit.commit?.author?.name ?? "Unknown";
entry = `- ${sha}: ${parsedCommit.commitMsg.header} (${author})`;
if (parsedCommit.commitMsg.type) {
const scopeStr = parsedCommit.commitMsg.scope
? `**${parsedCommit.commitMsg.scope}**: `
: "";
entry = `- ${scopeStr}${parsedCommit.commitMsg.subject} ([${author}](${url}))`;
}
return entry;
};
export const generateChangelogFromParsedCommits = (
parsedCommits: ParsedCommit[],
): string => {
let changelog = "";
for (const key of Object.keys(ConventionalCommitTypes)) {
const clBlock = parsedCommits
.filter((val) => val.commitMsg.type === key)
.map((val) => getFormattedChangelogEntry(val))
.reduce((acc, line) => `${acc}\n${line}`, "");
if (clBlock) {
changelog += `\n\n## ${(ConventionalCommitTypes as any)[key]}\n`;
changelog += clBlock.trim();
}
}
// Commits
const commits = parsedCommits
.filter((val) => val.commitMsg.type === null)
.map((val) => getFormattedChangelogEntry(val))
.reduce((acc, line) => `${acc}\n${line}`, "");
if (commits) {
changelog += "\n\n## Commits\n";
changelog += commits.trim();
}
return changelog;
};
export function getNextSemverBump(
commits: ParsedCommit[],
environment: string,
): string {
let hasBreakingChange = false;
let hasNewFeature = false;
let hasNewFix = false;
for (const commit of commits) {
const commitType = commit.commitMsg.type;
if (commitType === "fix") {
hasNewFix = true;
}
// Check for breaking changes
if (commitType === "breaking") {
hasBreakingChange = true;
}
// Check for new features
if (commitType === "feat") {
hasNewFeature = true;
}
}
// Determine semver bump based on commit types
if (hasBreakingChange) {
return "major";
} else if (hasNewFeature) {
return "minor";
} else if (hasNewFix) {
return "patch";
} else if (environment === "prod") {
return "patch";
} else {
return "";
}
}