Skip to content

Commit

Permalink
Merge pull request #3477 from Shopify/migrate-deploy-to-partners-adap…
Browse files Browse the repository at this point in the history
…ter-2

Migrate deploy to partners adapter, part 2
  • Loading branch information
gonzaloriestra committed Feb 28, 2024
2 parents f9ff722 + 650b03a commit d48a975
Show file tree
Hide file tree
Showing 33 changed files with 704 additions and 490 deletions.
72 changes: 72 additions & 0 deletions packages/app/src/cli/models/app/app.test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import {PartnersSession} from '../../services/context/partner-account-info.js'
import {WebhooksConfig} from '../extensions/specifications/types/app_config_webhook.js'
import {PaymentsAppExtensionConfigType} from '../extensions/specifications/payments_app_extension.js'
import {CreateAppOptions, DeveloperPlatformClient} from '../../utilities/developer-platform-client.js'
import {ActiveAppVersionQuerySchema} from '../../api/graphql/app_active_version.js'
import {AllAppExtensionRegistrationsQuerySchema} from '../../api/graphql/all_app_extension_registrations.js'
import {ExtensionUpdateDraftInput, ExtensionUpdateSchema} from '../../api/graphql/update_draft.js'
import {AppDeploySchema, AppDeployVariables} from '../../api/graphql/app_deploy.js'
import {
GenerateSignedUploadUrlSchema,
GenerateSignedUploadUrlVariables,
} from '../../api/graphql/generate_signed_upload_url.js'

export const DEFAULT_CONFIG = {
path: '/tmp/project/shopify.app.toml',
Expand Down Expand Up @@ -650,9 +658,66 @@ export const testPartnersUserSession: PartnersSession = {
},
}

const emptyAppExtensionRegistrations: AllAppExtensionRegistrationsQuerySchema = {
app: {
extensionRegistrations: [],
configurationRegistrations: [],
dashboardManagedExtensionRegistrations: [],
},
}

const emptyAppVersion: ActiveAppVersionQuerySchema = {
app: {
activeAppVersion: {
appModuleVersions: [],
},
},
}

const functionUploadUrlResponse = {
functionUploadUrlGenerate: {
generatedUrlDetails: {
headers: {},
maxSize: '200 kb',
url: 'https://example.com/upload-url',
moduleId: 'module-id',
maxBytes: 200,
},
},
}

const extensionUpdateResponse: ExtensionUpdateSchema = {
extensionUpdateDraft: {
clientMutationId: 'client-mutation-id',
userErrors: [],
},
}

const deployResponse: AppDeploySchema = {
appDeploy: {
appVersion: {
uuid: 'uuid',
id: 1,
versionTag: 'version-tag',
location: 'location',
message: 'message',
appModuleVersions: [],
},
userErrors: [],
},
}

const generateSignedUploadUrlResponse: GenerateSignedUploadUrlSchema = {
appVersionGenerateSignedUploadUrl: {
signedUploadUrl: 'signed-upload-url',
userErrors: [],
},
}

