Skip to content
Merged
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ USAGE
* [`swaggerhub project:get OWNER/PROJECT_NAME`](#swaggerhub-projectget)
* [`swaggerhub project:list [OWNER]`](#swaggerhub-projectlist)
* [`swaggerhub project:member:list OWNER/PROJECT_NAME`](#swaggerhub-projectmemberlist)
* [`swaggerhub spectral:upload OWNER/RULESET_NAME/VERSION directory`](#swaggerhub-spectralupload)
* [`swaggerhub spectral:download OWNER/RULESET_NAME/VERSION directory`](#swaggerhub-spectraldownload)
* [`swaggerhub spectral:upload OWNER/RULESET_NAME directory`](#swaggerhub-spectralupload)
* [`swaggerhub spectral:download OWNER/RULESET_NAME directory`](#swaggerhub-spectraldownload)

## `swaggerhub api:create`

Expand Down Expand Up @@ -1381,10 +1381,10 @@ Create or update organization's Spectral ruleset

```
USAGE
$ swaggerhub spectral:upload OWNER/RULESET_NAME/VERSION directory [-h]
$ swaggerhub spectral:upload OWNER/RULESET_NAME directory [-h]

ARGUMENTS
OWNER/RULESET_NAME/[VERSION] The Spectral ruleset details for SwaggerHub organization
OWNER/RULESET_NAME The Spectral ruleset details for SwaggerHub organization
directory Relative path to directory with ruleset files

FLAGS
Expand All @@ -1394,7 +1394,7 @@ DESCRIPTION
Create or update organization's Spectral ruleset

EXAMPLES
$ swaggerhub spectral:upload my_organization/my_api_ruleset/1.0.0 rules
$ swaggerhub spectral:upload my_organization/my_api_ruleset rules
```

_See code: [src/commands/spectral/upload.js](https://github.com/SmartBear/swaggerhub-cli/blob/v0.9.1/src/commands/spectral/upload.js)_
Expand All @@ -1406,10 +1406,10 @@ Fetch organization's Spectral ruleset

```
USAGE
$ swaggerhub spectral:download OWNER/RULESET_NAME/VERSION directory [-h]
$ swaggerhub spectral:download OWNER/RULESET_NAME directory [-h]

ARGUMENTS
OWNER/RULESET_NAME/[VERSION] The Spectral ruleset details for SwaggerHub organization
OWNER/RULESET_NAME The Spectral ruleset details for SwaggerHub organization
directory Relative path to directory the ruleset files should be saved to

FLAGS
Expand All @@ -1419,7 +1419,7 @@ DESCRIPTION
Fetch organization's Spectral ruleset

EXAMPLES
$ swaggerhub spectral:download my_organization/my_api_ruleset/1.0.0 rules
$ swaggerhub spectral:download my_organization/my_api_ruleset rules
```

_See code: [src/commands/spectral/download.js](https://github.com/SmartBear/swaggerhub-cli/blob/v0.9.1/src/commands/spectral/download.js)_
Expand Down
7 changes: 2 additions & 5 deletions src/commands/spectral/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ class DownloadSpectralRulesetCommand extends BaseCommand {
async run() {
const { args } = await this.parse(DownloadSpectralRulesetCommand)
const rulesetPath = getSpectralIdentifierArg(args)
const [owner, name, version = '1.0.0'] = splitPathParams(rulesetPath)
const rulesetPathWithVersion = [owner, name, version].join('/')

await this.getSpectralRuleset(rulesetPathWithVersion, args['directory'])
await this.getSpectralRuleset(rulesetPath, args['directory'])
}

getSpectralRuleset(pathParams, outputDir) {
Expand Down Expand Up @@ -50,12 +48,11 @@ class DownloadSpectralRulesetCommand extends BaseCommand {
DownloadSpectralRulesetCommand.description = `Fetch organization's Spectral ruleset`

DownloadSpectralRulesetCommand.examples = [
'swaggerhub spectral:download my_organization/my_api_ruleset/1.0.0 rules/',
'swaggerhub spectral:download my_organization/my_api_ruleset rules/',
]

DownloadSpectralRulesetCommand.args = {
'OWNER/RULESET_NAME/[VERSION]': Args.string({
'OWNER/RULESET_NAME': Args.string({
required: true,
description: 'Organization\'s Spectral ruleset to create or update on SwaggerHub'
}),
Expand Down
7 changes: 2 additions & 5 deletions src/commands/spectral/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ class UploadSpectralRulesetCommand extends BaseCommand {
async run() {
const { args } = await this.parse(UploadSpectralRulesetCommand)
const rulesetPath = getSpectralIdentifierArg(args)
const [owner, name, version = '1.0.0'] = splitPathParams(rulesetPath)
const rulesetPathWithVersion = [owner, name, version].join('/')

const zippedDirectory = await this.zipTheDirectory(args.directory)
await this.saveSpectralRuleset(rulesetPathWithVersion, zippedDirectory)
await this.saveSpectralRuleset(rulesetPath, zippedDirectory)
}

async zipTheDirectory(directoryPath) {
Expand All @@ -40,12 +38,11 @@ class UploadSpectralRulesetCommand extends BaseCommand {
UploadSpectralRulesetCommand.description = `Create or update organization's Spectral ruleset`

UploadSpectralRulesetCommand.examples = [
'swaggerhub spectral:upload my_organization/my_api_ruleset/1.0.0 rules/',
'swaggerhub spectral:upload my_organization/my_api_ruleset rules/',
]

UploadSpectralRulesetCommand.args = {
'OWNER/RULESET_NAME/[VERSION]': Args.string({
'OWNER/RULESET_NAME': Args.string({
required: true,
description: 'Organization\'s Spectral ruleset to create or update on SwaggerHub'
}),
Expand Down
9 changes: 6 additions & 3 deletions src/support/command/parse-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const optionalVersionRegex = new RegExp(/^\/?[\w\-.]+\/[\w\-.]+(\/[\w\-.]+)?(\/?
const requiredVersionRegex = new RegExp(/^\/?[\w\-.]+\/[\w\-.]+\/[\w\-.]+(\/?)$/)
const integrationIdentifierRegex = new RegExp(/^\/?[\w\-.]+\/[\w\-.]+\/[\w\-.]+\/[\w\-.]+(\/?)$/)
const projectIdentifierRegex = new RegExp(/^\/?[\w\-.]+\/[\w\-.]+$/)
const spectralIdentifierRegex = new RegExp(/^\/?[\w\-.]+\/[\w\-.]+$/)

const isValidIdentifier = (id, isVersionRequired) => isVersionRequired
? requiredVersionRegex.test(id)
Expand Down Expand Up @@ -53,10 +54,12 @@ const getProjectIdentifierArg = args => {
}

const getSpectralIdentifierArg = args => {
const isVersionRequired = !!args['OWNER/RULESET_NAME/VERSION']
const format = isVersionRequired ? 'OWNER/RULESET_NAME/VERSION' : 'OWNER/RULESET_NAME/[VERSION]'
const format = 'OWNER/RULESET_NAME'
const identifier = args[format]
return getIdentifierArg(isVersionRequired, format, identifier)
if (!spectralIdentifierRegex.test(identifier)) {
throw new CLIError(errorMsg.argsMustMatchFormat({ format }))
}
return identifier
}

const readConfigFile = filename => {
Expand Down
17 changes: 1 addition & 16 deletions test/commands/spectral/download.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ const unzipper = require('unzipper')

const org = 'org1'
const rulesetName = 'rulesetA'
const version = '1.0.2'
const rulesetPath = `${org}/${rulesetName}/${version}`
const rulesetPathWithoutVersion = `${org}/${rulesetName}`
const rulesetPath = `${org}/${rulesetName}`
const outputDir = 'rules'

describe('invalid spectral:download', () => {
Expand Down Expand Up @@ -52,19 +50,6 @@ describe('valid spectral:download', () => {
expect(ctx.stdout).to.be.undefined
})


test
.stub(config, 'getConfig', stub => stub.returns({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
.nock('https://api.swaggerhub.com/standardization', api => api
.get(`/spectral-rulesets/${rulesetPathWithoutVersion}/1.0.0/zip`)
.reply(200, zipBuffer, { 'Content-Type': 'application/zip' })
)
.stub(fs.promises, 'mkdir', stub => stub.resolves())
.stub(unzipper.Open, 'buffer', stub => stub.resolves({ extract: ({ path }) => Promise.resolve() }))
.command(['spectral:download', rulesetPathWithoutVersion, outputDir])
.it('runs spectral:download without version and extracts ruleset zip to directory', ctx => {
expect(ctx.stdout).to.be.undefined
})

test
.stub(config, 'getConfig', stub => stub.returns({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
Expand Down
16 changes: 1 addition & 15 deletions test/commands/spectral/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ const fs = require('fs')

const org = 'org1'
const rulesetName = 'rulesetA'
const version = '1.0.2'
const rulesetPath = `${org}/${rulesetName}/${version}`
const rulesetPathWithoutVersion = `${org}/${rulesetName}`
const rulesetPath = `${org}/${rulesetName}`
const inputDir = 'rules'

describe('invalid spectral:upload', () => {
Expand Down Expand Up @@ -50,18 +48,6 @@ describe('valid spectral:upload', () => {
expect(ctx.stdout).to.be.undefined
})

test
.stub(config, 'getConfig', stub => stub.returns({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
.nock('https://api.swaggerhub.com/standardization', api => api
.put(`/spectral-rulesets/${rulesetPathWithoutVersion}/1.0.0/zip`)
.reply(200, '{"success":true}', { 'Content-Type': 'application/json' })
)
.stub(fs, 'createReadStream', stub => stub.returns(zipBuffer))
.command(['spectral:upload', rulesetPathWithoutVersion, inputDir])
.it('runs spectral:upload without version and uploads zipped ruleset directory', ctx => {
expect(ctx.stdout).to.be.undefined
})

test
.stub(config, 'getConfig', stub => stub.returns({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
.nock('https://api.swaggerhub.com/standardization', api => api
Expand Down
25 changes: 3 additions & 22 deletions test/support/parse-input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,43 +202,24 @@ describe('getProjectIdentifierArg', () => {

describe('getSpectralIdentifierArg', () => {

context('valid version identifier', () =>
it('should be returned', () =>
expect(getSpectralIdentifierArg({ 'OWNER/RULESET_NAME/VERSION': 'owner/ruleset/1.2.3' })).to.equal('owner/ruleset/1.2.3')
)
)

context('valid identifier', () =>
it('should be returned', () =>
expect(getSpectralIdentifierArg({ 'OWNER/RULESET_NAME/[VERSION]': 'owner/ruleset' }, false)).to.equal('owner/ruleset')
expect(getSpectralIdentifierArg({ 'OWNER/RULESET_NAME': 'owner/ruleset' })).to.equal('owner/ruleset')
)
)

context('invalid identifier', () =>
it('should throw an exception', () =>
expect(() => { getSpectralIdentifierArg({ 'OWNER/RULESET_NAME/VERSION': 'owner/ruleset/version/extra' })})
expect(() => { getSpectralIdentifierArg({ 'OWNER/RULESET_NAME': 'owner/ruleset/1.2.3' })})
.to.throw(CLIError)
)
)

context('invalid identifier with space', () =>
it('should throw an exception', () =>
expect(() => { getSpectralIdentifierArg({ 'OWNER/RULESET_NAME/VERSION': 'owner/ruleset name/version' })})
.to.throw(CLIError)
)
)

context('invalid identifier with space and no version', () => {
it('should throw an exception', () =>
expect(() => { getSpectralIdentifierArg({ 'OWNER/RULESET_NAME/[VERSION]': 'owner/ruleset name' }, false)})
expect(() => { getSpectralIdentifierArg({ 'OWNER/RULESET_NAME': 'owner/ruleset name' })})
.to.throw(CLIError)
)
})

context('invalid identifier with version required', () =>
it('should throw an exception', () =>
expect(() => { getSpectralIdentifierArg({ 'OWNER/RULESET_NAME/VERSION': 'owner/ruleset' })}).to.throw(CLIError)
)
)
})

Expand Down
Loading