From cffe1c7a51a078fb0dee5b700add4c3840bdf27d Mon Sep 17 00:00:00 2001 From: Tully Date: Mon, 9 Apr 2018 15:06:19 +0200 Subject: [PATCH] feat: styleguide push --- .rubocop_todo.yml | 4 +-- README.md | 1 + lib/apiary/cli.rb | 1 + lib/apiary/command/styleguide.rb | 36 ++++++++++++++++++--- lib/apiary/version.rb | 2 +- spec/apiary/command/styleguide_spec.rb | 44 ++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 222e58f..1b438d1 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 diff --git a/README.md b/README.md index fba454e..40152b9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/apiary/cli.rb b/lib/apiary/cli.rb index dc62a56..5baa1a6 100644 --- a/lib/apiary/cli.rb +++ b/lib/apiary/cli.rb @@ -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' diff --git a/lib/apiary/command/styleguide.rb b/lib/apiary/command/styleguide.rb index e9ca033..0e84a78 100644 --- a/lib/apiary/command/styleguide.rb +++ b/lib/apiary/command/styleguide.rb @@ -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 @@ -196,11 +222,11 @@ 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) + @add_path = api_description_source_path(@options.add) if add + @add = api_description_source(@add_path) if add + @functions = get_functions(@options.functions) if functions + @rules = get_rules(@options.rules) if rules end def fetch_from_apiary diff --git a/lib/apiary/version.rb b/lib/apiary/version.rb index 3f94ac4..133e871 100644 --- a/lib/apiary/version.rb +++ b/lib/apiary/version.rb @@ -1,3 +1,3 @@ module Apiary - VERSION = '0.10.3'.freeze + VERSION = '0.11.3'.freeze end diff --git a/spec/apiary/command/styleguide_spec.rb b/spec/apiary/command/styleguide_spec.rb index a3846a8..446d2e1 100644 --- a/spec/apiary/command/styleguide_spec.rb +++ b/spec/apiary/command/styleguide_spec.rb @@ -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