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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
2.9 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
2022-07-01 11:34:44 +05:30
authorize :create_pipeline
2021-03-08 18:12:59 +05:30
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)
2022-07-16 23:28:13 +05:30
response(result)
2022-08-27 11:52:29 +05:30
rescue GRPC::InvalidArgument => e
Gitlab::ErrorTracking.track_and_raise_exception(e, sha: sha)
2021-03-11 19:13:27 +05:30
end
private
def response(result)
2022-07-16 23:28:13 +05:30
{
status: result.status,
errors: result.errors,
warnings: result.warnings,
stages: make_stages(result),
merged_yaml: result.merged_yaml,
includes: result.includes
}
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
2022-07-16 23:28:13 +05:30
def make_stages(result)
return [] unless result.valid?
make_groups(result.jobs)
2021-02-22 17:27:13 +05:30
.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