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`.
This commit is contained in:
parent
128a5e410f
commit
f10fe3ae97
|
@ -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
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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 }
|
||||
|
|
Loading…
Reference in New Issue