debian-mirror-gitlab/app/graphql/resolvers/ci/config_resolver.rb

101 lines
2.8 KiB
Ruby
Raw Normal View History

2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
module Resolvers
module Ci
class ConfigResolver < BaseResolver
2021-03-08 18:12:59 +05:30
include Gitlab::Graphql::Authorize::AuthorizeResource
include ResolvesProject
2021-02-22 17:27:13 +05:30
type Types::Ci::Config::ConfigType, null: true
2021-04-29 21:17:54 +05:30
description <<~MD
Linted and processed contents of a CI config.
Should not be requested more than once per request.
MD
2021-02-22 17:27:13 +05:30
2021-03-08 18:12:59 +05:30
authorize :read_pipeline
2021-10-27 15:23:28 +05:30
argument :project_path, GraphQL::Types::ID,
2021-03-08 18:12:59 +05:30
required: true,
2021-10-27 15:23:28 +05:30
description: 'Project of the CI config.'
2021-03-08 18:12:59 +05:30
2021-10-27 15:23:28 +05:30
argument :sha, GraphQL::Types::String,
2021-09-30 23:02:18 +05:30
required: false,
description: "Sha for the pipeline."
2021-10-27 15:23:28 +05:30
argument :content, GraphQL::Types::String,
2021-02-22 17:27:13 +05:30
required: true,
2021-04-17 20:07:23 +05:30
description: "Contents of `.gitlab-ci.yml`."
2021-03-08 18:12:59 +05:30
2021-10-27 15:23:28 +05:30
argument :dry_run, GraphQL::Types::Boolean,
2021-03-08 18:12:59 +05:30
required: false,
description: 'Run pipeline creation simulation, or only do static check.'
2021-09-30 23:02:18 +05:30
def resolve(project_path:, content:, sha: nil, dry_run: false)
2021-03-08 18:12:59 +05:30
project = authorized_find!(project_path: project_path)
result = ::Gitlab::Ci::Lint
2021-09-30 23:02:18 +05:30
.new(project: project, current_user: context[:current_user], sha: sha)
2021-03-08 18:12:59 +05:30
.validate(content, dry_run: dry_run)
2021-03-11 19:13:27 +05:30
response(result).merge(merged_yaml: result.merged_yaml)
end
private
def response(result)
2021-03-08 18:12:59 +05:30
if result.errors.empty?
{
status: :valid,
errors: [],
stages: make_stages(result.jobs)
}
else
{
status: :invalid,
errors: result.errors
}
end
2021-02-22 17:27:13 +05:30
end
def make_jobs(config_jobs)
2021-03-08 18:12:59 +05:30
config_jobs.map do |job|
2021-02-22 17:27:13 +05:30
{
2021-03-08 18:12:59 +05:30
name: job[:name],
2021-02-22 17:27:13 +05:30
stage: job[:stage],
2021-03-08 18:12:59 +05:30
group_name: CommitStatus.new(name: job[:name]).group_name,
2021-04-29 21:17:54 +05:30
needs: job[:needs] || [],
2021-03-08 18:12:59 +05:30
allow_failure: job[:allow_failure],
before_script: job[:before_script],
script: job[:script],
after_script: job[:after_script],
only: job[:only],
except: job[:except],
when: job[:when],
tags: job[:tag_list],
environment: job[:environment]
2021-02-22 17:27:13 +05:30
}
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
2021-03-08 18:12:59 +05:30
def find_object(project_path:)
resolve_project(full_path: project_path)
end
2021-02-22 17:27:13 +05:30
end
end
end