diff --git a/CHANGELOG b/CHANGELOG index 2af2056979d..bf1136afd03 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -92,6 +92,7 @@ v 8.12.0 (unreleased) - Refactor the triggers page and documentation !6217 - Show values of CI trigger variables only when clicked (Katarzyna Kobierska Ula Budziszewska) - Use default clone protocol on "check out, review, and merge locally" help page URL + - API for Ci Lint !5953 (Katarzyna Kobierska Urszula Budziszewska) v 8.11.5 (unreleased) - Optimize branch lookups and force a repository reload for Repository#find_branch diff --git a/app/controllers/ci/lints_controller.rb b/app/controllers/ci/lints_controller.rb index 67028b66a66..e03543a9267 100644 --- a/app/controllers/ci/lints_controller.rb +++ b/app/controllers/ci/lints_controller.rb @@ -11,9 +11,9 @@ module Ci if @content.blank? @status = false @error = "Please provide content of .gitlab-ci.yml" - elsif Ci::GitlabCiYamlProcessor.validate(@content) != "valid" + elsif Ci::GitlabCiYamlProcessor.errors(@content) != nil @status = false - @error = Ci::GitlabCiYamlProcessor.validate(@content) + @error = Ci::GitlabCiYamlProcessor.errors(@content) else @config_processor = Ci::GitlabCiYamlProcessor.new(@content) @stages = @config_processor.stages diff --git a/lib/api/lint.rb b/lib/api/lint.rb index 2d27bc65462..379c266abe1 100644 --- a/lib/api/lint.rb +++ b/lib/api/lint.rb @@ -2,7 +2,7 @@ module API class Lint < Grape::API resource :lint do params do - requires :content, type: String, desc: 'content of .gitlab-ci.yml' + requires :content, type: String, desc: 'Content of .gitlab-ci.yml' end desc 'Validation of .gitlab-ci.yml content' @@ -13,9 +13,9 @@ module API jobs: [] } - if Ci::GitlabCiYamlProcessor.validate(@content) != "valid" + if Ci::GitlabCiYamlProcessor.errors(@content) != nil status 200 - response[:errors].push(e.message) + response[:errors].push(Ci::GitlabCiYamlProcessor.errors(@content)) response[:status] = 'invalid' response diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 9799f494df6..40c84b93799 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -78,10 +78,10 @@ module Ci } end - def self.validate(content) + def self.errors(content) begin Ci::GitlabCiYamlProcessor.new(content) - "valid" + nil rescue ValidationError, Psych::SyntaxError => e e.message end diff --git a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb index bd5ea2bb97d..0125f149581 100644 --- a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb +++ b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb @@ -1251,21 +1251,21 @@ EOT end end - describe "#validate(config)" do + describe "#errors(content)" do describe "Error handling" do - it "returns error to parse YAML" do - config = YAML.dump("invalid: yaml: test") - expect(GitlabCiYamlProcessor.validate(config)).to eq "Invalid configuration format" + it "returns error if parse YAML failed" do + content = YAML.dump("invalid: yaml: test") + expect(GitlabCiYamlProcessor.errors(content)).to eq "Invalid configuration format" end it "returns errors if tags parameter is invalid" do - config = YAML.dump({ rspec: { script: "test", tags: "mysql" } }) - expect(GitlabCiYamlProcessor.validate(config)).to eq "jobs:rspec tags should be an array of strings" + content = YAML.dump({ rspec: { script: "test", tags: "mysql" } }) + expect(GitlabCiYamlProcessor.errors(content)).to eq "jobs:rspec tags should be an array of strings" end it "does not return errors" do - config = File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) - expect(GitlabCiYamlProcessor.validate(config)).to eq "valid" + content = File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) + expect(GitlabCiYamlProcessor.errors(content)).to eq nil end end end