diff --git a/lib/travis/api/v3/renderer/lint.rb b/lib/travis/api/v3/renderer/lint.rb new file mode 100644 index 00000000..7c782a2a --- /dev/null +++ b/lib/travis/api/v3/renderer/lint.rb @@ -0,0 +1,21 @@ +module Travis::API::V3 + module Renderer::Lint + AVAILABLE_ATTRIBUTES = [ :warnings ] + extend self + + 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/routes.rb b/lib/travis/api/v3/routes.rb index 9d0f8883..d22edce9 100644 --- a/lib/travis/api/v3/routes.rb +++ b/lib/travis/api/v3/routes.rb @@ -45,6 +45,11 @@ module Travis::API::V3 post :debug, '/debug' end + resource :lint do + route '/lint' + post :lint + end + resource :organization do capture id: :digit route '/org/{organization.id}' 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.rb b/lib/travis/api/v3/services.rb index 9fb812b6..637ea337 100644 --- a/lib/travis/api/v3/services.rb +++ b/lib/travis/api/v3/services.rb @@ -13,6 +13,7 @@ module Travis::API::V3 Crons = Module.new { extend Services } Job = Module.new { extend Services } Jobs = Module.new { extend Services } + Lint = Module.new { extend Services } Organization = Module.new { extend Services } Organizations = Module.new { extend Services } Owner = Module.new { extend Services } diff --git a/lib/travis/api/v3/services/lint/lint.rb b/lib/travis/api/v3/services/lint/lint.rb new file mode 100644 index 00000000..3ee9f665 --- /dev/null +++ b/lib/travis/api/v3/services/lint/lint.rb @@ -0,0 +1,12 @@ +require 'travis/yaml' + +module Travis::API::V3 + class Services::Lint::Lint < Service + params 'content' + def run! + request_body.rewind + content = params['content'.freeze] || request_body.read + 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..affa8f14 --- /dev/null +++ b/spec/v3/services/lint/lint_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe Travis::API::V3::Services::Lint::Lint do + let(:content) { "foo: bar" } + let(:parsed_body) { JSON.load(last_response.body) } + let(:headers) {{ 'CONTENT_TYPE' => 'text/yaml'}} + + + 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 == { + "@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, headers) } + example { expect(last_response).to be_ok } + example { expect(parsed_body).to be == { + "@type" => "lint", + "warnings" => [{ + "key" => [], + "message" => "unexpected key \"foo\", dropping"}, { + "key" => [], + "message" => "missing key \"language\", defaulting to \"ruby\""}]} + } + end +end