From 10fe7e1c39c173ff39747eed43b386cb084d25ae Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Tue, 17 Jun 2014 12:14:10 +0200 Subject: [PATCH] add endpoint for linting --- Gemfile | 1 + Gemfile.lock | 7 +++++++ lib/travis/api/app/endpoint/lint.rb | 19 +++++++++++++++++++ spec/unit/endpoint/lint_spec.rb | 18 ++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 lib/travis/api/app/endpoint/lint.rb create mode 100644 spec/unit/endpoint/lint_spec.rb diff --git a/Gemfile b/Gemfile index f8051f9c..89cee7dd 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ gemspec gem 'travis-core', github: 'travis-ci/travis-core' gem 'travis-support', github: 'travis-ci/travis-support' gem 'travis-sidekiqs', github: 'travis-ci/travis-sidekiqs', require: nil, ref: 'cde9741' +gem 'travis-yaml', github: 'travis-ci/travis-yaml' gem 'sinatra' gem 'sinatra-contrib', require: nil #github: 'sinatra/sinatra-contrib', require: nil diff --git a/Gemfile.lock b/Gemfile.lock index ddb365e2..4ae4edb4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -80,6 +80,12 @@ GIT specs: travis-support (0.0.1) +GIT + remote: git://github.com/travis-ci/travis-yaml.git + revision: 7e6e31e82240e170ffc20dfd02c0353b3bd5d33b + specs: + travis-yaml (0.1.0) + GIT remote: https://gist.github.com/4269321.git revision: 8e2d21b924a69dd48191df6a18e51769f5a88614 @@ -332,5 +338,6 @@ DEPENDENCIES travis-core! travis-sidekiqs! travis-support! + travis-yaml! unicorn yard-sinatra! diff --git a/lib/travis/api/app/endpoint/lint.rb b/lib/travis/api/app/endpoint/lint.rb new file mode 100644 index 00000000..30437e51 --- /dev/null +++ b/lib/travis/api/app/endpoint/lint.rb @@ -0,0 +1,19 @@ +require 'travis/api/app' +require 'travis/yaml' + +class Travis::Api::App + class Endpoint + class Lint < Endpoint + def lint + 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 } } + { lint: { warnings: warnings } }.to_json + end + + post('/', scope: :public) { lint } + put('/', scope: :public) { lint } + end + end +end diff --git a/spec/unit/endpoint/lint_spec.rb b/spec/unit/endpoint/lint_spec.rb new file mode 100644 index 00000000..025130de --- /dev/null +++ b/spec/unit/endpoint/lint_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe Travis::Api::App::Endpoint::Lint do + let(:content) { "foo: bar" } + let(:body) { "{\"lint\":{\"warnings\":[{\"key\":[],\"message\":\"unexpected key \\\"foo\\\", dropping\"},{\"key\":[],\"message\":\"missing key \\\"language\\\", defaulting to \\\"ruby\\\"\"}]}}" } + + it "accepts content in parameter" do + response = post('/lint', content: content) + response.should be_ok + response.body.should be == body + end + + it "accepts content as body" do + response = put('/lint', content) + response.should be_ok + response.body.should be == body + end +end