Skip to content

Commit

Permalink
Merge branch 'master' into snyk-upgrade-fd6db10093a758381a5af8281f0a71da
Browse files Browse the repository at this point in the history
  • Loading branch information
sarmadsangi authored May 31, 2021
2 parents 0ad6812 + f036864 commit d7ba75e
Show file tree
Hide file tree
Showing 25 changed files with 194 additions and 101 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [1.0.0-beta.8](https://github.com/zendesk/zcli/compare/v1.0.0-beta.7...v1.0.0-beta.8) (2021-03-13)

**Note:** Version bump only for package zcli-monorepo





# [1.0.0-beta.7](https://github.com/zendesk/zcli/compare/v1.0.0-beta.6...v1.0.0-beta.7) (2021-03-11)

**Note:** Version bump only for package zcli-monorepo





# [1.0.0-beta.6](https://github.com/zendesk/zcli/compare/v1.0.0-beta.5...v1.0.0-beta.6) (2021-02-14)

**Note:** Version bump only for package zcli-monorepo





# [1.0.0-beta.5](https://github.com/zendesk/zcli/compare/v1.0.0-beta.4...v1.0.0-beta.5) (2021-02-04)


Expand Down
2 changes: 1 addition & 1 deletion docs/apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ NOTE: You can set your apps config/settings in `zcli.apps.config.json` at the ro
{
"plan": "silver",
"app_id": 123,
"installation_id": 12434234,
"parameters": {
"someToken": "fksjdhfb231435",
"someSecret": 123
Expand Down Expand Up @@ -121,7 +122,6 @@ USAGE
OPTIONS
-h, --help show CLI help
--bind=bind [default: localhost] Bind apps server to a specific host
--config=config [default: zcli.apps.config.json] Configuration file for zcli::apps
--logs Tail logs
--port=port [default: 4567] Port for the http server to use
Expand Down
20 changes: 0 additions & 20 deletions docs/theme.md

This file was deleted.

2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
"--no-lockfile"
],
"useWorkspaces": true,
"version": "1.0.0-beta.5"
"version": "1.0.0-beta.8"
}
16 changes: 16 additions & 0 deletions packages/zcli-apps/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [1.0.0-beta.8](https://github.com/zendesk/zcli/compare/v1.0.0-beta.7...v1.0.0-beta.8) (2021-03-13)

**Note:** Version bump only for package @zendesk/zcli-apps





# [1.0.0-beta.6](https://github.com/zendesk/zcli/compare/v1.0.0-beta.5...v1.0.0-beta.6) (2021-02-14)

**Note:** Version bump only for package @zendesk/zcli-apps





# [1.0.0-beta.5](https://github.com/zendesk/zcli/compare/v1.0.0-beta.4...v1.0.0-beta.5) (2021-02-04)


Expand Down
8 changes: 4 additions & 4 deletions packages/zcli-apps/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zendesk/zcli-apps",
"description": "zcli apps commands live here",
"version": "1.0.0-beta.5",
"version": "1.0.0-beta.8",
"author": "@vegemite",
"npmRegistry": "https://registry.npmjs.org",
"publishConfig": {
Expand All @@ -18,13 +18,13 @@
"dependencies": {
"@oclif/command": "^1",
"@oclif/config": "^1",
"adm-zip": "0.5.1",
"adm-zip": "0.5.2",
"archiver": "4.0.2",
"chalk": "4.1.0",
"chalk": "4.1.1",
"cli-ux": "^5.4.1",
"cors": "^2.8.5",
"express": "^4.17.1",
"form-data": "3.0.0",
"form-data": "3.0.1",
"fs-extra": "9.1.0",
"morgan": "1.10.0",
"node-fetch": "^2.6.0",
Expand Down
10 changes: 8 additions & 2 deletions packages/zcli-apps/src/commands/apps/create.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Command } from '@oclif/command'
import { request } from '@zendesk/zcli-core'
import { promptAndGetSettings, uploadAppPkg, deployApp } from '../../utils/createApp'
import { uploadAppPkg, deployApp } from '../../utils/createApp'
import * as chalk from 'chalk'
import { getUploadJobStatus } from '../../utils/uploadApp'
import cli from 'cli-ux'
import { getManifestFile } from '../../utils/manifest'
import { createAppPkg } from '../../lib/package'
import { validateAppPath } from '../../lib/appPath'
import { getAllConfigs } from '../../utils/appConfig'
import { getAppSettings } from '../../utils/getAppSettings'

export default class Create extends Command {
static description = 'creates apps in your desired target account'
Expand Down Expand Up @@ -46,7 +48,11 @@ export default class Create extends Command {
try {
const { app_id }: any = await getUploadJobStatus(job_id, appPath)
cli.action.stop('Deployed')
const settings = manifest.parameters ? await promptAndGetSettings(manifest.parameters) : {}

const allConfigs = getAllConfigs(appPath)
const configParams = allConfigs?.parameters || {} // if there are no parameters in the config, just attach an empty object

const settings = manifest.parameters ? await getAppSettings(manifest, configParams) : {}
const installed = await request.requestAPI('api/v2/apps/installations.json', {
method: 'POST',
body: JSON.stringify({ app_id: `${app_id}`, settings: { name: manifest.name, ...settings } }),
Expand Down
3 changes: 1 addition & 2 deletions packages/zcli-apps/src/commands/apps/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ export default class Server extends Command {
async run () {
const { flags } = this.parse(Server)
const port = parseInt(flags.port)
const config = 'zcli.apps.config.json'
const { logs: tailLogs, bind: host } = flags
const { argv: appDirectories } = this.parse(Server)

const appPaths = getAppPaths(appDirectories)
const appJSON = await buildAppJSON(appPaths, port, config)
const appJSON = await buildAppJSON(appPaths, port)

const app = express()
app.use(cors())
Expand Down
20 changes: 11 additions & 9 deletions packages/zcli-apps/src/commands/apps/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import * as chalk from 'chalk'
import { request } from '@zendesk/zcli-core'
import cli from 'cli-ux'
import { getUploadJobStatus } from '../../utils/uploadApp'
import { promptAndGetSettings, uploadAppPkg, deployApp } from '../../utils/createApp'
import { uploadAppPkg, deployApp } from '../../utils/createApp'
import { getManifestFile } from '../../utils/manifest'
import { createAppPkg } from '../../lib/package'
import { Manifest, Installations } from '../../types'
import { Manifest, Installations, ZcliConfigFileContent } from '../../types'
import { validateAppPath } from '../../lib/appPath'
import { getAppSettings } from '../../utils/getAppSettings'

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 @@ -21,22 +22,24 @@ export default class Update extends Command {
static strict = false

getAppID (appPath: string) {
const allConfigs = getAllConfigs(appPath, 'zcli.apps.config.json')
const allConfigs = getAllConfigs(appPath)
const app_id = allConfigs ? allConfigs.app_id : undefined
if (!app_id) { throw new CLIError(chalk.red('App ID not found')) }
return app_id
}

async installApp (appId: string, uploadId: number, appPath: string, manifest: Manifest) {
async installApp (appConfig: ZcliConfigFileContent, uploadId: number, appPath: string, manifest: Manifest) {
cli.action.start('Deploying app')
const { job_id } = await deployApp('PUT', `api/v2/apps/${appId}`, uploadId)
const { job_id } = await deployApp('PUT', `api/v2/apps/${appConfig.app_id}`, uploadId)

try {
const { app_id }: any = await getUploadJobStatus(job_id, appPath)
cli.action.stop('Deployed')

const installations: Installations = await request.requestAPI('/api/v2/apps/installations.json', {}, true)
const settings = manifest.parameters ? await promptAndGetSettings(manifest.parameters) : {}

const configParams = appConfig?.parameters || {} // if there are no parameters in the config, just attach an empty object
const settings = manifest.parameters ? await getAppSettings(manifest, configParams) : {}
const installation_id = installations.installations.filter(i => i.app_id === app_id)[0].id
const updated = await request.requestAPI(`/api/v2/apps/installations/${installation_id}.json`, {
method: 'PUT',
Expand Down Expand Up @@ -64,8 +67,7 @@ export default class Update extends Command {
validateAppPath(appPath)

cli.action.start('Uploading app')

const appId = this.getAppID(appPath)
const appConfig = getAllConfigs(appPath) || {}
const manifest = getManifestFile(appPath)
const pkgPath = await createAppPkg(appPath)
const { id: upload_id } = await uploadAppPkg(pkgPath)
Expand All @@ -77,7 +79,7 @@ export default class Update extends Command {

cli.action.stop('Uploaded')
try {
await this.installApp(appId, upload_id, appPath, manifest)
await this.installApp(appConfig, upload_id, appPath, manifest)
} catch (error) {
this.error(chalk.red(error))
}
Expand Down
1 change: 1 addition & 0 deletions packages/zcli-apps/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_APPS_CONFIG_FILE = 'zcli.apps.config.json'
26 changes: 2 additions & 24 deletions packages/zcli-apps/src/lib/buildAppJSON.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as uuid from '../utils/uuid'
import * as buildAppJSON from './buildAppJSON'
import * as appPath from '../lib/appPath'
import * as manifest from '../utils/manifest'
import * as createApp from '../utils/createApp'
import { Manifest } from './../types'
import * as path from 'path'

Expand Down Expand Up @@ -223,27 +222,6 @@ describe('getInstallation', () => {
})
})

describe('getAppSettings', () => {
test
.stub(createApp, 'promptAndGetSettings', () => ({ someToken: 'ABC123' }))
.it('should return setting from config and prompt for missing config', async () => {
const settings = await buildAppJSON.getAppSettings(manifestOutput, { salesForceId: 222 })
expect(settings).to.deep.equals({
salesForceId: 222,
someToken: 'ABC123'
})
})

test
.it('should return all settings from config', async () => {
const settings = await buildAppJSON.getAppSettings(manifestOutput, { salesForceId: 222, someToken: 'XYZ786' })
expect(settings).to.deep.equals({
salesForceId: 222,
someToken: 'XYZ786'
})
})
})

describe('buildAppJSON', () => {
before(function () {
this.clock = sinon.useFakeTimers(new Date('2020-01-01'))
Expand All @@ -269,7 +247,7 @@ describe('buildAppJSON', () => {
.stub(uuid, 'uuidV4', () => mockId)
.stub(buildAppJSON, 'getLocationIcons', () => { return multiProductLocationIcons })
.it('should return a JSON object with zcli.apps.config.json file contents', async () => {
const appJSON = await buildAppJSON.buildAppJSON(['./app1'], 1234, 'zcli.apps.config.json')
const appJSON = await buildAppJSON.buildAppJSON(['./app1'], 1234)
expect(appJSON).to.deep.include({
apps: [
{
Expand Down Expand Up @@ -321,7 +299,7 @@ describe('buildAppJSON', () => {
.stub(uuid, 'uuidV4', () => mockId)
.stub(buildAppJSON, 'getLocationIcons', () => { return multiProductLocationIcons })
.it('should return a JSON object with zcli.apps.config.json file contents', async () => {
const appJSON = await buildAppJSON.buildAppJSON(['./app1'], 1234, 'zcli.apps.config.json')
const appJSON = await buildAppJSON.buildAppJSON(['./app1'], 1234)

expect(appJSON).to.deep.include({
apps: [
Expand Down
28 changes: 6 additions & 22 deletions packages/zcli-apps/src/lib/buildAppJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import {
LocationIcons,
Manifest,
ProductLocationIcons,
ZcliConfigFileContent,
Dictionary
ZcliConfigFileContent
} from '../types'
import * as path from 'path'
import * as fs from 'fs'
import { uuidV4 } from '../utils/uuid'
import { getManifestFile } from '../utils/manifest'
import { getAllConfigs } from '../utils/appConfig'
import { promptAndGetSettings } from '../utils/createApp'
import { getAppSettings } from '../utils/getAppSettings'
import { validateAppPath } from './appPath'

// The SVGs here are in the top bar or nav bar locations. Chat don’t have these locations thus not here.
Expand Down Expand Up @@ -52,7 +51,8 @@ export const getLocationIcons = (appPath: string, manifestLocations: Location):
}

export const getInstallation = (appId: string, app: App, configFileContents: ZcliConfigFileContent, appSettings: ConfigParameters): Installation => {
const installationId = uuidV4()
const installationId = configFileContents.installation_id || uuidV4()

return {
app_id: appId,
name: app.name,
Expand Down Expand Up @@ -84,29 +84,13 @@ export const getAppPayloadFromManifest = (appManifest: Manifest, port: number, a
}
}

export const getAppSettings = async (manifest: Manifest, configParams: ConfigParameters) => {
if (!manifest.parameters) return {}
const configContainsParam = (paramName: string) => Object.keys(configParams).includes(paramName)

const paramsNotInConfig = manifest.parameters.filter(param => !configContainsParam(param.name))
const configSettings = manifest.parameters.reduce((result: Dictionary<string>, param) => {
if (configContainsParam(param.name)) {
result[param.name] = configParams[param.name] as string
}
return result
}, {})

const promptSettings = paramsNotInConfig ? await promptAndGetSettings(paramsNotInConfig, manifest.name, false) : {}
return { ...configSettings, ...promptSettings }
}

export const buildAppJSON = async (appPaths: string[], port: number, configFileName: string): Promise<AppJSONPayload> => {
export const buildAppJSON = async (appPaths: string[], port: number): Promise<AppJSONPayload> => {
const appJSON: AppJSON = { apps: [], installations: [] }

for (const appPath of appPaths) {
validateAppPath(appPath)
const manifest = getManifestFile(appPath)
const zcliConfigFile = getAllConfigs(appPath, configFileName) || {}
const zcliConfigFile = getAllConfigs(appPath) || {}

const appId = zcliConfigFile.app_id?.toString() || uuidV4()
const configParams = zcliConfigFile.parameters || {} // if there are no parameters in the config, just attach an empty object
Expand Down
1 change: 1 addition & 0 deletions packages/zcli-apps/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface ZcliConfigFileContent {
zat_update_check?: string;
plan?: string;
app_id?: string;
installation_id?: string;
parameters?: ConfigParameters;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/zcli-apps/src/utils/appConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import * as path from 'path'
import * as fs from 'fs-extra'
import * as chalk from 'chalk'
import { CLIError } from '@oclif/errors'
import { DEFAULT_APPS_CONFIG_FILE } from '../constants'

export const getAllConfigs = (appPath: string, configFileName: string): ZcliConfigFileContent | undefined => {
export const getAllConfigs = (appPath: string, configFileName: string = DEFAULT_APPS_CONFIG_FILE): ZcliConfigFileContent | undefined => {
const configFilePath = path.join(appPath, configFileName)

if (fs.existsSync(configFilePath)) {
Expand Down
Loading

0 comments on commit d7ba75e

Please sign in to comment.