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
end
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
end
end
......
require 'spec_helper'
describe EE::Gitlab::Ci::Config do
let(:config_class) { ::Gitlab::Ci::Config }
let(:project) { create(:project, :repository) }
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
<<~HEREDOC
include:
- /ee/spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml
- #{local_location}
- #{remote_location}
image: ruby:2.2
......@@ -14,12 +32,21 @@ describe EE::Gitlab::Ci::Config do
end
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
context 'when the project does not have a valid license' 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
it "should raise a ValidationError" do
......@@ -31,26 +58,13 @@ describe EE::Gitlab::Ci::Config do
end
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
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
context "when gitlab_ci_yml has valid 'include' defined" 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
it 'should return a composed hash' do
......@@ -94,19 +108,20 @@ describe EE::Gitlab::Ci::Config do
end
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
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
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
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
......@@ -128,7 +143,6 @@ describe EE::Gitlab::Ci::Config do
end
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' })
end
end
......@@ -154,7 +168,6 @@ describe EE::Gitlab::Ci::Config do
end
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' } })
end
end
......@@ -181,7 +194,6 @@ describe EE::Gitlab::Ci::Config do
end
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' } })
end
end
......@@ -207,7 +219,6 @@ describe EE::Gitlab::Ci::Config do
end
it 'merges the jobs' do
WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({
job1: {
script: ["echo 'hello from remote file'"],
......@@ -233,7 +244,6 @@ describe EE::Gitlab::Ci::Config do
end
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({
job1: {
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