Commit 1a7a900d authored by Katarzyna Kobierska's avatar Katarzyna Kobierska

Improve code

parent 2c8b830f
...@@ -5,22 +5,18 @@ module API ...@@ -5,22 +5,18 @@ module API
requires :content, type: String, desc: 'Content of .gitlab-ci.yml' requires :content, type: String, desc: 'Content of .gitlab-ci.yml'
end end
post 'ci/lint' do namespace 'ci' do
error = Ci::GitlabCiYamlProcessor.validation_message(params[:content]) post '/lint' do
response = { errors = Ci::GitlabCiYamlProcessor.validation_message(params[:content])
status: '',
error: ''
}
if error.blank? status 200
response[:status] = 'valid'
if errors.blank?
{ status: 'valid', errors: [] }
else else
response[:error] = error { status: 'invalid', errors: [errors] }
response[:status] = 'invalid' end
end end
status 200
response
end end
end end
end end
...@@ -79,9 +79,7 @@ module Ci ...@@ -79,9 +79,7 @@ module Ci
end end
def self.validation_message(content) def self.validation_message(content)
if content.blank? return 'Please provide content of .gitlab-ci.yml' if content.blank?
'Please provide content of .gitlab-ci.yml'
else
begin begin
Ci::GitlabCiYamlProcessor.new(content) Ci::GitlabCiYamlProcessor.new(content)
nil nil
...@@ -89,7 +87,6 @@ module Ci ...@@ -89,7 +87,6 @@ module Ci
e.message e.message
end end
end end
end
private private
......
...@@ -1256,7 +1256,8 @@ EOT ...@@ -1256,7 +1256,8 @@ EOT
it "returns an error about invalid configutaion" do it "returns an error about invalid configutaion" do
content = YAML.dump("invalid: yaml: test") content = YAML.dump("invalid: yaml: test")
expect(GitlabCiYamlProcessor.validation_message(content)).to eq "Invalid configuration format" expect(GitlabCiYamlProcessor.validation_message(content))
.to eq "Invalid configuration format"
end end
end end
...@@ -1264,13 +1265,15 @@ EOT ...@@ -1264,13 +1265,15 @@ EOT
it "returns an error about invalid tags" do it "returns an error about invalid tags" do
content = YAML.dump({ rspec: { script: "test", tags: "mysql" } }) content = YAML.dump({ rspec: { script: "test", tags: "mysql" } })
expect(GitlabCiYamlProcessor.validation_message(content)).to eq "jobs:rspec tags should be an array of strings" expect(GitlabCiYamlProcessor.validation_message(content))
.to eq "jobs:rspec tags should be an array of strings"
end end
end end
context "when YMAL content is empty" do context "when YMAL content is empty" do
it "returns an error about missing content" do it "returns an error about missing content" do
expect(GitlabCiYamlProcessor.validation_message('')).to eq "Please provide content of .gitlab-ci.yml" expect(GitlabCiYamlProcessor.validation_message(''))
.to eq "Please provide content of .gitlab-ci.yml"
end end
end end
......
...@@ -15,7 +15,7 @@ describe API::Lint, api: true do ...@@ -15,7 +15,7 @@ describe API::Lint, api: true do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response).to be_an Hash expect(json_response).to be_an Hash
expect(json_response['status']).to eq('valid') expect(json_response['status']).to eq('valid')
expect(json_response['error']).to eq('') expect(json_response['errors']).to eq([])
end end
end end
...@@ -25,7 +25,7 @@ describe API::Lint, api: true do ...@@ -25,7 +25,7 @@ describe API::Lint, api: true do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response['status']).to eq('invalid') expect(json_response['status']).to eq('invalid')
expect(json_response['error']).to eq('Invalid configuration format') expect(json_response['errors']).to eq(['Invalid configuration format'])
end end
it "responds with errors about invalid configuration" do it "responds with errors about invalid configuration" do
...@@ -33,7 +33,7 @@ describe API::Lint, api: true do ...@@ -33,7 +33,7 @@ describe API::Lint, api: true do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response['status']).to eq('invalid') expect(json_response['status']).to eq('invalid')
expect(json_response['error']).to eq('jobs config should contain at least one visible job') expect(json_response['errors']).to eq(['jobs config should contain at least one visible job'])
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment