-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathslack-rc-notification.mjs
More file actions
457 lines (420 loc) · 14.8 KB
/
slack-rc-notification.mjs
File metadata and controls
457 lines (420 loc) · 14.8 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/**
* Slack RC Build Notification Script
*
* Posts a Slack message after an RC build. The *What's in this RC* block lists
* commits from **git history** (merge-base + ancestry path).
*
* Algorithm:
* - Run `git merge-base <headRef> <baseRef>` (default **headRef**: `HEAD`, override with
* `HEAD_REF`; default **baseRef**: `origin/main`, override with `MERGE_BASE_REF`) to get
* the fork point as a **merge-base commit hash**.
* - List commits on the fork-to-tip path only:
* `git log --ancestry-path <merge-base-hash>..<headRef>` (same full hash from `git merge-base`; output uses short hash + subject), with optional
* PR links from `#123` / `(#123)` in subjects.
* - Omit commits whose subject starts with `[skip ci] Bump version number to` (automation noise).
* - Cap the list (see `MAX_RC_COMMIT_LINES`) and append *…and N more* when needed.
*
* **Fail-open:** If merge-base fails, `git log` errors, or the ancestry path is empty,
* the notification is still sent: *What's in this RC* is omitted in favor of a short
* fallback with a link to release notes on GitHub when needed. The process exits 0;
* git problems are logged to stderr. This matches CI/local use where shallow clones or
* missing refs can make history unavailable.
*
* Required environment variables:
* - SEMVER: Semantic version (e.g. "7.40.0")
* - IOS_BUILD_NUMBER: iOS build number
* - ANDROID_BUILD_NUMBER: Android build number
* - SLACK_BOT_TOKEN: Slack Bot OAuth token for API calls
*
* Optional environment variables:
* - ANDROID_PUBLIC_URL: Public URL for Android APK download
* - IOS_PUBLIC_URL: Public URL for iOS build
* - BUILD_PIPELINE_URL: GitHub Actions pipeline URL (footer: pipeline + release notes link)
* - GITHUB_REPOSITORY: "owner/repo" (defaults to metamask-mobile)
* - MERGE_BASE_REF: Ref for merge-base (default: origin/main)
* - HEAD_REF: Tip ref for `git merge-base` and `git log` range (default: `HEAD`; e.g. a
* branch name or SHA to preview another tip without checking it out)
* - SLACK_RC_NOTIFICATION_DRY_RUN: Set to `1` or `true` to print the message JSON and
* exit without calling Slack (`SLACK_BOT_TOKEN` not required in this mode)
*/
import { execFileSync } from 'child_process';
// Configuration
const REPO_URL = process.env.GITHUB_REPOSITORY
? `https://github.com/${process.env.GITHUB_REPOSITORY}`
: 'https://github.com/MetaMask/metamask-mobile';
const MAX_RC_COMMIT_LINES = 10;
/** Commits whose subject starts with this (version bump automation) are omitted from the RC list. */
const SKIP_CI_BUMP_VERSION_SUBJECT = /^\[skip ci\] Bump version number to/;
/**
* Run `git merge-base` and return the merge-base **commit hash**, or `null` if git errors
* or output is empty (fail-open: caller skips the RC commit list).
* @param {string} headRef
* @param {string} baseRef
* @returns {string|null} Merge-base commit hash (hex string from `git merge-base`), or `null` on failure
*/
function getMergeBase(headRef, baseRef) {
try {
const out = execFileSync('git', ['merge-base', headRef, baseRef], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
});
const sha = out.trim();
return sha || null;
} catch (error) {
console.error(
`[slack-rc-notification] git merge-base ${headRef} ${baseRef} failed (RC commit list skipped): ${error.message}`,
);
return null;
}
}
/**
* @param {string} mergeBaseCommitHash - Commit hash returned by `git merge-base` (common ancestor of `headRef` and the base ref)
* @param {string} headRef - Tip ref (default in callers: `HEAD`, overridable via `HEAD_REF`)
* @returns {string[]} Raw `hash\\tsubject` lines from `git log --ancestry-path` from merge-base to `headRef`,
* excluding `[skip ci] Bump version number to …` subjects; empty array if no commits or if git fails (fail-open).
*/
function getAncestryPathLogLines(mergeBaseCommitHash, headRef) {
try {
const out = execFileSync(
'git',
['log', '--ancestry-path', `${mergeBaseCommitHash}..${headRef}`, '--pretty=format:%h\t%s'],
{
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
},
);
if (!out.trim()) {
return [];
}
return out
.trim()
.split('\n')
.filter((line) => {
const tab = line.indexOf('\t');
const subject = tab >= 0 ? line.slice(tab + 1) : line;
return !SKIP_CI_BUMP_VERSION_SUBJECT.test(subject.trim());
});
} catch (error) {
console.error(
`[slack-rc-notification] git log --ancestry-path failed (RC commit list skipped): ${error.message}`,
);
return [];
}
}
/**
* Link (#123) and standalone #123 in a commit subject to the repo PR URL (Slack mrkdwn).
* @param {string} subject
* @returns {string}
*/
function formatSubjectWithPrLinks(subject) {
let result = subject;
result = result.replace(/\(#(\d+)\)/g, (_, n) => `(<${REPO_URL}/pull/${n}|#${n}>)`);
result = result.replace(/(^|\s)#(\d+)\b/g, (_, lead, n) => `${lead}<${REPO_URL}/pull/${n}|#${n}>`);
return result;
}
/**
* Format one `hash\\tsubject` log line for Slack (bullet, short hash, subject with PR links).
* @param {string} line
* @returns {string}
*/
function formatCommitLineForSlack(line) {
const tab = line.indexOf('\t');
const hash = tab >= 0 ? line.slice(0, tab) : '';
const subject = tab >= 0 ? line.slice(tab + 1) : line;
const linked = formatSubjectWithPrLinks(subject);
return `• \`${hash}\` ${linked}`;
}
/**
* Build Slack mrkdwn for RC commits (capped) and optional "...and N more" line.
* @param {string[]} logLines - Full list of hash\\tsubject lines
* @param {number} maxEntries
* @returns {{ text: string, hasEntries: boolean }}
*/
function formatCommitsForSlack(logLines, maxEntries = MAX_RC_COMMIT_LINES) {
if (!logLines.length) {
return { text: '', hasEntries: false };
}
const slice = logLines.slice(0, maxEntries);
const remaining = logLines.length - slice.length;
const bullets = slice.map(formatCommitLineForSlack);
if (remaining > 0) {
bullets.push(`\n_...and ${remaining} more_`);
}
return { text: bullets.join('\n'), hasEntries: true };
}
/**
* Resolve merge-base with `MERGE_BASE_REF` (default `origin/main`) and collect commits
* via `git log --ancestry-path`.
* @returns {{ text: string, hasEntries: boolean }} Slack mrkdwn and whether to show the RC list
*/
function extractRcCommitsFromGit() {
const baseRef = process.env.MERGE_BASE_REF ?? 'origin/main';
const headRef = (process.env.HEAD_REF ?? '').trim() || 'HEAD';
console.log(`\n📖 Git history (merge-base ${headRef} with ${baseRef}, ancestry-path to ${headRef})...`);
const mergeBase = getMergeBase(headRef, baseRef);
if (!mergeBase) {
console.warn(
' Could not resolve merge-base; skipping “What’s in this RC” (Slack will show fallback + release notes link when available)',
);
return { text: '', hasEntries: false };
}
console.log(` merge-base: ${mergeBase}`);
const logLines = getAncestryPathLogLines(mergeBase, headRef);
if (!logLines.length) {
console.warn(
' No commits on ancestry path; skipping “What’s in this RC” (Slack will show fallback + release notes link when available)',
);
return { text: '', hasEntries: false };
}
console.log(` Found ${logLines.length} commit(s) on ancestry path`);
return formatCommitsForSlack(logLines);
}
/**
* Check if a URL is valid
* @param {string|undefined} url - The URL to check
* @returns {boolean} Whether the URL is valid
*/
function isValidUrl(url) {
if (!url || typeof url !== 'string') {
return false;
}
const trimmed = url.trim().toLowerCase();
if (trimmed === '' || trimmed === 'n/a' || trimmed === 'null' || trimmed === 'undefined') {
return false;
}
try {
const parsed = new URL(url);
return parsed.protocol === 'https:' || parsed.protocol === 'http:';
} catch {
return false;
}
}
/**
* Build the Slack message payload
* @param {Object} options - Message options
* @returns {Object} Slack message payload
*/
function buildSlackMessage(options) {
const {
version,
buildNumber,
androidUrl,
iosUrl,
pipelineUrl,
prNumber,
} = options;
const blocks = [
{
type: 'header',
text: {
type: 'plain_text',
text: `🚀 Mobile RC Build v${version} (${buildNumber})`,
emoji: true,
},
},
{
type: 'section',
fields: [
{
type: 'mrkdwn',
text: `*Version:*\n${version}`,
},
{
type: 'mrkdwn',
text: `*Build Number:*\n${buildNumber}`,
},
],
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: '*📦 Download Links:*',
},
},
{
type: 'section',
fields: [
{
type: 'mrkdwn',
text: isValidUrl(androidUrl)
? `*Android APK:*\n<${androidUrl}|Download>`
: '*Android APK:*\n_Not available_',
},
{
type: 'mrkdwn',
text: isValidUrl(iosUrl)
? `*iOS Build:*\n<${iosUrl}|TestFlight>`
: '*iOS Build:*\n_Check TestFlight_',
},
],
},
];
// Add link to cherry-picks section in PR comment
if (prNumber) {
const cherryPicksLink = `<${REPO_URL}/pull/${prNumber}#cherry-picks|View what's in this RC>`;
blocks.push(
{
type: 'divider',
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*📋 What's in this RC:*\n${cherryPicksLink}`,
},
},
);
} else {
const releaseNotesMrkdwn = `<${REPO_URL}/tree/release/${version}|View release notes>`;
blocks.push({
type: 'section',
text: {
type: 'mrkdwn',
text: `_Cherry-picks list available in the release PR. ${releaseNotesMrkdwn}_`,
},
});
}
// Add pipeline link
if (pipelineUrl) {
blocks.push(
{
type: 'divider',
},
{
type: 'context',
elements: [
{
type: 'mrkdwn',
text: `<${pipelineUrl}|View Build Pipeline> | <${REPO_URL}/tree/release/${version}|View full release notes>`,
},
],
},
);
}
return {
blocks,
text: `🚀 Mobile RC Build v${version} (${buildNumber}) is ready!`, // Fallback text
};
}
/**
* Post message to Slack channel using Web API
* @param {string} botToken - Slack bot token
* @param {string} channelName - Channel name to post to
* @param {Object} payload - Slack message payload
* @returns {Promise<{success: boolean, channelNotFound: boolean}>}
*/
async function postToSlack(botToken, channelName, payload) {
try {
const response = await fetch('https://slack.com/api/chat.postMessage', {
method: 'POST',
headers: {
Authorization: `Bearer ${botToken}`,
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify({
channel: channelName,
blocks: payload.blocks,
text: payload.text,
}),
});
const data = await response.json();
if (!data.ok) {
// Check if channel doesn't exist
if (data.error === 'channel_not_found') {
return { success: false, channelNotFound: true };
}
throw new Error(`Slack API error: ${data.error}`);
}
console.log('✅ Slack notification sent successfully');
return { success: true, channelNotFound: false };
} catch (error) {
console.error(`❌ Failed to post to Slack: ${error.message}`);
return { success: false, channelNotFound: false };
}
}
/**
* Get the Slack channel name for a release version
* @param {string} version - The version string
* @returns {string} The channel name
*/
function getSlackChannel(version) {
const formattedVersion = version.replace(/\./g, '-');
return `#release-mobile-${formattedVersion}`;
}
/**
* Main function
*/
async function main() {
const dryRunEnv = process.env.SLACK_RC_NOTIFICATION_DRY_RUN;
const isDryRun =
dryRunEnv === '1' || String(dryRunEnv).toLowerCase() === 'true';
// Validate required environment variables (fail open - just log and return).
// Dry-run only needs SEMVER so you can inspect blocks without Slack or a token.
const requiredEnvVars = isDryRun ? ['SEMVER'] : ['SEMVER', 'SLACK_BOT_TOKEN'];
const missingVars = requiredEnvVars.filter((v) => !process.env[v]);
if (missingVars.length > 0) {
console.warn(`⚠️ Missing required environment variables: ${missingVars.join(', ')}`);
console.warn('Skipping Slack notification (non-critical)');
return;
}
const version = process.env.SEMVER;
const iosBuildNumber = process.env.IOS_BUILD_NUMBER || 'N/A';
const androidBuildNumber = process.env.ANDROID_BUILD_NUMBER || 'N/A';
const buildNumber = `iOS ${iosBuildNumber} / Android ${androidBuildNumber}`;
const androidUrl = process.env.ANDROID_PUBLIC_URL;
const iosUrl = process.env.IOS_PUBLIC_URL;
const pipelineUrl = process.env.BUILD_PIPELINE_URL;
const botToken = process.env.SLACK_BOT_TOKEN;
const prNumber = process.env.PR_NUMBER || '';
const expectedChannelName = getSlackChannel(version);
console.log(`\n📣 Preparing Slack notification for RC v${version} (${buildNumber})`);
if (prNumber) {
console.log(`📍 Release PR: #${prNumber}`);
}
if (isDryRun) {
console.log('🧪 DRY RUN: will print payload JSON and not call Slack');
} else {
console.log(`📍 Target channel: ${expectedChannelName}`);
}
// Build and send the message
console.log('\n📤 Posting to Slack...');
const payload = buildSlackMessage({
version,
buildNumber,
androidUrl,
iosUrl,
pipelineUrl,
prNumber,
});
if (isDryRun) {
const preview = {
channel: expectedChannelName,
text: payload.text,
blocks: payload.blocks,
};
console.log('\n--- Slack payload (dry run) ---\n');
console.log(JSON.stringify(preview, null, 2));
console.log('\n--- end dry run ---\n');
return;
}
const result = await postToSlack(botToken, expectedChannelName, payload);
if (result.success) {
console.log(`\n✅ RC notification sent to ${expectedChannelName}`);
} else if (result.channelNotFound) {
console.warn(`\n⚠️ Channel ${expectedChannelName} not found in Slack workspace`);
console.warn(' This could mean:');
console.warn(' - The release channel has not been created yet');
console.warn(' - The bot does not have access to the channel');
console.warn(' - The channel name pattern is different');
console.warn('Skipping Slack notification (non-critical)');
} else {
// Fail open - log the error but don't exit with error code
console.log('\n⚠️ RC notification failed but continuing (non-critical)');
}
}
// Run - fail open on errors (non-critical notification)
main().catch((error) => {
console.error('⚠️ Unexpected error (non-critical):', error);
// Don't exit with error code - this is a non-critical notification
});