Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
feat: add cancel command
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Sep 19, 2019
1 parent 5600c46 commit bb4e782
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
40 changes: 40 additions & 0 deletions src/cancel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fetch from 'node-fetch'
import getEnvironment from './getEnvironment'
import config from './config'
import pkg from '../package.json'

export class CancelError extends Error {}

async function cancel(options) {
const { token: tokenOption, externalBuildId: externalBuildIdOption } = options

const token = tokenOption || config.get('token')

if (!token) {
throw new CancelError(
'Token missing: use ARGOS_TOKEN or the --token option.',
)
}

let environment = {}

if (process.env.ARGOS_CLI_TEST !== 'true') {
environment = getEnvironment(process.env)
}

const externalBuildId =
externalBuildIdOption ||
config.get('externalBuildId') ||
environment.externalBuildId

return fetch(`${config.get('endpoint')}/cancel-build`, {
headers: {
'X-Argos-CLI-Version': pkg.version,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({ token, externalBuildId }),
})
}

export default cancel
48 changes: 48 additions & 0 deletions src/cancel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import path from 'path'
import fetch from 'node-fetch'
import cancel from './cancel'
import config from './config'
import pkg from '../package.json'

jest.mock('node-fetch')

describe('cancel', () => {
beforeEach(() => {
config.set('endpoint', 'http://localhost')
})

afterEach(() => {
config.reset('endpoint')
})

describe('with missing token', () => {
it('should throw', async () => {
expect.assertions(1)

try {
await cancel({
directory: path.join(__dirname, '../__fixtures__/screenshots'),
})
} catch (error) {
expect(error.message).toMatch('Token missing')
}
})
})

describe.only('with all good', () => {
beforeEach(() => {
fetch.mockImplementation(async () => {})
})

it('should ping cancel-build api', async () => {
await cancel({
token: 'myToken',
})
expect(fetch.mock.calls[0][0]).toBe('http://localhost/cancel-build')
expect(fetch.mock.calls[0][1].headers).toEqual({
'X-Argos-CLI-Version': pkg.version,
'Content-Type': 'application/json',
})
})
})
})
42 changes: 39 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import updateNotifier from 'update-notifier'
import errorReporter, { initializeErrorReporter } from './errorReporter'
import pkg from '../package.json'
import upload, { UploadError } from './upload'
import cancel, { CancelError } from './cancel'
import { displayError, displaySuccess } from './display'

updateNotifier({ pkg }).notify()
Expand All @@ -19,14 +20,15 @@ if (process.env.NODE_ENV !== 'production') {

const list = value => value.split(',')

program.version(pkg.version)

program
.version(pkg.version)
.command('upload <directory>')
.description('Upload screenshots')
.option('-T, --token <token>', 'Repository token')
.option('-C, --commit <commit>', 'Git commit')
.option('-B, --branch <branch>', 'Git branch')
.option('-T, --token <token>', 'Repository token')
.option('--externalBuildId [string]', 'ID of the build (batch mode)')
.option('--externalBuildId [string]', 'ID of the build (batch mode only)')
.option(
'--batchCount [int]',
'Number of batches expected (batch mode)',
Expand Down Expand Up @@ -71,6 +73,40 @@ program
console.log(chalk.green(`build url: ${json.build.buildUrl}`))
})

program
.command('cancel')
.description('Cancel the build (batch mode only)')
.option('-T, --token <token>', 'Repository token')
.option('--externalBuildId [string]', 'ID of the build (batch mode only)')
.action(async command => {
console.log(`=== argos-cli: canceling build`)

let json

try {
const res = await cancel({ ...command })
json = await res.json()

if (json.error) {
throw new CancelError(json.error.message)
}
} catch (error) {
displayError('Sorry an error happened:')

if (error instanceof CancelError) {
console.error(chalk.bold.red(error.message))
} else {
errorReporter.captureException(error)
console.error(chalk.bold.red(error.message))
console.error(chalk.bold.red(error.stack))
}

process.exit(1)
}

displaySuccess('Build canceled.')
})

if (!process.argv.slice(2).length) {
program.outputHelp()
} else {
Expand Down

0 comments on commit bb4e782

Please sign in to comment.