From f42bafcf958f2e7eb086d45922653331f767b2de Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Tue, 25 Oct 2022 13:58:29 -0700 Subject: [PATCH 1/8] refactor(update): remove default non-interactive value to rely on falsy values Co-authored-by: Will Schurman --- packages/eas-cli/src/commands/update/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eas-cli/src/commands/update/index.ts b/packages/eas-cli/src/commands/update/index.ts index 88970c087c..79320b76a7 100644 --- a/packages/eas-cli/src/commands/update/index.ts +++ b/packages/eas-cli/src/commands/update/index.ts @@ -626,7 +626,7 @@ export default class UpdatePublish extends EasCommand { } private sanitizeFlags(flags: RawUpdateFlags): UpdateFlags { - const nonInteractive = flags['non-interactive'] ?? false; + const nonInteractive = flags['non-interactive']; const { auto, branch: branchName, message: updateMessage } = flags; if (nonInteractive && !auto && !(branchName && updateMessage)) { From ec24b9a73c69b89e129ac55d589cf37d7e9fd3c8 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Tue, 25 Oct 2022 13:59:10 -0700 Subject: [PATCH 2/8] refactor(updates): make non interactive optional to rely on falsy values --- packages/eas-cli/src/commands/update/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eas-cli/src/commands/update/index.ts b/packages/eas-cli/src/commands/update/index.ts index 79320b76a7..b31053f9f6 100644 --- a/packages/eas-cli/src/commands/update/index.ts +++ b/packages/eas-cli/src/commands/update/index.ts @@ -92,7 +92,7 @@ type UpdateFlags = { skipBundler: boolean; privateKeyPath?: string; json: boolean; - nonInteractive: boolean; + nonInteractive?: boolean; }; async function ensureChannelExistsAsync( From d8e9b1684e146e6e59152df8c60da63429dc6460 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Wed, 26 Oct 2022 00:45:16 +0200 Subject: [PATCH 3/8] Revert "refactor(update): remove default non-interactive value to rely on falsy values" This reverts commit 23ac36d36e35b7a87724229234d41dcac8cf02db. --- packages/eas-cli/src/commands/update/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eas-cli/src/commands/update/index.ts b/packages/eas-cli/src/commands/update/index.ts index b31053f9f6..8251483592 100644 --- a/packages/eas-cli/src/commands/update/index.ts +++ b/packages/eas-cli/src/commands/update/index.ts @@ -626,7 +626,7 @@ export default class UpdatePublish extends EasCommand { } private sanitizeFlags(flags: RawUpdateFlags): UpdateFlags { - const nonInteractive = flags['non-interactive']; + const nonInteractive = flags['non-interactive'] ?? false; const { auto, branch: branchName, message: updateMessage } = flags; if (nonInteractive && !auto && !(branchName && updateMessage)) { From 53e87d1fc11007a76968db510f30dfebd1588d41 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Wed, 26 Oct 2022 00:45:24 +0200 Subject: [PATCH 4/8] Revert "refactor(updates): make non interactive optional to rely on falsy values" This reverts commit 94ae7cc039c94f28b74a5cfd9857f79e5a11e82c. --- packages/eas-cli/src/commands/update/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eas-cli/src/commands/update/index.ts b/packages/eas-cli/src/commands/update/index.ts index 8251483592..88970c087c 100644 --- a/packages/eas-cli/src/commands/update/index.ts +++ b/packages/eas-cli/src/commands/update/index.ts @@ -92,7 +92,7 @@ type UpdateFlags = { skipBundler: boolean; privateKeyPath?: string; json: boolean; - nonInteractive?: boolean; + nonInteractive: boolean; }; async function ensureChannelExistsAsync( From c880df20b1834a9885ad81f8d5dd2a135d8fd679 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Tue, 25 Oct 2022 16:16:13 +0200 Subject: [PATCH 5/8] refactor(update): move ensure methods outside the update command --- packages/eas-cli/src/branch/queries.ts | 72 ++++++++++++++++- packages/eas-cli/src/channel/queries.ts | 65 +++++++++++++++ .../eas-cli/src/commands/branch/create.ts | 1 + .../eas-cli/src/commands/channel/create.ts | 1 + packages/eas-cli/src/commands/update/index.ts | 81 +++---------------- 5 files changed, 147 insertions(+), 73 deletions(-) diff --git a/packages/eas-cli/src/branch/queries.ts b/packages/eas-cli/src/branch/queries.ts index c7a74c21b6..34c35b7d6e 100644 --- a/packages/eas-cli/src/branch/queries.ts +++ b/packages/eas-cli/src/branch/queries.ts @@ -1,8 +1,15 @@ import chalk from 'chalk'; +import gql from 'graphql-tag'; import { ExpoGraphqlClient } from '../commandUtils/context/contextUtils/createGraphqlClient'; import { PaginatedQueryOptions } from '../commandUtils/pagination'; -import { UpdateBranchFragment } from '../graphql/generated'; +import { withErrorHandlingAsync } from '../graphql/client'; +import { + CreateUpdateBranchForAppMutation, + CreateUpdateBranchForAppMutationVariables, + UpdateBranch, + UpdateBranchFragment, +} from '../graphql/generated'; import { BranchQuery } from '../graphql/queries/BranchQuery'; import Log from '../log'; import { formatBranch, getBranchDescription } from '../update/utils'; @@ -11,6 +18,7 @@ import { paginatedQueryWithConfirmPromptAsync, paginatedQueryWithSelectPromptAsync, } from '../utils/queries'; +import { BranchNotFoundError } from './utils'; export const BRANCHES_LIMIT = 50; @@ -111,3 +119,65 @@ function renderPageOfBranches( ); } } + +export async function createUpdateBranchOnAppAsync( + graphqlClient: ExpoGraphqlClient, + { appId, name }: CreateUpdateBranchForAppMutationVariables +): Promise> { + const result = await withErrorHandlingAsync( + graphqlClient + .mutation( + gql` + mutation createUpdateBranchForApp($appId: ID!, $name: String!) { + updateBranch { + createUpdateBranchForApp(appId: $appId, name: $name) { + id + name + } + } + } + `, + { + appId, + name, + } + ) + .toPromise() + ); + const newBranch = result.updateBranch.createUpdateBranchForApp; + if (!newBranch) { + throw new Error(`Could not create branch ${name}.`); + } + return newBranch; +} + +export async function ensureBranchExistsAsync( + graphqlClient: ExpoGraphqlClient, + { + appId, + branchName, + }: { + appId: string; + branchName: string; + } +): Promise<{ branchId: string }> { + try { + const updateBranch = await BranchQuery.getBranchByNameAsync(graphqlClient, { + appId, + name: branchName, + }); + + const { id } = updateBranch; + return { branchId: id }; + } catch (error) { + if (error instanceof BranchNotFoundError) { + const newUpdateBranch = await createUpdateBranchOnAppAsync(graphqlClient, { + appId, + name: branchName, + }); + return { branchId: newUpdateBranch.id }; + } else { + throw error; + } + } +} diff --git a/packages/eas-cli/src/channel/queries.ts b/packages/eas-cli/src/channel/queries.ts index a1b116e5f6..515039836f 100644 --- a/packages/eas-cli/src/channel/queries.ts +++ b/packages/eas-cli/src/channel/queries.ts @@ -1,8 +1,12 @@ import chalk from 'chalk'; +import gql from 'graphql-tag'; import { ExpoGraphqlClient } from '../commandUtils/context/contextUtils/createGraphqlClient'; import { PaginatedQueryOptions } from '../commandUtils/pagination'; +import { withErrorHandlingAsync } from '../graphql/client'; import { + CreateUpdateChannelOnAppMutation, + CreateUpdateChannelOnAppMutationVariables, ViewBranchesOnUpdateChannelQueryVariables, ViewUpdateChannelsOnAppQueryVariables, } from '../graphql/generated'; @@ -197,3 +201,64 @@ function renderChannelHeaderContent({ Log.addNewLineIfNone(); Log.log(chalk`{bold Branches pointed at this channel and their most recent update group:}`); } + +export async function createChannelAsync( + graphqlClient: ExpoGraphqlClient, + { + appId, + branchId, + channelName, + }: { + appId: string; + branchId: string; + channelName: string; + } +): Promise { + // Point the new channel at a branch with its same name. + const branchMapping = JSON.stringify({ + data: [{ branchId, branchMappingLogic: 'true' }], + version: 0, + }); + return await withErrorHandlingAsync( + graphqlClient + .mutation( + gql` + mutation CreateUpdateChannelOnApp($appId: ID!, $name: String!, $branchMapping: String!) { + updateChannel { + createUpdateChannelForApp(appId: $appId, name: $name, branchMapping: $branchMapping) { + id + name + branchMapping + } + } + } + `, + { + appId, + name: channelName, + branchMapping, + } + ) + .toPromise() + ); +} + +export async function ensureChannelExistsAsync( + graphqlClient: ExpoGraphqlClient, + { appId, branchId, channelName }: { appId: string; branchId: string; channelName: string } +): Promise { + try { + await createChannelAsync(graphqlClient, { + appId, + channelName, + branchId, + }); + } catch (e: any) { + const isIgnorableError = + e.graphQLErrors?.length === 1 && + e.graphQLErrors[0].extensions.errorCode === 'CHANNEL_ALREADY_EXISTS'; + if (!isIgnorableError) { + throw e; + } + } +} diff --git a/packages/eas-cli/src/commands/branch/create.ts b/packages/eas-cli/src/commands/branch/create.ts index 046bad9d26..3739432c0d 100644 --- a/packages/eas-cli/src/commands/branch/create.ts +++ b/packages/eas-cli/src/commands/branch/create.ts @@ -16,6 +16,7 @@ import { getDisplayNameForProjectIdAsync } from '../../project/projectUtils'; import { promptAsync } from '../../prompts'; import { enableJsonOutput, printJsonOnlyOutput } from '../../utils/json'; +// NOTE(cedric): copied to src/branch/queries.ts to reuse in multiple commands export async function createUpdateBranchOnAppAsync( graphqlClient: ExpoGraphqlClient, { appId, name }: CreateUpdateBranchForAppMutationVariables diff --git a/packages/eas-cli/src/commands/channel/create.ts b/packages/eas-cli/src/commands/channel/create.ts index 9a64daaa24..ce21d36ef6 100644 --- a/packages/eas-cli/src/commands/channel/create.ts +++ b/packages/eas-cli/src/commands/channel/create.ts @@ -18,6 +18,7 @@ import formatFields from '../../utils/formatFields'; import { enableJsonOutput, printJsonOnlyOutput } from '../../utils/json'; import { createUpdateBranchOnAppAsync } from '../branch/create'; +// NOTE(cedric): copied to src/channel/queries.ts to reuse in multiple commands export async function createUpdateChannelOnAppAsync( graphqlClient: ExpoGraphqlClient, { diff --git a/packages/eas-cli/src/commands/update/index.ts b/packages/eas-cli/src/commands/update/index.ts index 88970c087c..421fa577fc 100644 --- a/packages/eas-cli/src/commands/update/index.ts +++ b/packages/eas-cli/src/commands/update/index.ts @@ -7,9 +7,10 @@ import chalk from 'chalk'; import nullthrows from 'nullthrows'; import { getEASUpdateURL } from '../../api'; -import { selectBranchOnAppAsync } from '../../branch/queries'; -import { BranchNotFoundError, getDefaultBranchNameAsync } from '../../branch/utils'; +import { ensureBranchExistsAsync, selectBranchOnAppAsync } from '../../branch/queries'; +import { getDefaultBranchNameAsync } from '../../branch/utils'; import { getUpdateGroupUrl } from '../../build/utils/url'; +import { ensureChannelExistsAsync } from '../../channel/queries'; import EasCommand from '../../commandUtils/EasCommand'; import { DynamicConfigContextFn } from '../../commandUtils/context/DynamicProjectConfigContextField'; import { ExpoGraphqlClient } from '../../commandUtils/context/contextUtils/createGraphqlClient'; @@ -22,10 +23,8 @@ import { Update, UpdateInfoGroup, UpdatePublishMutation, - ViewBranchQueryVariables, } from '../../graphql/generated'; import { PublishMutation } from '../../graphql/mutations/PublishMutation'; -import { BranchQuery } from '../../graphql/queries/BranchQuery'; import { UpdateQuery } from '../../graphql/queries/UpdateQuery'; import Log, { learnMore, link } from '../../log'; import { ora } from '../../ora'; @@ -57,8 +56,6 @@ import formatFields from '../../utils/formatFields'; import { enableJsonOutput, printJsonOnlyOutput } from '../../utils/json'; import { maybeWarnAboutEasOutagesAsync } from '../../utils/statuspageService'; import { getVcsClient } from '../../vcs'; -import { createUpdateBranchOnAppAsync } from '../branch/create'; -import { createUpdateChannelOnAppAsync } from '../channel/create'; import { configureAppJSONForEASUpdateAsync, configureNativeFilesForEASUpdateAsync, @@ -95,71 +92,6 @@ type UpdateFlags = { nonInteractive: boolean; }; -async function ensureChannelExistsAsync( - graphqlClient: ExpoGraphqlClient, - { - appId, - branchId, - channelName, - }: { - appId: string; - branchId: string; - channelName: string; - } -): Promise { - try { - await createUpdateChannelOnAppAsync(graphqlClient, { - appId, - channelName, - branchId, - }); - Log.withTick( - `Created a channel: ${chalk.bold(channelName)} pointed at branch: ${chalk.bold(channelName)}.` - ); - } catch (e: any) { - const isIgnorableError = - e.graphQLErrors?.length === 1 && - e.graphQLErrors[0].extensions.errorCode === 'CHANNEL_ALREADY_EXISTS'; - if (!isIgnorableError) { - throw e; - } - } -} - -export async function ensureBranchExistsAsync( - graphqlClient: ExpoGraphqlClient, - { appId, name: branchName }: ViewBranchQueryVariables -): Promise<{ - branchId: string; -}> { - try { - const updateBranch = await BranchQuery.getBranchByNameAsync(graphqlClient, { - appId, - name: branchName, - }); - - const { id } = updateBranch; - await ensureChannelExistsAsync(graphqlClient, { appId, branchId: id, channelName: branchName }); - return { branchId: id }; - } catch (error) { - if (error instanceof BranchNotFoundError) { - const newUpdateBranch = await createUpdateBranchOnAppAsync(graphqlClient, { - appId, - name: branchName, - }); - Log.withTick(`Created branch: ${chalk.bold(branchName)}`); - await ensureChannelExistsAsync(graphqlClient, { - appId, - branchId: newUpdateBranch.id, - channelName: branchName, - }); - return { branchId: newUpdateBranch.id }; - } else { - throw error; - } - } -} - export default class UpdatePublish extends EasCommand { static override description = 'publish an update group'; @@ -484,7 +416,12 @@ export default class UpdatePublish extends EasCommand { const { branchId } = await ensureBranchExistsAsync(graphqlClient, { appId: projectId, - name: branchName, + branchName, + }); + await ensureChannelExistsAsync(graphqlClient, { + appId: projectId, + branchId, + channelName: branchName, }); // Sort the updates into different groups based on their platform specific runtime versions From 298399ef5ca2cdcf022ac17115e1e2127a034c31 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Tue, 25 Oct 2022 16:51:23 +0200 Subject: [PATCH 6/8] fix(update): add back log about channel pointing to branch --- packages/eas-cli/src/commands/update/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/eas-cli/src/commands/update/index.ts b/packages/eas-cli/src/commands/update/index.ts index 421fa577fc..a53381aee4 100644 --- a/packages/eas-cli/src/commands/update/index.ts +++ b/packages/eas-cli/src/commands/update/index.ts @@ -423,6 +423,7 @@ export default class UpdatePublish extends EasCommand { branchId, channelName: branchName, }); + Log.withTick(`Channel: ${chalk.bold(branchName)} pointed at branch: ${chalk.bold(branchName)}`); // Sort the updates into different groups based on their platform specific runtime versions const updateGroups: PublishUpdateGroupInput[] = Object.entries(runtimeToPlatformMapping).map( From 73e983fc4b759f2f5f18cc9238e554920fad400a Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Mon, 31 Oct 2022 18:24:54 +0100 Subject: [PATCH 7/8] refactor(branch): reuse query from shared utilsin `branch:create` --- .../eas-cli/src/commands/branch/create.ts | 41 +------------------ .../eas-cli/src/commands/channel/create.ts | 2 +- 2 files changed, 2 insertions(+), 41 deletions(-) diff --git a/packages/eas-cli/src/commands/branch/create.ts b/packages/eas-cli/src/commands/branch/create.ts index 3739432c0d..0c07e886dc 100644 --- a/packages/eas-cli/src/commands/branch/create.ts +++ b/packages/eas-cli/src/commands/branch/create.ts @@ -1,53 +1,14 @@ import chalk from 'chalk'; -import gql from 'graphql-tag'; +import { createUpdateBranchOnAppAsync } from '../../branch/queries'; import { getDefaultBranchNameAsync } from '../../branch/utils'; import EasCommand from '../../commandUtils/EasCommand'; -import { ExpoGraphqlClient } from '../../commandUtils/context/contextUtils/createGraphqlClient'; import { EasNonInteractiveAndJsonFlags } from '../../commandUtils/flags'; -import { withErrorHandlingAsync } from '../../graphql/client'; -import { - CreateUpdateBranchForAppMutation, - CreateUpdateBranchForAppMutationVariables, - UpdateBranch, -} from '../../graphql/generated'; import Log from '../../log'; import { getDisplayNameForProjectIdAsync } from '../../project/projectUtils'; import { promptAsync } from '../../prompts'; import { enableJsonOutput, printJsonOnlyOutput } from '../../utils/json'; -// NOTE(cedric): copied to src/branch/queries.ts to reuse in multiple commands -export async function createUpdateBranchOnAppAsync( - graphqlClient: ExpoGraphqlClient, - { appId, name }: CreateUpdateBranchForAppMutationVariables -): Promise> { - const result = await withErrorHandlingAsync( - graphqlClient - .mutation( - gql` - mutation createUpdateBranchForApp($appId: ID!, $name: String!) { - updateBranch { - createUpdateBranchForApp(appId: $appId, name: $name) { - id - name - } - } - } - `, - { - appId, - name, - } - ) - .toPromise() - ); - const newBranch = result.updateBranch.createUpdateBranchForApp; - if (!newBranch) { - throw new Error(`Could not create branch ${name}.`); - } - return newBranch; -} - export default class BranchCreate extends EasCommand { static override description = 'create a branch'; diff --git a/packages/eas-cli/src/commands/channel/create.ts b/packages/eas-cli/src/commands/channel/create.ts index ce21d36ef6..11e9bdac3b 100644 --- a/packages/eas-cli/src/commands/channel/create.ts +++ b/packages/eas-cli/src/commands/channel/create.ts @@ -1,6 +1,7 @@ import chalk from 'chalk'; import gql from 'graphql-tag'; +import { createUpdateBranchOnAppAsync } from '../../branch/queries'; import { BranchNotFoundError } from '../../branch/utils'; import EasCommand from '../../commandUtils/EasCommand'; import { ExpoGraphqlClient } from '../../commandUtils/context/contextUtils/createGraphqlClient'; @@ -16,7 +17,6 @@ import { getDisplayNameForProjectIdAsync } from '../../project/projectUtils'; import { promptAsync } from '../../prompts'; import formatFields from '../../utils/formatFields'; import { enableJsonOutput, printJsonOnlyOutput } from '../../utils/json'; -import { createUpdateBranchOnAppAsync } from '../branch/create'; // NOTE(cedric): copied to src/channel/queries.ts to reuse in multiple commands export async function createUpdateChannelOnAppAsync( From e1a1c88610259dd0cc9fe0079d71ce2435c539af Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Mon, 31 Oct 2022 18:32:29 +0100 Subject: [PATCH 8/8] refactor(channel): reuse query from shared utilsin `channel:create` --- packages/eas-cli/src/channel/queries.ts | 4 +- .../eas-cli/src/commands/channel/create.ts | 52 +------------------ 2 files changed, 4 insertions(+), 52 deletions(-) diff --git a/packages/eas-cli/src/channel/queries.ts b/packages/eas-cli/src/channel/queries.ts index 515039836f..21b07e6fe0 100644 --- a/packages/eas-cli/src/channel/queries.ts +++ b/packages/eas-cli/src/channel/queries.ts @@ -202,7 +202,7 @@ function renderChannelHeaderContent({ Log.log(chalk`{bold Branches pointed at this channel and their most recent update group:}`); } -export async function createChannelAsync( +export async function createChannelOnAppAsync( graphqlClient: ExpoGraphqlClient, { appId, @@ -248,7 +248,7 @@ export async function ensureChannelExistsAsync( { appId, branchId, channelName }: { appId: string; branchId: string; channelName: string } ): Promise { try { - await createChannelAsync(graphqlClient, { + await createChannelOnAppAsync(graphqlClient, { appId, channelName, branchId, diff --git a/packages/eas-cli/src/commands/channel/create.ts b/packages/eas-cli/src/commands/channel/create.ts index 11e9bdac3b..e135111a0d 100644 --- a/packages/eas-cli/src/commands/channel/create.ts +++ b/packages/eas-cli/src/commands/channel/create.ts @@ -1,16 +1,10 @@ import chalk from 'chalk'; -import gql from 'graphql-tag'; import { createUpdateBranchOnAppAsync } from '../../branch/queries'; import { BranchNotFoundError } from '../../branch/utils'; +import { createChannelOnAppAsync } from '../../channel/queries'; import EasCommand from '../../commandUtils/EasCommand'; -import { ExpoGraphqlClient } from '../../commandUtils/context/contextUtils/createGraphqlClient'; import { EasNonInteractiveAndJsonFlags } from '../../commandUtils/flags'; -import { withErrorHandlingAsync } from '../../graphql/client'; -import { - CreateUpdateChannelOnAppMutation, - CreateUpdateChannelOnAppMutationVariables, -} from '../../graphql/generated'; import { BranchQuery } from '../../graphql/queries/BranchQuery'; import Log from '../../log'; import { getDisplayNameForProjectIdAsync } from '../../project/projectUtils'; @@ -18,48 +12,6 @@ import { promptAsync } from '../../prompts'; import formatFields from '../../utils/formatFields'; import { enableJsonOutput, printJsonOnlyOutput } from '../../utils/json'; -// NOTE(cedric): copied to src/channel/queries.ts to reuse in multiple commands -export async function createUpdateChannelOnAppAsync( - graphqlClient: ExpoGraphqlClient, - { - appId, - channelName, - branchId, - }: { - appId: string; - channelName: string; - branchId: string; - } -): Promise { - // Point the new channel at a branch with its same name. - const branchMapping = JSON.stringify({ - data: [{ branchId, branchMappingLogic: 'true' }], - version: 0, - }); - return await withErrorHandlingAsync( - graphqlClient - .mutation( - gql` - mutation CreateUpdateChannelOnApp($appId: ID!, $name: String!, $branchMapping: String!) { - updateChannel { - createUpdateChannelForApp(appId: $appId, name: $name, branchMapping: $branchMapping) { - id - name - branchMapping - } - } - } - `, - { - appId, - name: channelName, - branchMapping, - } - ) - .toPromise() - ); -} - export default class ChannelCreate extends EasCommand { static override description = 'create a channel'; @@ -133,7 +85,7 @@ export default class ChannelCreate extends EasCommand { const { updateChannel: { createUpdateChannelForApp: newChannel }, - } = await createUpdateChannelOnAppAsync(graphqlClient, { + } = await createChannelOnAppAsync(graphqlClient, { appId: projectId, channelName, branchId,