|
| 1 | +// eslint-disable-next-line import/no-extraneous-dependencies |
| 2 | +import { Octokit } from '@octokit/rest'; |
| 3 | + |
| 4 | +const RC_BUILD_COMMENT_MARKER = '<!-- metamask-bot-rc-build-announce -->'; |
| 5 | +const TESTFLIGHT_URL = 'https://testflight.apple.com/join/hBrjtFuA'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Checks if a URL value is valid (not empty, null, placeholder, and proper URL format). |
| 9 | + * @param {string | undefined} url - The URL to check |
| 10 | + * @returns {boolean} Whether the URL is valid |
| 11 | + */ |
| 12 | +function isValidUrl(url) { |
| 13 | + if (!url || typeof url !== 'string') { |
| 14 | + return false; |
| 15 | + } |
| 16 | + const trimmed = url.trim().toLowerCase(); |
| 17 | + if (trimmed === '' || trimmed === 'n/a' || trimmed === 'null' || trimmed === 'undefined') { |
| 18 | + return false; |
| 19 | + } |
| 20 | + try { |
| 21 | + const parsed = new URL(url); |
| 22 | + return parsed.protocol === 'https:' || parsed.protocol === 'http:'; |
| 23 | + } catch { |
| 24 | + return false; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Minimizes (hides) a comment using GitHub GraphQL API. |
| 30 | + * @param {Octokit} octokit - Octokit instance |
| 31 | + * @param {string} nodeId - The GraphQL node ID of the comment |
| 32 | + * @returns {Promise<boolean>} Whether the operation was successful |
| 33 | + */ |
| 34 | +async function minimizeComment(octokit, nodeId) { |
| 35 | + try { |
| 36 | + await octokit.graphql( |
| 37 | + ` |
| 38 | + mutation MinimizeComment($id: ID!, $classifier: ReportedContentClassifiers!) { |
| 39 | + minimizeComment(input: { subjectId: $id, classifier: $classifier }) { |
| 40 | + minimizedComment { |
| 41 | + isMinimized |
| 42 | + minimizedReason |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + `, |
| 47 | + { |
| 48 | + id: nodeId, |
| 49 | + classifier: 'OUTDATED', |
| 50 | + }, |
| 51 | + ); |
| 52 | + return true; |
| 53 | + } catch (error) { |
| 54 | + console.error(`Failed to minimize comment ${nodeId}:`, error.message); |
| 55 | + return false; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Posts a new PR comment with RC build links and minimizes older RC build comments. |
| 61 | + * |
| 62 | + * iOS uses TestFlight link with build number reference. |
| 63 | + * Android uses Bitrise public install page URL. |
| 64 | + * |
| 65 | + * Requires environment variables: GITHUB_TOKEN, GITHUB_REPOSITORY, PR_NUMBER, SEMVER, |
| 66 | + * BUILD_NUMBER, ANDROID_PUBLIC_URL, BITRISE_PIPELINE_URL |
| 67 | + */ |
| 68 | +async function start() { |
| 69 | + const { |
| 70 | + GITHUB_TOKEN, |
| 71 | + GITHUB_REPOSITORY, |
| 72 | + PR_NUMBER, |
| 73 | + SEMVER, |
| 74 | + BUILD_NUMBER, |
| 75 | + ANDROID_PUBLIC_URL, |
| 76 | + BITRISE_PIPELINE_URL, |
| 77 | + } = process.env; |
| 78 | + |
| 79 | + // Validate required environment variables |
| 80 | + if (!GITHUB_TOKEN?.trim() || !GITHUB_REPOSITORY?.trim() || !PR_NUMBER?.trim()) { |
| 81 | + console.error( |
| 82 | + 'Missing or empty required environment variables: GITHUB_TOKEN, GITHUB_REPOSITORY, PR_NUMBER', |
| 83 | + ); |
| 84 | + process.exit(1); |
| 85 | + } |
| 86 | + |
| 87 | + const [owner, repo] = GITHUB_REPOSITORY.split('/'); |
| 88 | + if (!owner || !repo) { |
| 89 | + console.error(`GITHUB_REPOSITORY must be in format owner/repo, got: ${GITHUB_REPOSITORY}`); |
| 90 | + process.exit(1); |
| 91 | + } |
| 92 | + |
| 93 | + const prNumber = parseInt(PR_NUMBER, 10); |
| 94 | + if (isNaN(prNumber) || prNumber <= 0) { |
| 95 | + console.error(`PR_NUMBER must be a positive integer, got: ${PR_NUMBER}`); |
| 96 | + process.exit(1); |
| 97 | + } |
| 98 | + |
| 99 | + const octokit = new Octokit({ auth: GITHUB_TOKEN }); |
| 100 | + |
| 101 | + // Build the comment body |
| 102 | + const rows = []; |
| 103 | + const version = SEMVER || 'Unknown'; |
| 104 | + const buildNum = BUILD_NUMBER || 'Unknown'; |
| 105 | + |
| 106 | + // iOS always uses TestFlight link with build number reference |
| 107 | + rows.push(`| **iOS** | [TestFlight](${TESTFLIGHT_URL}) | Go to TestFlight and download build \`${buildNum}\` |`); |
| 108 | + |
| 109 | + // Add Android row if public URL is available |
| 110 | + if (isValidUrl(ANDROID_PUBLIC_URL)) { |
| 111 | + rows.push(`| **Android** | [Install](${ANDROID_PUBLIC_URL}) | RC ${version} (${buildNum}) |`); |
| 112 | + } else { |
| 113 | + console.error('ERROR: No Android public install URL available.'); |
| 114 | + console.error(` ANDROID_PUBLIC_URL: ${ANDROID_PUBLIC_URL || '(not set)'}`); |
| 115 | + console.error('This may indicate a Bitrise configuration issue - artifact may not have public page enabled.'); |
| 116 | + process.exit(1); |
| 117 | + } |
| 118 | + |
| 119 | + const pipelineLink = isValidUrl(BITRISE_PIPELINE_URL) |
| 120 | + ? `[View Pipeline](${BITRISE_PIPELINE_URL})` |
| 121 | + : 'Not available'; |
| 122 | + |
| 123 | + const commentBody = `${RC_BUILD_COMMENT_MARKER} |
| 124 | +### :rocket: RC Builds Ready for Testing |
| 125 | +
|
| 126 | +| Platform | Link | Version | |
| 127 | +| :--- | :--- | :--- | |
| 128 | +${rows.join('\n')} |
| 129 | +
|
| 130 | +<details> |
| 131 | +<summary>More Info</summary> |
| 132 | +
|
| 133 | +* **Version**: \`${version}\` |
| 134 | +* **Build Number**: \`${buildNum}\` |
| 135 | +* **Bitrise Pipeline**: ${pipelineLink} |
| 136 | +</details> |
| 137 | +`; |
| 138 | + |
| 139 | + // Post new comment and minimize old ones |
| 140 | + try { |
| 141 | + console.log(`\n=== Searching for existing RC build comments on PR #${prNumber} ===`); |
| 142 | + const comments = await octokit.paginate(octokit.rest.issues.listComments, { |
| 143 | + owner, |
| 144 | + repo, |
| 145 | + issue_number: prNumber, |
| 146 | + }); |
| 147 | + |
| 148 | + console.log(`Found ${comments.length} total comments on PR #${prNumber}`); |
| 149 | + |
| 150 | + // Find all existing RC build bot comments |
| 151 | + const existingBotComments = comments.filter( |
| 152 | + (comment) => comment.body && comment.body.includes(RC_BUILD_COMMENT_MARKER), |
| 153 | + ); |
| 154 | + |
| 155 | + console.log(`Found ${existingBotComments.length} existing RC build comment(s)`); |
| 156 | + |
| 157 | + // Create new comment first (so it appears at the bottom) |
| 158 | + console.log('Creating new comment with RC build URLs...'); |
| 159 | + await octokit.rest.issues.createComment({ |
| 160 | + owner, |
| 161 | + repo, |
| 162 | + issue_number: prNumber, |
| 163 | + body: commentBody, |
| 164 | + }); |
| 165 | + console.log(`✓ Successfully created new comment with RC build URLs`); |
| 166 | + |
| 167 | + // Minimize all previous RC build comments |
| 168 | + if (existingBotComments.length > 0) { |
| 169 | + console.log(`\n=== Minimizing ${existingBotComments.length} older RC build comment(s) ===`); |
| 170 | + for (const comment of existingBotComments) { |
| 171 | + if (comment.node_id) { |
| 172 | + console.log(`Minimizing comment ID: ${comment.id} (node_id: ${comment.node_id})`); |
| 173 | + const success = await minimizeComment(octokit, comment.node_id); |
| 174 | + if (success) { |
| 175 | + console.log(`✓ Minimized comment ${comment.id}`); |
| 176 | + } |
| 177 | + } else { |
| 178 | + console.warn(`Comment ${comment.id} does not have a node_id, skipping minimization`); |
| 179 | + } |
| 180 | + } |
| 181 | + console.log(`✓ Finished processing older RC build comments`); |
| 182 | + } |
| 183 | + } catch (error) { |
| 184 | + console.error('Error posting/minimizing comments:', error); |
| 185 | + if (error.status === 403) { |
| 186 | + console.error('Permission denied. Ensure the GITHUB_TOKEN has "pull-requests: write" permission.'); |
| 187 | + } |
| 188 | + process.exit(1); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +start().catch((error) => { |
| 193 | + console.error(error); |
| 194 | + process.exit(1); |
| 195 | +}); |
0 commit comments