diff --git a/lib/travis/api/v3/renderer/lint.rb b/lib/travis/api/v3/renderer/lint.rb index 2fb59bb9..7c782a2a 100644 --- a/lib/travis/api/v3/renderer/lint.rb +++ b/lib/travis/api/v3/renderer/lint.rb @@ -1,10 +1,21 @@ module Travis::API::V3 module Renderer::Lint + AVAILABLE_ATTRIBUTES = [ :warnings ] extend self - def render(payload) - # Renderer.render_value(payload) - puts payload + def available_attributes + AVAILABLE_ATTRIBUTES + end + + def render(lint, **) + { + :@type => 'lint'.freeze, + :warnings => warnings_for(lint) + } + end + + def warnings_for(lint) + lint.nested_warnings.map { |k, m| { key: k, message: m } } end end end diff --git a/lib/travis/api/v3/router.rb b/lib/travis/api/v3/router.rb index 28085e81..91c5fd4a 100644 --- a/lib/travis/api/v3/router.rb +++ b/lib/travis/api/v3/router.rb @@ -23,7 +23,7 @@ module Travis::API::V3 metrics.name_after(factory) filtered = factory.filter_params(env_params) - service = factory.new(access_control, filtered.merge(params)) + service = factory.new(access_control, filtered.merge(params), env['rack.input'.freeze]) metrics.tick(:prepare) result = service.run diff --git a/lib/travis/api/v3/service.rb b/lib/travis/api/v3/service.rb index f627b74c..fa63ecf1 100644 --- a/lib/travis/api/v3/service.rb +++ b/lib/travis/api/v3/service.rb @@ -42,13 +42,14 @@ module Travis::API::V3 Queries[result_type] end - attr_accessor :access_control, :params + attr_accessor :access_control, :params, :request_body - def initialize(access_control, params) + def initialize(access_control, params, request_body) @access_control = access_control @params = params @queries = {} @github = {} + @request_body = request_body end def query(type = result_type) diff --git a/lib/travis/api/v3/services/lint/lint.rb b/lib/travis/api/v3/services/lint/lint.rb index 62dd43fe..e3e5cf2f 100644 --- a/lib/travis/api/v3/services/lint/lint.rb +++ b/lib/travis/api/v3/services/lint/lint.rb @@ -1,12 +1,12 @@ +require 'travis/yaml' + module Travis::API::V3 class Services::Lint::Lint < Service + params "content" def run! - # request.body.rewind - # content = params[:content] || request.body.read - # parsed = Travis::Yaml.parse(content) - # warnings = parsed.nested_warnings.map { |k, m| { key: k, message: m } } - # payload = { lint: { warnings: warnings } }.to_json - + request_body.rewind + content = params[:content] || request_body.read + parsed = Travis::Yaml.parse(content) end end end diff --git a/spec/v3/services/lint/lint_spec.rb b/spec/v3/services/lint/lint_spec.rb new file mode 100644 index 00000000..e111f33a --- /dev/null +++ b/spec/v3/services/lint/lint_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe Travis::API::V3::Services::Lint::Lint do + let(:content) { "foo: bar" } + let(:parsed_body) { JSON.load(last_response.body) } + + describe "accepts content in parameter" do + before { post("v3/lint", content: content ) } + example { expect(last_response).to be_ok } + example { expect(parsed_body).to be == { + "@warnings" => [{ + "@type" => "warning", + "message" => "query parameter foo: bar not whitelisted, ignored", + "warning_type" => "ignored_parameter", "parameter"=>"foo: bar"}], + "@type" => "lint", + "warnings" => [{ + "key" => [], + "message" => "unexpected key \"foo\", dropping"}, { + "key" => [], + "message" => "missing key \"language\", defaulting to \"ruby\""}]} + } + end + + describe "accepts content as body" do + before { post("/v3/lint", content) } + example { expect(last_response).to be_ok } + example { expect(parsed_body).to be == { + "@warnings" => [{ + "@type" => "warning", + "message" => "query parameter foo: bar not whitelisted, ignored", + "warning_type" => "ignored_parameter", "parameter"=>"foo: bar"}], + "@type" => "lint", + "warnings" => [{ + "key" => [], + "message" => "unexpected key \"foo\", dropping"}, { + "key" => [], + "message" => "missing key \"language\", defaulting to \"ruby\""}]} + } + end +end