export function testDeveloperPlatformClient(stubs: Partial<DeveloperPlatformClient> = {}): DeveloperPlatformClient {
return {
session: () => Promise.resolve(testPartnersUserSession),
refreshToken: () => Promise.resolve(testPartnersUserSession.token),
accountInfo: () => Promise.resolve(testPartnersUserSession.accountInfo),
appFromId: (_clientId: string) => Promise.resolve(testOrganizationApp()),
organizations: () => Promise.resolve([testOrganization()]),
Expand All @@ -665,6 +730,13 @@ export function testDeveloperPlatformClient(stubs: Partial<DeveloperPlatformClie
createApp: (_organization: Organization, _name: string, _options?: CreateAppOptions) =>
Promise.resolve(testOrganizationApp()),
devStoresForOrg: (_organizationId: string) => Promise.resolve([]),
appExtensionRegistrations: (_appId: string) => Promise.resolve(emptyAppExtensionRegistrations),
activeAppVersion: (_appId: string) => Promise.resolve(emptyAppVersion),
functionUploadUrl: () => Promise.resolve(functionUploadUrlResponse),
updateExtension: (input: ExtensionUpdateDraftInput) => Promise.resolve(extensionUpdateResponse),
deploy: (input: AppDeployVariables) => Promise.resolve(deployResponse),
generateSignedUploadUrl: (input: GenerateSignedUploadUrlVariables) =>
Promise.resolve(generateSignedUploadUrlResponse),
...stubs,
}
}
Expand Down
18 changes: 11 additions & 7 deletions packages/app/src/cli/models/extensions/extension-instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import {
testWebPixelExtension,
testWebhookExtensions,
testFlowActionExtension,
testDeveloperPlatformClient,
} from '../app/app.test-data.js'
import {FunctionConfigType} from '../extensions/specifications/function.js'
import {ExtensionBuildOptions} from '../../services/build/extension.js'
import {DeveloperPlatformClient} from '../../utilities/developer-platform-client.js'
import {joinPath} from '@shopify/cli-kit/node/path'
import {describe, expect, test} from 'vitest'
import {inTemporaryDirectory, readFile} from '@shopify/cli-kit/node/fs'
import {Writable} from 'stream'

const developerPlatformClient: DeveloperPlatformClient = testDeveloperPlatformClient()

function functionConfiguration(): FunctionConfigType {
return {
name: 'foo',
Expand Down Expand Up @@ -176,23 +180,23 @@ describe('deployConfig', async () => {
test('returns deployConfig when defined', async () => {
const extensionInstance = await testThemeExtensions()

const got = await extensionInstance.deployConfig({token: 'token', apiKey: 'apiKey'})
const got = await extensionInstance.deployConfig({developerPlatformClient, apiKey: 'apiKey'})

expect(got).toMatchObject({theme_extension: {files: {}}})
})

test('returns transformed config when defined', async () => {
const extensionInstance = await testAppConfigExtensions()

const got = await extensionInstance.deployConfig({token: 'token', apiKey: 'apiKey'})
const got = await extensionInstance.deployConfig({developerPlatformClient, apiKey: 'apiKey'})

expect(got).toMatchObject({embedded: true})
})

test('returns undefined when the transformed config is empty', async () => {
const extensionInstance = await testAppConfigExtensions(true)

const got = await extensionInstance.deployConfig({token: 'token', apiKey: 'apiKey'})
const got = await extensionInstance.deployConfig({developerPlatformClient, apiKey: 'apiKey'})

expect(got).toBeUndefined()
})
Expand All @@ -209,7 +213,7 @@ describe('bundleConfig', async () => {
app: 'My app',
extensionsNonUuidManaged: {},
},
token: 'token',
developerPlatformClient,
apiKey: 'apiKey',
})

Expand All @@ -231,7 +235,7 @@ describe('bundleConfig', async () => {
app: 'My app',
extensionsNonUuidManaged: {},
},
token: 'token',
developerPlatformClient,
apiKey: 'apiKey',
})

Expand All @@ -253,7 +257,7 @@ describe('bundleConfig', async () => {
app: 'My app',
extensionsNonUuidManaged: {'point-of-sale': 'uuid'},
},
token: 'token',
developerPlatformClient,
apiKey: 'apiKey',
})

Expand All @@ -274,7 +278,7 @@ describe('bundleConfig', async () => {
app: 'My app',
extensionsNonUuidManaged: {webhooks: 'uuid'},
},
token: 'token',
developerPlatformClient,
apiKey: 'apiKey',
})

Expand Down
20 changes: 12 additions & 8 deletions packages/app/src/cli/models/extensions/extension-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {bundleThemeExtension} from '../../services/extensions/bundle.js'
import {Identifiers} from '../app/identifiers.js'
import {uploadWasmBlob} from '../../services/deploy/upload.js'
import {DeveloperPlatformClient} from '../../utilities/developer-platform-client.js'
import {ok} from '@shopify/cli-kit/node/result'
import {constantize, slugify} from '@shopify/cli-kit/common/string'
import {randomUUID} from '@shopify/cli-kit/node/crypto'
Expand Down Expand Up @@ -160,16 +161,19 @@ export class ExtensionInstance<TConfiguration extends BaseConfigType = BaseConfi
return !this.isAppConfigExtension
}

