diff options
-rw-r--r-- | lib/ci/api/entities.rb | 7 | ||||
-rw-r--r-- | lib/ci/api/lint.rb | 33 | ||||
-rw-r--r-- | spec/requests/ci/api/lint_spec.rb | 43 |
3 files changed, 59 insertions, 24 deletions
diff --git a/lib/ci/api/entities.rb b/lib/ci/api/entities.rb index 8052908e78a..3f5bdaba3f5 100644 --- a/lib/ci/api/entities.rb +++ b/lib/ci/api/entities.rb @@ -59,13 +59,6 @@ module Ci expose :id, :variables expose :pipeline, using: Commit, as: :commit end - - class Lint < Grape::Entity - expose :content - expose :status - expose :builds - expose :stages - end end end end diff --git a/lib/ci/api/lint.rb b/lib/ci/api/lint.rb index d781441d2b7..53a80318195 100644 --- a/lib/ci/api/lint.rb +++ b/lib/ci/api/lint.rb @@ -4,19 +4,34 @@ module Ci before { authenticate! } resources :lint do + post do - content = params[:content] + begin + response = {} + @content = params[:content] - if content - config_processor = Ci::GitlabCiYamlProcessor.new(content) - stages = config_processor.stages - builds = config_processor.builds - status = true + if @content + @config_processor = Ci::GitlabCiYamlProcessor.new(@content) + @stages = @config_processor.stages + @builds = @config_processor.builds - response = { status: status, stages: stages, builds: builds } - end + response = { + content: @content, + status: "syntax is correct" + } + + stage_builds = @stages.each do |stage| + response["#{stage}"] = @builds.select { |build| build[:stage] == stage } + end + else + render_api_error!("Please provide content of .gitlab-ci.yml", 400) + end - response + response + + rescue Ci::GitlabCiYamlProcessor::ValidationError, Psych::SyntaxError => e + error!({ content: @content, status: "syntax is incorrect", message: e.message }) + end end end end diff --git a/spec/requests/ci/api/lint_spec.rb b/spec/requests/ci/api/lint_spec.rb index 2ffd88120ae..9bd8154ce6d 100644 --- a/spec/requests/ci/api/lint_spec.rb +++ b/spec/requests/ci/api/lint_spec.rb @@ -3,21 +3,48 @@ require 'spec_helper' describe Ci::API::API do include ApiHelpers - let(:content) do + let(:user) { create(:user) } + let(:yaml_content) do File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) end - describe "Builds API for Lint" do + describe 'POST /ci/lint' do + context "with valid .gitlab-ci.yaml content" do + context "authorized user" do + it "validate content" do + post ci_api('/lint'), { private_token: user.private_token, content: yaml_content } - describe 'POST /ci/lint' do - before { content } + expect(response).to have_http_status(201) + expect(json_response).to be_an Hash + expect(json_response['status']).to eq('syntax is correct') + end + end + + context "unauthorized user" do + it "does not validate content" do + post ci_api('/lint'), { content: yaml_content } - context "with valid .gitlab-ci.yaml file" do - it "has success status" do - # binding.pry - expect(response).to have_content(true) + expect(response).to have_http_status(401) end end end + + context "with invalid .gitlab_ci.yml content" do + it "validate content" do + post ci_api('/lint'), { private_token: user.private_token, content: 'invalid content'} + + expect(response).to have_http_status(500) + expect(json_response['status']).to eq('syntax is incorrect') + end + end + + context "no content" do + it "shows error message" do + post ci_api('/lint'), { private_token: user.private_token } + + expect(response).to have_http_status(400) + expect(json_response['message']).to eq('Please provide content of .gitlab-ci.yml') + end + end end end |