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

Commit 5669860

Browse files
author
Peter Grilli (Tu1ly)
authored
Merge pull request #166 from apiaryio/tu1ly/encoding-fix
feat: styleguide push
2 parents f3f6c7a + 1cd7a7f commit 5669860

File tree

6 files changed

+82
-8
lines changed

6 files changed

+82
-8
lines changed

.rubocop_todo.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ Metrics/AbcSize:
1010
Max: 33
1111

1212
Metrics/BlockLength:
13-
Max: 150
13+
Max: 200
1414

1515
Metrics/ClassLength:
16-
Max: 190 # or whatever ends up being appropriate
16+
Max: 250 # or whatever ends up being appropriate
1717

1818
Metrics/PerceivedComplexity:
1919
Max: 12

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ Usage:
132132
133133
Options:
134134
[--fetch], [--no-fetch] # Fetch styleguide rules and functions from apiary.io
135+
[--push], [--no-push] # Push styleguide rules and functions to apiary.io
135136
[--add=ADD] # Path to API Description Document. When given a directory, it will look for `apiary.apib` and `swagger.yaml` file
136137
[--functions=FUNCTIONS] # Path to to the file with functions definitions
137138
[--rules=RULES] # Path to to the file with rules definitions - `functions.js` and `rules.json` are loaded if not specified

lib/apiary/cli.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def publish
4949

5050
desc 'styleguide', 'Check API Description Document against styleguide rules (Apiary.io pro plan is required - https://apiary.io/plans )'
5151
method_option :fetch, type: :boolean, desc: 'Fetch styleguide rules and functions from apiary.io'
52+
method_option :push, type: :boolean, desc: 'Push styleguide rules and functions to apiary.io'
5253
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'
5354
method_option :functions, type: :string, desc: 'Path to to the file with functions definitions'
5455
method_option :rules, type: :string, desc: 'Path to to the file with rules definitions - `functions.js` and `rules.json` are loaded if not specified'

lib/apiary/command/styleguide.rb

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,37 @@ def execute
3636
check_api_key
3737
if @options.fetch
3838
fetch
39+
elsif @options.push
40+
push
3941
else
4042
validate
4143
end
4244
end
4345

46+
def push
47+
begin
48+
load(add: false)
49+
rescue StandardError => e
50+
abort "Error: #{e.message}"
51+
end
52+
53+
path = 'styleguide-cli/set-assertions/'
54+
headers = @options.headers.clone
55+
headers[:authentication] = "Token #{@options.api_key}"
56+
57+
data = {
58+
functions: @functions,
59+
rules: @rules
60+
}.to_json
61+
62+
begin
63+
call_apiary(path, data, headers, :post)
64+
rescue => e
65+
puts e
66+
abort "Error: Can not write into the rules/functions file: #{e}"
67+
end
68+
end
69+
4470
def fetch
4571
begin
4672
assertions = fetch_from_apiary
@@ -196,11 +222,13 @@ def check_api_key
196222
abort 'Error: API key must be provided through environment variable APIARY_API_KEY. \Please go to https://login.apiary.io/tokens to obtain it.'
197223
end
198224

199-
def load
200-
@add_path = api_description_source_path(@options.add)
201-
@add = api_description_source(@add_path)
202-
@functions = get_functions(@options.functions)
203-
@rules = get_rules(@options.rules)
225+
def load(add: true, functions: true, rules: true)
226+
if add
227+
@add_path = api_description_source_path(@options.add)
228+
@add = api_description_source(@add_path)
229+
end
230+
@functions = get_functions(@options.functions) if functions
231+
@rules = get_rules(@options.rules) if rules
204232
end
205233

206234
def fetch_from_apiary

lib/apiary/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Apiary
2-
VERSION = '0.10.3'.freeze
2+
VERSION = '0.11.0'.freeze
33
end

spec/apiary/command/styleguide_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,48 @@ def test_abort(command, message)
172172
end.to output("[\n\n]\n").to_stdout
173173
end
174174
end
175+
176+
describe 'push' do
177+
it 'call command without APIARY_API_KEY set' do
178+
opts = {
179+
push: true,
180+
api_key: '',
181+
functions: 'features/support',
182+
rules: 'features/support'
183+
}
184+
command = Apiary::Command::Styleguide.new(opts)
185+
test_abort(command, 'Error: API key must be provided through environment variable APIARY_API_KEY.')
186+
end
187+
188+
it 'call command with incorrect APIARY_API_KEY' do
189+
opts = {
190+
push: true,
191+
api_key: 'xxx'
192+
}
193+
194+
command = Apiary::Command::Styleguide.new(opts)
195+
stub_request(:post, "https://#{command.options.api_host}/styleguide-cli/set-assertions/").to_return(status: [403, 'This resource requires authenticated API call.'])
196+
197+
test_abort(command, 'Error: Apiary service responded with: 403')
198+
end
199+
200+
it 'call command with correct APIARY_API_KEY' do
201+
opts = {
202+
push: true,
203+
api_key: 'xxx',
204+
functions: 'function testFunction(data) { return "failed"; }',
205+
rules: '[{"ruleName": "testName","functionName": "testFunction","target": "Request_Body","intent": "testIntent"}]'
206+
}
207+
208+
command = Apiary::Command::Styleguide.new(opts)
209+
stub_request(:post, "https://#{command.options.api_host}/styleguide-cli/set-assertions/").to_return(status: 200, body: '')
210+
211+
expect do
212+
begin
213+
command.execute
214+
rescue SystemExit
215+
end
216+
end.to output('').to_stdout
217+
end
218+
end
175219
end

0 commit comments

Comments
 (0)