From f10fe3ae97ade4ad73963b86c9ab2d7e4e021c61 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 19 Dec 2018 19:44:32 +0100 Subject: [PATCH] Add API::Helpers::Version and expose API root URL This commits adds a new class that is supposed to represent Grape API version, like `v3` or `v4`. --- app/models/project.rb | 9 +++++++++ lib/api/api.rb | 8 -------- lib/api/helpers/version.rb | 29 ++++++++++++++++++++++++++++ spec/lib/api/api_spec.rb | 17 +--------------- spec/lib/api/helpers/version_spec.rb | 26 +++++++++++++++++++++++++ spec/models/project_spec.rb | 24 ++++++++++++++++++++++- 6 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 lib/api/helpers/version.rb create mode 100644 spec/lib/api/helpers/version_spec.rb diff --git a/app/models/project.rb b/app/models/project.rb index cd558752080..6052febe774 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1700,6 +1700,15 @@ class Project < ActiveRecord::Base .append(key: 'CI_PROJECT_VISIBILITY', value: visibility) .concat(container_registry_variables) .concat(auto_devops_variables) + # .concat(api_variables) + end + + def api_variables + Gitlab::Ci::Variables::Collection.new.tap do |variables| + API::Helpers::Version.new('v4').tap do |version| + variables.append(key: 'CI_API_V4_URL', value: version.root_url) + end + end end def container_registry_variables diff --git a/lib/api/api.rb b/lib/api/api.rb index f9bb1201472..19da0b2c434 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -10,14 +10,6 @@ module API NAMESPACE_OR_PROJECT_REQUIREMENTS = { id: NO_SLASH_URL_PART_REGEX }.freeze COMMIT_ENDPOINT_REQUIREMENTS = NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(sha: NO_SLASH_URL_PART_REGEX).freeze - def self.root_path(version: 'v4') - unless versions.include?(version) - raise ArgumentError, 'Unknown API version!' - end - - File.join('/', prefix.to_s, version.to_s) - end - insert_before Grape::Middleware::Error, GrapeLogging::Middleware::RequestLogger, logger: Logger.new(LOG_FILENAME), diff --git a/lib/api/helpers/version.rb b/lib/api/helpers/version.rb new file mode 100644 index 00000000000..0068002003e --- /dev/null +++ b/lib/api/helpers/version.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module API + module Helpers + class Version + include Helpers::RelatedResourcesHelpers + + def initialize(version) + @version = version.to_s + + unless API.versions.include?(version) + raise ArgumentError, 'Unknown API version!' + end + end + + def root_path + File.join('/', API.prefix.to_s, @version) + end + + def root_url + expose_url(root_path) + end + + def to_s + @version + end + end + end +end diff --git a/spec/lib/api/api_spec.rb b/spec/lib/api/api_spec.rb index 31881551980..ceef0b41e59 100644 --- a/spec/lib/api/api_spec.rb +++ b/spec/lib/api/api_spec.rb @@ -15,22 +15,7 @@ describe API::API do describe '.versions' do it 'returns all available versions' do - expect(described_class.versions).to eq ['v3', 'v4'] - end - end - - describe '.root_path' do - it 'returns predefined API version path' do - expect(described_class.root_path).to eq '/api/v4' - end - - it 'returns a version provided as keyword argument' do - expect(described_class.root_path(version: 'v3')).to eq '/api/v3' - end - - it 'raises an error if version is not known' do - expect { described_class.root_path(version: 'v10') } - .to raise_error ArgumentError + expect(described_class.versions).to eq %w[v3 v4] end end end diff --git a/spec/lib/api/helpers/version_spec.rb b/spec/lib/api/helpers/version_spec.rb new file mode 100644 index 00000000000..63e7e1e6e95 --- /dev/null +++ b/spec/lib/api/helpers/version_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe API::Helpers::Version do + describe '.new' do + it 'is possible to initialize it with existing API version' do + expect(described_class.new('v4').to_s).to eq 'v4' + end + + it 'raises an error when unsupported API version is provided' do + expect { described_class.new('v111') }.to raise_error ArgumentError + end + end + + describe '#root_path' do + it 'returns a root path of the API version' do + expect(described_class.new('v4').root_path).to eq '/api/v4' + end + end + + describe '#root_url' do + it 'returns an URL for a root path for the API version' do + expect(described_class.new('v4').root_url) + .to match %r{^http?://.*/api/v4$} + end + end +end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 4b6592020c1..9c098af7c56 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -3496,7 +3496,29 @@ describe Project do end end - context '#auto_devops_variables' do + describe '#api_variables' do + set(:project) { create(:project) } + + it 'exposes API v4 URL' do + expect(project.api_variables.first[:key]).to eq 'CI_API_V4_URL' + expect(project.api_variables.first[:value]).to include '/api/v4' + end + + it 'contains an URL variable for every supported API version' do + supported_versions = API::API.versions.select do |version| + API::API.routes.select { |route| route.version == version }.many? + end + + required_variables = supported_versions.map do |version| + "CI_API_#{version.upcase}_URL" + end + + expect(project.api_variables.map { |variable| variable[:key] }) + .to contain_exactly(*required_variables) + end + end + + describe '#auto_devops_variables' do set(:project) { create(:project) } subject { project.auto_devops_variables }