Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow only installed apps to be shown in uninstall command #102

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/commands/app/uninstall.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseCommand } from "./base-command";
import { flags } from "@contentstack/cli-utilities";
import { getOrg, getApp, fetchApp } from "../../util";
import { getOrg, fetchApp, getInstalledApps } from "../../util";
import { commonMsg, uninstallAppMsg } from "../../messages";
import { UninstallAppFactory } from "../../factories/uninstall-app-factory";

Expand Down Expand Up @@ -34,7 +34,7 @@ export default class Uninstall extends BaseCommand<typeof Uninstall> {

// fetch app details
if (!this.flags['app-uid']) {
app = await getApp(this.flags, this.sharedConfig.org, {managementSdk: this.managementAppSdk, log: this.log})
app = await getInstalledApps(this.flags, this.sharedConfig.org, {managementSdk: this.managementAppSdk, log: this.log})
} else {
app = await fetchApp(this.flags, this.sharedConfig.org, {managementSdk: this.managementAppSdk, log: this.log})
}
Expand Down
42 changes: 41 additions & 1 deletion src/util/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,45 @@ function uninstallApp(flags: FlagInput, orgUid: string, options: CommonOptions,
.uninstall()
}

async function fetchInstalledApps(flags: FlagInput, orgUid: string, options: CommonOptions) {
const { managementSdk, log } = options;
const apps = (await fetchApps(flags, orgUid, options)) || [];
let batchRequests = [];
// Make calls in batch. 10 requests per batch allowed.
while (apps.length) {
batchRequests.push(apps.splice(0, 10));
}
const results = [];
for (const batch of batchRequests) {
const promises = batch.map(async (app) => {
try {
const installations = await managementSdk
.organization(orgUid)
.app(app.uid)
.installation()
.findAll();
return installations.items.length ? installations.items : null;
}
catch (error) {
log("Unable to fetch installations.", "warn");
log(error, "error");
throw error;
}
});
results.push(await Promise.all(promises));
}
for (let i = batchRequests.length - 1; i >= 0; i--) {
const batchLength = batchRequests[i].length;
for (let j = batchLength - 1; j >= 0; j--) {
if (!results[i][j]) {
batchRequests[i].splice(j, 1);
results[i].splice(j, 1);
}
}
}
return batchRequests.flat();
}

export {
getOrganizations,
getOrgAppUiLocation,
Expand All @@ -189,5 +228,6 @@ export {
installApp,
getStacks,
fetchStack,
uninstallApp
uninstallApp,
fetchInstalledApps,
};
33 changes: 28 additions & 5 deletions src/util/inquirer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import messages, { $t, commonMsg, errors, uninstallAppMsg } from "../messages";
import {
CommonOptions,
getOrganizations,
fetchApps,
getStacks,
fetchAppInstallations
fetchAppInstallations,
fetchInstalledApps,
fetchApps
} from "./common-utils";

/**
Expand Down Expand Up @@ -103,7 +104,28 @@ async function getOrg(flags: FlagInput, options: CommonOptions) {

async function getApp(flags: FlagInput, orgUid: string, options: CommonOptions) : Promise<Record<string, any> | undefined> {
cliux.loader("Loading Apps");
const apps = (await fetchApps(flags, orgUid, options)) || [];
const apps = (await fetchApps(flags, orgUid, options));
cliux.loader("done");

if (apps.length === 0) {
throw new Error(messages.APPS_NOT_FOUND)
}

flags.app = await cliux
.inquire({
type: "search-list",
name: "App",
choices: apps,
message: messages.CHOOSE_APP
})
.then((name) => apps.find(app => app.name === name)?.uid)

return apps.find(app => app.uid === flags.app);
}

async function getInstalledApps(flags: FlagInput, orgUid: string, options: CommonOptions) : Promise<Record<string, any> | undefined> {
cliux.loader("Loading Apps");
const apps = (await fetchInstalledApps(flags, orgUid, options));
cliux.loader("done");

if (apps.length === 0) {
Expand Down Expand Up @@ -201,7 +223,7 @@ async function getInstallation(
// fetch stacks from where the app has to be uninstalled
cliux.loader("done");
const stacks: Stack[] = await getStacks({managementSdk: managementSdkForStacks, log: options.log}, orgUid);
installations = populateMissingDataInInstallations(installations, stacks)
installations = populateMissingDataInInstallations(installations as [Installation], stacks)
// To support uninstall all flag
if (uninstallAll) {
return installations.map(installation => installation.uid).join(',')
Expand Down Expand Up @@ -249,6 +271,7 @@ export {
getDirName,
getDeveloperHubUrl,
getApp,
getInstalledApps,
getStack,
getInstallation
getInstallation,
};