Skip to content

Commit

Permalink
feat(apps:update): add ability to use ZENDESK_APP_ID env var during a…
Browse files Browse the repository at this point in the history
…pps: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: #173

* fix: type check

* fix: remove generated code

* fix: lints

* chore: add new unit test

* chore: update unit test

* fix: lints

* chore: reset mock
  • Loading branch information
yoshdog authored Sep 3, 2024
1 parent 3dd84a8 commit a14e3fe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/login.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
13 changes: 7 additions & 6 deletions packages/zcli-apps/src/commands/apps/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand All @@ -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)

Check warning on line 33 in packages/zcli-apps/src/commands/apps/update.ts

View workflow job for this annotation

GitHub Actions / build-and-check (ubuntu-latest, 18.x)

Unexpected any. Specify a different type

Check warning on line 33 in packages/zcli-apps/src/commands/apps/update.ts

View workflow job for this annotation

GitHub Actions / build-and-check (macos-latest, 18.x)

Unexpected any. Specify a different type

Check warning on line 33 in packages/zcli-apps/src/commands/apps/update.ts

View workflow job for this annotation

GitHub Actions / build-and-check (windows-latest, 18.x)

Unexpected any. Specify a different type
Expand All @@ -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)
Expand All @@ -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))
}
Expand Down
23 changes: 23 additions & 0 deletions packages/zcli-apps/tests/functional/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
})
3 changes: 2 additions & 1 deletion packages/zcli-core/src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a14e3fe

Please sign in to comment.