Skip to content

Add create-project command to the cli #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,30 @@ Output on success:
Deploy successful
```

### create-project
Create a new Rollbar project.

```
rollbar-cli create-project [options]

Create a new Rollbar project

Options:
--version Show version number [boolean]
-v, --verbose Verbose status output [boolean]
-q, --quiet Silent status output [boolean]
--help Show help [boolean]
--access-token Use an Account Access Token with 'write' scope
[string] [required]
--name Name of the project. Must start with a letter; can contain letters, numbers, spaces, underscores, hyphens, periods, and commas. Max length 32 characters.
[string] [required]
```

Example:
```
rollbar-cli create-project --access-token 1234 --name TestProject
```

## Release History & Changelog

See our [Releases](https://github.com/rollbar/rollbar-cli/releases) page for a list of all releases, including changes.
Expand Down
6 changes: 6 additions & 0 deletions src/common/rollbar-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class RollbarAPI {
return this.processResponse(resp);
}

async createProject(request) {
const resp = await this.axios.post('/projects', request);

return this.processResponse(resp);
}

async sigendURLsourcemaps(request) {

const resp = await this.axios.post(
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ function cli() {
})
.command(require('./sourcemaps/command'))
.command(require('./deploy/command'))
.command(require('./project/command'))
.help()
.argv

return 0;
}

module.exports = cli;

38 changes: 38 additions & 0 deletions src/project/command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const Deployer = require('./deployer.js');
const Output = require('../common/output.js');

exports.command = 'create-project [options]'

exports.describe = 'Create new Rollbar project'

exports.builder = function (yargs) {
return yargs
.option('access-token', {
describe: 'Use an Account Access Token with \'write\' scope',
requiresArg: true,
type: 'string',
demandOption: true
})
.option('name', {
describe: 'Name of the project. Must start with a letter; can contain letters, numbers, spaces, underscores, hyphens, periods, and commas. Max length 32 characters.',
requiresArg: true,
type: 'string',
demandOption: true
})
}

exports.handler = async function (argv) {
global.output = new Output({
verbose: argv['verbose'],
quiet: argv['quiet']
});

const deployer = new Deployer({
accessToken: argv['access-token'],
name: argv['name']
})

await deployer.createProject();
}
32 changes: 32 additions & 0 deletions src/project/deployer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const RollbarAPI = require('../common/rollbar-api');

class Deployer {
constructor(options) {
this.rollbarAPI = new RollbarAPI(options.accessToken);
this.name = options.name;
}

async createProject() {
let error;
try {
error = await this.rollbarAPI.createProject(this.buildRequest());
} catch (e) {
error = e.message;
}

if (error)
output.error('Error', error);
else
output.success('','Create project successful');
}

buildRequest() {
return {
name: this.name
}
}
}

module.exports = Deployer;
37 changes: 37 additions & 0 deletions test/project/command.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* globals describe */
/* globals it */
/* globals beforeEach */
/* globals afterEach */

const expect = require('chai').expect;

const Command = require('../../src/project/command');
const Output = require('../../src/common/output');
const yargs = require('yargs');
const sinon = require('sinon');

describe('create-project Command()', function() {
beforeEach(function() {
global.output = new Output({verbose: false});
this.currentTest.stubWarn = sinon.spy(global.output, 'warn');
this.currentTest.stubSuccess = sinon.spy(global.output, 'success');
});

afterEach(function() {
global.output = null;
this.currentTest.stubWarn.restore();
this.currentTest.stubSuccess.restore();
});

it('returns help output', async () => {
const parser = yargs.command(Command).help();

const result = await new Promise((resolve) => {
parser.parse('--help', (_err, _argv, output) => {
resolve(output);
})
});

expect(result).to.have.string('create-project [options]');
});
});
83 changes: 83 additions & 0 deletions test/project/deployer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* globals describe */
/* globals it */
/* globals beforeEach */

const expect = require('chai').expect;
const sinon = require('sinon');

const Deployer = require('../../src/project/deployer');
const Output = require('../../src/common/output');

describe('Deployer()', function() {
it('should initialize successfully', function() {
const options = {
accessToken: 'xxxxxxxxxxxxxxxxxxxx',
name: 'TestProject'
};
const deployer = new Deployer(options);

expect(deployer.name).to.equal(options.name);
expect(deployer).to.have.property('rollbarAPI');
expect(deployer.rollbarAPI.accessToken).to.equal(options.accessToken);
});
});

describe('.createProject()', function() {
beforeEach(function() {
global.output = new Output({quiet: true});
});

it('should deploy successfully', async function() {
const options = {
accessToken: 'xxxxxxxxxxxxxxxxxxxx',
name: 'TestProject',
};
const deployer = new Deployer(options);
const stub = sinon.stub(deployer.rollbarAPI.axios, 'post');
stub.resolves({
"err": 0,
"result": {
"status": "enabled",
"name": "TestProject",
"date_modified": 1661194738,
"account_id": 436103,
"date_created": 1661194738,
"id": 585165,
"settings_data": {
"grouping": {
"auto_upgrade": true,
"recent_versions": [
"21.0.0"
]
}
}
}
});

await deployer.createProject();

expect(stub.callCount).to.equal(1);

stub.restore();
});

it('should throw an error if project name already exists', async function() {
const options = {
accessToken: 'xxxxxxxxxxxxxxxxxxxx',
name: 'TestProject'
};

const deployer = new Deployer(options);
const stub = sinon.stub(deployer.rollbarAPI.axios, 'post');
stub.resolves({
"err": 1,
"message": "Project with this name already exists"
});

await deployer.createProject();

expect(stub.callCount).to.equal(1);

stub.restore();
});
});