From 9c6abb911ef218baa68338c65b446b700bfb5f98 Mon Sep 17 00:00:00 2001 From: Akash Srivastava Date: Sat, 23 Feb 2019 19:37:49 +0530 Subject: [PATCH] Valid .gitlab-ci.yml API --- lib/gitlab/client.rb | 1 + lib/gitlab/client/lint.rb | 19 +++++++ .../invalid_content_gitlab_ci_yml.json | 6 +++ spec/fixtures/no_content_gitlab_ci_yml.json | 3 ++ .../fixtures/valid_content_gitlab_ci_yml.json | 4 ++ spec/gitlab/client/lint_spec.rb | 54 +++++++++++++++++++ 6 files changed, 87 insertions(+) create mode 100644 lib/gitlab/client/lint.rb create mode 100644 spec/fixtures/invalid_content_gitlab_ci_yml.json create mode 100644 spec/fixtures/no_content_gitlab_ci_yml.json create mode 100644 spec/fixtures/valid_content_gitlab_ci_yml.json create mode 100644 spec/gitlab/client/lint_spec.rb diff --git a/lib/gitlab/client.rb b/lib/gitlab/client.rb index e0552010e..ee7ef7791 100644 --- a/lib/gitlab/client.rb +++ b/lib/gitlab/client.rb @@ -28,6 +28,7 @@ class Client < API include Jobs include Keys include Labels + include Lint include Markdown include MergeRequestApprovals include MergeRequests diff --git a/lib/gitlab/client/lint.rb b/lib/gitlab/client/lint.rb new file mode 100644 index 000000000..e88f36181 --- /dev/null +++ b/lib/gitlab/client/lint.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class Gitlab::Client + # Defines methods related to lint/validations. + # @see https://docs.gitlab.com/ce/api/lint.html + module Lint + # Checks if your .gitlab-ci.yml file is valid. + # + # @example + # Gitlab.validate_gitlab_ci_yml("{ \"image\": \"ruby:2.6\", \"services\": [\"postgres\"], \"before_script\": [\"bundle install\", \"bundle exec rake db:create\"], \"variables\": {\"DB_NAME\": \"postgres\"}, \"types\": [\"test\", \"deploy\", \"notify\"], \"rspec\": { \"script\": \"rake spec\", \"tags\": [\"ruby\", \"postgres\"], \"only\": [\"branches\"]}}") + # + # @param [String] content the .gitlab-ci.yaml content. + # @return Returns information about validity of the yml. + def validate_gitlab_ci_yml(content) + body = { content: content } + post('/lint', body: body) + end + end +end diff --git a/spec/fixtures/invalid_content_gitlab_ci_yml.json b/spec/fixtures/invalid_content_gitlab_ci_yml.json new file mode 100644 index 000000000..697ed2b91 --- /dev/null +++ b/spec/fixtures/invalid_content_gitlab_ci_yml.json @@ -0,0 +1,6 @@ +{ + "status": "invalid", + "errors": [ + "variables config should be a hash of key value pairs" + ] +} diff --git a/spec/fixtures/no_content_gitlab_ci_yml.json b/spec/fixtures/no_content_gitlab_ci_yml.json new file mode 100644 index 000000000..ee627ec85 --- /dev/null +++ b/spec/fixtures/no_content_gitlab_ci_yml.json @@ -0,0 +1,3 @@ +{ + "error": "content is missing" +} diff --git a/spec/fixtures/valid_content_gitlab_ci_yml.json b/spec/fixtures/valid_content_gitlab_ci_yml.json new file mode 100644 index 000000000..18529f312 --- /dev/null +++ b/spec/fixtures/valid_content_gitlab_ci_yml.json @@ -0,0 +1,4 @@ +{ + "status": "valid", + "errors": [] +} diff --git a/spec/gitlab/client/lint_spec.rb b/spec/gitlab/client/lint_spec.rb new file mode 100644 index 000000000..ead30a1f8 --- /dev/null +++ b/spec/gitlab/client/lint_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Client do + describe '.validate_gitlab_ci_yml' do + context 'when valid content' do + before do + stub_post('/lint', 'valid_content_gitlab_ci_yml') + @lint_response = Gitlab.validate_gitlab_ci_yml('{ "image": "ruby:2.6", "services": ["postgres"], "before_script": ["bundle install", "bundle exec rake db:create"], "variables": {"DB_NAME": "postgres"}, "types": ["test", "deploy", "notify"], "rspec": { "script": "rake spec", "tags": ["ruby", "postgres"], "only": ["branches"]}}') + end + + it 'gets the correct resource' do + expect(a_post('/lint') + .with(body: { content: '{ "image": "ruby:2.6", "services": ["postgres"], "before_script": ["bundle install", "bundle exec rake db:create"], "variables": {"DB_NAME": "postgres"}, "types": ["test", "deploy", "notify"], "rspec": { "script": "rake spec", "tags": ["ruby", "postgres"], "only": ["branches"]}}' })).to have_been_made + end + + it 'returns correct information about validity of the response' do + expect(@lint_response.status).to eq('valid') + expect(@lint_response.errors).to eq([]) + end + end + context 'when invalid content' do + before do + stub_post('/lint', 'invalid_content_gitlab_ci_yml') + @lint_response = Gitlab.validate_gitlab_ci_yml('Not a valid content') + end + + it 'gets the correct resource' do + expect(a_post('/lint') + .with(body: { content: 'Not a valid content' })).to have_been_made + end + + it 'returns correct information about validity of the response' do + expect(@lint_response.status).to eq('invalid') + expect(@lint_response.errors).to include('variables config should be a hash of key value pairs') + end + end + context 'when without content attribute' do + before do + stub_post('/lint', 'no_content_gitlab_ci_yml') + @lint_response = Gitlab.validate_gitlab_ci_yml(nil) + end + + it 'gets the correct resource' do + expect(a_post('/lint')).to have_been_made + end + + it 'returns correct information about validity of the response' do + expect(@lint_response.error).to eq('content is missing') + end + end + end +end