diff options
author | Katarzyna Kobierska <kkobierska@gmail.com> | 2016-08-29 16:07:19 +0300 |
---|---|---|
committer | Katarzyna Kobierska <kkobierska@gmail.com> | 2016-09-07 13:10:49 +0300 |
commit | 2c8b830fdbf749e8bb7461d5c3ce4699b77ce3ca (patch) | |
tree | 10e0c44ca36349acf1378d3191267c9d1493b321 /lib | |
parent | bbba62fa51419b14be4f39873afdd45b5b248764 (diff) |
Code refactoring
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/lint.rb | 43 | ||||
-rw-r--r-- | lib/ci/gitlab_ci_yaml_processor.rb | 16 |
2 files changed, 28 insertions, 31 deletions
diff --git a/lib/api/lint.rb b/lib/api/lint.rb index ff35e948e0c..b1c6f52bccb 100644 --- a/lib/api/lint.rb +++ b/lib/api/lint.rb @@ -1,33 +1,26 @@ module API class Lint < Grape::API - resource :lint do - params do - requires :content, type: String, desc: 'Content of .gitlab-ci.yml' - end - - desc 'Validation of .gitlab-ci.yml content' - post do - response = { - status: '', - error: [], - jobs: [] - } - - if Ci::GitlabCiYamlProcessor.errors(params[:content]).nil? - config_processor = Ci::GitlabCiYamlProcessor.new(params[:content]) + desc 'Validation of .gitlab-ci.yml content' + params do + requires :content, type: String, desc: 'Content of .gitlab-ci.yml' + end - config_processor.builds.each do |build| - response[:jobs].push("#{build[:name]}") - response[:status] = 'valid' - end - else - response[:error].push(Ci::GitlabCiYamlProcessor.errors(params[:content])) - response[:status] = 'invalid' - end + post 'ci/lint' do + error = Ci::GitlabCiYamlProcessor.validation_message(params[:content]) + response = { + status: '', + error: '' + } - status 200 - response + if error.blank? + response[:status] = 'valid' + else + response[:error] = error + response[:status] = 'invalid' end + + status 200 + response end end end diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 40c84b93799..c547193ce4c 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -78,12 +78,16 @@ module Ci } end - def self.errors(content) - begin - Ci::GitlabCiYamlProcessor.new(content) - nil - rescue ValidationError, Psych::SyntaxError => e - e.message + def self.validation_message(content) + if content.blank? + 'Please provide content of .gitlab-ci.yml' + else + begin + Ci::GitlabCiYamlProcessor.new(content) + nil + rescue ValidationError, Psych::SyntaxError => e + e.message + end end end |