2019-09-30 21:07:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
return if Rails.env.production?
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
require 'graphql/rake_task'
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
namespace :gitlab do
|
2019-12-04 20:38:33 +05:30
|
|
|
OUTPUT_DIR = Rails.root.join("doc/api/graphql/reference")
|
2021-04-17 20:07:23 +05:30
|
|
|
TEMP_SCHEMA_DIR = Rails.root.join('tmp/tests/graphql')
|
2019-12-04 20:38:33 +05:30
|
|
|
TEMPLATES_DIR = 'lib/gitlab/graphql/docs/templates/'
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
# Make all feature flags enabled so that all feature flag
|
|
|
|
# controlled fields are considered visible and are output.
|
|
|
|
# Also avoids pipeline failures in case developer
|
|
|
|
# dumps schema with flags disabled locally before pushing
|
|
|
|
task enable_feature_flags: :environment do
|
|
|
|
class Feature
|
|
|
|
def self.enabled?(*args)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
# Defines tasks for dumping the GraphQL schema:
|
|
|
|
# - gitlab:graphql:schema:dump
|
|
|
|
# - gitlab:graphql:schema:idl
|
|
|
|
# - gitlab:graphql:schema:json
|
|
|
|
GraphQL::RakeTask.new(
|
|
|
|
schema_name: 'GitlabSchema',
|
2020-04-08 14:13:33 +05:30
|
|
|
dependencies: [:environment, :enable_feature_flags],
|
2021-04-17 20:07:23 +05:30
|
|
|
directory: TEMP_SCHEMA_DIR,
|
2019-12-26 22:10:19 +05:30
|
|
|
idl_outfile: "gitlab_schema.graphql",
|
|
|
|
json_outfile: "gitlab_schema.json"
|
|
|
|
)
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
namespace :graphql do
|
2021-03-11 19:13:27 +05:30
|
|
|
desc 'GitLab | GraphQL | Analyze queries'
|
|
|
|
task analyze: [:environment, :enable_feature_flags] do |t, args|
|
|
|
|
queries = if args.to_a.present?
|
|
|
|
args.to_a.flat_map { |path| Gitlab::Graphql::Queries.find(path) }
|
|
|
|
else
|
|
|
|
Gitlab::Graphql::Queries.all
|
|
|
|
end
|
|
|
|
|
|
|
|
queries.each do |defn|
|
|
|
|
$stdout.puts defn.file
|
|
|
|
summary, errs = defn.validate(GitlabSchema)
|
|
|
|
|
|
|
|
if summary == :client_query
|
|
|
|
$stdout.puts " - client query"
|
|
|
|
elsif errs.present?
|
|
|
|
$stdout.puts " - invalid query".color(:red)
|
|
|
|
else
|
|
|
|
complexity = defn.complexity(GitlabSchema)
|
|
|
|
color = case complexity
|
|
|
|
when 0..GitlabSchema::DEFAULT_MAX_COMPLEXITY
|
|
|
|
:green
|
|
|
|
when GitlabSchema::DEFAULT_MAX_COMPLEXITY..GitlabSchema::AUTHENTICATED_COMPLEXITY
|
|
|
|
:yellow
|
|
|
|
when GitlabSchema::AUTHENTICATED_COMPLEXITY..GitlabSchema::ADMIN_COMPLEXITY
|
|
|
|
:orange
|
|
|
|
else
|
|
|
|
:red
|
|
|
|
end
|
|
|
|
|
|
|
|
$stdout.puts " - complexity: #{complexity}".color(color)
|
|
|
|
end
|
|
|
|
|
|
|
|
$stdout.puts ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'GitLab | GraphQL | Validate queries'
|
2021-03-08 18:12:59 +05:30
|
|
|
task validate: [:environment, :enable_feature_flags] do |t, args|
|
|
|
|
queries = if args.to_a.present?
|
|
|
|
args.to_a.flat_map { |path| Gitlab::Graphql::Queries.find(path) }
|
|
|
|
else
|
|
|
|
Gitlab::Graphql::Queries.all
|
|
|
|
end
|
|
|
|
|
|
|
|
failed = queries.flat_map do |defn|
|
|
|
|
summary, errs = defn.validate(GitlabSchema)
|
|
|
|
|
|
|
|
case summary
|
|
|
|
when :client_query
|
|
|
|
warn("SKIP #{defn.file}: client query")
|
|
|
|
else
|
2021-03-11 19:13:27 +05:30
|
|
|
warn("#{'OK'.color(:green)} #{defn.file}") if errs.empty?
|
2021-03-08 18:12:59 +05:30
|
|
|
errs.each do |err|
|
|
|
|
warn(<<~MSG)
|
2021-03-11 19:13:27 +05:30
|
|
|
#{'ERROR'.color(:red)} #{defn.file}: #{err.message} (at #{err.path.join('.')})
|
2021-03-08 18:12:59 +05:30
|
|
|
MSG
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
errs.empty? ? [] : [defn.file]
|
|
|
|
end
|
|
|
|
|
|
|
|
if failed.present?
|
|
|
|
format_output(
|
|
|
|
"#{failed.count} GraphQL #{'query'.pluralize(failed.count)} out of #{queries.count} failed validation:",
|
|
|
|
*failed.map do |name|
|
|
|
|
known_failure = Gitlab::Graphql::Queries.known_failure?(name)
|
|
|
|
"- #{name}" + (known_failure ? ' (known failure)' : '')
|
|
|
|
end
|
|
|
|
)
|
|
|
|
abort unless failed.all? { |name| Gitlab::Graphql::Queries.known_failure?(name) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
desc 'GitLab | GraphQL | Generate GraphQL docs'
|
2020-04-08 14:13:33 +05:30
|
|
|
task compile_docs: [:environment, :enable_feature_flags] do
|
2021-04-29 21:17:54 +05:30
|
|
|
renderer = Gitlab::Graphql::Docs::Renderer.new(GitlabSchema, render_options)
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
renderer.write
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
puts "Documentation compiled."
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
desc 'GitLab | GraphQL | Check if GraphQL docs are up to date'
|
2020-04-08 14:13:33 +05:30
|
|
|
task check_docs: [:environment, :enable_feature_flags] do
|
2021-04-29 21:17:54 +05:30
|
|
|
renderer = Gitlab::Graphql::Docs::Renderer.new(GitlabSchema, render_options)
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
doc = File.read(Rails.root.join(OUTPUT_DIR, 'index.md'))
|
|
|
|
|
|
|
|
if doc == renderer.contents
|
|
|
|
puts "GraphQL documentation is up to date"
|
|
|
|
else
|
2019-12-26 22:10:19 +05:30
|
|
|
format_output('GraphQL documentation is outdated! Please update it by running `bundle exec rake gitlab:graphql:compile_docs`.')
|
|
|
|
abort
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
desc 'GitLab | GraphQL | Update GraphQL docs and schema'
|
|
|
|
task update_all: [:compile_docs, 'schema:dump']
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_options
|
|
|
|
{
|
|
|
|
output_dir: OUTPUT_DIR,
|
|
|
|
template: Rails.root.join(TEMPLATES_DIR, 'default.md.haml')
|
|
|
|
}
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def format_output(*strs)
|
2019-12-26 22:10:19 +05:30
|
|
|
heading = '#' * 10
|
|
|
|
puts heading
|
|
|
|
puts '#'
|
2021-03-08 18:12:59 +05:30
|
|
|
strs.each { |str| puts "# #{str}" }
|
2019-12-26 22:10:19 +05:30
|
|
|
puts '#'
|
|
|
|
puts heading
|
|
|
|
end
|