Commit b0f9827d authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

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

Adds ci config linting resolver

See merge request gitlab-org/gitlab!46912
parents 30f1d083 4a285e4d
# 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
response = if result.errors.empty?
{
status: :valid,
errors: [],
stages: make_stages(result.jobs)
}
else
{
status: :invalid,
errors: result.errors
}
end
response.merge(merged_yaml: result.merged_yaml)
end
private
def make_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: job.dig(:needs, :job) || []
}
end
end
def make_groups(job_data)
jobs = make_jobs(job_data)
jobs_by_group = jobs.group_by { |job| job[:group_name] }
jobs_by_group.map do |name, jobs|
{ jobs: jobs, name: name, stage: jobs.first[:stage], size: jobs.size }
end
end
def make_stages(jobs)
make_groups(jobs)
.group_by { |group| group[:stage] }
.map { |name, groups| { name: name, groups: groups } }
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], 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], 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], 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], 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', 'The configuration file is valid', value: :valid
value 'INVALID', 'The configuration file is not valid', value: :invalid
end
end
end
end
......@@ -91,6 +91,11 @@ module Types
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. Should not be requested more than once per request.',
resolver: Resolvers::Ci::ConfigResolver,
complexity: 126 # AUTHENTICATED_COMPLEXITY / 2 + 1
def design_management
DesignManagementObject.new(nil)
end
......
---
title: 'Expose GraphQL resolver for processing CI config'
merge_request: 46912
author:
type: added
......@@ -2237,6 +2237,101 @@ type BurnupChartDailyTotals {
scopeWeight: Int!
}
type CiConfig {
"""
Linting errors
"""
errors: [String!]
"""
Merged CI config YAML
"""
mergedYaml: String
"""
Stages of the pipeline
"""
stages: [CiConfigStage!]
"""
Status of linting, can be either valid or invalid
"""
status: CiConfigStatus
}
type CiConfigGroup {
"""
Jobs in group
"""
jobs: [CiConfigJob!]
"""
Name of the job group
"""
name: String
"""
Size of the job group
"""
size: Int
}
type CiConfigJob {
"""
Name of the job group
"""
groupName: String
"""
Name of the job
"""
name: String
"""
Builds that must complete before the jobs run
"""
needs: [CiConfigNeed!]
"""
Name of the job stage
"""
stage: String
}
type CiConfigNeed {
"""
Name of the need
"""
name: String
}
type CiConfigStage {
"""
Groups of jobs for the stage
"""
groups: [CiConfigGroup!]
"""
Name of the stage
"""
name: String
}
"""
Values for YAML processor result
"""
enum CiConfigStatus {
"""
The configuration file is not valid
"""
INVALID
"""
The configuration file is valid
"""
VALID
}
type CiGroup {
"""
Detailed status of the group
......@@ -17905,6 +18000,16 @@ type PromoteToEpicPayload {
}
type Query {
"""
Get linted and processed contents of a CI config. Should not be requested more than once per request.
"""
ciConfig(
"""
Contents of .gitlab-ci.yml
"""
content: String!
): CiConfig
"""
Find a container repository
"""
......
......@@ -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` | CiConfigStage! => Array | Stages of the pipeline |
| `status` | CiConfigStatus | Status of linting, can be either valid or invalid |
### CiConfigGroup
| Field | Type | Description |
| ----- | ---- | ----------- |
| `jobs` | CiConfigJob! => Array | 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` | CiConfigNeed! => Array | 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` | CiConfigGroup! => Array | Groups of jobs for the stage |
| `name` | String | Name of the stage |
### CiGroup
| Field | Type | Description |
......@@ -3925,6 +3964,15 @@ Types of blob viewers.
| `rich` | |
| `simple` | |
### CiConfigStatus
Values for YAML processor result.
| Value | Description |
| ----- | ----------- |
| `INVALID` | The configuration file is not valid |
| `VALID` | The configuration file is valid |
### 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 {
name
groups {
name
size
jobs {
name
groupName
stage
needs {
name
}
}
}
}
}
}
)
end
before do
post_graphql_query
end
it_behaves_like 'a working graphql query'
it 'returns the correct structure' do
expect(graphql_data['ciConfig']).to eq(
"status" => "VALID",
"errors" => [],
"stages" =>
[
{
"name" => "build",
"groups" =>
[
{
"name" => "rspec",
"size" => 2,
"jobs" =>
[
{ "name" => "rspec 0 1", "groupName" => "rspec", "stage" => "build", "needs" => [] },
{ "name" => "rspec 0 2", "groupName" => "rspec", "stage" => "build", "needs" => [] }
]
},
{
"name" => "spinach", "size" => 1, "jobs" =>
[
{ "name" => "spinach", "groupName" => "spinach", "stage" => "build", "needs" => [] }
]
}
]
},
{
"name" => "test",
"groups" =>
[
{
"name" => "docker",
"size" => 1,
"jobs" => [
{ "name" => "docker", "groupName" => "docker", "stage" => "test", "needs" => [{ "name" => "spinach" }, { "name" => "rspec 0 1" }] }
]
}
]
}
]
)
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]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment