From 21779d00186d75349165d6c07dbe04aace68017c Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Thu, 24 Jan 2019 16:23:57 +0000 Subject: Add metadata about the GitLab server to GraphQL --- app/graphql/resolvers/metadata_resolver.rb | 11 ++++++++ app/graphql/types/metadata_type.rb | 10 +++++++ app/graphql/types/query_type.rb | 10 ++++++- app/policies/global_policy.rb | 4 +++ .../unreleased/56809-graphql-version-api.yml | 5 ++++ lib/api/helpers/graphql_helpers.rb | 22 +++++++++++++++ lib/api/version.rb | 18 +++++++++++- spec/graphql/resolvers/metadata_resolver_spec.rb | 11 ++++++++ spec/graphql/types/metadata_type_spec.rb | 5 ++++ spec/graphql/types/query_type_spec.rb | 15 +++++++++- spec/policies/global_policy_spec.rb | 12 ++++++++ spec/requests/api/graphql/metadata_query_spec.rb | 32 ++++++++++++++++++++++ spec/requests/api/version_spec.rb | 18 +++++++++++- spec/support/helpers/graphql_helpers.rb | 3 +- 14 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 app/graphql/resolvers/metadata_resolver.rb create mode 100644 app/graphql/types/metadata_type.rb create mode 100644 changelogs/unreleased/56809-graphql-version-api.yml create mode 100644 lib/api/helpers/graphql_helpers.rb create mode 100644 spec/graphql/resolvers/metadata_resolver_spec.rb create mode 100644 spec/graphql/types/metadata_type_spec.rb create mode 100644 spec/requests/api/graphql/metadata_query_spec.rb diff --git a/app/graphql/resolvers/metadata_resolver.rb b/app/graphql/resolvers/metadata_resolver.rb new file mode 100644 index 00000000000..3a79e6434fb --- /dev/null +++ b/app/graphql/resolvers/metadata_resolver.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Resolvers + class MetadataResolver < BaseResolver + type Types::MetadataType, null: false + + def resolve(**args) + { version: Gitlab::VERSION, revision: Gitlab.revision } + end + end +end diff --git a/app/graphql/types/metadata_type.rb b/app/graphql/types/metadata_type.rb new file mode 100644 index 00000000000..2d8bad0614b --- /dev/null +++ b/app/graphql/types/metadata_type.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Types + class MetadataType < ::Types::BaseObject + graphql_name 'Metadata' + + field :version, GraphQL::STRING_TYPE, null: false + field :revision, GraphQL::STRING_TYPE, null: false + end +end diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 954bcc0a5a3..472fe5d6ec2 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Types - class QueryType < BaseObject + class QueryType < ::Types::BaseObject graphql_name 'Query' field :project, Types::ProjectType, @@ -10,6 +10,14 @@ module Types description: "Find a project", authorize: :read_project + field :metadata, Types::MetadataType, + null: true, + resolver: Resolvers::MetadataResolver, + description: 'Metadata about GitLab' do |*args| + + authorize :read_instance_metadata + end + field :echo, GraphQL::STRING_TYPE, null: false, function: Functions::Echo.new end end diff --git a/app/policies/global_policy.rb b/app/policies/global_policy.rb index 16c58730878..d412a591fdc 100644 --- a/app/policies/global_policy.rb +++ b/app/policies/global_policy.rb @@ -68,6 +68,10 @@ class GlobalPolicy < BasePolicy enable :read_users_list end + rule { ~anonymous }.policy do + enable :read_instance_metadata + end + rule { admin }.policy do enable :read_custom_attribute enable :update_custom_attribute diff --git a/changelogs/unreleased/56809-graphql-version-api.yml b/changelogs/unreleased/56809-graphql-version-api.yml new file mode 100644 index 00000000000..72a4b4e5819 --- /dev/null +++ b/changelogs/unreleased/56809-graphql-version-api.yml @@ -0,0 +1,5 @@ +--- +title: Add metadata about the GitLab server to GraphQL +merge_request: 24636 +author: +type: added diff --git a/lib/api/helpers/graphql_helpers.rb b/lib/api/helpers/graphql_helpers.rb new file mode 100644 index 00000000000..94010ab1bc2 --- /dev/null +++ b/lib/api/helpers/graphql_helpers.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module API + module Helpers + # GraphqlHelpers is used by the REST API when it is acting like a client + # against the graphql API. Helper code for the graphql server implementation + # should be in app/graphql/ or lib/gitlab/graphql/ + module GraphqlHelpers + def conditionally_graphql!(fallback:, query:, context: {}, transform: nil) + return fallback.call unless Feature.enabled?(:graphql) + + result = GitlabSchema.execute(query, context: context) + + if transform + transform.call(result) + else + result + end + end + end + end +end diff --git a/lib/api/version.rb b/lib/api/version.rb index 74cd857f447..eca1b529094 100644 --- a/lib/api/version.rb +++ b/lib/api/version.rb @@ -2,13 +2,29 @@ module API class Version < Grape::API + helpers ::API::Helpers::GraphqlHelpers + before { authenticate! } + METADATA_QUERY = <<~EOF + { + metadata { + version + revision + } + } + EOF + desc 'Get the version information of the GitLab instance.' do detail 'This feature was introduced in GitLab 8.13.' end get '/version' do - { version: Gitlab::VERSION, revision: Gitlab.revision } + conditionally_graphql!( + query: METADATA_QUERY, + context: { current_user: current_user }, + transform: ->(result) { result.dig('data', 'metadata') }, + fallback: -> { { version: Gitlab::VERSION, revision: Gitlab.revision } } + ) end end end diff --git a/spec/graphql/resolvers/metadata_resolver_spec.rb b/spec/graphql/resolvers/metadata_resolver_spec.rb new file mode 100644 index 00000000000..e662ed127a5 --- /dev/null +++ b/spec/graphql/resolvers/metadata_resolver_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Resolvers::MetadataResolver do + include GraphqlHelpers + + describe '#resolve' do + it 'returns version and revision' do + expect(resolve(described_class)).to eq(version: Gitlab::VERSION, revision: Gitlab.revision) + end + end +end diff --git a/spec/graphql/types/metadata_type_spec.rb b/spec/graphql/types/metadata_type_spec.rb new file mode 100644 index 00000000000..55205bf5b6a --- /dev/null +++ b/spec/graphql/types/metadata_type_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe GitlabSchema.types['Metadata'] do + it { expect(described_class.graphql_name).to eq('Metadata') } +end diff --git a/spec/graphql/types/query_type_spec.rb b/spec/graphql/types/query_type_spec.rb index e1df6f9811d..fd75b8f700b 100644 --- a/spec/graphql/types/query_type_spec.rb +++ b/spec/graphql/types/query_type_spec.rb @@ -5,7 +5,7 @@ describe GitlabSchema.types['Query'] do expect(described_class.graphql_name).to eq('Query') end - it { is_expected.to have_graphql_fields(:project, :echo) } + it { is_expected.to have_graphql_fields(:project, :echo, :metadata) } describe 'project field' do subject { described_class.fields['project'] } @@ -20,4 +20,17 @@ describe GitlabSchema.types['Query'] do is_expected.to require_graphql_authorizations(:read_project) end end + + describe 'metadata field' do + subject { described_class.fields['metadata'] } + + it 'returns metadata' do + is_expected.to have_graphql_type(Types::MetadataType) + is_expected.to have_graphql_resolver(Resolvers::MetadataResolver) + end + + it 'authorizes with log_in' do + is_expected.to require_graphql_authorizations(:read_instance_metadata) + end + end end diff --git a/spec/policies/global_policy_spec.rb b/spec/policies/global_policy_spec.rb index 30d68e7dc9d..12be3927e18 100644 --- a/spec/policies/global_policy_spec.rb +++ b/spec/policies/global_policy_spec.rb @@ -181,6 +181,18 @@ describe GlobalPolicy do end end + describe 'read instance metadata' do + context 'regular user' do + it { is_expected.to be_allowed(:read_instance_metadata) } + end + + context 'anonymous' do + let(:current_user) { nil } + + it { is_expected.not_to be_allowed(:read_instance_metadata) } + end + end + describe 'read instance statistics' do context 'regular user' do it { is_expected.to be_allowed(:read_instance_statistics) } diff --git a/spec/requests/api/graphql/metadata_query_spec.rb b/spec/requests/api/graphql/metadata_query_spec.rb new file mode 100644 index 00000000000..4c56c559cf9 --- /dev/null +++ b/spec/requests/api/graphql/metadata_query_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'getting project information' do + include GraphqlHelpers + + let(:query) { graphql_query_for('metadata', {}, all_graphql_fields_for('Metadata')) } + + context 'logged in' do + it 'returns version and revision' do + post_graphql(query, current_user: create(:user)) + + expect(graphql_errors).to be_nil + expect(graphql_data).to eq( + 'metadata' => { + 'version' => Gitlab::VERSION, + 'revision' => Gitlab.revision + } + ) + end + end + + context 'anonymous user' do + it 'returns nothing' do + post_graphql(query, current_user: nil) + + expect(graphql_errors).to be_nil + expect(graphql_data).to eq('metadata' => nil) + end + end +end diff --git a/spec/requests/api/version_spec.rb b/spec/requests/api/version_spec.rb index 38b618191fb..e06f8bbc095 100644 --- a/spec/requests/api/version_spec.rb +++ b/spec/requests/api/version_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe API::Version do - describe 'GET /version' do + shared_examples_for 'GET /version' do context 'when unauthenticated' do it 'returns authentication error' do get api('/version') @@ -22,4 +22,20 @@ describe API::Version do end end end + + context 'with graphql enabled' do + before do + stub_feature_flags(graphql: true) + end + + include_examples 'GET /version' + end + + context 'with graphql disabled' do + before do + stub_feature_flags(graphql: false) + end + + include_examples 'GET /version' + end end diff --git a/spec/support/helpers/graphql_helpers.rb b/spec/support/helpers/graphql_helpers.rb index e468ee4676d..d9529262483 100644 --- a/spec/support/helpers/graphql_helpers.rb +++ b/spec/support/helpers/graphql_helpers.rb @@ -77,8 +77,9 @@ module GraphqlHelpers def query_graphql_field(name, attributes = {}, fields = nil) fields ||= all_graphql_fields_for(name.classify) attributes = attributes_to_graphql(attributes) + attributes = "(#{attributes})" if attributes.present? <<~QUERY - #{name}(#{attributes}) { + #{name}#{attributes} { #{fields} } QUERY -- cgit v1.2.1 From 01b1076bd869a5bbcbaf6df55e825cc39f705e2a Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Tue, 5 Mar 2019 15:46:00 +0000 Subject: Apply suggestion to spec/graphql/types/query_type_spec.rb --- spec/graphql/types/query_type_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/graphql/types/query_type_spec.rb b/spec/graphql/types/query_type_spec.rb index fd75b8f700b..07c61ea7647 100644 --- a/spec/graphql/types/query_type_spec.rb +++ b/spec/graphql/types/query_type_spec.rb @@ -29,7 +29,7 @@ describe GitlabSchema.types['Query'] do is_expected.to have_graphql_resolver(Resolvers::MetadataResolver) end - it 'authorizes with log_in' do + it 'authorizes with read_instance_metadata' do is_expected.to require_graphql_authorizations(:read_instance_metadata) end end -- cgit v1.2.1