Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

feat: styleguide push #166

Merged
merged 1 commit into from
Apr 13, 2018
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
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Metrics/AbcSize:
Max: 33

Metrics/BlockLength:
Max: 150
Max: 200

Metrics/ClassLength:
Max: 190 # or whatever ends up being appropriate
Max: 250 # or whatever ends up being appropriate

Metrics/PerceivedComplexity:
Max: 12
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ Usage:
Options:
[--fetch], [--no-fetch] # Fetch styleguide rules and functions from apiary.io
[--push], [--no-push] # Push styleguide rules and functions to apiary.io
[--add=ADD] # Path to API Description Document. When given a directory, it will look for `apiary.apib` and `swagger.yaml` file
[--functions=FUNCTIONS] # Path to to the file with functions definitions
[--rules=RULES] # Path to to the file with rules definitions - `functions.js` and `rules.json` are loaded if not specified
Expand Down
1 change: 1 addition & 0 deletions lib/apiary/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def publish

desc 'styleguide', 'Check API Description Document against styleguide rules (Apiary.io pro plan is required - https://apiary.io/plans )'
method_option :fetch, type: :boolean, desc: 'Fetch styleguide rules and functions from apiary.io'
method_option :push, type: :boolean, desc: 'Push styleguide rules and functions to apiary.io'
method_option :add, type: :string, desc: 'Path to API Description Document. When given a directory, it will look for `apiary.apib` and `swagger.yaml` file'
method_option :functions, type: :string, desc: 'Path to to the file with functions definitions'
method_option :rules, type: :string, desc: 'Path to to the file with rules definitions - `functions.js` and `rules.json` are loaded if not specified'
Expand Down
38 changes: 33 additions & 5 deletions lib/apiary/command/styleguide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,37 @@ def execute
check_api_key
if @options.fetch
fetch
elsif @options.push
push
else
validate
end
end

def push
begin
load(add: false)
rescue StandardError => e
abort "Error: #{e.message}"
end

path = 'styleguide-cli/set-assertions/'
headers = @options.headers.clone
headers[:authentication] = "Token #{@options.api_key}"

data = {
functions: @functions,
rules: @rules
}.to_json

begin
call_apiary(path, data, headers, :post)
rescue => e
puts e
abort "Error: Can not write into the rules/functions file: #{e}"
end
end

def fetch
begin
assertions = fetch_from_apiary
Expand Down Expand Up @@ -196,11 +222,13 @@ def check_api_key
abort 'Error: API key must be provided through environment variable APIARY_API_KEY. \Please go to https://login.apiary.io/tokens to obtain it.'
end

def load
@add_path = api_description_source_path(@options.add)
@add = api_description_source(@add_path)
@functions = get_functions(@options.functions)
@rules = get_rules(@options.rules)
def load(add: true, functions: true, rules: true)
if add
@add_path = api_description_source_path(@options.add)
@add = api_description_source(@add_path)
end
@functions = get_functions(@options.functions) if functions
@rules = get_rules(@options.rules) if rules
end

def fetch_from_apiary
Expand Down
2 changes: 1 addition & 1 deletion lib/apiary/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Apiary
VERSION = '0.10.3'.freeze
VERSION = '0.11.0'.freeze
end
44 changes: 44 additions & 0 deletions spec/apiary/command/styleguide_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,48 @@ def test_abort(command, message)
end.to output("[\n\n]\n").to_stdout
end
end

describe 'push' do
it 'call command without APIARY_API_KEY set' do
opts = {
push: true,
api_key: '',
functions: 'features/support',
rules: 'features/support'
}
command = Apiary::Command::Styleguide.new(opts)
test_abort(command, 'Error: API key must be provided through environment variable APIARY_API_KEY.')
end

it 'call command with incorrect APIARY_API_KEY' do
opts = {
push: true,
api_key: 'xxx'
}

command = Apiary::Command::Styleguide.new(opts)
stub_request(:post, "https://#{command.options.api_host}/styleguide-cli/set-assertions/").to_return(status: [403, 'This resource requires authenticated API call.'])

test_abort(command, 'Error: Apiary service responded with: 403')
end

it 'call command with correct APIARY_API_KEY' do
opts = {
push: true,
api_key: 'xxx',
functions: 'function testFunction(data) { return "failed"; }',
rules: '[{"ruleName": "testName","functionName": "testFunction","target": "Request_Body","intent": "testIntent"}]'
}

command = Apiary::Command::Styleguide.new(opts)
stub_request(:post, "https://#{command.options.api_host}/styleguide-cli/set-assertions/").to_return(status: 200, body: '')

expect do
begin
command.execute
rescue SystemExit
end
end.to output('').to_stdout
end
end
end