Skip to content

Commit

Permalink
add webhook:delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
dsokal committed Apr 8, 2021
1 parent 460d977 commit 700623a
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
102 changes: 102 additions & 0 deletions packages/eas-cli/src/commands/webhook/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { getConfig } from '@expo/config';
import { Command } from '@oclif/command';
import assert from 'assert';
import chalk from 'chalk';
import nullthrows from 'nullthrows';
import ora from 'ora';

import { WebhookFragment } from '../../graphql/generated';
import { WebhookMutation } from '../../graphql/mutations/WebhookMutation';
import { WebhookQuery } from '../../graphql/queries/WebhookQuery';
import Log from '../../log';
import { findProjectRootAsync, getProjectIdAsync } from '../../project/projectUtils';
import { promptAsync, toggleConfirmAsync } from '../../prompts';
import { ensureLoggedInAsync } from '../../user/actions';
import { formatWebhook } from '../../webhooks/formatWebhook';

export default class WebhookDelete extends Command {
static description = 'Delete a webhook on the current project.';

static args = [
{
name: 'ID',
required: false,
description: 'ID of the webhook to delete',
},
];

async run() {
await ensureLoggedInAsync();
let {
args: { ID: webhookId },
} = this.parse(WebhookDelete);

const projectDir = await findProjectRootAsync(process.cwd());
if (!projectDir) {
throw new Error('Please run this command inside a project directory.');
}
const { exp } = getConfig(projectDir, { skipSDKVersionRequirement: true });
const projectId = await getProjectIdAsync(exp);

let webhook: WebhookFragment | undefined =
webhookId && (await WebhookQuery.byIdAsync(webhookId));
if (!webhookId) {
const webhooks = await fetchWebhooksByAppId(projectId);
if (webhooks.length === 0) {
process.exit(1);
}
({ webhook } = await promptAsync({
type: 'autocomplete',
name: 'webhook',
message: 'Pick the webhook to be deleted:',
choices: webhooks.map(i => ({
title: `${chalk.bold(i.url)} (Event: ${i.event}, ID: ${i.id})`,
value: i,
})),
}));
webhookId = nullthrows(webhook).id;
}

assert(webhook, 'Webhook must be defined here');

Log.addNewLineIfNone();
Log.log(formatWebhook(webhook));
Log.newLine();
Log.warn(`You are about to permamently delete this webhook.\nThis action is irreversible.`);
Log.newLine();
const confirmed = await toggleConfirmAsync({
message: 'Are you sure you wish to proceed?',
});

if (!confirmed) {
Log.error(`Canceled deletion of the webhook`);
process.exit(1);
}

const spinner = ora('Deleting the webhook').start();
try {
await WebhookMutation.deleteWebhookAsync(webhookId);
spinner.succeed('Successfully deleted the webhook');
} catch (err) {
spinner.fail('Failed to delete the webhook');
throw err;
}
}
}

async function fetchWebhooksByAppId(appId: string): Promise<WebhookFragment[]> {
const spinner = ora('Fetching webhooks').start();
try {
const webhooks = await WebhookQuery.byAppIdAsync(appId);
if (webhooks.length === 0) {
spinner.fail('There are no webhooks on the project');
return [];
} else {
spinner.succeed(`Successfully fetched ${webhooks.length} webhooks`);
return webhooks;
}
} catch (err) {
spinner.fail("Something went wrong and we couldn't fetch the webhook list");
throw err;
}
}
20 changes: 20 additions & 0 deletions packages/eas-cli/src/graphql/mutations/WebhookMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { graphqlClient, withErrorHandlingAsync } from '../client';
import {
CreateWebhookMutation,
CreateWebhookMutationVariables,
DeleteWebhookMutation,
DeleteWebhookMutationVariables,
UpdateWebhookMutation,
UpdateWebhookMutationVariables,
WebhookFragment,
Expand Down Expand Up @@ -58,4 +60,22 @@ export const WebhookMutation = {
);
return data.webhook.updateWebhook;
},
async deleteWebhookAsync(webhookId: string): Promise<void> {
await withErrorHandlingAsync(
graphqlClient
.mutation<DeleteWebhookMutation, DeleteWebhookMutationVariables>(
gql`
mutation DeleteWebhookMutation($webhookId: ID!) {
webhook {
deleteWebhook(webhookId: $webhookId) {
id
}
}
}
`,
{ webhookId }
)
.toPromise()
);
},
};

0 comments on commit 700623a

Please sign in to comment.