Commit c6b716d2 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Use latest default branch SHA to get external local files

This commit makes it explicit that we want to use latest SHA on a
default branch to get external CI/CD files, _via_ `include` directive.
parent 253b677a
...@@ -27,7 +27,7 @@ module EE ...@@ -27,7 +27,7 @@ module EE
end end
def process_external_files(config, project, opts) def process_external_files(config, project, opts)
sha = opts.fetch(:sha, project.repository.commit.sha) sha = opts.fetch(:sha, project.repository.root_ref_sha)
::Gitlab::Ci::External::Processor.new(config, project, sha).perform ::Gitlab::Ci::External::Processor.new(config, project, sha).perform
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe EE::Gitlab::Ci::Config do describe EE::Gitlab::Ci::Config do
let(:config_class) { ::Gitlab::Ci::Config }
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:remote_location) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } let(:remote_location) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:local_location) { 'ee/spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml' }
let(:remote_file_content) do
<<~HEREDOC
variables:
AUTO_DEVOPS_DOMAIN: domain.example.com
POSTGRES_USER: user
POSTGRES_PASSWORD: testing-password
POSTGRES_ENABLED: "true"
POSTGRES_DB: $CI_ENVIRONMENT_SLUG
HEREDOC
end
let(:local_file_content) do
File.read(Rails.root.join(local_location))
end
let(:gitlab_ci_yml) do let(:gitlab_ci_yml) do
<<~HEREDOC <<~HEREDOC
include: include:
- /ee/spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml - #{local_location}
- #{remote_location} - #{remote_location}
image: ruby:2.2 image: ruby:2.2
HEREDOC HEREDOC
end end
let(:config) do let(:config) do
::Gitlab::Ci::Config.new(gitlab_ci_yml, project: project, sha: '12345') config_class.new(gitlab_ci_yml, project: project, sha: '12345')
end
before do
WebMock.stub_request(:get, remote_location)
.to_return(body: remote_file_content)
allow(project.repository)
.to receive(:blob_data_at).and_return(local_file_content)
end end
context 'when the project does not have a valid license' do context 'when the project does not have a valid license' do
before do before do
allow(project).to receive(:feature_available?).with(:external_files_in_gitlab_ci).and_return(false) allow(project).to receive(:feature_available?)
.with(:external_files_in_gitlab_ci).and_return(false)
end end
it "should raise a ValidationError" do it "should raise a ValidationError" do
...@@ -31,26 +58,13 @@ describe EE::Gitlab::Ci::Config do ...@@ -31,26 +58,13 @@ describe EE::Gitlab::Ci::Config do
end end
context 'when the project has a valid license' do context 'when the project has a valid license' do
let(:remote_file_content) do
<<~HEREDOC
variables:
AUTO_DEVOPS_DOMAIN: domain.example.com
POSTGRES_USER: user
POSTGRES_PASSWORD: testing-password
POSTGRES_ENABLED: "true"
POSTGRES_DB: $CI_ENVIRONMENT_SLUG
HEREDOC
end
let(:local_file_content) { File.read(Rails.root.join('ee/spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml')) }
before do before do
allow(project).to receive(:feature_available?).with(:external_files_in_gitlab_ci).and_return(true) allow(project).to receive(:feature_available?)
.with(:external_files_in_gitlab_ci).and_return(true)
end end
context "when gitlab_ci_yml has valid 'include' defined" do context "when gitlab_ci_yml has valid 'include' defined" do
before do before do
allow_any_instance_of(Gitlab::Ci::External::File::Local).to receive(:fetch_local_content).and_return(local_file_content)
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
end end
it 'should return a composed hash' do it 'should return a composed hash' do
...@@ -81,7 +95,7 @@ describe EE::Gitlab::Ci::Config do ...@@ -81,7 +95,7 @@ describe EE::Gitlab::Ci::Config do
context "when gitlab_ci.yml has invalid 'include' defined" do context "when gitlab_ci.yml has invalid 'include' defined" do
let(:gitlab_ci_yml) do let(:gitlab_ci_yml) do
<<~HEREDOC <<~HEREDOC
include: invalid include: invalid
HEREDOC HEREDOC
end end
...@@ -94,19 +108,20 @@ describe EE::Gitlab::Ci::Config do ...@@ -94,19 +108,20 @@ describe EE::Gitlab::Ci::Config do
end end
describe 'external file version' do describe 'external file version' do
context 'when external file SHA is defined' do context 'when external local file SHA is defined' do
it 'is using a defined value' do it 'is using a defined value' do
expect(project.repository).to receive(:find_commit).with('eeff1122') expect(project.repository).to receive(:blob_data_at)
.with('eeff1122', local_location)
config.new(gitlab_ci_yml, project: project, sha: 'eeff1122') config_class.new(gitlab_ci_yml, project: project, sha: 'eeff1122')
end end
end end
context 'when external file SHA is not defined' do context 'when external local file SHA is not defined' do
it 'is using latest SHA on the default branch' do it 'is using latest SHA on the default branch' do
expect(project.repository).to receive(:root_ref_sha) expect(project.repository).to receive(:root_ref_sha)
config.new(gitlab_ci_yml, project: project) config_class.new(gitlab_ci_yml, project: project)
end end
end end
end end
...@@ -128,7 +143,6 @@ describe EE::Gitlab::Ci::Config do ...@@ -128,7 +143,6 @@ describe EE::Gitlab::Ci::Config do
end end
it 'should take precedence' do it 'should take precedence' do
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({ image: 'ruby:2.2' }) expect(config.to_hash).to eq({ image: 'ruby:2.2' })
end end
end end
...@@ -154,7 +168,6 @@ describe EE::Gitlab::Ci::Config do ...@@ -154,7 +168,6 @@ describe EE::Gitlab::Ci::Config do
end end
it 'should merge the variables dictionaries' do it 'should merge the variables dictionaries' do
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({ variables: { A: 'alpha', B: 'beta', C: 'gamma', D: 'delta' } }) expect(config.to_hash).to eq({ variables: { A: 'alpha', B: 'beta', C: 'gamma', D: 'delta' } })
end end
end end
...@@ -181,7 +194,6 @@ describe EE::Gitlab::Ci::Config do ...@@ -181,7 +194,6 @@ describe EE::Gitlab::Ci::Config do
end end
it 'later declarations should take precedence' do it 'later declarations should take precedence' do
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({ variables: { A: 'alpha', B: 'beta', C: 'gamma', D: 'delta' } }) expect(config.to_hash).to eq({ variables: { A: 'alpha', B: 'beta', C: 'gamma', D: 'delta' } })
end end
end end
...@@ -207,7 +219,6 @@ describe EE::Gitlab::Ci::Config do ...@@ -207,7 +219,6 @@ describe EE::Gitlab::Ci::Config do
end end
it 'merges the jobs' do it 'merges the jobs' do
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({ expect(config.to_hash).to eq({
job1: { job1: {
script: ["echo 'hello from remote file'"], script: ["echo 'hello from remote file'"],
...@@ -233,7 +244,6 @@ describe EE::Gitlab::Ci::Config do ...@@ -233,7 +244,6 @@ describe EE::Gitlab::Ci::Config do
end end
it 'uses the script from the gitlab_ci.yml' do it 'uses the script from the gitlab_ci.yml' do
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({ expect(config.to_hash).to eq({
job1: { job1: {
script: ["echo 'hello from main file'"], script: ["echo 'hello from main file'"],
......
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