@amplitude/ampli
Version:
Amplitude CLI
93 lines (92 loc) • 4.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const base_1 = require("./base");
const errors_1 = require("../errors");
const pull_1 = require("./pull");
const icons_1 = require("../ui/icons");
const configure_1 = require("./configure");
const { bold } = chalk_1.default;
class RefreshAction extends base_1.default {
async run() {
var _a, _b, _c, _d, _e, _f, _g;
const settings = this.getSettings();
const orgId = settings.getOrgId();
const workspaceId = settings.getWorkspaceId();
const sourceId = settings.getSourceId();
const currentBranchName = settings.getBranch();
if (!sourceId) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.runPullBefore('refresh')), 1);
}
const { org, workspace, branches, defaultBranch } = await this.getOrgWorkspaceBranches(orgId, workspaceId);
const branch = currentBranchName ? branches.find(b => b.name === currentBranchName) : defaultBranch;
if (!branch) {
this.error(new errors_1.UserFixableError(errors_1.USER_ERROR_MESSAGES.branchDoesNotExist(currentBranchName)), 1);
return;
}
if (branch.default) {
this.println(`${icons_1.ICON_WARNING_W_TEXT} No refresh necessary. Current branch is default.`);
return;
}
const pullRequest = await this.getPullRequest(orgId, workspaceId, branch.id, defaultBranch.id);
if (!pullRequest) {
this.println(`${icons_1.ICON_WARNING_W_TEXT} Unable to refresh. No pull request found for current branch.`);
return;
}
if (!pullRequest.changes.backward.changeCount) {
this.println(`${icons_1.ICON_SUCCESS} No changes to refresh.`);
return;
}
const graphql = await this.getAmpliGraphQLClient();
const versionId = await this.getVersionIdFromSettings(settings, branch);
if (!versionId) {
this.error(new Error(errors_1.USER_ERROR_MESSAGES.versionDoesNotExist(settings.getVersion())), 1);
return;
}
const version = await this.getBranchVersion(orgId, workspaceId, branch.id, versionId);
if (!version) {
this.error(new Error(errors_1.USER_ERROR_MESSAGES.versionNotFoundOnServer(versionId)), 1);
return;
}
if (version.staging) {
const sources = await this.getSources(orgId, workspaceId, defaultBranch, branch, versionId, sourceId);
const source = sources[0];
if (!source) {
this.error(new Error(errors_1.USER_ERROR_MESSAGES.sourceDoesNotExist(sourceId)), 1);
return;
}
const versionChangesResponse = await graphql.versionChanges({
orgId,
workspaceId,
branchId: branch.id,
versionId,
versionIdToDiffAgainst: undefined,
sourceId: source.id,
});
if ((_d = (_c = (_b = (_a = versionChangesResponse.orgs) === null || _a === void 0 ? void 0 : _a[0].workspaces) === null || _b === void 0 ? void 0 : _b[0].branches) === null || _c === void 0 ? void 0 : _c[0].versions) === null || _d === void 0 ? void 0 : _d[0].changes.forward.changeCount) {
this.println(`${icons_1.ICON_WARNING_W_TEXT} Refresh failed due to unpublished changes in your tracking plan. Publish or discard changes to continue.
${this.getBranchUrl(org.url, workspace.name, branch.name)}`);
return;
}
}
try {
const resp = await graphql.refreshPullRequest({
input: {
id: pullRequest.id,
},
});
settings.setVersionId(resp.refreshPullRequest.originBranch.currentVersionId);
}
catch (e) {
const responseError = (_f = (_e = e.response) === null || _e === void 0 ? void 0 : _e.errors) === null || _f === void 0 ? void 0 : _f[0];
if (responseError && ((_g = responseError.extensions) === null || _g === void 0 ? void 0 : _g.code) === 'BAD_USER_INPUT') {
this.println(`${icons_1.ICON_WARNING_W_TEXT} ${responseError.message}`);
return;
}
throw e;
}
this.println(`${icons_1.ICON_SUCCESS} Successfully refreshed branch ${bold(branch.name)} to contain latest changes from ${bold(defaultBranch.name)} tracking plan.`);
await new pull_1.default(Object.assign(Object.assign({}, this.flags), { branch: undefined, version: undefined, path: undefined, omitApiKeys: undefined, [configure_1.DEPRECATED_RUNTIMES]: undefined }), { source: undefined }, this.config, () => { }).run();
}
}
exports.default = RefreshAction;