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: support pushing app by app id instead of app version id #45

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 20 additions & 4 deletions src/commands/code/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Flags } from '@oclif/core';
import { Listr } from 'listr2';

import { AuthenticatedCommand } from 'commands-base/authenticated-command';
import { APP_VERSION_ID_TO_ENTER } from 'consts/messages';
import { APP_ID_TO_ENTER, APP_VERSION_ID_TO_ENTER } from 'consts/messages';
import { defaultVersionByAppId } from 'services/app-versions-service';
import { DynamicChoicesService } from 'services/dynamic-choices-service';
import {
buildAssetToDeployTask,
Expand All @@ -16,22 +17,26 @@ import logger from 'utils/logger';
const MESSAGES = {
directory: 'Directory path of you project in your machine. If not included will use the current working directory.',
appVersionId: APP_VERSION_ID_TO_ENTER,
appId: APP_ID_TO_ENTER,
};

export default class Push extends AuthenticatedCommand {
DEBUG_TAG = 'code_push';
static description = 'Push your project to get hosted on monday-code.';

static examples = [
'<%= config.bin %> <%= command.id %> -d PROJECT DIRECTORY PATH -i APP_VERSION_ID_TO_PUSH',
'<%= config.bin %> <%= command.id %> -i APP_VERSION_ID_TO_PUSH',
'<%= config.bin %> <%= command.id %> -a APP_ID_TO_PUSH',
];

static flags = Push.serializeFlags({
directoryPath: Flags.string({
char: 'd',
description: MESSAGES.directory,
}),
appId: Flags.string({
char: 'a',
description: MESSAGES.appId,
}),
appVersionId: Flags.integer({
char: 'i',
aliases: ['v'],
Expand All @@ -40,16 +45,27 @@ export default class Push extends AuthenticatedCommand {
});

static args = {};
DEBUG_TAG = 'code_push';

public async run(): Promise<void> {
const { flags } = await this.parse(Push);
let appVersionId;

const appId = flags.appId;
if (appId) {
const latestDraftVersion = await defaultVersionByAppId(Number(appId));
if (!latestDraftVersion) throw new Error('No draft version found for the given app id.');
appVersionId = latestDraftVersion.id;
} else {
appVersionId = flags.appVersionId;
}

let appVersionId = flags.appVersionId;
if (!appVersionId) {
const appAndAppVersion = await DynamicChoicesService.chooseAppAndAppVersion();
appVersionId = appAndAppVersion.appVersionId;
}

logger.debug(`push code to appVersionId: ${appVersionId}`, this.DEBUG_TAG);
this.preparePrintCommand(this, { appVersionId, directoryPath: flags.directoryPath });
const tasks = new Listr<PushCommandTasksContext>(
[
Expand Down
7 changes: 7 additions & 0 deletions src/services/app-versions-service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { APP_VERSION_STATUS } from 'consts/app-versions';
import { listAppVersionsByAppIdUrl } from 'consts/urls';
import { execute } from 'services/api-service';
import { listAppVersionsSchema } from 'services/schemas/app-versions-schemas';
Expand Down Expand Up @@ -28,3 +29,9 @@ export const listAppVersionsByAppId = async (appId: AppId): Promise<Array<AppVer
throw new Error('Failed to list app versions.');
}
};

export const defaultVersionByAppId = async (appId: AppId): Promise<AppVersion | undefined> => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add unit test

const appVersions = await listAppVersionsByAppId(appId);
const latestVersion = appVersions.sort((a, b) => b.id - a.id)[0];
return latestVersion.status === APP_VERSION_STATUS.DRAFT ? latestVersion : undefined;
};
Loading