Commit 6b5b4096 authored by Matija Čupić's avatar Matija Čupić

Load config variables from external project

Loads config variables from external project when CI config is loaded
from external project.

Changelog: fixed
parent 913e1a6d
...@@ -191,7 +191,8 @@ class Projects::PipelinesController < Projects::ApplicationController ...@@ -191,7 +191,8 @@ class Projects::PipelinesController < Projects::ApplicationController
def config_variables def config_variables
respond_to do |format| respond_to do |format|
format.json do format.json do
result = Ci::ListConfigVariablesService.new(@project, current_user).execute(params[:sha]) project = @project.uses_external_project_ci_config? ? @project.ci_config_external_project : @project
result = Ci::ListConfigVariablesService.new(project, current_user).execute(params[:sha])
result.nil? ? head(:no_content) : render(json: result) result.nil? ? head(:no_content) : render(json: result)
end end
......
...@@ -2537,6 +2537,10 @@ class Project < ApplicationRecord ...@@ -2537,6 +2537,10 @@ class Project < ApplicationRecord
ci_config_path.blank? || ci_config_path == Gitlab::FileDetector::PATTERNS[:gitlab_ci] ci_config_path.blank? || ci_config_path == Gitlab::FileDetector::PATTERNS[:gitlab_ci]
end end
def uses_external_project_ci_config?
!!(ci_config_path =~ %r{@.+/.+})
end
def limited_protected_branches(limit) def limited_protected_branches(limit)
protected_branches.limit(limit) protected_branches.limit(limit)
end end
...@@ -2645,6 +2649,10 @@ class Project < ApplicationRecord ...@@ -2645,6 +2649,10 @@ class Project < ApplicationRecord
repository.gitlab_ci_yml_for(sha, ci_config_path_or_default) repository.gitlab_ci_yml_for(sha, ci_config_path_or_default)
end end
def ci_config_external_project
Project.find_by_full_path(ci_config_path.split('@', 2).last)
end
def enabled_group_deploy_keys def enabled_group_deploy_keys
return GroupDeployKey.none unless group return GroupDeployKey.none unless group
......
...@@ -171,9 +171,6 @@ variables: ...@@ -171,9 +171,6 @@ variables:
You cannot set job-level variables to be pre-filled when you run a pipeline manually. You cannot set job-level variables to be pre-filled when you run a pipeline manually.
Pre-filled variables do not show up when the CI/CD configuration is [external to the project](settings.md#specify-a-custom-cicd-configuration-file).
See the [related issue](https://gitlab.com/gitlab-org/gitlab/-/issues/336184) for more details.
### Run a pipeline by using a URL query string ### Run a pipeline by using a URL query string
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/24146) in GitLab 12.5. > [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/24146) in GitLab 12.5.
......
...@@ -1292,6 +1292,38 @@ RSpec.describe Projects::PipelinesController do ...@@ -1292,6 +1292,38 @@ RSpec.describe Projects::PipelinesController do
end end
end end
context 'when project uses external project ci config' do
let(:other_project) { create(:project) }
let(:sha) { 'master' }
let(:service) { ::Ci::ListConfigVariablesService.new(other_project, user) }
let(:ci_config) do
{
variables: {
KEY1: { value: 'val 1', description: 'description 1' }
},
test: {
stage: 'test',
script: 'echo'
}
}
end
before do
project.update!(ci_config_path: ".gitlab-ci.yml@#{other_project.full_path}")
synchronous_reactive_cache(service)
end
it 'returns other project config variables' do
expect(::Ci::ListConfigVariablesService).to receive(:new).with(other_project, anything).and_return(service)
get_config_variables
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['KEY1']).to eq({ 'value' => 'val 1', 'description' => 'description 1' })
end
end
private private
def stub_gitlab_ci_yml_for_sha(sha, result) def stub_gitlab_ci_yml_for_sha(sha, result)
......
...@@ -2542,7 +2542,7 @@ RSpec.describe Project, factory_default: :keep do ...@@ -2542,7 +2542,7 @@ RSpec.describe Project, factory_default: :keep do
end end
describe '#uses_default_ci_config?' do describe '#uses_default_ci_config?' do
let(:project) { build(:project)} let(:project) { build(:project) }
it 'has a custom ci config path' do it 'has a custom ci config path' do
project.ci_config_path = 'something_custom' project.ci_config_path = 'something_custom'
...@@ -2563,6 +2563,44 @@ RSpec.describe Project, factory_default: :keep do ...@@ -2563,6 +2563,44 @@ RSpec.describe Project, factory_default: :keep do
end end
end end
describe '#uses_external_project_ci_config?' do
subject(:uses_external_project_ci_config) { project.uses_external_project_ci_config? }
let(:project) { build(:project) }
context 'when ci_config_path is configured with external project' do
before do
project.ci_config_path = '.gitlab-ci.yml@hello/world'
end
it { is_expected.to eq(true) }
end
context 'when ci_config_path is nil' do
before do
project.ci_config_path = nil
end
it { is_expected.to eq(false) }
end
context 'when ci_config_path is configured with a file in the project' do
before do
project.ci_config_path = 'hello/world/gitlab-ci.yml'
end
it { is_expected.to eq(false) }
end
context 'when ci_config_path is configured with remote file' do
before do
project.ci_config_path = 'https://example.org/file.yml'
end
it { is_expected.to eq(false) }
end
end
describe '#latest_successful_build_for_ref' do describe '#latest_successful_build_for_ref' do
let_it_be(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let_it_be(:pipeline) { create_pipeline(project) } let_it_be(:pipeline) { create_pipeline(project) }
...@@ -7043,6 +7081,15 @@ RSpec.describe Project, factory_default: :keep do ...@@ -7043,6 +7081,15 @@ RSpec.describe Project, factory_default: :keep do
end end
end end
describe '#ci_config_external_project' do
subject(:ci_config_external_project) { project.ci_config_external_project }
let(:other_project) { create(:project) }
let(:project) { build(:project, ci_config_path: ".gitlab-ci.yml@#{other_project.full_path}") }
it { is_expected.to eq(other_project) }
end
describe '#enabled_group_deploy_keys' do describe '#enabled_group_deploy_keys' do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
......
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