-
-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathutils.ts
181 lines (158 loc) · 4.92 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
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
import * as core from '@actions/core';
import { prerelease, rcompare, valid } from 'semver';
// @ts-ignore
import DEFAULT_RELEASE_TYPES from '@semantic-release/commit-analyzer/lib/default-release-types';
import { compareCommits, listTags } from './github';
import { defaultChangelogRules } from './defaults';
import { Await } from './ts';
import { context } from '@actions/github';
type Tags = Await<ReturnType<typeof listTags>>;
export async function getValidTags(
prefixRegex: RegExp,
shouldFetchAllTags: boolean
) {
const tags = await listTags(shouldFetchAllTags);
const invalidTags = tags.filter(
(tag) => !valid(tag.name.replace(prefixRegex, ''))
);
invalidTags.forEach((name) => core.debug(`Found Invalid Tag: ${name}.`));
const validTags = tags
.filter((tag) => valid(tag.name.replace(prefixRegex, '')))
.sort((a, b) =>
rcompare(a.name.replace(prefixRegex, ''), b.name.replace(prefixRegex, ''))
);
validTags.forEach((tag) => core.debug(`Found Valid Tag: ${tag.name}.`));
return validTags;
}
interface FinalCommit {
sha: string | null;
commit: {
message: string;
};
}
export async function getCommits(
baseRef: string,
headRef: string
): Promise<{ message: string; hash: string | null }[]> {
let commits: Array<FinalCommit>;
commits = await compareCommits(baseRef, headRef);
core.info('We found ' + commits.length + ' commits using classic compare!');
if (commits.length < 1) {
core.info(
'We did not find enough commits, attempting to scan closed PR method.'
);
commits = getClosedPRCommits();
}
if (commits.length == 0) {
return [];
}
return commits
.filter((commit: FinalCommit) => !!commit.commit.message)
.map((commit: FinalCommit) => ({
message: commit.commit.message,
hash: commit.sha,
}));
}
function getClosedPRCommits() {
let commits = Array<FinalCommit>();
if (!('pull_request' in context.payload)) {
core.debug('We are in a closed PR context continuing.');
core.debug(JSON.stringify(context.payload.commits));
let pr_commit_count = context.payload.commits.length;
core.info(
'We found ' + pr_commit_count + ' commits from the Closed PR method.'
);
commits = context.payload.commits
.filter((commit: FinalCommit) => !!commit.commit.message)
.filter((commit: FinalCommit) => ({
message: commit.commit.message,
hash: commit.sha,
}));
core.debug(
'After processing we are going to present ' + commits.length + ' commits!'
);
}
return commits;
}
export function getBranchFromRef(ref: string) {
return ref.replace('refs/heads/', '');
}
export function isPr(ref: string) {
return ref.includes('refs/pull/');
}
export function getLatestTag(
tags: Tags,
prefixRegex: RegExp,
tagPrefix: string
) {
return (
tags.find((tag) => !prerelease(tag.name.replace(prefixRegex, ''))) || {
name: `${tagPrefix}0.0.0`,
commit: {
sha: 'HEAD',
},
}
);
}
export function getLatestPrereleaseTag(
tags: Tags,
identifier: string,
prefixRegex: RegExp
) {
return tags
.filter((tag) => prerelease(tag.name.replace(prefixRegex, '')))
.find((tag) => tag.name.replace(prefixRegex, '').match(identifier));
}
export function mapCustomReleaseRules(customReleaseTypes: string) {
const releaseRuleSeparator = ',';
const releaseTypeSeparator = ':';
return customReleaseTypes
.split(releaseRuleSeparator)
.filter((customReleaseRule) => {
const parts = customReleaseRule.split(releaseTypeSeparator);
if (parts.length < 2) {
core.warning(
`${customReleaseRule} is not a valid custom release definition.`
);
return false;
}
const defaultRule = defaultChangelogRules[parts[0].toLowerCase()];
if (customReleaseRule.length !== 3) {
core.debug(
`${customReleaseRule} doesn't mention the section for the changelog.`
);
core.debug(
defaultRule
? `Default section (${defaultRule.section}) will be used instead.`
: "The commits matching this rule won't be included in the changelog."
);
}
if (!DEFAULT_RELEASE_TYPES.includes(parts[1])) {
core.warning(`${parts[1]} is not a valid release type.`);
return false;
}
return true;
})
.map((customReleaseRule) => {
const [type, release, section] =
customReleaseRule.split(releaseTypeSeparator);
const defaultRule = defaultChangelogRules[type.toLowerCase()];
return {
type,
release,
section: section || defaultRule?.section,
};
});
}
export function mergeWithDefaultChangelogRules(
mappedReleaseRules: ReturnType<typeof mapCustomReleaseRules> = []
) {
const mergedRules = mappedReleaseRules.reduce(
(acc, curr) => ({
...acc,
[curr.type]: curr,
}),
{ ...defaultChangelogRules }
);
return Object.values(mergedRules).filter((rule) => !!rule.section);
}