Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate .gitlab-ci.yml API #487

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/gitlab/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Client < API
include Jobs
include Keys
include Labels
include Lint
include Markdown
include MergeRequestApprovals
include MergeRequests
Expand Down
19 changes: 19 additions & 0 deletions lib/gitlab/client/lint.rb
Original file line number Diff line number Diff line change
@@ -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 <Gitlab::ObjectifiedHash> Returns information about validity of the yml.
def validate_gitlab_ci_yml(content)
body = { content: content }
post('/lint', body: body)
end
end
end
6 changes: 6 additions & 0 deletions spec/fixtures/invalid_content_gitlab_ci_yml.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"status": "invalid",
"errors": [
"variables config should be a hash of key value pairs"
]
}
3 changes: 3 additions & 0 deletions spec/fixtures/no_content_gitlab_ci_yml.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"error": "content is missing"
}
4 changes: 4 additions & 0 deletions spec/fixtures/valid_content_gitlab_ci_yml.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": "valid",
"errors": []
}
54 changes: 54 additions & 0 deletions spec/gitlab/client/lint_spec.rb
Original file line number Diff line number Diff line change
@@ -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