@amplitude/ampli
Version:
Amplitude CLI
312 lines (311 loc) • 14.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ora = require("ora");
const chalk_1 = require("chalk");
const AmpliGraphQL_1 = require("../graphql/AmpliGraphQL");
const auth_1 = require("../auth");
const errors_1 = require("../errors");
const settings_1 = require("../settings");
const sentry_1 = require("../sentry");
const icons_1 = require("../ui/icons");
const compareByName_1 = require("../util/sort/compareByName");
const compareVersions_1 = require("../util/sort/compareVersions");
const semver_1 = require("../util/semver");
const runtime_1 = require("../util/runtime");
const types_1 = require("../types");
const ampli_1 = require("../ampli");
const constants_1 = require("../constants");
class BaseAction {
constructor(flags, args, config) {
this.flags = flags;
this.args = args;
this.config = config;
this.id = config.id;
this.print = config.print;
this.println = config.println;
this.debug = config.debug;
this.error = config.error;
}
async getAmpliGraphQLClient(zone) {
if (!BaseAction.ampliGraphQLClient) {
this.authenticate();
BaseAction.ampliGraphQLClient = await AmpliGraphQL_1.default(await auth_1.default.getHttpHeaders(), zone);
}
return BaseAction.ampliGraphQLClient;
}
authenticate(userId) {
const { token, withOrg } = this.flags;
auth_1.default.authenticate(token, userId, withOrg);
}
async getUser(zone) {
const graphql = await this.getAmpliGraphQLClient(zone);
const { orgs } = await graphql.orgs();
const user = Object.assign(Object.assign({}, orgs[0].user), { zone: graphql.zone, orgs: orgs.map(org => ({ id: org.id, name: org.name, workspaces: org.workspaces })) });
const settings = this.getSettings();
settings.setUser(user);
sentry_1.default.captureUser(user);
return user;
}
getSettings() {
return settings_1.getSettings();
}
startSpinner(text) {
if (!this.flags.showProgress) {
this.println(text);
return;
}
if (this.spinner) {
this.spinner.succeed();
}
this.spinner = ora(text).start();
}
stopSpinner(success) {
if (this.spinner) {
if (success) {
this.spinner.stopAndPersist({ symbol: icons_1.ICON_FOUND });
}
else {
this.spinner.stopAndPersist({ symbol: icons_1.ICON_NOT_FOUND });
}
this.spinner = undefined;
}
}
async getOrgWorkspaceBranches(orgId, workspaceId) {
const graphql = await this.getAmpliGraphQLClient();
const response = await graphql.branches({ orgId, workspaceId });
if (response.orgs.length === 0) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.userDoesntHaveAccessToWorkspace), 1);
}
const org = response.orgs[0];
const workspace = org.workspaces[0];
if (workspace == null) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.workspaceNoLongerExist), 1);
}
const branches = workspace.branches ? [...workspace.branches] : [];
branches.sort(compareByName_1.default);
const defaultBranch = branches.find(b => b.default);
if (!defaultBranch) {
this.error(new Error(errors_1.USER_ERROR_MESSAGES.defaultBranchDoesNotExist), 1);
}
return { org, workspace, branches, defaultBranch };
}
async getBranchVersions(orgId, workspaceId, branchId) {
const graphql = await this.getAmpliGraphQLClient();
const response = await graphql.branchVersions({ orgId, workspaceId, branchId });
if (response.orgs.length === 0) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.userDoesntHaveAccessToWorkspace), 1);
}
const org = response.orgs[0];
const workspace = org.workspaces[0];
if (workspace == null) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.workspaceNoLongerExist), 1);
return [];
}
if (workspace.branches == null || workspace.branches.length === 0) {
return [];
}
const versions = workspace.branches[0].versions ? [...workspace.branches[0].versions] : [];
versions.sort((v1, v2) => compareVersions_1.default(semver_1.default.fromAmpliVersion(v1), semver_1.default.fromAmpliVersion(v2)));
return versions;
}
async getBranchVersion(orgId, workspaceId, branchId, versionId) {
var _a;
const graphql = await this.getAmpliGraphQLClient();
const response = await graphql.branchVersions({ orgId, workspaceId, branchId, versionId });
if (response.orgs.length === 0) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.userDoesntHaveAccessToWorkspace), 1);
}
const org = response.orgs[0];
const workspace = org.workspaces[0];
if (workspace == null) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.workspaceNoLongerExist), 1);
return undefined;
}
if (workspace.branches == null || workspace.branches.length === 0) {
return undefined;
}
return (_a = workspace.branches[0].versions) === null || _a === void 0 ? void 0 : _a[0];
}
async getBranchVersionId(orgId, workspaceId, branchId, versionNumber) {
const graphql = await this.getAmpliGraphQLClient();
const response = await graphql.branchVersions({ orgId, workspaceId, branchId });
if (response.orgs.length === 0) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.userDoesntHaveAccessToWorkspace), 1);
}
const org = response.orgs[0];
const workspace = org.workspaces[0];
if (workspace == null) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.workspaceNoLongerExist), 1);
return undefined;
}
if (workspace.branches == null || workspace.branches.length === 0) {
return undefined;
}
const version = workspace.branches[0].versions.find(v => semver_1.default.fromAmpliVersion(v).equalTo(versionNumber));
return version === null || version === void 0 ? void 0 : version.id;
}
async getVersionIdFromSettings(settings, branch) {
const versionId = settings.getVersionId();
if (versionId) {
return versionId;
}
if (!branch) {
return undefined;
}
const versionNumber = settings.getVersion();
if (!versionNumber) {
return branch.currentVersionId;
}
return this.getBranchVersionId(settings.getOrgId(), settings.getWorkspaceId(), branch.id, versionNumber);
}
async getSources(orgId, workspaceId, defaultBranch, sourceBranch, sourceVersionId, sourceId) {
const graphql = await this.getAmpliGraphQLClient();
const branch = sourceBranch !== null && sourceBranch !== void 0 ? sourceBranch : defaultBranch;
const versionId = sourceBranch && sourceVersionId ? sourceVersionId : branch.currentVersionId;
const response = await graphql.sources({ orgId, workspaceId, branchId: branch.id, versionId, sourceId });
if (response.orgs.length === 0) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.userDoesntHaveAccessToWorkspace), 1);
}
const org = response.orgs[0];
const workspace = org.workspaces[0];
if (workspace == null) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.workspaceNoLongerExist), 1);
return [];
}
const { branches } = workspace;
if (branches == null || branches.length === 0) {
return [];
}
const { versions } = branches[0];
if (versions == null || versions.length === 0) {
return [];
}
const sources = [...versions[0].sources];
sources.sort(compareByName_1.default);
return sources;
}
async getMergedVersionByVersionId(orgId, workspaceId, versionId) {
var _a;
const graphql = await this.getAmpliGraphQLClient();
const response = await graphql.mergedVersions({ orgId, workspaceId, versionIds: [versionId] });
if (response.orgs.length === 0) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.userDoesntHaveAccessToWorkspace), 1);
}
const org = response.orgs[0];
const workspace = org.workspaces[0];
if (workspace == null) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.workspaceNoLongerExist), 1);
return undefined;
}
return (_a = workspace.mergedVersions) === null || _a === void 0 ? void 0 : _a[0];
}
async getMergedVersionByVersionSemVer(orgId, workspaceId, branchName, versionSemVer) {
const graphql = await this.getAmpliGraphQLClient();
const response = await graphql.mergedVersions({ orgId, workspaceId, branchNames: [branchName] });
if (response.orgs.length === 0) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.userDoesntHaveAccessToWorkspace), 1);
}
const org = response.orgs[0];
const workspace = org.workspaces[0];
if (workspace == null) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.workspaceNoLongerExist), 1);
return undefined;
}
const version = workspace.mergedVersions.find(v => v.featureSemVer === versionSemVer);
const semVersion = versionSemVer != null ? semver_1.default.fromString(versionSemVer) : null;
const nextVersions = semVersion != null ? workspace.mergedVersions.filter(v => compareVersions_1.default(semver_1.default.fromString(v.featureSemVer), semVersion) > 0) : [];
nextVersions.sort((v1, v2) => {
const featureSemVerCompareResult = compareVersions_1.default(semver_1.default.fromString(v1.featureSemVer), semver_1.default.fromString(v2.featureSemVer));
if (featureSemVerCompareResult !== 0) {
return featureSemVerCompareResult;
}
return compareVersions_1.default(semver_1.default.fromString(v1.mainSemVer), semver_1.default.fromString(v2.mainSemVer));
});
return version;
}
compareSettingsAndActualRuntimes(settings, settingsRuntime, actualRuntime) {
let runtime;
if (settingsRuntime && settingsRuntime.id !== actualRuntime.id) {
this.println(`${icons_1.ICON_WARNING_W_TEXT} Source was reconfigured externally since your last pull. Run ${chalk_1.default.bold('ampli pull')} to update the local configuration.`);
runtime = settingsRuntime;
}
else {
if (runtime_1.isSupportedRuntime(actualRuntime.id)) {
settings.setRuntime(actualRuntime);
}
runtime = actualRuntime;
}
return this.checkRuntimeIsSupported(runtime) ? runtime : undefined;
}
static identifyUser(user) {
const { id: orgId, name: orgName } = user.orgs[0];
const fullUserName = `${user.firstName} ${user.lastName}`;
ampli_1.ampli.identify(user.id, {
name: fullUserName,
email: user.email,
});
ampli_1.ampli.groupIdentify('org id', orgId, {
'org name': orgName,
});
ampli_1.ampli.setGroup(user.id, 'org id', orgId);
}
async getPullRequest(orgId, workspaceId, originBranchId, defaultBranchId) {
const graphql = await this.getAmpliGraphQLClient();
const response = await graphql.pullRequests({ orgId, workspaceId, originBranchId });
if (response.orgs.length === 0) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.userDoesntHaveAccessToWorkspace), 1);
}
const org = response.orgs[0];
const workspace = org.workspaces[0];
if (workspace == null) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.workspaceNoLongerExist), 1);
return undefined;
}
return workspace.pullRequests.filter(pr => pr.targetBranch.id === defaultBranchId)[0];
}
getBranchUrl(orgUrl, workspaceName, branchName) {
const settings = this.getSettings();
const zone = settings.getZone() || types_1.DEFAULT_ZONE;
return `${constants_1.APP_SETTINGS.ampli(zone).webUrl}/${orgUrl}/${encodeURIComponent(workspaceName)}/home/${encodeURIComponent(branchName)}/latest`;
}
async getRuntimes() {
const graphql = await this.getAmpliGraphQLClient();
const response = await graphql.runtimes();
return response.runtimes.map(r => {
const [languageId] = runtime_1.getRuntimeLanguageAndVersion(r.languageId);
const languageName = r.languageName.split(' (')[0];
const platformId = r.platformId === '*' ? languageId : r.platformId;
const platformName = r.platformId === '*' ? languageName : r.platformName;
return {
id: r.id,
code: `${r.platformId}:${r.languageId}`,
platform: {
id: platformId,
name: platformName,
},
language: {
id: languageId,
name: languageName,
},
sdk: runtime_1.RuntimeSDKs[r.id],
};
});
}
checkRuntimeIsSupported(runtime) {
if (runtime_1.isSupportedRuntime(runtime.id)) {
return true;
}
if (runtime_1.isPlatformOnlyRuntime(runtime.id)) {
this.println(`${icons_1.ICON_ERROR_W_TEXT} Source requires additional configuration. Run ${chalk_1.default.bold('ampli configure')} to configure source.`);
return false;
}
if (runtime_1.isUnknownRuntime(runtime.id)) {
throw new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.unsupportedRuntime(runtime.id));
}
const docUrl = runtime_1.UnsupportedRuntimeDocUrls[runtime.id] || runtime_1.UnsupportedRuntimeBaseDocUrl;
this.println(`${icons_1.ICON_ERROR_W_TEXT} Code generation is not supported for ${runtime.platform.name}. \
Run ${chalk_1.default.bold('ampli configure')} to select a supported platform or see implementation docs for ${runtime.platform.name} at ${docUrl}.`);
return false;
}
}
exports.default = BaseAction;