From bb2f886b5e09778a094f559fb2e2bbe3949a7a98 Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Wed, 28 Aug 2024 14:09:13 +1000 Subject: [PATCH 1/8] feat: add ability to use ZENDESK_APP_ID env var during apps:update Someones we want to have seperate apps for staging and production. In these cases they will have a seperate app_id. In this change we will allow users to set the `ZENDESK_APP_ID` environment variable to specify which app_id to use. This will allow developers to set this in their CICD pipelines. Fixes: https://github.com/zendesk/zcli/issues/173 --- docs/login.md | 2 +- packages/zcli-apps/src/commands/apps/update.ts | 13 +++++++------ packages/zcli-core/src/lib/env.ts | 3 ++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/docs/login.md b/docs/login.md index 7f9a03cb..c94c8fbb 100644 --- a/docs/login.md +++ b/docs/login.md @@ -30,5 +30,5 @@ EXAMPLES NOTE: For development purposes, you can specify a domain different from `zendesk.com` for logging in to a different environment. For example, if the environment is hosted on `example.com`, you can run `zcli login -s zendesk-subdomain -d example.com -i` and you will be logged in to `zendesk-subdomain.example.com`. If the option is not specified, the default `zendesk.com` domain will be used. -NOTE: For CI/CD or unattended login you can set `ZENDESK_SUBDOMAIN`, `ZENDESK_EMAIL` and `ZENDESK_API_TOKEN` environment variables. You don't need to run login command if you have set these environment variables. +NOTE: For CI/CD or unattended login you can set `ZENDESK_APP_ID`, `ZENDESK_SUBDOMAIN`, `ZENDESK_EMAIL` and `ZENDESK_API_TOKEN` environment variables. You don't need to run login command if you have set these environment variables. You can also set the `ZENDESK_DOMAIN` environment variable for different environments. diff --git a/packages/zcli-apps/src/commands/apps/update.ts b/packages/zcli-apps/src/commands/apps/update.ts index f93fab7c..6b019857 100644 --- a/packages/zcli-apps/src/commands/apps/update.ts +++ b/packages/zcli-apps/src/commands/apps/update.ts @@ -8,6 +8,7 @@ import { getManifestFile } from '../../utils/manifest' import { createAppPkg } from '../../lib/package' import { Manifest, ZcliConfigFileContent } from '../../types' import { validateAppPath } from '../../lib/appPath' +import { EnvVars } from '../../../../zcli-core/src/lib/env' export default class Update extends Command { static description = 'updates an existing private app in the Zendesk products specified in the apps manifest file.' @@ -18,16 +19,15 @@ export default class Update extends Command { static strict = false - getAppID (appPath: string) { - const allConfigs = getAllConfigs(appPath) - const app_id = allConfigs ? allConfigs.app_id : undefined + getAppID (appConfig: ZcliConfigFileContent) { + const app_id = process.env[EnvVars.APP_ID] || (appConfig ? appConfig.app_id : undefined) if (!app_id) { throw new CLIError(chalk.red('App ID not found')) } return app_id } - async installApp (appConfig: ZcliConfigFileContent, uploadId: number, appPath: string, manifest: Manifest) { + async installApp (appConfig: ZcliConfigFileContent, uploadId: number, appPath: string, manifest: Manifest, appID: string) { CliUx.ux.action.start('Deploying app') - const { job_id } = await deployApp('PUT', `api/v2/apps/${appConfig.app_id}`, uploadId) + const { job_id } = await deployApp('PUT', `api/v2/apps/${appID}`, uploadId) try { const { app_id }: any = await getUploadJobStatus(job_id, appPath) @@ -54,6 +54,7 @@ export default class Update extends Command { CliUx.ux.action.start('Uploading app') const appConfig = getAllConfigs(appPath) || {} + const appID = this.getAppID(appConfig) const manifest = getManifestFile(appPath) const pkgPath = await createAppPkg(appPath) const { id: upload_id } = await uploadAppPkg(pkgPath) @@ -65,7 +66,7 @@ export default class Update extends Command { CliUx.ux.action.stop('Uploaded') try { - await this.installApp(appConfig, upload_id, appPath, manifest) + await this.installApp(appConfig, upload_id, appPath, manifest, appID) } catch (error) { this.error(chalk.red(error)) } diff --git a/packages/zcli-core/src/lib/env.ts b/packages/zcli-core/src/lib/env.ts index b9eb893c..3561d2d5 100644 --- a/packages/zcli-core/src/lib/env.ts +++ b/packages/zcli-core/src/lib/env.ts @@ -4,7 +4,8 @@ export const EnvVars = { EMAIL: 'ZENDESK_EMAIL', PASSWORD: 'ZENDESK_PASSWORD', API_TOKEN: 'ZENDESK_API_TOKEN', - OAUTH_TOKEN: 'ZENDESK_OAUTH_TOKEN' + OAUTH_TOKEN: 'ZENDESK_OAUTH_TOKEN', + APP_ID: 'ZENDESK_APP_ID' } export const varExists = (...args: any[]) => !args.filter(envVar => !process.env[envVar]).length From ebae091b93df4397656e7d34dc8ae19eda54adc2 Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Wed, 28 Aug 2024 17:34:14 +1000 Subject: [PATCH 2/8] fix: type check --- packages/zcli-apps/src/commands/apps/update.ts | 2 +- packages/zcli-core/src/lib/env.d.ts | 10 ++++++++++ packages/zcli-core/src/lib/env.js | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 packages/zcli-core/src/lib/env.d.ts create mode 100644 packages/zcli-core/src/lib/env.js diff --git a/packages/zcli-apps/src/commands/apps/update.ts b/packages/zcli-apps/src/commands/apps/update.ts index 6b019857..80862c1c 100644 --- a/packages/zcli-apps/src/commands/apps/update.ts +++ b/packages/zcli-apps/src/commands/apps/update.ts @@ -8,7 +8,7 @@ import { getManifestFile } from '../../utils/manifest' import { createAppPkg } from '../../lib/package' import { Manifest, ZcliConfigFileContent } from '../../types' import { validateAppPath } from '../../lib/appPath' -import { EnvVars } from '../../../../zcli-core/src/lib/env' +import { EnvVars } from '@zendesk/zcli-core/src/lib/env' export default class Update extends Command { static description = 'updates an existing private app in the Zendesk products specified in the apps manifest file.' diff --git a/packages/zcli-core/src/lib/env.d.ts b/packages/zcli-core/src/lib/env.d.ts new file mode 100644 index 00000000..ddb159b2 --- /dev/null +++ b/packages/zcli-core/src/lib/env.d.ts @@ -0,0 +1,10 @@ +export declare const EnvVars: { + SUBDOMAIN: string; + DOMAIN: string; + EMAIL: string; + PASSWORD: string; + API_TOKEN: string; + OAUTH_TOKEN: string; + APP_ID: string; +}; +export declare const varExists: (...args: any[]) => boolean; diff --git a/packages/zcli-core/src/lib/env.js b/packages/zcli-core/src/lib/env.js new file mode 100644 index 00000000..10111046 --- /dev/null +++ b/packages/zcli-core/src/lib/env.js @@ -0,0 +1,14 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.varExists = exports.EnvVars = void 0; +exports.EnvVars = { + SUBDOMAIN: 'ZENDESK_SUBDOMAIN', + DOMAIN: 'ZENDESK_DOMAIN', + EMAIL: 'ZENDESK_EMAIL', + PASSWORD: 'ZENDESK_PASSWORD', + API_TOKEN: 'ZENDESK_API_TOKEN', + OAUTH_TOKEN: 'ZENDESK_OAUTH_TOKEN', + APP_ID: 'ZENDESK_APP_ID' +}; +const varExists = (...args) => !args.filter(envVar => !process.env[envVar]).length; +exports.varExists = varExists; From e478264ba0ac5dc6461c2193056e9225c4adb974 Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Wed, 28 Aug 2024 17:41:20 +1000 Subject: [PATCH 3/8] fix: remove generated code --- packages/zcli-apps/tests/functional/update.test.ts | 5 +++++ packages/zcli-core/src/lib/env.d.ts | 10 ---------- packages/zcli-core/src/lib/env.js | 14 -------------- packages/zcli-core/src/lib/env.ts | 2 +- 4 files changed, 6 insertions(+), 25 deletions(-) create mode 100644 packages/zcli-apps/tests/functional/update.test.ts delete mode 100644 packages/zcli-core/src/lib/env.d.ts delete mode 100644 packages/zcli-core/src/lib/env.js diff --git a/packages/zcli-apps/tests/functional/update.test.ts b/packages/zcli-apps/tests/functional/update.test.ts new file mode 100644 index 00000000..e2c90a39 --- /dev/null +++ b/packages/zcli-apps/tests/functional/update.test.ts @@ -0,0 +1,5 @@ +import { expect, test } from '@oclif/test' + +describe('apps update', function () { + +}) diff --git a/packages/zcli-core/src/lib/env.d.ts b/packages/zcli-core/src/lib/env.d.ts deleted file mode 100644 index ddb159b2..00000000 --- a/packages/zcli-core/src/lib/env.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -export declare const EnvVars: { - SUBDOMAIN: string; - DOMAIN: string; - EMAIL: string; - PASSWORD: string; - API_TOKEN: string; - OAUTH_TOKEN: string; - APP_ID: string; -}; -export declare const varExists: (...args: any[]) => boolean; diff --git a/packages/zcli-core/src/lib/env.js b/packages/zcli-core/src/lib/env.js deleted file mode 100644 index 10111046..00000000 --- a/packages/zcli-core/src/lib/env.js +++ /dev/null @@ -1,14 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.varExists = exports.EnvVars = void 0; -exports.EnvVars = { - SUBDOMAIN: 'ZENDESK_SUBDOMAIN', - DOMAIN: 'ZENDESK_DOMAIN', - EMAIL: 'ZENDESK_EMAIL', - PASSWORD: 'ZENDESK_PASSWORD', - API_TOKEN: 'ZENDESK_API_TOKEN', - OAUTH_TOKEN: 'ZENDESK_OAUTH_TOKEN', - APP_ID: 'ZENDESK_APP_ID' -}; -const varExists = (...args) => !args.filter(envVar => !process.env[envVar]).length; -exports.varExists = varExists; diff --git a/packages/zcli-core/src/lib/env.ts b/packages/zcli-core/src/lib/env.ts index 3561d2d5..ccd706ec 100644 --- a/packages/zcli-core/src/lib/env.ts +++ b/packages/zcli-core/src/lib/env.ts @@ -5,7 +5,7 @@ export const EnvVars = { PASSWORD: 'ZENDESK_PASSWORD', API_TOKEN: 'ZENDESK_API_TOKEN', OAUTH_TOKEN: 'ZENDESK_OAUTH_TOKEN', - APP_ID: 'ZENDESK_APP_ID' + APP_ID: 'ZENDESK_APP_ID', } export const varExists = (...args: any[]) => !args.filter(envVar => !process.env[envVar]).length From 6e4e6996db9b96b86a432c86f63b2edf2893f132 Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Wed, 28 Aug 2024 17:44:07 +1000 Subject: [PATCH 4/8] fix: lints --- packages/zcli-apps/tests/functional/update.test.ts | 5 ----- packages/zcli-core/src/lib/env.ts | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) delete mode 100644 packages/zcli-apps/tests/functional/update.test.ts diff --git a/packages/zcli-apps/tests/functional/update.test.ts b/packages/zcli-apps/tests/functional/update.test.ts deleted file mode 100644 index e2c90a39..00000000 --- a/packages/zcli-apps/tests/functional/update.test.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { expect, test } from '@oclif/test' - -describe('apps update', function () { - -}) diff --git a/packages/zcli-core/src/lib/env.ts b/packages/zcli-core/src/lib/env.ts index ccd706ec..3561d2d5 100644 --- a/packages/zcli-core/src/lib/env.ts +++ b/packages/zcli-core/src/lib/env.ts @@ -5,7 +5,7 @@ export const EnvVars = { PASSWORD: 'ZENDESK_PASSWORD', API_TOKEN: 'ZENDESK_API_TOKEN', OAUTH_TOKEN: 'ZENDESK_OAUTH_TOKEN', - APP_ID: 'ZENDESK_APP_ID', + APP_ID: 'ZENDESK_APP_ID' } export const varExists = (...args: any[]) => !args.filter(envVar => !process.env[envVar]).length From 363ef0aca2d4e0bde6acc83186e8b4fc2889787d Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Wed, 28 Aug 2024 17:50:43 +1000 Subject: [PATCH 5/8] chore: add new unit test --- .../zcli-apps/tests/functional/create.test.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/zcli-apps/tests/functional/create.test.ts b/packages/zcli-apps/tests/functional/create.test.ts index 233b04f3..27e52b45 100644 --- a/packages/zcli-apps/tests/functional/create.test.ts +++ b/packages/zcli-apps/tests/functional/create.test.ts @@ -160,5 +160,28 @@ describe('apps', function () { expect(ctx.stdout).to.contain(successUpdateMessage) }) }) + + describe('with ZENDESK_APP_ID set', () => { + test + .stub(packageUtil, 'createAppPkg', () => createAppPkgStub) + .env({...env, ZENDESK_APP_ID: '666'}) + .do(() => { + createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled') + uploadAppPkgStub.onFirstCall().resolves({ id: 819 }) + }) + .nock('https://z3ntest.zendesk.com/', api => { + api + .put('/api/v2/apps/666', { upload_id: 819 }) + .reply(200, { job_id: 129 }) + api + .get('/api/v2/apps/job_statuses/129') + .reply(200, { status: 'completed', message: 'awesome', app_id: 123456 }) + }) + .stdout() + .command(['apps:update', singleProductApp]) + .it('should update said apps', async ctx => { + expect(ctx.stdout).to.contain(successUpdateMessage) + }) + }) }) }) From 8f115ade6bdaa4ac055d2c2abcd7dd4ebf76f3a1 Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Wed, 28 Aug 2024 17:52:29 +1000 Subject: [PATCH 6/8] chore: update unit test --- packages/zcli-apps/tests/functional/create.test.ts | 2 +- .../functional/mocks/single_product_app/zcli.apps.config.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/zcli-apps/tests/functional/create.test.ts b/packages/zcli-apps/tests/functional/create.test.ts index 27e52b45..7ea246c2 100644 --- a/packages/zcli-apps/tests/functional/create.test.ts +++ b/packages/zcli-apps/tests/functional/create.test.ts @@ -175,7 +175,7 @@ describe('apps', function () { .reply(200, { job_id: 129 }) api .get('/api/v2/apps/job_statuses/129') - .reply(200, { status: 'completed', message: 'awesome', app_id: 123456 }) + .reply(200, { status: 'completed', message: 'awesome', app_id: 666 }) }) .stdout() .command(['apps:update', singleProductApp]) diff --git a/packages/zcli-apps/tests/functional/mocks/single_product_app/zcli.apps.config.json b/packages/zcli-apps/tests/functional/mocks/single_product_app/zcli.apps.config.json index 67bb252a..265b98e1 100644 --- a/packages/zcli-apps/tests/functional/mocks/single_product_app/zcli.apps.config.json +++ b/packages/zcli-apps/tests/functional/mocks/single_product_app/zcli.apps.config.json @@ -1 +1 @@ -{"app_id":123456} +{"app_id":666} From a049683aa665f06b5441ddbf88c13dedca27428f Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Wed, 28 Aug 2024 17:59:22 +1000 Subject: [PATCH 7/8] fix: lints --- packages/zcli-apps/tests/functional/create.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/zcli-apps/tests/functional/create.test.ts b/packages/zcli-apps/tests/functional/create.test.ts index 7ea246c2..69ed284b 100644 --- a/packages/zcli-apps/tests/functional/create.test.ts +++ b/packages/zcli-apps/tests/functional/create.test.ts @@ -164,7 +164,7 @@ describe('apps', function () { describe('with ZENDESK_APP_ID set', () => { test .stub(packageUtil, 'createAppPkg', () => createAppPkgStub) - .env({...env, ZENDESK_APP_ID: '666'}) + .env({ ...env, ZENDESK_APP_ID: '666' }) .do(() => { createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled') uploadAppPkgStub.onFirstCall().resolves({ id: 819 }) From 5a412b32ffe10816dff3cbacd87d4a6ff7b3ed73 Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Wed, 28 Aug 2024 18:01:18 +1000 Subject: [PATCH 8/8] chore: reset mock --- .../functional/mocks/single_product_app/zcli.apps.config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/zcli-apps/tests/functional/mocks/single_product_app/zcli.apps.config.json b/packages/zcli-apps/tests/functional/mocks/single_product_app/zcli.apps.config.json index 265b98e1..67bb252a 100644 --- a/packages/zcli-apps/tests/functional/mocks/single_product_app/zcli.apps.config.json +++ b/packages/zcli-apps/tests/functional/mocks/single_product_app/zcli.apps.config.json @@ -1 +1 @@ -{"app_id":666} +{"app_id":123456}