Skip to content
Snippets Groups Projects
Commit e61b6b79 authored by Laura Montemayor's avatar Laura Montemayor :zero:
Browse files

Merge branch 'lm-add-ci-config-0' into 'master'

Adds ci config linting resolver

See merge request !46912
parents ca1f46db 0a3ea491
No related branches found
No related tags found
No related merge requests found
Pipeline #223151116 failed
Showing
with 1878 additions and 142 deletions
# frozen_string_literal: true
module Resolvers
module Ci
class ConfigResolver < BaseResolver
type Types::Ci::Config::ConfigType, null: true
argument :content, GraphQL::STRING_TYPE,
required: true,
description: 'Contents of .gitlab-ci.yml'
def resolve(content:)
result = ::Gitlab::Ci::YamlProcessor.new(content).execute
if result.errors.empty?
stages = process_stage_groups(result.stages, result.jobs)
response = {
status: :valid,
errors: [],
stages: stages.select { |stage| !stage[:groups].empty? }
}
else
response = {
status: :invalid,
errors: [result.errors.first]
}
end
response.tap do |response|
response[:merged_yaml] = result.merged_yaml
end
end
private
def process_stages(config_stages)
config_stages.map { |stage| { name: stage, groups: [] } }
end
def process_jobs(config_jobs)
config_jobs.map do |job_name, job|
{
name: job_name,
stage: job[:stage],
group_name: CommitStatus.new(name: job_name).group_name,
needs: process_needs(job) || []
}
end
end
def process_needs(job)
job.dig(:needs, :job)&.map do |job_need|
{ name: job_need[:name], artifacts: job_need[:artifacts] }
end
end
def process_groups(job_data)
jobs = process_jobs(job_data)
group_names = jobs.map { |job| job[:group_name] }.uniq
jobs_by_group = jobs.group_by { |job| job[:group_name] }
group_names.map do |group|
group_jobs = jobs_by_group[group]
{ jobs: group_jobs, name: group, stage: group_jobs.first[:stage], size: group_jobs.count }
end
end
def process_stage_groups(stage_data, job_data)
stages = process_stages(stage_data)
groups = process_groups(job_data)
groups_by_stage = groups.group_by { |group| group[:stage] }
stages.each do |stage|
next unless groups_by_stage[stage[:name]]
stage[:groups] = groups_by_stage[stage[:name]]
end
end
end
end
end
# frozen_string_literal: true
module Types
module Ci
# rubocop: disable Graphql/AuthorizeTypes
module Config
class ConfigType < BaseObject
graphql_name 'CiConfig'
field :errors, [GraphQL::STRING_TYPE], null: true,
description: 'Linting errors'
field :merged_yaml, GraphQL::STRING_TYPE, null: true,
description: 'Merged CI config YAML'
field :stages, Types::Ci::Config::StageType.connection_type, null: true,
description: 'Stages of the pipeline'
field :status, Types::Ci::Config::StatusEnum, null: true,
description: 'Status of linting, can be either valid or invalid'
end
end
end
end
# frozen_string_literal: true
module Types
module Ci
# rubocop: disable Graphql/AuthorizeTypes
module Config
class GroupType < BaseObject
graphql_name 'CiConfigGroup'
field :name, GraphQL::STRING_TYPE, null: true,
description: 'Name of the job group'
field :jobs, Types::Ci::Config::JobType.connection_type, null: true,
description: 'Jobs in group'
field :size, GraphQL::INT_TYPE, null: true,
description: 'Size of the job group'
end
end
end
end
# frozen_string_literal: true
module Types
module Ci
# rubocop: disable Graphql/AuthorizeTypes
module Config
class JobType < BaseObject
graphql_name 'CiConfigJob'
field :name, GraphQL::STRING_TYPE, null: true,
description: 'Name of the job'
field :group_name, GraphQL::STRING_TYPE, null: true,
description: 'Name of the job group'
field :stage, GraphQL::STRING_TYPE, null: true,
description: 'Name of the job stage'
field :needs, Types::Ci::Config::NeedType.connection_type, null: true,
description: 'Builds that must complete before the jobs run'
end
end
end
end
# frozen_string_literal: true
module Types
module Ci
# rubocop: disable Graphql/AuthorizeTypes
module Config
class NeedType < BaseObject
graphql_name 'CiConfigNeed'
field :name, GraphQL::STRING_TYPE, null: true,
description: 'Name of the need'
end
end
end
end
# frozen_string_literal: true
module Types
module Ci
# rubocop: disable Graphql/AuthorizeTypes
module Config
class StageType < BaseObject
graphql_name 'CiConfigStage'
field :name, GraphQL::STRING_TYPE, null: true,
description: 'Name of the stage'
field :groups, Types::Ci::Config::GroupType.connection_type, null: true,
description: 'Groups of jobs for the stage'
end
end
end
end
# frozen_string_literal: true
module Types
module Ci
module Config
class StatusEnum < BaseEnum
graphql_name 'CiConfigStatus'
description 'Values for YAML processor result'
value 'VALID', 'Valid `gitlab-ci.yml`', value: :valid
value 'INVALID', 'Invalid `gitlab-ci.yml`', value: :invalid
end
end
end
end
......@@ -92,6 +92,11 @@ class QueryType < ::Types::BaseObject
description: 'Get runner setup instructions',
resolver: Resolvers::Ci::RunnerSetupResolver
field :ci_config, Types::Ci::Config::ConfigType, null: true,
description: 'Get linted and processed contents of a CI config',
resolver: Resolvers::Ci::ConfigResolver,
complexity: 220
def design_management
DesignManagementObject.new(nil)
end
......
---
title: 'Expose GraphQL resolver for processing CI config'
merge_request: 46912
author:
type: added
......@@ -2237,6 +2237,321 @@ type BurnupChartDailyTotals {
scopeWeight: Int!
}
type CiConfig {
"""
Linting errors
"""
errors: [String!]
"""
Merged CI config YAML
"""
mergedYaml: String
"""
Stages of the pipeline
"""
stages(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String
"""
Returns the elements in the list that come before the specified cursor.
"""
before: String
"""
Returns the first _n_ elements from the list.
"""
first: Int
"""
Returns the last _n_ elements from the list.
"""
last: Int
): CiConfigStageConnection
"""
Status of linting, can be either valid or invalid
"""
status: CiConfigStatus
}
type CiConfigGroup {
"""
Jobs in group
"""
jobs(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String
"""
Returns the elements in the list that come before the specified cursor.
"""
before: String
"""
Returns the first _n_ elements from the list.
"""
first: Int
"""
Returns the last _n_ elements from the list.
"""
last: Int
): CiConfigJobConnection
"""
Name of the job group
"""
name: String
"""
Size of the job group
"""
size: Int
}
"""
The connection type for CiConfigGroup.
"""
type CiConfigGroupConnection {
"""
A list of edges.
"""
edges: [CiConfigGroupEdge]
"""
A list of nodes.
"""
nodes: [CiConfigGroup]
"""
Information to aid in pagination.
"""
pageInfo: PageInfo!
}
"""
An edge in a connection.
"""
type CiConfigGroupEdge {
"""
A cursor for use in pagination.
"""
cursor: String!
"""
The item at the end of the edge.
"""
node: CiConfigGroup
}
type CiConfigJob {
"""
Name of the job group
"""
groupName: String
"""
Name of the job
"""
name: String
"""
Builds that must complete before the jobs run
"""
needs(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String
"""
Returns the elements in the list that come before the specified cursor.
"""
before: String
"""
Returns the first _n_ elements from the list.
"""
first: Int
"""
Returns the last _n_ elements from the list.
"""
last: Int
): CiConfigNeedConnection
"""
Name of the job stage
"""
stage: String
}
"""
The connection type for CiConfigJob.
"""
type CiConfigJobConnection {
"""
A list of edges.
"""
edges: [CiConfigJobEdge]
"""
A list of nodes.
"""
nodes: [CiConfigJob]
"""
Information to aid in pagination.
"""
pageInfo: PageInfo!
}
"""
An edge in a connection.
"""
type CiConfigJobEdge {
"""
A cursor for use in pagination.
"""
cursor: String!
"""
The item at the end of the edge.
"""
node: CiConfigJob
}
type CiConfigNeed {
"""
Name of the need
"""
name: String
}
"""
The connection type for CiConfigNeed.
"""
type CiConfigNeedConnection {
"""
A list of edges.
"""
edges: [CiConfigNeedEdge]
"""
A list of nodes.
"""
nodes: [CiConfigNeed]
"""
Information to aid in pagination.
"""
pageInfo: PageInfo!
}
"""
An edge in a connection.
"""
type CiConfigNeedEdge {
"""
A cursor for use in pagination.
"""
cursor: String!
"""
The item at the end of the edge.
"""
node: CiConfigNeed
}
type CiConfigStage {
"""
Groups of jobs for the stage
"""
groups(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String
"""
Returns the elements in the list that come before the specified cursor.
"""
before: String
"""
Returns the first _n_ elements from the list.
"""
first: Int
"""
Returns the last _n_ elements from the list.
"""
last: Int
): CiConfigGroupConnection
"""
Name of the stage
"""
name: String
}
"""
The connection type for CiConfigStage.
"""
type CiConfigStageConnection {
"""
A list of edges.
"""
edges: [CiConfigStageEdge]
"""
A list of nodes.
"""
nodes: [CiConfigStage]
"""
Information to aid in pagination.
"""
pageInfo: PageInfo!
}
"""
An edge in a connection.
"""
type CiConfigStageEdge {
"""
A cursor for use in pagination.
"""
cursor: String!
"""
The item at the end of the edge.
"""
node: CiConfigStage
}
"""
Values for YAML processor result
"""
enum CiConfigStatus {
"""
Invalid `gitlab-ci.yml`
"""
INVALID
"""
Valid `gitlab-ci.yml`
"""
VALID
}
type CiGroup {
"""
Detailed status of the group
......@@ -17701,6 +18016,16 @@ type PromoteToEpicPayload {
}
type Query {
"""
Get linted and processed contents of a CI config
"""
ciConfig(
"""
Contents of .gitlab-ci.yml
"""
content: String!
): CiConfig
"""
Find a container repository
"""
......
This diff is collapsed.
......@@ -367,6 +367,45 @@ Represents the total number of issues and their weights for a particular day.
| `scopeCount` | Int! | Number of issues as of this day |
| `scopeWeight` | Int! | Total weight of issues as of this day |
### CiConfig
| Field | Type | Description |
| ----- | ---- | ----------- |
| `errors` | String! => Array | Linting errors |
| `mergedYaml` | String | Merged CI config YAML |
| `stages` | CiConfigStageConnection | Stages of the pipeline |
| `status` | CiConfigStatus | Status of linting, can be either valid or invalid |
### CiConfigGroup
| Field | Type | Description |
| ----- | ---- | ----------- |
| `jobs` | CiConfigJobConnection | Jobs in group |
| `name` | String | Name of the job group |
| `size` | Int | Size of the job group |
### CiConfigJob
| Field | Type | Description |
| ----- | ---- | ----------- |
| `groupName` | String | Name of the job group |
| `name` | String | Name of the job |
| `needs` | CiConfigNeedConnection | Builds that must complete before the jobs run |
| `stage` | String | Name of the job stage |
### CiConfigNeed
| Field | Type | Description |
| ----- | ---- | ----------- |
| `name` | String | Name of the need |
### CiConfigStage
| Field | Type | Description |
| ----- | ---- | ----------- |
| `groups` | CiConfigGroupConnection | Groups of jobs for the stage |
| `name` | String | Name of the stage |
### CiGroup
| Field | Type | Description |
......@@ -3877,6 +3916,15 @@ Types of blob viewers.
| `rich` | |
| `simple` | |
### CiConfigStatus
Values for YAML processor result.
| Value | Description |
| ----- | ----------- |
| `INVALID` | Invalid `gitlab-ci.yml` |
| `VALID` | Valid `gitlab-ci.yml` |
### CommitActionMode
Mode of a commit action.
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Resolvers::Ci::ConfigResolver do
include GraphqlHelpers
describe '#resolve' do
before do
yaml_processor_double = instance_double(::Gitlab::Ci::YamlProcessor)
allow(yaml_processor_double).to receive(:execute).and_return(fake_result)
allow(::Gitlab::Ci::YamlProcessor).to receive(:new).and_return(yaml_processor_double)
end
context 'with a valid .gitlab-ci.yml' do
let(:fake_result) do
::Gitlab::Ci::YamlProcessor::Result.new(
ci_config: ::Gitlab::Ci::Config.new(content),
errors: [],
warnings: []
)
end
let_it_be(:content) do
File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci_includes.yml'))
end
it 'lints the ci config file' do
response = resolve(described_class, args: { content: content }, ctx: {})
expect(response[:status]).to eq(:valid)
expect(response[:errors]).to be_empty
end
end
context 'with an invalid .gitlab-ci.yml' do
let(:content) { 'invalid' }
let(:fake_result) do
Gitlab::Ci::YamlProcessor::Result.new(
ci_config: nil,
errors: ['Invalid configuration format'],
warnings: []
)
end
it 'responds with errors about invalid syntax' do
response = resolve(described_class, args: { content: content }, ctx: {})
expect(response[:status]).to eq(:invalid)
expect(response[:errors]).to eq(['Invalid configuration format'])
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Types::Ci::Config::ConfigType do
specify { expect(described_class.graphql_name).to eq('CiConfig') }
it 'exposes the expected fields' do
expected_fields = %i[
errors
mergedYaml
stages
status
]
expect(described_class).to have_graphql_fields(*expected_fields)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Types::Ci::Config::GroupType do
specify { expect(described_class.graphql_name).to eq('CiConfigGroup') }
it 'exposes the expected fields' do
expected_fields = %i[
name
jobs
size
]
expect(described_class).to have_graphql_fields(*expected_fields)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Types::Ci::Config::JobType do
specify { expect(described_class.graphql_name).to eq('CiConfigJob') }
it 'exposes the expected fields' do
expected_fields = %i[
name
group_name
stage
needs
]
expect(described_class).to have_graphql_fields(*expected_fields)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Types::Ci::Config::NeedType do
specify { expect(described_class.graphql_name).to eq('CiConfigNeed') }
it 'exposes the expected fields' do
expected_fields = %i[
name
]
expect(described_class).to have_graphql_fields(*expected_fields)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Types::Ci::Config::StageType do
specify { expect(described_class.graphql_name).to eq('CiConfigStage') }
it 'exposes the expected fields' do
expected_fields = %i[
name
groups
]
expect(described_class).to have_graphql_fields(*expected_fields)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Query.ciConfig' do
include GraphqlHelpers
subject(:post_graphql_query) { post_graphql(query, current_user: user) }
let(:user) { create(:user) }
let_it_be(:content) do
File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci_includes.yml'))
end
let(:query) do
%(
query {
ciConfig(content: "#{content}") {
status
errors
stages {
nodes {
name
groups {
nodes {
name
size
jobs {
nodes {
name
groupName
stage
needs {
nodes {
name
}
}
}
}
}
}
}
}
}
}
)
end
before do
post_graphql_query
end
it_behaves_like 'a working graphql query'
it 'returns the correct structure' do
response_stages = graphql_data['ciConfig']['stages']['nodes']
response_groups = response_stages.map { |stage| stage['groups']['nodes'] }.flatten
response_jobs = response_groups.first['jobs']['nodes']
response_needs = response_groups.last['jobs']['nodes'].first['needs']['nodes']
expect(graphql_data['ciConfig']['status']).to eq('VALID')
expect(response_stages).to include(
hash_including('name' => 'build'), hash_including('name' => 'test')
)
expect(response_groups).to include(
hash_including('name' => 'rspec', 'size' => 2),
hash_including('name' => 'spinach', 'size' => 1),
hash_including('name' => 'docker', 'size' => 1)
)
expect(response_jobs).to include(
hash_including('groupName' => 'rspec', 'name' => 'rspec 0 1', 'needs' => { 'nodes' => [] }, 'stage' => 'build'),
hash_including('groupName' => 'rspec', 'name' => 'rspec 0 2', 'needs' => { 'nodes' => [] }, 'stage' => 'build')
)
expect(response_needs).to include(
hash_including('name' => 'rspec 0 1'), hash_including('name' => 'spinach')
)
end
end
rspec 0 1:
stage: build
script: 'rake spec'
needs: []
rspec 0 2:
stage: build
script: 'rake spec'
needs: []
spinach:
stage: build
script: 'rake spinach'
needs: []
docker:
stage: test
script: 'curl http://dockerhub/URL'
needs: [spinach, rspec 0 1]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment