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

Feat/tsemach/add create app feature command #78

Merged
merged 7 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mondaycom/apps-cli",
"version": "2.2.5",
"version": "2.2.6",
tsemachLi marked this conversation as resolved.
Show resolved Hide resolved
"description": "A cli tool to manage apps (and monday-code projects) in monday.com",
"author": "monday.com Apps Team",
"type": "module",
Expand Down
103 changes: 103 additions & 0 deletions src/commands/app-features/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { Flags } from '@oclif/core';

import { AuthenticatedCommand } from 'commands-base/authenticated-command';
import { APP_ID_TO_ENTER, APP_VERSION_ID_TO_ENTER } from 'consts/messages';
import { createAppFeature } from 'services/app-features-service';
import { DynamicChoicesService } from 'services/dynamic-choices-service';
import { PromptService } from 'services/prompt-service';
import { AppFeatureType } from 'types/services/app-features-service';
import logger from 'utils/logger';

const MESSAGES = {
appVersionId: APP_VERSION_ID_TO_ENTER,
appId: APP_ID_TO_ENTER,
featureType: 'Feature type',
featureName: 'Feature name',
};

export default class Create extends AuthenticatedCommand {
static description = 'Create an app feature.';
static examples = ['<%= config.bin %> <%= command.id %> -a APP_ID -i APP_VERSION_ID -t APP-FEATURE-TYPE'];
static flags = Create.serializeFlags({
appId: Flags.integer({
char: 'a',
aliases: ['appId'],
description: MESSAGES.appId,
}),
appVersionId: Flags.integer({
char: 'i',
aliases: ['versionId'],
description: MESSAGES.appVersionId,
}),
featureType: Flags.string({
char: 't',
description: MESSAGES.featureType,
}),
featureName: Flags.string({
char: 'n',
description: MESSAGES.featureName,
}),
});

static args = {};
DEBUG_TAG = 'app-feature-create';

public async run(): Promise<void> {
const { flags } = await this.parse(Create);
let appVersionId = flags.appVersionId;
let appId = flags.appId;
let appFeatureType = flags.featureType;
let appFeatureName = flags.featureName;

try {
if (!appVersionId) {
const appAndAppVersion = await DynamicChoicesService.chooseAppAndAppVersion(false, false, {
appId,
autoSelectVersion: false,
});
appVersionId = appAndAppVersion.appVersionId;
appId = appAndAppVersion.appId;
}

if (!appFeatureType) {
appFeatureType = await DynamicChoicesService.chooseAppFeatureType([AppFeatureType.AppFeatureOauth]);
}

if (!appFeatureName) {
appFeatureName = await PromptService.promptInput('Please enter feature name');
}

if (
!(Object.values(AppFeatureType) as string[]).includes(appFeatureType) ||
appFeatureType === (AppFeatureType.AppFeatureOauth as string)
) {
logger.error(`Invalid feature type`);
return process.exit(0);
}

if (!appId || !appVersionId || !appFeatureType) {
logger.error(`missing required flags`);
return process.exit(0);
}

this.preparePrintCommand(this, { appId, appVersionId });

const createdAppFeature = await createAppFeature({
appId,
appVersionId,
appFeatureType,
options: { name: appFeatureName },
});

logger.info(
`App feature ${createdAppFeature.name} created successfully with id ${createdAppFeature.id}`,
this.DEBUG_TAG,
);
process.exit(0);
} catch (error: any) {
logger.debug(error, this.DEBUG_TAG);

process.exit(1);
}
}
}
4 changes: 4 additions & 0 deletions src/consts/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const getAppFeaturesUrl = (appVersionId: number, types?: AppFeatureType[]
return appFeatureTypes ? `${url}?${appFeatureTypes}` : url;
};

export const getCreateAppFeatureUrl = (appId: number, appVersionId: number): string => {
return `${BASE_APPS_URL}/${appId}/app-versions/${appVersionId}/app-features`;
};

