|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +/*:: |
| 14 | +import type {PackageJson} from '../shared/monorepoUtils'; |
| 15 | +*/ |
| 16 | + |
| 17 | +const {validateAndParseDotSlashFile} = require('./utils/dotslash-utils'); |
| 18 | +const {REPO_ROOT} = require('../shared/consts'); |
| 19 | +const {parseArgs, styleText} = require('util'); |
| 20 | +const path = require('path'); |
| 21 | +const {Octokit} = require('@octokit/rest'); |
| 22 | +const { |
| 23 | + FIRST_PARTY_DOTSLASH_FILES, |
| 24 | +} = require('./write-dotslash-release-asset-urls'); |
| 25 | + |
| 26 | +const config = { |
| 27 | + allowPositionals: true, |
| 28 | + options: { |
| 29 | + token: {type: 'string'}, |
| 30 | + releaseId: {type: 'string'}, |
| 31 | + force: {type: 'boolean', default: false}, |
| 32 | + dryRun: {type: 'boolean', default: false}, |
| 33 | + help: {type: 'boolean'}, |
| 34 | + }, |
| 35 | +}; |
| 36 | + |
| 37 | +async function main() { |
| 38 | + const { |
| 39 | + positionals: [version], |
| 40 | + values: {help, token, releaseId, force, dryRun}, |
| 41 | + /* $FlowFixMe[incompatible-call] Natural Inference rollout. See |
| 42 | + * https://fburl.com/workplace/6291gfvu */ |
| 43 | + } = parseArgs(config); |
| 44 | + |
| 45 | + if (help) { |
| 46 | + console.log(` |
| 47 | + Usage: node ./scripts/releases/upload-release-assets-for-dotslash.js <version> --release_id <id> --token <github-token> [--force] |
| 48 | +
|
| 49 | + Scans first-party DotSlash files in the repo for URLs referencing assets of |
| 50 | + an upcoming release, and uploads the actual assets to the GitHub release |
| 51 | + identified by the given release ID. |
| 52 | +
|
| 53 | + If run with --force, the script will overwrite any assets that happen to |
| 54 | + already exist at the given URLs. This is useful for retrying failed or |
| 55 | + corrupted uploads. |
| 56 | +`); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (version == null) { |
| 61 | + throw new Error('Missing version argument'); |
| 62 | + } |
| 63 | + |
| 64 | + await uploadReleaseAssetsForDotSlash({ |
| 65 | + version, |
| 66 | + token, |
| 67 | + releaseId, |
| 68 | + force, |
| 69 | + dryRun, |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +async function uploadReleaseAssetsForDotSlash( |
| 74 | + {version, token, releaseId, force = false, dryRun = false} /*: { |
| 75 | + version: string, |
| 76 | + token: string, |
| 77 | + releaseId: string, |
| 78 | + force?: boolean, |
| 79 | + dryRun?: boolean, |
| 80 | + } */, |
| 81 | +) /*: Promise<void> */ { |
| 82 | + const releaseTag = `v${version}`; |
| 83 | + const releaseAssetPrefix = `https://github.com/facebook/react-native/releases/download/${encodeURIComponent(releaseTag)}/`; |
| 84 | + const octokit = new Octokit({auth: token}); |
| 85 | + const existingAssets = await octokit.repos.listReleaseAssets({ |
| 86 | + owner: 'facebook', |
| 87 | + repo: 'react-native', |
| 88 | + release_id: releaseId, |
| 89 | + }); |
| 90 | + const existingAssetsByName = new Map( |
| 91 | + existingAssets.data.map(asset => [asset.name, asset]), |
| 92 | + ); |
| 93 | + for (const filename of FIRST_PARTY_DOTSLASH_FILES) { |
| 94 | + const fullPath = path.join(REPO_ROOT, filename); |
| 95 | + console.log(`Uploading assets for ${filename}...`); |
| 96 | + const uploadPromises = []; |
| 97 | + await processDotSlashFileInPlace( |
| 98 | + fullPath, |
| 99 | + ( |
| 100 | + providers, |
| 101 | + // Ignore _suggestedFilename in favour of reading the actual asset URLs |
| 102 | + _suggestedFilename, |
| 103 | + ) => { |
| 104 | + let upstreamUrl, targetReleaseAssetInfo; |
| 105 | + for (const provider of providers) { |
| 106 | + if (provider.type != null && provider.type !== 'http') { |
| 107 | + continue; |
| 108 | + } |
| 109 | + const url = provider.url; |
| 110 | + if (url.startsWith(releaseAssetPrefix)) { |
| 111 | + const name = decodeURIComponent( |
| 112 | + url.slice(releaseAssetPrefix.length), |
| 113 | + ); |
| 114 | + targetReleaseAssetInfo = {name, url}; |
| 115 | + } else { |
| 116 | + upstreamUrl = url; |
| 117 | + } |
| 118 | + if (upstreamUrl != null && targetReleaseAssetInfo != null) { |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + if (targetReleaseAssetInfo == null) { |
| 123 | + // This DotSlash providers array does not reference any relevant release asset URLs, so we can ignore it. |
| 124 | + return; |
| 125 | + } |
| 126 | + if (upstreamUrl == null) { |
| 127 | + throw new Error( |
| 128 | + `No upstream URL found for release asset ${targetReleaseAssetInfo.name}`, |
| 129 | + ); |
| 130 | + } |
| 131 | + uploadPromises.push( |
| 132 | + (async () => { |
| 133 | + if (existingAssetsByName.has(targetReleaseAssetInfo.name)) { |
| 134 | + if (!force) { |
| 135 | + console.log( |
| 136 | + `[${targetReleaseAssetInfo.name}] Skipping existing release asset...`, |
| 137 | + ); |
| 138 | + return; |
| 139 | + } |
| 140 | + if (dryRun) { |
| 141 | + console.log( |
| 142 | + `[${targetReleaseAssetInfo.name}] Dry run: Not deleting existing release asset.`, |
| 143 | + ); |
| 144 | + } else { |
| 145 | + console.log( |
| 146 | + `[${targetReleaseAssetInfo.name}] Deleting existing release asset...`, |
| 147 | + ); |
| 148 | + await octokit.repos.deleteReleaseAsset({ |
| 149 | + owner: 'facebook', |
| 150 | + repo: 'react-native', |
| 151 | + asset_id: existingAssetsByName.get( |
| 152 | + targetReleaseAssetInfo.name, |
| 153 | + ).id, |
| 154 | + }); |
| 155 | + } |
| 156 | + } |
| 157 | + console.log( |
| 158 | + `[${targetReleaseAssetInfo.name}] Downloading from ${upstreamUrl}...`, |
| 159 | + ); |
| 160 | + const response = await fetch(upstreamUrl); |
| 161 | + const data = await response.buffer(); |
| 162 | + const contentType = response.headers.get('content-type'); |
| 163 | + if (dryRun) { |
| 164 | + console.log( |
| 165 | + `[${targetReleaseAssetInfo.name}] Dry run: Not uploading to release.`, |
| 166 | + ); |
| 167 | + return; |
| 168 | + } else { |
| 169 | + console.log( |
| 170 | + `[${targetReleaseAssetInfo.name}] Uploading to release...`, |
| 171 | + ); |
| 172 | + await octokit.repos.uploadReleaseAsset({ |
| 173 | + owner: 'facebook', |
| 174 | + repo: 'react-native', |
| 175 | + release_id: releaseId, |
| 176 | + name: targetReleaseAssetInfo.name, |
| 177 | + data, |
| 178 | + headers: { |
| 179 | + 'content-type': contentType, |
| 180 | + }, |
| 181 | + }); |
| 182 | + } |
| 183 | + })(), |
| 184 | + ); |
| 185 | + }, |
| 186 | + ); |
| 187 | + await Promise.all(uploadPromises); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +module.exports = { |
| 192 | + uploadReleaseAssetsForDotSlash, |
| 193 | +}; |
| 194 | + |
| 195 | +if (require.main === module) { |
| 196 | + void main(); |
| 197 | +} |
0 commit comments