Skip to content

Commit

Permalink
[eas-cli] Fix flags and args
Browse files Browse the repository at this point in the history
Fixed flags and args in `webhook` commands

See: https://linear.app/expo/issue/ENG-9175/update-oclif-in-eas-cli
  • Loading branch information
radoslawkrzemien committed Dec 1, 2023
1 parent a32ee86 commit 5d4f380
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 23 deletions.
42 changes: 39 additions & 3 deletions packages/eas-cli/src/commands/webhook/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,38 @@ import { WebhookMutation } from '../../graphql/mutations/WebhookMutation';
import { ora } from '../../ora';
import { prepareInputParamsAsync } from '../../webhooks/input';

interface RawWebhookCreateFlags {
event?: string;
url?: string;
secret?: string;
'non-interactive'?: boolean;
}

interface WebhookCreateCommandFlags {
event?: WebhookType;
url?: string;
secret?: string;
'non-interactive': boolean;
}

export function maybeGetWebhookType(
webhookTypeString: string | undefined
): WebhookType | undefined {
if (!webhookTypeString) {
return undefined;
}
return Object.values(WebhookType).find(webhookType => webhookType === webhookTypeString);
}

const EVENT_FLAG_OPTIONS = [WebhookType.Build, WebhookType.Submit];

export default class WebhookCreate extends EasCommand {
static override description = 'create a webhook';

static override flags = {
event: Flags.enum({
event: Flags.string({
description: 'Event type that triggers the webhook',
options: [WebhookType.Build, WebhookType.Submit],
options: EVENT_FLAG_OPTIONS,
}),
url: Flags.string({
description: 'Webhook URL',
Expand All @@ -31,7 +56,8 @@ export default class WebhookCreate extends EasCommand {
};

async runAsync(): Promise<void> {
const { flags } = await this.parse(WebhookCreate);
const { flags: rawFlags } = await this.parse(WebhookCreate);
const flags = await this.sanitizeFlagsAsync(rawFlags);
const {
privateProjectConfig: { projectId },
loggedIn: { graphqlClient },
Expand All @@ -49,4 +75,14 @@ export default class WebhookCreate extends EasCommand {
throw err;
}
}

private async sanitizeFlagsAsync(
flags: RawWebhookCreateFlags
): Promise<WebhookCreateCommandFlags> {
return {
...flags,
event: maybeGetWebhookType(flags.event),
'non-interactive': flags['non-interactive'] ?? false,
};
}
}
15 changes: 8 additions & 7 deletions packages/eas-cli/src/commands/webhook/delete.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Args } from '@oclif/core';
import assert from 'assert';
import chalk from 'chalk';
import nullthrows from 'nullthrows';
Expand All @@ -16,13 +17,12 @@ import { formatWebhook } from '../../webhooks/formatWebhook';
export default class WebhookDelete extends EasCommand {
static override description = 'delete a webhook';

static override args = [
{
name: 'ID',
static override args = {
ID: Args.string({
required: false,
description: 'ID of the webhook to delete',
},
];
}),
};

static override flags = {
...EASNonInteractiveFlag,
Expand All @@ -45,8 +45,9 @@ export default class WebhookDelete extends EasCommand {
nonInteractive,
});

let webhook: WebhookFragment | undefined =
webhookId && (await WebhookQuery.byIdAsync(graphqlClient, webhookId));
let webhook: WebhookFragment | undefined = webhookId
? await WebhookQuery.byIdAsync(graphqlClient, webhookId)
: undefined;
if (!webhookId) {
const webhooks = await fetchWebhooksByAppIdAsync(graphqlClient, projectId);
if (webhooks.length === 0) {
Expand Down
29 changes: 24 additions & 5 deletions packages/eas-cli/src/commands/webhook/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,27 @@ import { ora } from '../../ora';
import { getDisplayNameForProjectIdAsync } from '../../project/projectUtils';
import { enableJsonOutput, printJsonOnlyOutput } from '../../utils/json';
import { formatWebhook } from '../../webhooks/formatWebhook';
import { maybeGetWebhookType } from './create';

interface RawWebhookListFlags {
event?: string;
json?: boolean;
}

interface WebhookListCommandFlags {
event?: WebhookType;
json?: boolean;
}

const EVENT_FLAG_OPTIONS = [WebhookType.Build, WebhookType.Submit];

export default class WebhookList extends EasCommand {
static override description = 'list webhooks';

static override flags = {
event: Flags.enum({
event: Flags.string({
description: 'Event type that triggers the webhook',
options: [WebhookType.Build, WebhookType.Submit],
options: EVENT_FLAG_OPTIONS,
}),
...EasJsonOnlyFlag,
};
Expand All @@ -28,9 +41,8 @@ export default class WebhookList extends EasCommand {
};

async runAsync(): Promise<void> {
const {
flags: { event, json },
} = await this.parse(WebhookList);
const { flags: rawFlags } = await this.parse(WebhookList);
const { event, json } = await this.sanitizeFlagsAsync(rawFlags);
if (json) {
enableJsonOutput();
}
Expand Down Expand Up @@ -72,4 +84,11 @@ export default class WebhookList extends EasCommand {
throw err;
}
}

private async sanitizeFlagsAsync(flags: RawWebhookListFlags): Promise<WebhookListCommandFlags> {
return {
...flags,
event: maybeGetWebhookType(flags.event),
};
}
}
36 changes: 33 additions & 3 deletions packages/eas-cli/src/commands/webhook/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ import { WebhookQuery } from '../../graphql/queries/WebhookQuery';
import { ora } from '../../ora';
import pick from '../../utils/expodash/pick';
import { prepareInputParamsAsync } from '../../webhooks/input';
import { maybeGetWebhookType } from './create';

interface RawWebhookUpdateFlags {
id: string;
event?: string;
url?: string;
secret?: string;
'non-interactive'?: boolean;
}

interface WebhookUpdateCommandFlags {
id: string;
event?: WebhookType;
url?: string;
secret?: string;
'non-interactive': boolean;
}

const EVENT_FLAG_OPTIONS = [WebhookType.Build, WebhookType.Submit];

export default class WebhookUpdate extends EasCommand {
static override description = 'update a webhook';
Expand All @@ -17,9 +36,9 @@ export default class WebhookUpdate extends EasCommand {
description: 'Webhook ID',
required: true,
}),
event: Flags.enum({
event: Flags.string({
description: 'Event type that triggers the webhook',
options: [WebhookType.Build, WebhookType.Submit],
options: EVENT_FLAG_OPTIONS,
}),
url: Flags.string({
description: 'Webhook URL',
Expand All @@ -36,7 +55,8 @@ export default class WebhookUpdate extends EasCommand {
};

async runAsync(): Promise<void> {
const { flags } = await this.parse(WebhookUpdate);
const { flags: rawFlags } = await this.parse(WebhookUpdate);
const flags = await this.sanitizeFlagsAsync(rawFlags);
const {
loggedIn: { graphqlClient },
} = await this.getContextAsync(WebhookUpdate, { nonInteractive: flags['non-interactive'] });
Expand All @@ -58,4 +78,14 @@ export default class WebhookUpdate extends EasCommand {
throw err;
}
}

private async sanitizeFlagsAsync(
flags: RawWebhookUpdateFlags
): Promise<WebhookUpdateCommandFlags> {
return {
...flags,
event: maybeGetWebhookType(flags.event),
'non-interactive': flags['non-interactive'] ?? false,
};
}
}
11 changes: 6 additions & 5 deletions packages/eas-cli/src/commands/webhook/view.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Args } from '@oclif/core';

import EasCommand from '../../commandUtils/EasCommand';
import { WebhookQuery } from '../../graphql/queries/WebhookQuery';
import Log from '../../log';
Expand All @@ -7,13 +9,12 @@ import { formatWebhook } from '../../webhooks/formatWebhook';
export default class WebhookView extends EasCommand {
static override description = 'view a webhook';

static override args = [
{
name: 'ID',
static override args = {
ID: Args.string({
required: true,
description: 'ID of the webhook to view',
},
];
}),
};

static override contextDefinition = {
...this.ContextOptions.LoggedIn,
Expand Down

0 comments on commit 5d4f380

Please sign in to comment.