Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 43 additions & 50 deletions src/commands/app/undeploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,65 +50,60 @@ class Undeploy extends BaseCommand {
}

const spinner = ora()
try {
const { aio: aioConfig, packagejson: packageJson } = await this.getFullConfig({}, flags)
const cliDetails = await getAccessToken({ useCachedToken: !flags.unpublish })
const appInfo = {
name: packageJson.name,
version: packageJson.version,
project: aioConfig?.project,
runtimeNamespace: aioConfig?.runtime?.namespace

const { aio: aioConfig, packagejson: packageJson } = await this.getFullConfig({}, flags)
const cliDetails = await getAccessToken({ useCachedToken: !flags.unpublish })
const appInfo = {
name: packageJson.name,
version: packageJson.version,
project: aioConfig?.project,
runtimeNamespace: aioConfig?.runtime?.namespace
}

if (cliDetails?.accessToken) {
try {
// send audit log at start (don't wait for deployment to finish)
await sendAppUndeployAuditLog({
accessToken: cliDetails?.accessToken,
cliCommandFlags: flags,
appInfo,
env: cliDetails.env
})
} catch (error) {
if (flags.verbose) {
this.warn('Error: Audit Log Service Error: Failed to send audit log event for deployment.')
this.warn(error.message)
}
}
}

for (let i = 0; i < keys.length; ++i) {
const k = keys[i]
// TODO: remove this check once the deploy service is enabled by default
const v = setRuntimeApiHostAndAuthHandler(values[i])

await this.undeployOneExt(k, v, flags, spinner)
if (cliDetails?.accessToken) {
// send logs for case of web-assets undeployment
try {
// send audit log at start (don't wait for deployment to finish)
await sendAppUndeployAuditLog({
await sendAppAssetsUndeployedAuditLog({
accessToken: cliDetails?.accessToken,
cliCommandFlags: flags,
appInfo,
env: cliDetails.env
})
} catch (error) {
if (flags.verbose) {
this.warn('Error: Audit Log Service Error: Failed to send audit log event for deployment.')
this.warn(error.message)
}
}
}

for (let i = 0; i < keys.length; ++i) {
const k = keys[i]
// TODO: remove this check once the deploy service is enabled by default
const v = setRuntimeApiHostAndAuthHandler(values[i])

await this.undeployOneExt(k, v, flags, spinner)
if (cliDetails?.accessToken) {
// send logs for case of web-assets undeployment
try {
await sendAppAssetsUndeployedAuditLog({
accessToken: cliDetails?.accessToken,
cliCommandFlags: flags,
appInfo,
env: cliDetails.env
})
} catch (error) {
this.warn('Warning: Audit Log Service Error: Failed to send audit log event for un-deployment.')
}
this.warn('Warning: Audit Log Service Error: Failed to send audit log event for un-deployment.')
}
}
}

// 1.2. unpublish extension manifest
if (flags.unpublish) {
const payload = await this.unpublishExtensionPoints(libConsoleCLI, undeployConfigs, aioConfig, flags['force-unpublish'])
this.log(chalk.blue(chalk.bold(`New Extension Point(s) in Workspace '${aioConfig.project.workspace.name}': '${Object.keys(payload.endpoints)}'`)))
} else {
this.log('skipping unpublish phase...')
}
} catch (error) {
spinner.stop()
// delegate to top handler
throw error
// 1.2. unpublish extension manifest
if (flags.unpublish) {
const payload = await this.unpublishExtensionPoints(libConsoleCLI, undeployConfigs, aioConfig, flags['force-unpublish'])
this.log(chalk.blue(chalk.bold(`New Extension Point(s) in Workspace '${aioConfig.project.workspace.name}': '${Object.keys(payload.endpoints)}'`)))
} else {
this.log('skipping unpublish phase...')
}

const command = await this.config.findCommand('app:clean')
Expand Down Expand Up @@ -151,8 +146,7 @@ class Undeploy extends BaseCommand {
}
spinner.succeed(chalk.green(`Un-deploying actions for ${extName}`))
} catch (err) {
spinner.fail(chalk.green(`Un-deploying actions for ${extName}`))
throw err
spinner.warn(chalk.yellow(`Error when un-deploying actions for ${extName}: ${err.message}`))
}
} else {
this.log('no manifest file, skipping action undeploy')
Expand All @@ -168,8 +162,7 @@ class Undeploy extends BaseCommand {

spinner.succeed(chalk.green(`Un-Deploying web assets for ${extName}`))
} catch (err) {
spinner.fail(chalk.green(`Un-Deploying web assets for ${extName}`))
throw err
spinner.warn(chalk.yellow(`Error when un-deploying web assets for ${extName}: ${err.message}`))
}
} else {
this.log('no frontend, skipping frontend undeploy')
Expand Down
16 changes: 12 additions & 4 deletions test/commands/app/undeploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describe('run', () => {
mockFS.existsSync.mockReset()
command = new TheCommand([])
command.error = jest.fn()
command.warn = jest.fn()
command.log = jest.fn()
command.appConfig = cloneDeep(mockConfigData)
command.appConfig.actions = { dist: 'actions' }
Expand Down Expand Up @@ -325,27 +326,34 @@ describe('run', () => {
expect(command.log).toHaveBeenCalledWith('no frontend, skipping frontend undeploy')
})

test('should fail if scripts.undeployActions fails', async () => {
test('should not fail if scripts.undeployActions fails', async () => {
command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig())

const error = new Error('mock failure Actions')
mockRuntimeLib.undeployActions.mockRejectedValue(error)

await expect(command.run()).rejects.toThrow(error)
await expect(command.run()).resolves.toBeUndefined()

// multiple check lines because error wraps text
expect(stdout.output).toContain('Error when un-deploying actions for application: mock failure Actions')

expect(command.error).toHaveBeenCalledTimes(0)
expect(mockRuntimeLib.undeployActions).toHaveBeenCalledTimes(1)
expect(mockWebLib.undeployWeb).toHaveBeenCalledTimes(1)
})

test('should fail if scripts.undeployWeb fails', async () => {
test('should not fail if scripts.undeployWeb fails', async () => {
command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig())

const error = new Error('mock failure UI')
mockWebLib.undeployWeb.mockRejectedValue(error)

await expect(command.run()).rejects.toThrow(error)
await expect(command.run()).resolves.toBeUndefined()

expect(stdout.output).toContain('Error when un-deploying web assets for application: mock failure UI')

expect(command.error).toHaveBeenCalledTimes(0)
expect(mockRuntimeLib.undeployActions).toHaveBeenCalledTimes(1)
expect(mockWebLib.undeployWeb).toHaveBeenCalledTimes(1)
})

Expand Down