|
| 1 | +import * as _ from "lodash"; |
| 2 | +import * as api from "../api"; |
| 3 | +import * as utils from "../utils"; |
| 4 | +import { Distribution } from "./distribution"; |
| 5 | +import { FirebaseError } from "../error"; |
| 6 | + |
| 7 | +// tslint:disable-next-line:no-var-requires |
| 8 | +const pkg = require("../../package.json"); |
| 9 | + |
| 10 | +/** |
| 11 | + * Proxies HTTPS requests to the App Distribution server backend. |
| 12 | + */ |
| 13 | +export class AppDistributionClient { |
| 14 | + static MAX_POLLING_RETRIES = 30; |
| 15 | + static POLLING_INTERVAL_MS = 1000; |
| 16 | + |
| 17 | + constructor(private readonly appId: string) {} |
| 18 | + |
| 19 | + async provisionApp(): Promise<void> { |
| 20 | + await api.request("POST", `/v1alpha/apps/${this.appId}`, { |
| 21 | + origin: api.appDistributionOrigin, |
| 22 | + auth: true, |
| 23 | + }); |
| 24 | + |
| 25 | + utils.logSuccess("provisioned for app distribution"); |
| 26 | + } |
| 27 | + |
| 28 | + async getJwtToken(): Promise<string> { |
| 29 | + const apiResponse = await api.request("GET", `/v1alpha/apps/${this.appId}/jwt`, { |
| 30 | + auth: true, |
| 31 | + origin: api.appDistributionOrigin, |
| 32 | + }); |
| 33 | + |
| 34 | + return _.get(apiResponse, "body.token"); |
| 35 | + } |
| 36 | + |
| 37 | + async uploadDistribution(token: string, distribution: Distribution): Promise<string> { |
| 38 | + const apiResponse = await api.request("POST", "/spi/v1/jwt_distributions", { |
| 39 | + origin: api.appDistributionUploadOrigin, |
| 40 | + headers: { |
| 41 | + Authorization: `Bearer ${token}`, |
| 42 | + "X-APP-DISTRO-API-CLIENT-ID": pkg.name, |
| 43 | + "X-APP-DISTRO-API-CLIENT-TYPE": distribution.platform(), |
| 44 | + "X-APP-DISTRO-API-CLIENT-VERSION": pkg.version, |
| 45 | + }, |
| 46 | + files: { |
| 47 | + file: { |
| 48 | + stream: distribution.readStream(), |
| 49 | + size: distribution.fileSize(), |
| 50 | + contentType: "multipart/form-data", |
| 51 | + }, |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + return _.get(apiResponse, "response.headers.etag"); |
| 56 | + } |
| 57 | + |
| 58 | + async pollReleaseIdByHash(hash: string, retryCount = 0): Promise<string> { |
| 59 | + try { |
| 60 | + return await this.getReleaseIdByHash(hash); |
| 61 | + } catch (err) { |
| 62 | + if (retryCount >= AppDistributionClient.MAX_POLLING_RETRIES) { |
| 63 | + throw new FirebaseError(`failed to find the uploaded release: ${err.message}`, { exit: 1 }); |
| 64 | + } |
| 65 | + |
| 66 | + await new Promise((resolve) => |
| 67 | + setTimeout(resolve, AppDistributionClient.POLLING_INTERVAL_MS) |
| 68 | + ); |
| 69 | + |
| 70 | + return this.pollReleaseIdByHash(hash, retryCount + 1); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + async getReleaseIdByHash(hash: string): Promise<string> { |
| 75 | + const apiResponse = await api.request( |
| 76 | + "GET", |
| 77 | + `/v1alpha/apps/${this.appId}/release_by_hash/${hash}`, |
| 78 | + { |
| 79 | + origin: api.appDistributionOrigin, |
| 80 | + auth: true, |
| 81 | + } |
| 82 | + ); |
| 83 | + |
| 84 | + return _.get(apiResponse, "body.release.id"); |
| 85 | + } |
| 86 | + |
| 87 | + async addReleaseNotes(releaseId: string, releaseNotes: string): Promise<void> { |
| 88 | + if (!releaseNotes) { |
| 89 | + utils.logWarning("no release notes specified, skipping"); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + utils.logBullet("adding release notes..."); |
| 94 | + |
| 95 | + const data = { |
| 96 | + releaseNotes: { |
| 97 | + releaseNotes, |
| 98 | + }, |
| 99 | + }; |
| 100 | + |
| 101 | + try { |
| 102 | + await api.request("POST", `/v1alpha/apps/${this.appId}/releases/${releaseId}/notes`, { |
| 103 | + origin: api.appDistributionOrigin, |
| 104 | + auth: true, |
| 105 | + data, |
| 106 | + }); |
| 107 | + } catch (err) { |
| 108 | + throw new FirebaseError(`failed to add release notes with ${err.message}`, { exit: 1 }); |
| 109 | + } |
| 110 | + |
| 111 | + utils.logSuccess("added release notes successfully"); |
| 112 | + } |
| 113 | + |
| 114 | + async enableAccess( |
| 115 | + releaseId: string, |
| 116 | + emails: string[] = [], |
| 117 | + groupIds: string[] = [] |
| 118 | + ): Promise<void> { |
| 119 | + if (emails.length === 0 && groupIds.length === 0) { |
| 120 | + utils.logWarning("no testers or groups specified, skipping"); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + utils.logBullet("adding testers/groups..."); |
| 125 | + |
| 126 | + const data = { |
| 127 | + emails, |
| 128 | + groupIds, |
| 129 | + }; |
| 130 | + |
| 131 | + try { |
| 132 | + await api.request("POST", `/v1alpha/apps/${this.appId}/releases/${releaseId}/enable_access`, { |
| 133 | + origin: api.appDistributionOrigin, |
| 134 | + auth: true, |
| 135 | + data, |
| 136 | + }); |
| 137 | + } catch (err) { |
| 138 | + let errorMessage = err.message; |
| 139 | + if (_.has(err, "context.body.error")) { |
| 140 | + const errorStatus = _.get(err, "context.body.error.status"); |
| 141 | + if (errorStatus === "FAILED_PRECONDITION") { |
| 142 | + errorMessage = "invalid testers"; |
| 143 | + } else if (errorStatus === "INVALID_ARGUMENT") { |
| 144 | + errorMessage = "invalid groups"; |
| 145 | + } |
| 146 | + } |
| 147 | + throw new FirebaseError(`failed to add testers/groups: ${errorMessage}`, { exit: 1 }); |
| 148 | + } |
| 149 | + |
| 150 | + utils.logSuccess("added testers/groups successfully"); |
| 151 | + } |
| 152 | +} |
0 commit comments