summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatarzyna Kobierska <kkobierska@gmail.com>2016-08-25 13:40:35 +0200
committerKatarzyna Kobierska <kkobierska@gmail.com>2016-09-07 12:10:49 +0200
commit9e313c129418f847498e771abd6bea53884682b5 (patch)
tree0604e2383bde88722e7b565d54b188f90c426ebe
parentde2e8d4a559bd99479f46e527d8e78f67ad37de6 (diff)
downloadgitlab-ce-9e313c129418f847498e771abd6bea53884682b5.tar.gz
Add class method to encapsulate exception
-rw-r--r--app/controllers/ci/lints_controller.rb6
-rw-r--r--lib/api/lint.rb34
-rw-r--r--lib/ci/gitlab_ci_yaml_processor.rb9
-rw-r--r--spec/lib/ci/gitlab_ci_yaml_processor_spec.rb19
4 files changed, 47 insertions, 21 deletions
diff --git a/app/controllers/ci/lints_controller.rb b/app/controllers/ci/lints_controller.rb
index a7af3cb8345..67028b66a66 100644
--- a/app/controllers/ci/lints_controller.rb
+++ b/app/controllers/ci/lints_controller.rb
@@ -11,15 +11,15 @@ module Ci
if @content.blank?
@status = false
@error = "Please provide content of .gitlab-ci.yml"
+ elsif Ci::GitlabCiYamlProcessor.validate(@content) != "valid"
+ @status = false
+ @error = Ci::GitlabCiYamlProcessor.validate(@content)
else
@config_processor = Ci::GitlabCiYamlProcessor.new(@content)
@stages = @config_processor.stages
@builds = @config_processor.builds
@status = true
end
- rescue Ci::GitlabCiYamlProcessor::ValidationError, Psych::SyntaxError => e
- @error = e.message
- @status = false
rescue
@error = 'Undefined error'
@status = false
diff --git a/lib/api/lint.rb b/lib/api/lint.rb
index 68eabb571d6..2d27bc65462 100644
--- a/lib/api/lint.rb
+++ b/lib/api/lint.rb
@@ -7,31 +7,29 @@ module API
desc 'Validation of .gitlab-ci.yml content'
post do
- status 200
-
- begin
- response = {
- status: '',
- errors: [],
- jobs: []
- }
-
- config_processor = Ci::GitlabCiYamlProcessor.new(params[:content])
-
- config_processor.builds.each do |build|
- response[:jobs].push("#{build[:name]}")
- response[:status] = 'valid'
- end
-
- response
+ response = {
+ status: '',
+ errors: [],
+ jobs: []
+ }
- rescue Ci::GitlabCiYamlProcessor::ValidationError, Psych::SyntaxError => e
+ if Ci::GitlabCiYamlProcessor.validate(@content) != "valid"
status 200
response[:errors].push(e.message)
response[:status] = 'invalid'
response
end
+
+ config_processor = Ci::GitlabCiYamlProcessor.new(params[:content])
+
+ config_processor.builds.each do |build|
+ response[:jobs].push("#{build[:name]}")
+ response[:status] = 'valid'
+ 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 47efd5bd9f2..9799f494df6 100644
--- a/lib/ci/gitlab_ci_yaml_processor.rb
+++ b/lib/ci/gitlab_ci_yaml_processor.rb
@@ -78,6 +78,15 @@ module Ci
}
end
+ def self.validate(content)
+ begin
+ Ci::GitlabCiYamlProcessor.new(content)
+ "valid"
+ rescue ValidationError, Psych::SyntaxError => e
+ e.message
+ end
+ end
+
private
def initial_parsing
diff --git a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
index be51d942af7..bd5ea2bb97d 100644
--- a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
+++ b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
@@ -1250,5 +1250,24 @@ EOT
end
end
end
+
+ describe "#validate(config)" 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"
+ 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"
+ 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"
+ end
+ end
+ end
end
end