|
1 | 1 | import * as fs from "fs-extra"; |
2 | | -import { FirebaseError, getErrMsg } from "../error"; |
3 | 2 | import { logger } from "../logger"; |
4 | 3 | import * as pathUtil from "path"; |
| 4 | +import * as utils from "../utils"; |
| 5 | +import { UploadReleaseResult, TestDevice, ReleaseTest } from "../appdistribution/types"; |
| 6 | +import { AppDistributionClient } from "./client"; |
| 7 | +import { FirebaseError, getErrMsg, getErrStatus } from "../error"; |
| 8 | + |
| 9 | +const TEST_MAX_POLLING_RETRIES = 40; |
| 10 | +const TEST_POLLING_INTERVAL_MILLIS = 30_000; |
5 | 11 |
|
6 | 12 | export enum DistributionFileType { |
7 | 13 | IPA = "ipa", |
8 | 14 | APK = "apk", |
9 | 15 | AAB = "aab", |
10 | 16 | } |
11 | 17 |
|
| 18 | +/** Upload a distribution */ |
| 19 | +export async function upload( |
| 20 | + requests: AppDistributionClient, |
| 21 | + appName: string, |
| 22 | + distribution: Distribution, |
| 23 | +): Promise<string> { |
| 24 | + utils.logBullet("uploading binary..."); |
| 25 | + try { |
| 26 | + const operationName = await requests.uploadRelease(appName, distribution); |
| 27 | + |
| 28 | + // The upload process is asynchronous, so poll to figure out when the upload has finished successfully |
| 29 | + const uploadResponse = await requests.pollUploadStatus(operationName); |
| 30 | + |
| 31 | + const release = uploadResponse.release; |
| 32 | + switch (uploadResponse.result) { |
| 33 | + case UploadReleaseResult.RELEASE_CREATED: |
| 34 | + utils.logSuccess( |
| 35 | + `uploaded new release ${release.displayVersion} (${release.buildVersion}) successfully!`, |
| 36 | + ); |
| 37 | + break; |
| 38 | + case UploadReleaseResult.RELEASE_UPDATED: |
| 39 | + utils.logSuccess( |
| 40 | + `uploaded update to existing release ${release.displayVersion} (${release.buildVersion}) successfully!`, |
| 41 | + ); |
| 42 | + break; |
| 43 | + case UploadReleaseResult.RELEASE_UNMODIFIED: |
| 44 | + utils.logSuccess( |
| 45 | + `re-uploaded already existing release ${release.displayVersion} (${release.buildVersion}) successfully!`, |
| 46 | + ); |
| 47 | + break; |
| 48 | + default: |
| 49 | + utils.logSuccess( |
| 50 | + `uploaded release ${release.displayVersion} (${release.buildVersion}) successfully!`, |
| 51 | + ); |
| 52 | + } |
| 53 | + utils.logSuccess(`View this release in the Firebase console: ${release.firebaseConsoleUri}`); |
| 54 | + utils.logSuccess(`Share this release with testers who have access: ${release.testingUri}`); |
| 55 | + utils.logSuccess( |
| 56 | + `Download the release binary (link expires in 1 hour): ${release.binaryDownloadUri}`, |
| 57 | + ); |
| 58 | + return uploadResponse.release.name; |
| 59 | + } catch (err: unknown) { |
| 60 | + if (getErrStatus(err) === 404) { |
| 61 | + throw new FirebaseError( |
| 62 | + `App Distribution could not find your app ${appName}. ` + |
| 63 | + `Make sure to onboard your app by pressing the "Get started" ` + |
| 64 | + "button on the App Distribution page in the Firebase console: " + |
| 65 | + "https://console.firebase.google.com/project/_/appdistribution", |
| 66 | + { exit: 1 }, |
| 67 | + ); |
| 68 | + } |
| 69 | + throw new FirebaseError(`Failed to upload release. ${getErrMsg(err)}`, { exit: 1 }); |
| 70 | + } |
| 71 | +} |
| 72 | + |
12 | 73 | /** |
13 | 74 | * Object representing an APK, AAB or IPA file. Used for uploading app distributions. |
14 | 75 | */ |
@@ -58,3 +119,63 @@ export class Distribution { |
58 | 119 | return this.fileName; |
59 | 120 | } |
60 | 121 | } |
| 122 | + |
| 123 | +/** Wait for release tests to complete */ |
| 124 | +export async function awaitTestResults( |
| 125 | + releaseTests: ReleaseTest[], |
| 126 | + requests: AppDistributionClient, |
| 127 | +): Promise<void> { |
| 128 | + const releaseTestNames = new Set( |
| 129 | + releaseTests.map((rt) => rt.name).filter((n): n is string => !!n), |
| 130 | + ); |
| 131 | + for (let i = 0; i < TEST_MAX_POLLING_RETRIES; i++) { |
| 132 | + utils.logBullet(`${releaseTestNames.size} automated test results are pending...`); |
| 133 | + await delay(TEST_POLLING_INTERVAL_MILLIS); |
| 134 | + for (const releaseTestName of releaseTestNames) { |
| 135 | + const releaseTest = await requests.getReleaseTest(releaseTestName); |
| 136 | + if (releaseTest.deviceExecutions.every((e) => e.state === "PASSED")) { |
| 137 | + releaseTestNames.delete(releaseTestName); |
| 138 | + if (releaseTestNames.size === 0) { |
| 139 | + utils.logSuccess("Automated test(s) passed!"); |
| 140 | + return; |
| 141 | + } else { |
| 142 | + continue; |
| 143 | + } |
| 144 | + } |
| 145 | + for (const execution of releaseTest.deviceExecutions) { |
| 146 | + const device = deviceToString(execution.device); |
| 147 | + switch (execution.state) { |
| 148 | + case "PASSED": |
| 149 | + case "IN_PROGRESS": |
| 150 | + continue; |
| 151 | + case "FAILED": |
| 152 | + throw new FirebaseError( |
| 153 | + `Automated test failed for ${device}: ${execution.failedReason}`, |
| 154 | + { exit: 1 }, |
| 155 | + ); |
| 156 | + case "INCONCLUSIVE": |
| 157 | + throw new FirebaseError( |
| 158 | + `Automated test inconclusive for ${device}: ${execution.inconclusiveReason}`, |
| 159 | + { exit: 1 }, |
| 160 | + ); |
| 161 | + default: |
| 162 | + throw new FirebaseError( |
| 163 | + `Unsupported automated test state for ${device}: ${execution.state}`, |
| 164 | + { exit: 1 }, |
| 165 | + ); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + throw new FirebaseError("It took longer than expected to run your test(s), please try again.", { |
| 171 | + exit: 1, |
| 172 | + }); |
| 173 | +} |
| 174 | + |
| 175 | +function delay(ms: number): Promise<number> { |
| 176 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 177 | +} |
| 178 | + |
| 179 | +function deviceToString(device: TestDevice): string { |
| 180 | + return `${device.model} (${device.version}/${device.orientation}/${device.locale})`; |
| 181 | +} |
0 commit comments