2019-07-11 04:06:35 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
return if Rails.env.production?
|
|
|
|
|
2019-10-31 23:06:26 -04:00
|
|
|
require 'graphql/rake_task'
|
|
|
|
|
2019-07-11 04:06:35 -04:00
|
|
|
namespace :gitlab do
|
2019-09-10 04:11:43 -04:00
|
|
|
OUTPUT_DIR = Rails.root.join("doc/api/graphql/reference")
|
|
|
|
TEMPLATES_DIR = 'lib/gitlab/graphql/docs/templates/'
|
2019-07-11 04:06:35 -04:00
|
|
|
|
2020-03-12 05:09:55 -04:00
|
|
|
# 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
|
2020-02-19 10:09:09 -05:00
|
|
|
class Feature
|
|
|
|
def self.enabled?(*args)
|
2020-03-12 05:09:55 -04:00
|
|
|
true
|
2020-02-19 10:09:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-31 23:06:26 -04:00
|
|
|
# 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-03-12 05:09:55 -04:00
|
|
|
dependencies: [:environment, :enable_feature_flags],
|
2019-10-31 23:06:26 -04:00
|
|
|
directory: OUTPUT_DIR,
|
|
|
|
idl_outfile: "gitlab_schema.graphql",
|
|
|
|
json_outfile: "gitlab_schema.json"
|
|
|
|
)
|
|
|
|
|
2019-07-11 04:06:35 -04:00
|
|
|
namespace :graphql do
|
2020-01-23 01:08:32 -05:00
|
|
|
desc 'GitLab | GraphQL | Generate GraphQL docs'
|
2020-03-12 05:09:55 -04:00
|
|
|
task compile_docs: [:environment, :enable_feature_flags] do
|
2019-07-11 04:06:35 -04:00
|
|
|
renderer = Gitlab::Graphql::Docs::Renderer.new(GitlabSchema.graphql_definition, render_options)
|
|
|
|
|
2019-10-16 14:08:01 -04:00
|
|
|
renderer.write
|
2019-07-11 04:06:35 -04:00
|
|
|
|
|
|
|
puts "Documentation compiled."
|
|
|
|
end
|
2019-10-16 14:08:01 -04:00
|
|
|
|
2020-01-23 01:08:32 -05:00
|
|
|
desc 'GitLab | GraphQL | Check if GraphQL docs are up to date'
|
2020-03-12 05:09:55 -04:00
|
|
|
task check_docs: [:environment, :enable_feature_flags] do
|
2019-10-16 14:08:01 -04:00
|
|
|
renderer = Gitlab::Graphql::Docs::Renderer.new(GitlabSchema.graphql_definition, render_options)
|
|
|
|
|
|
|
|
doc = File.read(Rails.root.join(OUTPUT_DIR, 'index.md'))
|
|
|
|
|
|
|
|
if doc == renderer.contents
|
|
|
|
puts "GraphQL documentation is up to date"
|
|
|
|
else
|
2019-10-31 23:06:26 -04:00
|
|
|
format_output('GraphQL documentation is outdated! Please update it by running `bundle exec rake gitlab:graphql:compile_docs`.')
|
|
|
|
abort
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-23 01:08:32 -05:00
|
|
|
desc 'GitLab | GraphQL | Check if GraphQL schemas are up to date'
|
2020-03-12 05:09:55 -04:00
|
|
|
task check_schema: [:environment, :enable_feature_flags] do
|
2019-10-31 23:06:26 -04:00
|
|
|
idl_doc = File.read(Rails.root.join(OUTPUT_DIR, 'gitlab_schema.graphql'))
|
|
|
|
json_doc = File.read(Rails.root.join(OUTPUT_DIR, 'gitlab_schema.json'))
|
|
|
|
|
|
|
|
if idl_doc == GitlabSchema.to_definition && json_doc == GitlabSchema.to_json
|
|
|
|
puts "GraphQL schema is up to date"
|
|
|
|
else
|
|
|
|
format_output('GraphQL schema is outdated! Please update it by running `bundle exec rake gitlab:graphql:schema:dump`.')
|
2019-10-16 14:08:01 -04:00
|
|
|
abort
|
|
|
|
end
|
|
|
|
end
|
2019-07-11 04:06:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_options
|
|
|
|
{
|
|
|
|
output_dir: OUTPUT_DIR,
|
|
|
|
template: Rails.root.join(TEMPLATES_DIR, 'default.md.haml')
|
|
|
|
}
|
|
|
|
end
|
2019-10-31 23:06:26 -04:00
|
|
|
|
|
|
|
def format_output(str)
|
|
|
|
heading = '#' * 10
|
|
|
|
puts heading
|
|
|
|
puts '#'
|
|
|
|
puts "# #{str}"
|
|
|
|
puts '#'
|
|
|
|
puts heading
|
|
|
|
end
|