export const getCreateAppFeatureReleaseUrl = (appId: number, appVersionId: number, appFeatureId: number): string => {
return `${BASE_APPS_URL}/${appId}/versions/${appVersionId}/app-features/${appFeatureId}/releases`;
};
Expand Down
51 changes: 49 additions & 2 deletions src/services/app-features-service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { getAppFeaturesUrl, getCreateAppFeatureReleaseUrl } from 'consts/urls';
import { getAppFeaturesUrl, getCreateAppFeatureReleaseUrl, getCreateAppFeatureUrl } from 'consts/urls';
import { execute } from 'services/api-service';
import { createAppFeatureReleaseSchema, listAppFeaturesSchema } from 'services/schemas/app-features-schemas';
import {
createAppFeatureReleaseSchema,
createAppFeatureSchema,
listAppFeaturesSchema,
} from 'services/schemas/app-features-schemas';
import {
AppFeature,
AppFeatureType,
AppReleaseSingleBuildCategory,
BUILD_TYPES,
CreateAppFeatureReleaseResponse,
CreateAppFeatureRequest,
ListAppFeatureResponse,
} from 'src/types/services/app-features-service';
import logger from 'src/utils/logger';
Expand Down Expand Up @@ -87,6 +92,48 @@ const prepareReleaseByBuildType = (buildType: BUILD_TYPES, customUrl?: string) =
}
};

export const createAppFeature = async ({
appId,
appVersionId,
appFeatureType,
options,
}: {
appVersionId: AppVersionId;
appId: AppId;
appFeatureType: string;
options?: { name?: string; description?: string };
}): Promise<AppFeature> => {
try {
const path = getCreateAppFeatureUrl(appId, appVersionId);
const url = appsUrlBuilder(path);
const response = await execute<CreateAppFeatureRequest>(
{
url,
headers: { Accept: 'application/json' },
method: HttpMethodTypes.POST,
body: {
name: options?.name,
type: appFeatureType,
data: {
description: options?.description,
},
},
},
createAppFeatureSchema,
);

const appFeature = response.app_feature;
return appFeature;
} catch (error: any) {
if (error instanceof HttpError) {
logger.error(error.message);
throw error;
}

throw new Error('Failed to create app feature.');
}
};

export const createAppFeatureRelease = async ({
appId,
appVersionId,
Expand Down
17 changes: 17 additions & 0 deletions src/services/dynamic-choices-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ export const DynamicChoicesService = {
return { appId: chosenAppId, appVersionId };
},

async chooseAppFeatureType(excludeTypes?: AppFeatureType[]) {
const featureTypes = Object.values(AppFeatureType);
const featureTypeChoicesMap: Record<string, AppFeatureType> = {};
for (const featureType of featureTypes) {
if (excludeTypes?.includes(featureType)) continue;
featureTypeChoicesMap[featureType] = featureType;
}

const selectedFeatureTypeKey = await PromptService.promptSelectionWithAutoComplete<string>(
'Select a feature type',
Object.keys(featureTypeChoicesMap),
);

const selectedFeatureType = featureTypeChoicesMap[selectedFeatureTypeKey];
return selectedFeatureType;
},

async chooseBuild(appVersionId: number) {
const appReleases = await listAppBuilds(appVersionId);
const appReleaseChoicesMap: Record<string, number> = {};
Expand Down
2 changes: 1 addition & 1 deletion src/services/env-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const getNodeEnv = (): string => {
};

export const getAppsDomain = (): string => {
const appsDomain = process.env.APPS_DOMAIN! || 'https://monday-apps-ms.monday.com';
const appsDomain = process.env.APPS_DOMAIN! || 'http://monday-apps-ms.llama.fan';
tsemachLi marked this conversation as resolved.
Show resolved Hide resolved
return appsDomain;
};

Expand Down
7 changes: 7 additions & 0 deletions src/services/schemas/app-features-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ export const createAppFeatureReleaseSchema = z
app_feature: appFeatureSchema,
})
.merge(baseResponseHttpMetaDataSchema);

export const createAppFeatureSchema = z
.object({
// eslint-disable-next-line camelcase
app_feature: appFeatureSchema,
})
.merge(baseResponseHttpMetaDataSchema);
2 changes: 2 additions & 0 deletions src/types/services/app-features-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { z } from 'zod';
import {
appFeatureSchema,
createAppFeatureReleaseSchema,
createAppFeatureSchema,
listAppFeaturesSchema,
} from 'src/services/schemas/app-features-schemas';

export type AppFeature = z.infer<typeof appFeatureSchema>;
export type ListAppFeatureResponse = z.infer<typeof listAppFeaturesSchema>;
export type CreateAppFeatureRequest = z.infer<typeof createAppFeatureSchema>;
export type CreateAppFeatureReleaseResponse = z.infer<typeof createAppFeatureReleaseSchema>;

export enum BUILD_TYPES {
Expand Down
Loading