diff --git a/packages/cli/src/commands/spaces/transfer.ts b/packages/cli/src/commands/spaces/transfer.ts new file mode 100644 index 0000000000..1d06f294cd --- /dev/null +++ b/packages/cli/src/commands/spaces/transfer.ts @@ -0,0 +1,34 @@ +import color from '@heroku-cli/color' +import {Command, flags} from '@heroku-cli/command' +import {ux} from '@oclif/core' +import heredoc from 'tsheredoc' + +export default class Transfer extends Command { + static topic = 'spaces' + static description = 'transfer a space to another team' + static examples = [heredoc(` + $ heroku spaces:transfer --space=space-name --team=team-name + Transferring space-name to team-name... done + `)] + + static flags = { + space: flags.string({required: true, description: 'name of space'}), + team: flags.string({required: true, description: 'desired owner of space'}), + } + + public async run(): Promise { + const {flags} = await this.parse(Transfer) + const space = flags.space + const team = flags.team + + try { + ux.action.start(`Transferring space ${color.yellow(space)} to team ${color.green(team)}`) + await this.heroku.post(`/spaces/${space}/transfer`, {body: {new_owner: team}}) + } catch (error) { + const {body: {message}} = error as {body: {message: string}} + ux.error(message) + } finally { + ux.action.stop() + } + } +} diff --git a/packages/cli/test/unit/commands/spaces/transfer.unit.test.ts b/packages/cli/test/unit/commands/spaces/transfer.unit.test.ts new file mode 100644 index 0000000000..7232c95230 --- /dev/null +++ b/packages/cli/test/unit/commands/spaces/transfer.unit.test.ts @@ -0,0 +1,61 @@ +import {expect} from '@oclif/test' +import * as nock from 'nock' +import {stderr} from 'stdout-stderr' +import Cmd from '../../../../src/commands/spaces/transfer' +import runCommand from '../../../helpers/runCommand' + +describe('spaces:transfer', function () { + it('yields success when the API succeeds', async function () { + const space = 'dimension-c137' + const team = 'jerry' + const api = nock('https://api.heroku.com:443') + .post(`/spaces/${space}/transfer`, { + new_owner: team, + }) + .reply(201, { + created_at: '2019-01-09T22:31:33Z', + id: '5dd30c44-078f-4424-8585-cb98adf86723', + name: space, + organization: {id: '12cd6520-b000-4882-a655-8ae84a0132cb', name: team}, + team: {id: '12cd6520-b000-4882-a655-8ae84a0132cb', name: team}, + region: {id: '3544427c-5b3b-4e1e-b01a-b66362573b26', name: 'virginia'}, + shield: false, + state: 'allocated', + cidr: '10.0.0.0/16', + data_cidr: '10.1.0.0/16', + updated_at: '2019-07-16T10:19:10Z', + }) + await runCommand(Cmd, [ + '--team', + team, + '--space', + space, + ]) + expect(stderr.output).to.contain('done') + api.done() + }) + + it('yields the API error messages when the API fails', async function () { + const space = 'dimension-c137' + const team = 'jerry' + const message = 'rikki tikki tavi!' + const id = 'oops' + const api = nock('https://api.heroku.com:443') + .post(`/spaces/${space}/transfer`, {new_owner: team}) + .reply(500, {id, message}) + + try { + await runCommand(Cmd, [ + '--team', + team, + '--space', + space, + ]) + } catch (error) { + const {message: errorMessage} = error as {message: string} + expect(errorMessage).to.eq(message) + } + + api.done() + }) +}) diff --git a/packages/spaces/commands/transfer.js b/packages/spaces/commands/transfer.js deleted file mode 100644 index 029c87febd..0000000000 --- a/packages/spaces/commands/transfer.js +++ /dev/null @@ -1,36 +0,0 @@ -'use strict' - -const cli = require('heroku-cli-util') - -async function run(context, heroku) { - const space = context.flags.space - const team = context.flags.team - const request = heroku.request({ - method: 'POST', - path: `/spaces/${space}/transfer`, - body: {new_owner: team}, - }) - try { - await cli.action(`Transferring space ${cli.color.yellow(space)} to team ${cli.color.green(team)}`, request) - } catch (error) { - cli.error(error.body.message) - } -} - -module.exports = { - topic: 'spaces', - command: 'transfer', - description: 'transfer a space to another team', - help: `Example: - - $ heroku spaces:transfer --space=space-name --team=team-name - Transferring space-name to team-name... done -`, - needsApp: false, - needsAuth: true, - flags: [ - {name: 'space', hasValue: true, required: true, description: 'name of space'}, - {name: 'team', hasValue: true, required: true, description: 'desired owner of space'}, - ], - run: cli.command(run), -} diff --git a/packages/spaces/index.js b/packages/spaces/index.js index 6c0d9353c1..44d7ebbb19 100644 --- a/packages/spaces/index.js +++ b/packages/spaces/index.js @@ -16,7 +16,6 @@ exports.commands = [ require('./commands/vpn/wait'), require('./commands/vpn/destroy'), require('./commands/vpn/update'), - require('./commands/transfer'), require('./commands/drains/get'), require('./commands/trusted-ips'), require('./commands/trusted-ips/add'), diff --git a/packages/spaces/test/unit/commands/transfer.unit.test.js b/packages/spaces/test/unit/commands/transfer.unit.test.js deleted file mode 100644 index 983d229173..0000000000 --- a/packages/spaces/test/unit/commands/transfer.unit.test.js +++ /dev/null @@ -1,53 +0,0 @@ -'use strict' -/* globals beforeEach */ - -const nock = require('nock') -const cmd = require('../../../commands/transfer') -const expect = require('chai').expect -const cli = require('heroku-cli-util') - -describe('spaces:transfer', function () { - beforeEach(() => cli.mockConsole()) - - it('yields success when the API succeeds', function () { - const space = 'dimension-c137' - const team = 'jerry' - const api = nock('https://api.heroku.com:443') - .post(`/spaces/${space}/transfer`, { - new_owner: team, - }) - .reply(201, - { - created_at: '2019-01-09T22:31:33Z', - id: '5dd30c44-078f-4424-8585-cb98adf86723', - name: space, - organization: {id: '12cd6520-b000-4882-a655-8ae84a0132cb', name: team}, - team: {id: '12cd6520-b000-4882-a655-8ae84a0132cb', name: team}, - region: {id: '3544427c-5b3b-4e1e-b01a-b66362573b26', name: 'virginia'}, - shield: false, - state: 'allocated', - cidr: '10.0.0.0/16', - data_cidr: '10.1.0.0/16', - updated_at: '2019-07-16T10:19:10Z', - }, - ) - return cmd.run({flags: {team, space}}) - .then(() => expect(cli.stderr).to.contain('done')) - .then(() => api.done()) - }) - - it('yields the API error messages when the API fails', function () { - const space = 'dimension-c137' - const team = 'jerry' - const message = 'rikki tikki tavi!' - const id = 'oops' - - const api = nock('https://api.heroku.com:443') - .post(`/spaces/${space}/transfer`, {new_owner: team}) - .reply(500, {id, message}) - - return cmd.run({flags: {team, space}}) - .then(() => expect(cli.stderr).to.contain(message)) - .then(() => api.done()) - }) -})