async deployConfig({apiKey, token}: ExtensionDeployConfigOptions): Promise<{[key: string]: unknown} | undefined> {
if (this.isFunctionExtension) return this.functionDeployConfig({apiKey, token})
async deployConfig({
apiKey,
developerPlatformClient,
}: ExtensionDeployConfigOptions): Promise<{[key: string]: unknown} | undefined> {
if (this.isFunctionExtension) return this.functionDeployConfig({apiKey, developerPlatformClient})
return this.commonDeployConfig(apiKey)
}

async functionDeployConfig({
apiKey,
token,
developerPlatformClient,
}: ExtensionDeployConfigOptions): Promise<{[key: string]: unknown} | undefined> {
const {moduleId} = await uploadWasmBlob(this.localIdentifier, this.outputPath, apiKey, token)
const {moduleId} = await uploadWasmBlob(this.localIdentifier, this.outputPath, developerPlatformClient)
return this.specification.deployConfig?.(this.configuration, this.directory, apiKey, moduleId)
}

Expand Down Expand Up @@ -312,8 +316,8 @@ export class ExtensionInstance<TConfiguration extends BaseConfigType = BaseConfi
return context
}

async bundleConfig({identifiers, token, apiKey}: ExtensionBundleConfigOptions) {
const configValue = await this.deployConfig({apiKey, token})
async bundleConfig({identifiers, developerPlatformClient, apiKey}: ExtensionBundleConfigOptions) {
const configValue = await this.deployConfig({apiKey, developerPlatformClient})
if (!configValue) return undefined

const result = {
Expand All @@ -331,11 +335,11 @@ export class ExtensionInstance<TConfiguration extends BaseConfigType = BaseConfi

export interface ExtensionDeployConfigOptions {
apiKey: string
token: string
developerPlatformClient: DeveloperPlatformClient
}

export interface ExtensionBundleConfigOptions {
identifiers: Identifiers
token: string
developerPlatformClient: DeveloperPlatformClient
apiKey: string
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {FunctionConfigType} from './function.js'
import {testFunctionExtension} from '../../app/app.test-data.js'
import {testDeveloperPlatformClient, testFunctionExtension} from '../../app/app.test-data.js'
import {ExtensionInstance} from '../extension-instance.js'
import * as upload from '../../../services/deploy/upload.js'
import {DeveloperPlatformClient} from '../../../utilities/developer-platform-client.js'
import {inTemporaryDirectory, mkdir, touchFile, writeFile} from '@shopify/cli-kit/node/fs'
import {joinPath} from '@shopify/cli-kit/node/path'
import {AbortError} from '@shopify/cli-kit/node/error'
Expand All @@ -10,6 +11,8 @@ import {getPathValue} from '@shopify/cli-kit/common/object'

vi.mock('../../../services/deploy/upload.js')

const developerPlatformClient: DeveloperPlatformClient = testDeveloperPlatformClient()

describe('functionConfiguration', () => {
let extension: ExtensionInstance<FunctionConfigType>
const moduleId = 'module_id'
Expand Down Expand Up @@ -63,7 +66,7 @@ describe('functionConfiguration', () => {
await writeFile(extension.inputQueryPath, inputQuery)

// When
const got = await extension.deployConfig({apiKey, token})
const got = await extension.deployConfig({apiKey, developerPlatformClient})

// Then
expect(got).toEqual({
Expand Down Expand Up @@ -101,7 +104,7 @@ describe('functionConfiguration', () => {
extension.configuration.ui = undefined

// When
const got = await extension.deployConfig({apiKey, token})
const got = await extension.deployConfig({apiKey, developerPlatformClient})

// Then
expect(got).toEqual({
Expand Down Expand Up @@ -134,7 +137,7 @@ describe('functionConfiguration', () => {
await writeFile(joinPath(extension.directory, inputQueryFileName), inputQuery)

// When
const got = await extension.deployConfig({apiKey, token})
const got = await extension.deployConfig({apiKey, developerPlatformClient})

// Then
expect(getPathValue(got!, 'targets')).toEqual([
Expand All @@ -149,7 +152,7 @@ describe('functionConfiguration', () => {
extension.configuration.targeting = [{target: 'some.api.target1', input_query: 'this-is-not-a-file.graphql'}]

// When & Then
await expect(() => extension.deployConfig({apiKey, token})).rejects.toThrowError(AbortError)
await expect(() => extension.deployConfig({apiKey, developerPlatformClient})).rejects.toThrowError(AbortError)
})

describe('with legacy type', async () => {
Expand All @@ -172,7 +175,7 @@ describe('functionConfiguration', () => {
await writeFile(extension.inputQueryPath, inputQuery)

// When
const got = await extension.deployConfig({apiKey, token})
const got = await extension.deployConfig({apiKey, developerPlatformClient})

// Then
expect(got).toEqual({
Expand Down Expand Up @@ -210,7 +213,7 @@ describe('functionConfiguration', () => {
extension.configuration.ui = undefined

// When
const got = await extension.deployConfig({apiKey, token})
const got = await extension.deployConfig({apiKey, developerPlatformClient})

// Then
expect(got).toEqual({
Expand Down Expand Up @@ -247,7 +250,7 @@ describe('functionConfiguration', () => {
await writeFile(joinPath(localesDir, 'en.default.json'), JSON.stringify(enLocale))

// When
const got = await extension.deployConfig({apiKey, token})
const got = await extension.deployConfig({apiKey, developerPlatformClient})

// Then
const expectedLocalization = {
Expand All @@ -270,7 +273,7 @@ describe('functionConfiguration', () => {
// When
const got = (await extension.deployConfig({
apiKey,
token,
developerPlatformClient,
})) as unknown as {ui: {ui_extension_handle: string}}

// Then
Expand All @@ -287,7 +290,7 @@ describe('functionConfiguration', () => {
// When
const got = (await extension.deployConfig({
apiKey,
token,
developerPlatformClient,
})) as unknown as {ui: {ui_extension_handle: string}}

// Then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import {PaymentsAppExtensionConfigType} from './payments_app_extension.js'
import {OffsitePaymentsAppExtensionConfigType} from './payments_app_extension_schemas/offsite_payments_app_extension_schema.js'
import {testPaymentsAppExtension} from '../../app/app.test-data.js'
import {testDeveloperPlatformClient, testPaymentsAppExtension} from '../../app/app.test-data.js'
import {ExtensionInstance} from '../extension-instance.js'
import * as upload from '../../../services/deploy/upload.js'
import {loadLocalExtensionsSpecifications} from '../load-specifications.js'
import {DeveloperPlatformClient} from '../../../utilities/developer-platform-client.js'
import {inTemporaryDirectory, writeFile} from '@shopify/cli-kit/node/fs'
import {beforeEach, describe, expect, test, vi} from 'vitest'
import {zod} from '@shopify/cli-kit/node/schema'

vi.mock('../../../services/deploy/upload.js')

const developerPlatformClient: DeveloperPlatformClient = testDeveloperPlatformClient()

describe('PaymentsAppExtension', () => {
let extension: ExtensionInstance<PaymentsAppExtensionConfigType>
const moduleId = 'module_id'
Expand Down Expand Up @@ -64,7 +67,7 @@ describe('PaymentsAppExtension', () => {
await writeFile(extension.inputQueryPath, inputQuery)

// When
const result = await extension.deployConfig({apiKey, token})
const result = await extension.deployConfig({apiKey, developerPlatformClient})
const extensionConfiguration = extension.configuration as OffsitePaymentsAppExtensionConfigType

// Then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as loadLocales from '../../../utilities/extensions/locales-configuration.js'
import {ExtensionInstance} from '../extension-instance.js'
import {loadLocalExtensionsSpecifications} from '../load-specifications.js'
import {describe, expect, test, vi} from 'vitest'
import {err, ok} from '@shopify/cli-kit/node/result'
import {DeveloperPlatformClient} from '../../../utilities/developer-platform-client.js'
import {testDeveloperPlatformClient} from '../../app/app.test-data.js'
import {inTemporaryDirectory, mkdir, touchFile} from '@shopify/cli-kit/node/fs'
import {joinPath} from '@shopify/cli-kit/node/path'
import {err, ok} from '@shopify/cli-kit/node/result'
import {zod} from '@shopify/cli-kit/node/schema'
import {describe, expect, test, vi} from 'vitest'

const developerPlatformClient: DeveloperPlatformClient = testDeveloperPlatformClient()

describe('ui_extension', async () => {
interface GetUIExtensionProps {
Expand Down Expand Up @@ -228,7 +232,7 @@ Please check the configuration in ${uiExtension.configurationPath}`),
// When
const deployConfig = await uiExtension.deployConfig({
apiKey: 'apiKey',
token: 'token',
developerPlatformClient,
})

// Then
Expand Down
Loading

0 comments on commit d48a975

Please sign in to comment.