Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
c54c046e
Commit
c54c046e
authored
Sep 15, 2020
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Show expanded YAML when linting CI config via API
Show YAML with includes and extend entries expanded.
parent
464484f7
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
99 additions
and
16 deletions
+99
-16
lib/api/lint.rb
lib/api/lint.rb
+11
-6
lib/gitlab/ci/yaml_processor/result.rb
lib/gitlab/ci/yaml_processor/result.rb
+4
-0
spec/lib/gitlab/ci/yaml_processor/result_spec.rb
spec/lib/gitlab/ci/yaml_processor/result_spec.rb
+45
-0
spec/requests/api/lint_spec.rb
spec/requests/api/lint_spec.rb
+39
-10
No files found.
lib/api/lint.rb
View file @
c54c046e
...
...
@@ -6,18 +6,23 @@ module API
desc
'Validation of .gitlab-ci.yml content'
params
do
requires
:content
,
type:
String
,
desc:
'Content of .gitlab-ci.yml'
optional
:include_merged_yaml
,
type:
Boolean
,
desc:
'Whether or not to include merged CI config yaml in the response'
end
post
'/lint'
do
error
=
Gitlab
::
Ci
::
YamlProcessor
.
validation_message
(
params
[
:content
],
user:
current_user
)
result
=
Gitlab
::
Ci
::
YamlProcessor
.
new
(
params
[
:content
],
user:
current_user
).
execute
error
=
result
.
errors
.
first
status
200
if
error
.
blank?
response
=
if
error
.
blank?
{
status:
'valid'
,
errors:
[]
}
else
{
status:
'invalid'
,
errors:
[
error
]
}
end
response
.
tap
do
|
response
|
response
[
:merged_yaml
]
=
result
.
merged_yaml
if
params
[
:include_merged_yaml
]
end
end
end
end
...
...
lib/gitlab/ci/yaml_processor/result.rb
View file @
c54c046e
...
...
@@ -95,6 +95,10 @@ module Gitlab
}.
compact
}.
compact
end
def
merged_yaml
@ci_config
&
.
to_hash
&
.
to_yaml
end
private
def
variables
...
...
spec/lib/gitlab/ci/yaml_processor/result_spec.rb
0 → 100644
View file @
c54c046e
# frozen_string_literal: true
require
'spec_helper'
module
Gitlab
module
Ci
class
YamlProcessor
RSpec
.
describe
Result
do
include
StubRequests
let
(
:user
)
{
create
(
:user
)
}
let
(
:ci_config
)
{
Gitlab
::
Ci
::
Config
.
new
(
config_content
,
user:
user
)
}
let
(
:result
)
{
described_class
.
new
(
ci_config:
ci_config
,
warnings:
ci_config
&
.
warnings
)
}
describe
'#merged_yaml'
do
subject
(
:merged_yaml
)
{
result
.
merged_yaml
}
let
(
:config_content
)
do
YAML
.
dump
(
include:
{
remote:
'https://example.com/sample.yml'
},
test:
{
stage:
'test'
,
script:
'echo'
}
)
end
let
(
:included_yml
)
do
YAML
.
dump
(
another_test:
{
stage:
'test'
,
script:
'echo 2'
}
)
end
before
do
stub_full_request
(
'https://example.com/sample.yml'
).
to_return
(
body:
included_yml
)
end
it
'returns expanded yaml config'
do
expanded_config
=
YAML
.
safe_load
(
merged_yaml
,
[
Symbol
])
included_config
=
YAML
.
safe_load
(
included_yml
,
[
Symbol
])
expect
(
expanded_config
).
to
include
(
*
included_config
.
keys
)
end
end
end
end
end
end
spec/requests/api/lint_spec.rb
View file @
c54c046e
...
...
@@ -17,24 +17,53 @@ RSpec.describe API::Lint do
expect
(
json_response
[
'status'
]).
to
eq
(
'valid'
)
expect
(
json_response
[
'errors'
]).
to
eq
([])
end
it
'outputs expanded yaml content'
do
post
api
(
'/ci/lint'
),
params:
{
content:
yaml_content
,
include_merged_yaml:
true
}
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
).
to
have_key
(
'merged_yaml'
)
end
end
context
'with an invalid .gitlab_ci.yml'
do
context
'with invalid syntax'
do
let
(
:yaml_content
)
{
'invalid content'
}
it
'responds with errors about invalid syntax'
do
post
api
(
'/ci/lint'
),
params:
{
content:
'invalid content'
}
post
api
(
'/ci/lint'
),
params:
{
content:
yaml_content
}
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
[
'status'
]).
to
eq
(
'invalid'
)
expect
(
json_response
[
'errors'
]).
to
eq
([
'Invalid configuration format'
])
end
it
"responds with errors about invalid configuration"
do
post
api
(
'/ci/lint'
),
params:
{
content:
'{ image: "ruby:2.7", services: ["postgres"] }'
}
it
'outputs expanded yaml content'
do
post
api
(
'/ci/lint'
),
params:
{
content:
yaml_content
,
include_merged_yaml:
true
}
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
).
to
have_key
(
'merged_yaml'
)
end
end
context
'with invalid configuration'
do
let
(
:yaml_content
)
{
'{ image: "ruby:2.7", services: ["postgres"] }'
}
it
'responds with errors about invalid configuration'
do
post
api
(
'/ci/lint'
),
params:
{
content:
yaml_content
}
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
[
'status'
]).
to
eq
(
'invalid'
)
expect
(
json_response
[
'errors'
]).
to
eq
([
'jobs config should contain at least one visible job'
])
end
it
'outputs expanded yaml content'
do
post
api
(
'/ci/lint'
),
params:
{
content:
yaml_content
,
include_merged_yaml:
true
}
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
).
to
have_key
(
'merged_yaml'
)
end
end
end
context
'without the content parameter'
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment