@mrpelz/boilerplate-common 12.3.1 → 12.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mrpelz/boilerplate-common",
3
- "version": "12.3.1",
3
+ "version": "12.4.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/mrpelz/boilerplate.git",
@@ -13,7 +13,10 @@
13
13
  "module": "dist/main.js",
14
14
  "types": "dist/main.d.ts",
15
15
  "bin": {
16
- "boilerplate-bootstrap": "scripts/bootstrap.sh"
16
+ "boilerplate-bootstrap": "scripts/bootstrap.sh",
17
+ "boilerplate-ci-post-prerelease-containers-published": "scripts/ci/post-prerelease-containers-published.js",
18
+ "boilerplate-ci-post-prerelease-packages-published": "scripts/ci/post-prerelease-packages-published.js",
19
+ "boilerplate-ci-post-prerelease-pipeline": "scripts/ci/post-prerelease-pipeline.js"
17
20
  },
18
21
  "files": [
19
22
  "dist/**/*.{js,map,ts}",
@@ -26,7 +29,7 @@
26
29
  "jest.config.js",
27
30
  "Makefile",
28
31
  "README.md",
29
- "scripts/**/*.sh",
32
+ "scripts/**/*",
30
33
  "tsconfig.build.json",
31
34
  "tsconfig.json",
32
35
  "tsconfig.meta.json"
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env -S node --experimental-modules
2
+ /* eslint-disable no-console */
3
+
4
+ import {
5
+ fetchMergeRequests,
6
+ fetchPipelineUser,
7
+ getEnvironment,
8
+ getFetchGitlab,
9
+ getProcessedBody,
10
+ getSlug,
11
+ replaceComments,
12
+ } from './utils.js';
13
+
14
+ (async () => {
15
+ try {
16
+ const environment = getEnvironment();
17
+ const fetchGitlab = getFetchGitlab(environment);
18
+
19
+ const {
20
+ CI_COMMIT_TAG: version,
21
+ CI_COMMIT_TAG_MESSAGE: tagMessage,
22
+ CI_PROJECT_URL: projectUrl,
23
+ } = process.env;
24
+ if (!projectUrl || !tagMessage || !version) return;
25
+
26
+ console.info({
27
+ CI_COMMIT_TAG: version,
28
+ CI_COMMIT_TAG_MESSAGE: tagMessage,
29
+ CI_PROJECT_URL: projectUrl,
30
+ });
31
+
32
+ const [body, headline] = getProcessedBody(`
33
+ ## Prerelease Published as Containers
34
+
35
+ Containers in [tag "${version}"](${projectUrl}/-/tags/${version}) have been published to the [Gitlab container-registry](${projectUrl}/container_registry):
36
+
37
+ They are available as tag \`<package-name>:pre-${tagMessage}\`.
38
+ `);
39
+
40
+ const [projectMergeRequests, pipelineUsername] = await Promise.all([
41
+ fetchMergeRequests(fetchGitlab, environment.projectId),
42
+ fetchPipelineUser(
43
+ fetchGitlab,
44
+ environment.projectId,
45
+ environment.pipelineId,
46
+ ),
47
+ ]);
48
+
49
+ const matchingMergeRequests = projectMergeRequests.filter(
50
+ ({ source_branch }) => getSlug(source_branch) === tagMessage,
51
+ );
52
+
53
+ console.info(
54
+ `Found ${matchingMergeRequests.length} matching merge request(s).`,
55
+ );
56
+
57
+ await Promise.all(
58
+ matchingMergeRequests.map(async (matchingMergeRequest) =>
59
+ replaceComments(
60
+ fetchGitlab,
61
+ environment.projectId,
62
+ pipelineUsername,
63
+ matchingMergeRequest.iid,
64
+ headline,
65
+ body,
66
+ ),
67
+ ),
68
+ );
69
+ } catch (error) {
70
+ console.error(new Error('Error.', { cause: error }));
71
+ process.exit(1);
72
+ }
73
+ })();
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env -S node --experimental-modules
2
+ /* eslint-disable no-console */
3
+
4
+ import { readFile } from 'node:fs/promises';
5
+
6
+ import {
7
+ fetchMergeRequests,
8
+ fetchPipelineUser,
9
+ getEnvironment,
10
+ getFetchGitlab,
11
+ getProcessedBody,
12
+ getSlug,
13
+ replaceComments,
14
+ } from './utils.js';
15
+
16
+ (async () => {
17
+ try {
18
+ const environment = getEnvironment();
19
+ const fetchGitlab = getFetchGitlab(environment);
20
+
21
+ const {
22
+ CI_COMMIT_TAG: version,
23
+ CI_COMMIT_TAG_MESSAGE: tagMessage,
24
+ CI_PROJECT_URL: projectUrl,
25
+ } = process.env;
26
+ if (!projectUrl || !tagMessage || !version) return;
27
+
28
+ const packagesFile = await readFile('packages.txt', { encoding: 'utf8' });
29
+ const packagesSpecs = packagesFile.trim().split('\n');
30
+
31
+ console.info({
32
+ CI_COMMIT_TAG: version,
33
+ CI_COMMIT_TAG_MESSAGE: tagMessage,
34
+ CI_PROJECT_URL: projectUrl,
35
+ });
36
+
37
+ const [body, headline] = getProcessedBody(`
38
+ ## Prerelease Published as Packages
39
+
40
+ Packages in [tag "${version}"](${projectUrl}/-/tags/${version}) have been published to the [Gitlab NPM-registry](${projectUrl}/-/packages):
41
+
42
+ ${packagesSpecs.map((packageSpec) => `* ${packageSpec}`).join('\n')}
43
+
44
+ They are using the distribution tag \`pre-${tagMessage}\`, install using:
45
+
46
+ \`\`\`
47
+ npm install \\
48
+ ${packagesSpecs.map((packageSpec) => `"${packageSpec.split('@').slice(0, -1).join('@')}@pre-${tagMessage}"`).join(' \\\n')}
49
+ \`\`\`
50
+ `);
51
+
52
+ const [projectMergeRequests, pipelineUsername] = await Promise.all([
53
+ fetchMergeRequests(fetchGitlab, environment.projectId),
54
+ fetchPipelineUser(
55
+ fetchGitlab,
56
+ environment.projectId,
57
+ environment.pipelineId,
58
+ ),
59
+ ]);
60
+
61
+ const matchingMergeRequests = projectMergeRequests.filter(
62
+ ({ source_branch }) => getSlug(source_branch) === tagMessage,
63
+ );
64
+
65
+ console.info(
66
+ `Found ${matchingMergeRequests.length} matching merge request(s).`,
67
+ );
68
+
69
+ await Promise.all(
70
+ matchingMergeRequests.map(async (matchingMergeRequest) =>
71
+ replaceComments(
72
+ fetchGitlab,
73
+ environment.projectId,
74
+ pipelineUsername,
75
+ matchingMergeRequest.iid,
76
+ headline,
77
+ body,
78
+ ),
79
+ ),
80
+ );
81
+ } catch (error) {
82
+ console.error(new Error('Error.', { cause: error }));
83
+ process.exit(1);
84
+ }
85
+ })();
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env -S node --experimental-modules
2
+ /* eslint-disable no-console */
3
+
4
+ import {
5
+ fetchMergeRequests,
6
+ fetchPipelineUser,
7
+ getEnvironment,
8
+ getFetchGitlab,
9
+ getProcessedBody,
10
+ getSlug,
11
+ replaceComments,
12
+ } from './utils.js';
13
+
14
+ (async () => {
15
+ try {
16
+ const environment = getEnvironment();
17
+ const fetchGitlab = getFetchGitlab(environment);
18
+
19
+ const {
20
+ CI_COMMIT_TAG: version,
21
+ CI_COMMIT_TAG_MESSAGE: tagMessage,
22
+ CI_PROJECT_URL: projectUrl,
23
+ } = process.env;
24
+ if (!projectUrl || !tagMessage || !version) return;
25
+
26
+ console.info({
27
+ CI_COMMIT_TAG: version,
28
+ CI_COMMIT_TAG_MESSAGE: tagMessage,
29
+ CI_PROJECT_URL: projectUrl,
30
+ });
31
+
32
+ const [body, headline] = getProcessedBody(`
33
+ ## Prerelease Pipeline
34
+
35
+ [Tag "${version}"](${projectUrl}/-/tags/${version}) has started [a release pipeline](${projectUrl}/-/pipelines/${environment.pipelineId}).
36
+ `);
37
+
38
+ const [projectMergeRequests, pipelineUsername] = await Promise.all([
39
+ fetchMergeRequests(fetchGitlab, environment.projectId),
40
+ fetchPipelineUser(
41
+ fetchGitlab,
42
+ environment.projectId,
43
+ environment.pipelineId,
44
+ ),
45
+ ]);
46
+
47
+ const matchingMergeRequests = projectMergeRequests.filter(
48
+ ({ source_branch }) => getSlug(source_branch) === tagMessage,
49
+ );
50
+
51
+ console.info(
52
+ `Found ${matchingMergeRequests.length} matching merge request(s).`,
53
+ );
54
+
55
+ await Promise.all(
56
+ matchingMergeRequests.map(async (matchingMergeRequest) =>
57
+ replaceComments(
58
+ fetchGitlab,
59
+ environment.projectId,
60
+ pipelineUsername,
61
+ matchingMergeRequest.iid,
62
+ headline,
63
+ body,
64
+ ),
65
+ ),
66
+ );
67
+ } catch (error) {
68
+ console.error(new Error('Error.', { cause: error }));
69
+ process.exit(1);
70
+ }
71
+ })();
@@ -0,0 +1,303 @@
1
+ #!/usr/bin/env -S node --experimental-modules
2
+ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
3
+ /* eslint-disable no-console */
4
+
5
+ /**
6
+ * @param {string} input
7
+ * @returns {[string, string]}
8
+ */
9
+ export const getProcessedBody = (input) => {
10
+ const lines = input
11
+ .trim()
12
+ .split('\n')
13
+ .map((line) => line.trim());
14
+
15
+ return [lines.join('\n'), lines.at(0) ?? ''];
16
+ };
17
+
18
+ /**
19
+ * @param {string} input
20
+ * @returns {string}
21
+ */
22
+ export const getSlug = (input) => input.replaceAll(/[/._]/g, '-');
23
+
24
+ /**
25
+ * @typedef {object} Environment
26
+ * @property {string} apiUrl
27
+ * @property {number} pipelineId
28
+ * @property {number} projectId
29
+ * @property {string} token
30
+ */
31
+
32
+ /**
33
+ * @param {any} obj
34
+ * @returns {obj is Environment}
35
+ */
36
+ export const isEnvironment = (obj) =>
37
+ obj &&
38
+ typeof obj === 'object' &&
39
+ typeof obj.apiUrl === 'string' &&
40
+ typeof obj.pipelineId === 'number' &&
41
+ typeof obj.projectId === 'number' &&
42
+ typeof obj.token === 'string';
43
+
44
+ /**
45
+ * @returns {Environment}
46
+ */
47
+ export const getEnvironment = () => {
48
+ const {
49
+ CI_API_V4_URL: apiUrl,
50
+ CI_BOT_TOKEN: token,
51
+ CI_PIPELINE_ID: pipelineId,
52
+ CI_PROJECT_ID: projectId,
53
+ } = process.env;
54
+
55
+ const environment = {
56
+ apiUrl,
57
+ pipelineId: pipelineId ? Number.parseInt(pipelineId, 10) : undefined,
58
+ projectId: projectId ? Number.parseInt(projectId, 10) : undefined,
59
+ token,
60
+ };
61
+
62
+ if (!isEnvironment(environment)) {
63
+ throw new Error('Missing required environment variables.');
64
+ }
65
+
66
+ console.info({
67
+ CI_API_V4_URL: environment.apiUrl,
68
+ CI_BOT_TOKEN: environment.token,
69
+ CI_PIPELINE_ID: environment.pipelineId,
70
+ CI_PROJECT_ID: environment.projectId,
71
+ });
72
+
73
+ return environment;
74
+ };
75
+
76
+ /**
77
+ * @param {Environment} Environment
78
+ * @param {string} endpoint
79
+ * @param {RequestInit} [init={}]
80
+ * @returns {Promise<any>}
81
+ * @throws on response error
82
+ */
83
+ const _fetchGitlab = async ({ apiUrl, token }, endpoint, init = {}) => {
84
+ const response = await fetch(`${apiUrl}${endpoint}`, {
85
+ headers: {
86
+ 'content-type': 'application/json',
87
+ 'private-token': token,
88
+ ...init.headers,
89
+ },
90
+ ...init,
91
+ });
92
+
93
+ if (!response.ok) {
94
+ throw new Error(
95
+ `API Error: ${response.status} ${response.statusText} - ${await response.text()}`,
96
+ );
97
+ }
98
+
99
+ return response.json().catch(() => undefined);
100
+ };
101
+
102
+ /**
103
+ * @typedef {function} FetchGitlab
104
+ * @param {string} endpoint
105
+ * @param {RequestInit} [init={}]
106
+ * @returns {Promise<any>}
107
+ * @throws on response error
108
+ */
109
+
110
+ /**
111
+ * @param {Environment} environment
112
+ * @returns {FetchGitlab}
113
+ * @throws
114
+ */
115
+ export const getFetchGitlab =
116
+ (environment) =>
117
+ /**
118
+ * @param {string} endpoint
119
+ * @param {RequestInit} [init={}]
120
+ * @returns {Promise<any>}
121
+ */
122
+ (endpoint, init) =>
123
+ _fetchGitlab(environment, endpoint, init);
124
+
125
+ /**
126
+ * @typedef {object} MergeRequest
127
+ * @property {string} source_branch
128
+ * @property {number} iid
129
+ */
130
+
131
+ /**
132
+ * @param {any} obj
133
+ * @returns {obj is MergeRequest}
134
+ */
135
+ export const isMergeRequest = (obj) =>
136
+ obj &&
137
+ typeof obj === 'object' &&
138
+ typeof obj.source_branch === 'string' &&
139
+ typeof obj.iid === 'number';
140
+
141
+ /**
142
+ * @param {FetchGitlab} fetchGitlab
143
+ * @param {number} projectId
144
+ * @returns {Promise<MergeRequest[]>}
145
+ */
146
+ export const fetchMergeRequests = async (fetchGitlab, projectId) => {
147
+ const data = await fetchGitlab(
148
+ `/projects/${projectId}/merge_requests?state=opened&scope=all&order_by=updated_at&sort=desc`,
149
+ );
150
+
151
+ return Array.isArray(data) && data.every((element) => isMergeRequest(element))
152
+ ? data
153
+ : [];
154
+ };
155
+
156
+ /**
157
+ * @typedef {object} Author
158
+ * @property {string} username
159
+ */
160
+
161
+ /**
162
+ * @typedef {object} CommentObject
163
+ * @property {number} id
164
+ * @property {string} body
165
+ * @property {Author} author
166
+ */
167
+
168
+ /**
169
+ * @param {any} obj
170
+ * @returns {obj is CommentObject}
171
+ */
172
+ export const isCommentObject = (obj) =>
173
+ obj &&
174
+ typeof obj === 'object' &&
175
+ typeof obj.id === 'number' &&
176
+ typeof obj.body === 'string' &&
177
+ obj.author &&
178
+ typeof obj.author === 'object' &&
179
+ typeof obj.author.username === 'string';
180
+
181
+ /**
182
+ * @param {FetchGitlab} fetchGitlab
183
+ * @param {number} projectId
184
+ * @param {number} mrId
185
+ * @returns {Promise<CommentObject[]>}
186
+ */
187
+ export const fetchComments = async (fetchGitlab, projectId, mrId) => {
188
+ const data = await fetchGitlab(
189
+ `/projects/${projectId}/merge_requests/${mrId}/notes`,
190
+ );
191
+
192
+ return Array.isArray(data) &&
193
+ data.every((element) => isCommentObject(element))
194
+ ? data
195
+ : [];
196
+ };
197
+
198
+ /**
199
+ * @param {FetchGitlab} fetchGitlab
200
+ * @param {number} projectId
201
+ * @param {number} mrId
202
+ * @param {number} commentId
203
+ * @returns {Promise<void>}
204
+ */
205
+ export const deleteComment = async (
206
+ fetchGitlab,
207
+ projectId,
208
+ mrId,
209
+ commentId,
210
+ ) => {
211
+ await fetchGitlab(
212
+ `/projects/${projectId}/merge_requests/${mrId}/notes/${commentId}`,
213
+ { method: 'DELETE' },
214
+ );
215
+
216
+ console.log(`Deleted comment with ID: ${commentId}`);
217
+ };
218
+
219
+ /**
220
+ * @param {FetchGitlab} fetchGitlab
221
+ * @param {number} projectId
222
+ * @param {number} mrId
223
+ * @param {string} body
224
+ * @returns {Promise<void>}
225
+ */
226
+ export const postComment = async (fetchGitlab, projectId, mrId, body) => {
227
+ await fetchGitlab(`/projects/${projectId}/merge_requests/${mrId}/notes`, {
228
+ body: JSON.stringify({ body }),
229
+ method: 'POST',
230
+ });
231
+
232
+ console.log('Comment posted successfully.');
233
+ };
234
+
235
+ /**
236
+ * @typedef {object} PipelineUser
237
+ * @property {Author} user
238
+ */
239
+
240
+ /**
241
+ * @param {any} obj
242
+ * @returns {obj is PipelineUser}
243
+ */
244
+ export const isPipelineUser = (obj) =>
245
+ obj &&
246
+ typeof obj === 'object' &&
247
+ obj.user &&
248
+ typeof obj.user === 'object' &&
249
+ typeof obj.user.username === 'string';
250
+
251
+ /**
252
+ * @param {FetchGitlab} fetchGitlab
253
+ * @param {number} projectId
254
+ * @param {number} pipelineId
255
+ * @returns {Promise<string>}
256
+ */
257
+ export const fetchPipelineUser = async (fetchGitlab, projectId, pipelineId) => {
258
+ const data = await fetchGitlab(
259
+ `/projects/${projectId}/pipelines/${pipelineId}`,
260
+ );
261
+
262
+ if (!isPipelineUser(data)) {
263
+ throw new Error('Invalid pipeline user data');
264
+ }
265
+
266
+ return data.user.username;
267
+ };
268
+
269
+ /**
270
+ * @param {FetchGitlab} fetchGitlab
271
+ * @param {number} projectId
272
+ * @param {string} pipelineUsername
273
+ * @param {number} mergeRequestId
274
+ * @param {string} headline
275
+ * @param {string} body
276
+ * @returns {Promise<void>}
277
+ */
278
+ export const replaceComments = async (
279
+ fetchGitlab,
280
+ projectId,
281
+ pipelineUsername,
282
+ mergeRequestId,
283
+ headline,
284
+ body,
285
+ ) => {
286
+ const comments = await fetchComments(fetchGitlab, projectId, mergeRequestId);
287
+
288
+ const matchingComments = await comments.filter(
289
+ (comment) =>
290
+ comment.author.username === pipelineUsername &&
291
+ comment.body.split('\n').at(0) === headline,
292
+ );
293
+
294
+ console.info(`Found ${matchingComments.length} matching comment(s).`);
295
+
296
+ await Promise.all(
297
+ matchingComments.map((matchingComment) =>
298
+ deleteComment(fetchGitlab, projectId, mergeRequestId, matchingComment.id),
299
+ ),
300
+ );
301
+
302
+ await postComment(fetchGitlab, projectId, mergeRequestId, body);
303
+ };