From a14e3fe2333b8fcf00e8b76654e79054337315ed Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Tue, 3 Sep 2024 11:49:02 +1000 Subject: [PATCH] feat(apps:update): add ability to use ZENDESK_APP_ID env var during apps:update (#254) * 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 * fix: type check * fix: remove generated code * fix: lints * chore: add new unit test * chore: update unit test * fix: lints * chore: reset mock --- docs/login.md | 2 +- .../zcli-apps/src/commands/apps/update.ts | 13 ++++++----- .../zcli-apps/tests/functional/create.test.ts | 23 +++++++++++++++++++ packages/zcli-core/src/lib/env.ts | 3 ++- 4 files changed, 33 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..80862c1c 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 '@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.' @@ -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-apps/tests/functional/create.test.ts b/packages/zcli-apps/tests/functional/create.test.ts index 233b04f3..69ed284b 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: 666 }) + }) + .stdout() + .command(['apps:update', singleProductApp]) + .it('should update said apps', async ctx => { + expect(ctx.stdout).to.contain(successUpdateMessage) + }) + }) }) }) 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