forked from mathieudutour/github-tag-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
173 lines (149 loc) · 4.43 KB
/
Copy pathutils.ts
File metadata and controls
173 lines (149 loc) · 4.43 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import * as core from '@actions/core';
import { prerelease, rcompare, valid } from 'semver';
import { compareCommits, listTags } from './github.js';
import { defaultChangelogRules } from './defaults.js';
// Mirror of the list exported by `@semantic-release/commit-analyzer`
// (its internal `lib/default-release-types.js` is not a public export).
// See: https://github.com/semantic-release/commit-analyzer
const DEFAULT_RELEASE_TYPES: readonly string[] = [
'major',
'premajor',
'minor',
'preminor',
'patch',
'prepatch',
'prerelease',
];
type Tags = Awaited<ReturnType<typeof listTags>>;
export async function getValidTags(
prefixRegex: RegExp,
shouldFetchAllTags: boolean
) {
const tags = await listTags(shouldFetchAllTags);
const invalidTags = tags.filter(
(tag) =>
!prefixRegex.test(tag.name) || !valid(tag.name.replace(prefixRegex, ''))
);
invalidTags.forEach((tag) => {
core.debug(`Found Invalid Tag: ${tag.name}.`);
});
const validTags = tags
.filter(
(tag) =>
prefixRegex.test(tag.name) && 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;
}
export async function getCommits(
baseRef: string,
headRef: string
): Promise<{ message: string; hash: string | null }[]> {
const commits = await compareCommits(baseRef, headRef);
return commits
.filter((commit) => !!commit.commit.message)
.map((commit) => ({
message: commit.commit.message,
hash: commit.sha,
}));
}
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 interface MappedReleaseRule {
type: string;
release: string;
section: string | undefined;
}
export function mapCustomReleaseRules(
customReleaseTypes: string
): MappedReleaseRule[] {
const releaseRuleSeparator = ',';
const releaseTypeSeparator = ':';
return customReleaseTypes
.split(releaseRuleSeparator)
.filter((customReleaseRule) => {
const parts = customReleaseRule.split(releaseTypeSeparator);
const rawType = parts[0];
const rawRelease = parts[1];
if (rawType === undefined || rawRelease === undefined) {
core.warning(
`${customReleaseRule} is not a valid custom release definition.`
);
return false;
}
const defaultRule = defaultChangelogRules[rawType.toLowerCase()];
if (parts.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(rawRelease)) {
core.warning(`${rawRelease} is not a valid release type.`);
return false;
}
return true;
})
.map((customReleaseRule) => {
const parts = customReleaseRule.split(releaseTypeSeparator);
const type = parts[0] ?? '';
const release = parts[1] ?? '';
const section = parts[2];
const defaultRule = defaultChangelogRules[type.toLowerCase()];
const resolvedSection: string | undefined =
section ?? defaultRule?.section;
return {
type,
release,
section: resolvedSection,
};
});
}
export function mergeWithDefaultChangelogRules(
mappedReleaseRules: ReturnType<typeof mapCustomReleaseRules> = []
) {
const mergedRules = mappedReleaseRules.reduce(
(acc, curr) => ({
...acc,
[curr.type]: curr,
}),
{ ...defaultChangelogRules }
);
return Object.values(mergedRules).filter((rule) => Boolean(rule.section));
}