summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2019-01-04 17:29:01 +0000
committerDouwe Maan <douwe@gitlab.com>2019-01-04 17:29:01 +0000
commit303f049358645312c6f77d08750e489475494201 (patch)
tree1a0a80849d9c711dac76b0344b37d17146029be5
parentd85e04e45247a64c704a44a2c8abecba94972061 (diff)
parent8707827d3925441a838ba425e65dfdb8d3845a31 (diff)
downloadgitlab-ce-303f049358645312c6f77d08750e489475494201.tar.gz
Merge branch 'feature/gb/expose-ci-api-url-variable' into 'master'
Expose `CI_API_V4_URL` CI/CD variable Closes #54621 See merge request gitlab-org/gitlab-ce!23936
-rw-r--r--app/models/project.rb7
-rw-r--r--changelogs/unreleased/feature-gb-expose-ci-api-url-variable.yml5
-rw-r--r--doc/ci/variables/README.md3
-rw-r--r--lib/api/helpers/version.rb29
-rw-r--r--spec/lib/api/api_spec.rb21
-rw-r--r--spec/lib/api/helpers/version_spec.rb26
-rw-r--r--spec/models/ci/build_spec.rb1
-rw-r--r--spec/models/project_spec.rb24
8 files changed, 114 insertions, 2 deletions
diff --git a/app/models/project.rb b/app/models/project.rb
index 58b10662ff0..e2f010a0432 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -1705,6 +1705,13 @@ 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|
+ variables.append(key: 'CI_API_V4_URL', value: API::Helpers::Version.new('v4').root_url)
+ end
end
def container_registry_variables
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..19dc615c5f8
--- /dev/null
+++ b/changelogs/unreleased/feature-gb-expose-ci-api-url-variable.yml
@@ -0,0 +1,5 @@
+---
+title: Expose CI/CD predefined variable `CI_API_V4_URL`
+merge_request: 23936
+author:
+type: added
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] |
diff --git a/lib/api/helpers/version.rb b/lib/api/helpers/version.rb
new file mode 100644
index 00000000000..7f53094e90c
--- /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
+ @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
new file mode 100644
index 00000000000..ceef0b41e59
--- /dev/null
+++ b/spec/lib/api/api_spec.rb
@@ -0,0 +1,21 @@
+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 %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..34006e0930b
--- /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 eq 'http://localhost/api/v4'
+ end
+ end
+end
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 },
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index 2170ef1f155..72284035b57 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -3525,7 +3525,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 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
+
+ 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 }