From 128a5e410f4c51bbbcb1540435d3bea0b9a9c04d Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 19 Dec 2018 16:16:36 +0100 Subject: [PATCH 1/7] Expose method that returns GitLab API paths --- lib/api/api.rb | 8 ++++++++ spec/lib/api/api_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 spec/lib/api/api_spec.rb diff --git a/lib/api/api.rb b/lib/api/api.rb index 19da0b2c434..f9bb1201472 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -10,6 +10,14 @@ 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/spec/lib/api/api_spec.rb b/spec/lib/api/api_spec.rb new file mode 100644 index 00000000000..31881551980 --- /dev/null +++ b/spec/lib/api/api_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' + +describe API::API do + describe '.prefix' do + it 'has a prefix defined' do + expect(described_class.prefix).to eq :api + end + end + + describe '.version' do + it 'uses most recent version of the API' do + expect(described_class.version).to eq 'v4' + end + end + + 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 + end + end +end From f10fe3ae97ade4ad73963b86c9ab2d7e4e021c61 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 19 Dec 2018 19:44:32 +0100 Subject: [PATCH 2/7] 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 } From fb998f2edf41684a4d3f4a8a258f26a6fb08e193 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 20 Dec 2018 10:54:07 +0100 Subject: [PATCH 3/7] Expose CI_API_V4_URL predefined CI/CD variable --- app/models/project.rb | 2 +- spec/models/ci/build_spec.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/project.rb b/app/models/project.rb index 6052febe774..d829d976afc 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1700,7 +1700,7 @@ class Project < ActiveRecord::Base .append(key: 'CI_PROJECT_VISIBILITY', value: visibility) .concat(container_registry_variables) .concat(auto_devops_variables) - # .concat(api_variables) + .concat(api_variables) end def api_variables diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 7baf4d93804..28b1a1e37e5 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -2132,6 +2132,7 @@ describe Ci::Build do { key: 'CI_PROJECT_NAMESPACE', value: project.namespace.full_path, public: true }, { key: 'CI_PROJECT_URL', value: project.web_url, public: true }, { key: 'CI_PROJECT_VISIBILITY', value: 'private', public: true }, + { key: 'CI_API_V4_URL', value: 'http://localhost/api/v4', public: true }, { key: 'CI_PIPELINE_IID', value: pipeline.iid.to_s, public: true }, { key: 'CI_CONFIG_PATH', value: pipeline.ci_yaml_file_path, public: true }, { key: 'CI_PIPELINE_SOURCE', value: pipeline.source, public: true }, From e39e9ae90f3f99b8b79db4c5c44e36e862d28de2 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 20 Dec 2018 10:55:51 +0100 Subject: [PATCH 4/7] Add changelog for new predefined CI/CD variable --- .../unreleased/feature-gb-expose-ci-api-url-variable.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelogs/unreleased/feature-gb-expose-ci-api-url-variable.yml diff --git a/changelogs/unreleased/feature-gb-expose-ci-api-url-variable.yml b/changelogs/unreleased/feature-gb-expose-ci-api-url-variable.yml new file mode 100644 index 00000000000..0b7fffa70d4 --- /dev/null +++ b/changelogs/unreleased/feature-gb-expose-ci-api-url-variable.yml @@ -0,0 +1,5 @@ +--- +title: Expose CI/CD predefined variable +merge_request: 23936 +author: +type: added From 5e99a077ec05d970e6102dac7a0dc0cfa98a04e6 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 20 Dec 2018 10:58:53 +0100 Subject: [PATCH 5/7] Add new docs for CI_API_V4_URL CI/CD variable --- doc/ci/variables/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/ci/variables/README.md b/doc/ci/variables/README.md index 209a2c15d90..ed0adc5414b 100644 --- a/doc/ci/variables/README.md +++ b/doc/ci/variables/README.md @@ -80,7 +80,8 @@ future GitLab releases.** | **CI_MERGE_REQUEST_TARGET_BRANCH_NAME** | 11.6 | all | The target branch name of the merge request if it's [pipelines for merge requests](../merge_request_pipelines/index.md) | | **CI_NODE_INDEX** | 11.5 | all | Index of the job in the job set. If the job is not parallelized, this variable is not set. | | **CI_NODE_TOTAL** | 11.5 | all | Total number of instances of this job running in parallel. If the job is not parallelized, this variable is set to `1`. | -| **CI_PIPELINE_ID** | 8.10 | 0.5 | The unique id of the current pipeline that GitLab CI uses internally | +| **CI_API_V4_URL** | 11.7 | all | The GitLab API v4 root URL | +| **CI_PIPELINE_ID** | 8.10 | all | The unique id of the current pipeline that GitLab CI uses internally | | **CI_PIPELINE_IID** | 11.0 | all | The unique id of the current pipeline scoped to project | | **CI_PIPELINE_SOURCE** | 10.0 | all | Indicates how the pipeline was triggered. Possible options are: `push`, `web`, `trigger`, `schedule`, `api`, and `pipeline`. For pipelines created before GitLab 9.5, this will show as `unknown` | | **CI_PIPELINE_TRIGGERED** | all | all | The flag to indicate that job was [triggered] | From 476cba6ff355197ca7137af1fcab74a7d6c776ad Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 3 Jan 2019 14:09:53 +0100 Subject: [PATCH 6/7] Minor improvements to CI_API_V4_URL variable exposure --- changelogs/unreleased/feature-gb-expose-ci-api-url-variable.yml | 2 +- lib/api/helpers/version.rb | 2 +- spec/models/project_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/changelogs/unreleased/feature-gb-expose-ci-api-url-variable.yml b/changelogs/unreleased/feature-gb-expose-ci-api-url-variable.yml index 0b7fffa70d4..19dc615c5f8 100644 --- a/changelogs/unreleased/feature-gb-expose-ci-api-url-variable.yml +++ b/changelogs/unreleased/feature-gb-expose-ci-api-url-variable.yml @@ -1,5 +1,5 @@ --- -title: Expose CI/CD predefined variable +title: Expose CI/CD predefined variable `CI_API_V4_URL` merge_request: 23936 author: type: added diff --git a/lib/api/helpers/version.rb b/lib/api/helpers/version.rb index 0068002003e..a322d99e9aa 100644 --- a/lib/api/helpers/version.rb +++ b/lib/api/helpers/version.rb @@ -18,7 +18,7 @@ module API end def root_url - expose_url(root_path) + @root_url ||= expose_url(root_path) end def to_s diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 9c098af7c56..c1186a00d4e 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -3504,7 +3504,7 @@ describe Project do expect(project.api_variables.first[:value]).to include '/api/v4' end - it 'contains an URL variable for every supported API version' do + it 'contains a 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 From 8707827d3925441a838ba425e65dfdb8d3845a31 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Fri, 4 Jan 2019 15:32:40 +0100 Subject: [PATCH 7/7] Improve readablity of CI_API_V4_URL related code --- app/models/project.rb | 4 +--- lib/api/helpers/version.rb | 2 +- spec/lib/api/helpers/version_spec.rb | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index d829d976afc..1109cb4383f 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1705,9 +1705,7 @@ class Project < ActiveRecord::Base 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 + variables.append(key: 'CI_API_V4_URL', value: API::Helpers::Version.new('v4').root_url) end end diff --git a/lib/api/helpers/version.rb b/lib/api/helpers/version.rb index a322d99e9aa..7f53094e90c 100644 --- a/lib/api/helpers/version.rb +++ b/lib/api/helpers/version.rb @@ -18,7 +18,7 @@ module API end def root_url - @root_url ||= expose_url(root_path) + @root_url ||= expose_url(root_path) end def to_s diff --git a/spec/lib/api/helpers/version_spec.rb b/spec/lib/api/helpers/version_spec.rb index 63e7e1e6e95..34006e0930b 100644 --- a/spec/lib/api/helpers/version_spec.rb +++ b/spec/lib/api/helpers/version_spec.rb @@ -20,7 +20,7 @@ describe API::Helpers::Version do 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$} + .to eq 'http://localhost/api/v4' end end end