Commit 3f3f1d62 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'changed-pages-ci-var' into 'master'

Populate CI_MERGE_REQUEST_CHANGED_PAGE_* variables

See merge request gitlab-org/gitlab!25256
parents bc3ce000 af7aeab7
...@@ -531,6 +531,7 @@ module Ci ...@@ -531,6 +531,7 @@ module Ci
.concat(persisted_variables) .concat(persisted_variables)
.concat(scoped_variables) .concat(scoped_variables)
.concat(job_variables) .concat(job_variables)
.concat(environment_changed_page_variables)
.concat(persisted_environment_variables) .concat(persisted_environment_variables)
.to_runner_variables .to_runner_variables
end end
...@@ -569,6 +570,15 @@ module Ci ...@@ -569,6 +570,15 @@ module Ci
end end
end end
def environment_changed_page_variables
Gitlab::Ci::Variables::Collection.new.tap do |variables|
break variables unless environment_status
variables.append(key: 'CI_MERGE_REQUEST_CHANGED_PAGE_PATHS', value: environment_status.changed_paths.join(','))
variables.append(key: 'CI_MERGE_REQUEST_CHANGED_PAGE_URLS', value: environment_status.changed_urls.join(','))
end
end
def deploy_token_variables def deploy_token_variables
Gitlab::Ci::Variables::Collection.new.tap do |variables| Gitlab::Ci::Variables::Collection.new.tap do |variables|
break variables unless gitlab_deploy_token break variables unless gitlab_deploy_token
...@@ -970,6 +980,14 @@ module Ci ...@@ -970,6 +980,14 @@ module Ci
options&.dig(:environment, :url) || persisted_environment&.external_url options&.dig(:environment, :url) || persisted_environment&.external_url
end end
def environment_status
strong_memoize(:environment_status) do
if has_environment? && merge_request
EnvironmentStatus.new(project, persisted_environment, merge_request, pipeline.sha)
end
end
end
# The format of the retry option changed in GitLab 11.5: Before it was # The format of the retry option changed in GitLab 11.5: Before it was
# integer only, after it is a hash. New builds are created with the new # integer only, after it is a hash. New builds are created with the new
# format, but builds created before GitLab 11.5 and saved in database still # format, but builds created before GitLab 11.5 and saved in database still
......
...@@ -62,9 +62,9 @@ class EnvironmentStatus ...@@ -62,9 +62,9 @@ class EnvironmentStatus
end end
def changes def changes
return [] unless has_route_map? strong_memoize(:changes) do
has_route_map? ? changed_files.map { |file| build_change(file) }.compact : []
changed_files.map { |file| build_change(file) }.compact end
end end
def changed_files def changed_files
...@@ -72,6 +72,14 @@ class EnvironmentStatus ...@@ -72,6 +72,14 @@ class EnvironmentStatus
.merge_request_diff_files.where(deleted_file: false) .merge_request_diff_files.where(deleted_file: false)
end end
def changed_paths
changes.map { |change| change[:path] }
end
def changed_urls
changes.map { |change| change[:external_url] }
end
def has_route_map? def has_route_map?
project.route_map_for(sha).present? project.route_map_for(sha).present?
end end
......
---
title: Added CI_MERGE_REQUEST_CHANGED_PAGE_* to Predefined Variables reference
merge_request: 25256
author:
type: added
...@@ -64,6 +64,8 @@ future GitLab releases.** ...@@ -64,6 +64,8 @@ future GitLab releases.**
| `CI_JOB_TOKEN` | 9.0 | 1.2 | Token used for authenticating with the [GitLab Container Registry][registry] and downloading [dependent repositories][dependent-repositories] | | `CI_JOB_TOKEN` | 9.0 | 1.2 | Token used for authenticating with the [GitLab Container Registry][registry] and downloading [dependent repositories][dependent-repositories] |
| `CI_JOB_URL` | 11.1 | 0.5 | Job details URL | | `CI_JOB_URL` | 11.1 | 0.5 | Job details URL |
| `CI_MERGE_REQUEST_ASSIGNEES` | 11.9 | all | Comma-separated list of username(s) of assignee(s) for the merge request if [the pipelines are for merge requests](../merge_request_pipelines/index.md). Available only if `only: [merge_requests]` is used and the merge request is created. | | `CI_MERGE_REQUEST_ASSIGNEES` | 11.9 | all | Comma-separated list of username(s) of assignee(s) for the merge request if [the pipelines are for merge requests](../merge_request_pipelines/index.md). Available only if `only: [merge_requests]` is used and the merge request is created. |
| `CI_MERGE_REQUEST_CHANGED_PAGE_PATHS` | 12.9 | all | Comma-separated list of paths of changed pages in a deployed [Review App](../review_apps/index.md) for a [Merge Request](../merge_request_pipelines/index.md). A [Route Map](../review_apps/index.md#route-maps) must be configured. |
| `CI_MERGE_REQUEST_CHANGED_PAGE_URLS` | 12.9 | all | Comma-separated list of URLs of changed pages in a deployed [Review App](../review_apps/index.md) for a [Merge Request](../merge_request_pipelines/index.md). A [Route Map](../review_apps/index.md#route-maps) must be configured. |
| `CI_MERGE_REQUEST_ID` | 11.6 | all | The ID of the merge request if [the pipelines are for merge requests](../merge_request_pipelines/index.md). Available only if `only: [merge_requests]` is used and the merge request is created. | | `CI_MERGE_REQUEST_ID` | 11.6 | all | The ID of the merge request if [the pipelines are for merge requests](../merge_request_pipelines/index.md). Available only if `only: [merge_requests]` is used and the merge request is created. |
| `CI_MERGE_REQUEST_IID` | 11.6 | all | The IID of the merge request if [the pipelines are for merge requests](../merge_request_pipelines/index.md). Available only if `only: [merge_requests]` is used and the merge request is created. | | `CI_MERGE_REQUEST_IID` | 11.6 | all | The IID of the merge request if [the pipelines are for merge requests](../merge_request_pipelines/index.md). Available only if `only: [merge_requests]` is used and the merge request is created. |
| `CI_MERGE_REQUEST_LABELS` | 11.9 | all | Comma-separated label names of the merge request if [the pipelines are for merge requests](../merge_request_pipelines/index.md). Available only if `only: [merge_requests]` is used and the merge request is created. | | `CI_MERGE_REQUEST_LABELS` | 11.9 | all | Comma-separated label names of the merge request if [the pipelines are for merge requests](../merge_request_pipelines/index.md). Available only if `only: [merge_requests]` is used and the merge request is created. |
......
...@@ -2509,6 +2509,64 @@ describe Ci::Build do ...@@ -2509,6 +2509,64 @@ describe Ci::Build do
end end
end end
describe 'CHANGED_PAGES variables' do
let(:route_map_yaml) do
<<~ROUTEMAP
- source: 'bar/branch-test.txt'
public: '/bar/branches'
ROUTEMAP
end
before do
allow_any_instance_of(Project)
.to receive(:route_map_for).with(/.+/)
.and_return(Gitlab::RouteMap.new(route_map_yaml))
end
context 'with a deployment environment and a merge request' do
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
let(:environment) { create(:environment, project: merge_request.project, name: "foo-#{project.default_branch}") }
let(:build) { create(:ci_build, pipeline: pipeline, environment: environment.name) }
it 'populates CI_MERGE_REQUEST_CHANGED_PAGES_* variables' do
expect(subject).to include(
{ key: 'CI_MERGE_REQUEST_CHANGED_PAGE_PATHS', value: '/bar/branches', public: true, masked: false },
{ key: 'CI_MERGE_REQUEST_CHANGED_PAGE_URLS', value: File.join(environment.external_url, '/bar/branches'), public: true, masked: false }
)
end
context 'with a deployment environment and no merge request' do
let(:environment) { create(:environment, project: project, name: "foo-#{project.default_branch}") }
let(:build) { create(:ci_build, pipeline: pipeline, environment: environment.name) }
it 'does not append CHANGED_PAGES variables' do
ci_variables = subject.select { |var| var[:key] =~ /MERGE_REQUEST_CHANGED_PAGES/ }
expect(ci_variables).to be_empty
end
end
context 'with no deployment environment and a present merge request' do
let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project, target_project: project) }
let(:build) { create(:ci_build, pipeline: merge_request.all_pipelines.take) }
it 'does not append CHANGED_PAGES variables' do
ci_variables = subject.select { |var| var[:key] =~ /MERGE_REQUEST_CHANGED_PAGES/ }
expect(ci_variables).to be_empty
end
end
context 'with no deployment environment and no merge request' do
it 'does not append CHANGED_PAGES variables' do
ci_variables = subject.select { |var| var[:key] =~ /MERGE_REQUEST_CHANGED_PAGES/ }
expect(ci_variables).to be_empty
end
end
end
end
context 'when build has user' do context 'when build has user' do
let(:user_variables) do let(:user_variables) do
[ [
......
...@@ -51,8 +51,10 @@ describe EnvironmentStatus do ...@@ -51,8 +51,10 @@ describe EnvironmentStatus do
# - source: /files\/(.+)/ # - source: /files\/(.+)/
# public: '\1' # public: '\1'
describe '#changes' do describe '#changes' do
subject { environment_status.changes }
it 'contains only added and modified public pages' do it 'contains only added and modified public pages' do
expect(environment_status.changes).to contain_exactly( expect(subject).to contain_exactly(
{ {
path: 'ruby-style-guide.html', path: 'ruby-style-guide.html',
external_url: "#{environment.external_url}/ruby-style-guide.html" external_url: "#{environment.external_url}/ruby-style-guide.html"
...@@ -64,6 +66,18 @@ describe EnvironmentStatus do ...@@ -64,6 +66,18 @@ describe EnvironmentStatus do
end end
end end
describe '#changed_paths' do
subject { environment_status.changed_urls }
it { is_expected.to contain_exactly("#{environment.external_url}/ruby-style-guide.html", "#{environment.external_url}/html/page.html") }
end
describe '#changed_urls' do
subject { environment_status.changed_paths }
it { is_expected.to contain_exactly('ruby-style-guide.html', 'html/page.html') }
end
describe '.for_merge_request' do describe '.for_merge_request' do
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
let!(:pipeline) { create(:ci_pipeline, sha: sha, merge_requests_as_head_pipeline: [merge_request]) } let!(:pipeline) { create(:ci_pipeline, sha: sha, merge_requests_as_head_pipeline: [merge_request]) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment