Commit 797046e3 authored by Matija Čupić's avatar Matija Čupić

Reconcile differences in lib/gitlab/ci/external/file

parent 6a8133b9
module Gitlab
module Ci
module External
module File
class Base
YAML_WHITELIST_EXTENSION = /(yml|yaml)$/i.freeze
def initialize(location, opts = {})
@location = location
end
def valid?
location.match(YAML_WHITELIST_EXTENSION) && content
end
def content
raise NotImplementedError, 'content must be implemented and return a string or nil'
end
def error_message
raise NotImplementedError, 'error_message must be implemented and return a string'
end
end
end
end
end
end
...@@ -2,27 +2,28 @@ module Gitlab ...@@ -2,27 +2,28 @@ module Gitlab
module Ci module Ci
module External module External
module File module File
class Local class Local < Base
attr_reader :location, :project, :branch_name attr_reader :location, :project, :sha
def initialize(location, opts = {}) def initialize(location, opts = {})
@location = location super
@project = opts.fetch(:project) @project = opts.fetch(:project)
@sha = opts.fetch(:sha) @sha = opts.fetch(:sha)
end end
def valid? def content
local_file_content @content ||= fetch_local_content
end end
def content def error_message
local_file_content "Local file '#{location}' is not valid."
end end
private private
def local_file_content def fetch_local_content
@local_file_content ||= project.repository.blob_data_at(sha, location) project.repository.blob_data_at(sha, location)
end end
end end
end end
......
...@@ -2,28 +2,24 @@ module Gitlab ...@@ -2,28 +2,24 @@ module Gitlab
module Ci module Ci
module External module External
module File module File
class Remote class Remote < Base
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
attr_reader :location attr_reader :location
def initialize(location, opts = {})
@location = location
end
def valid?
::Gitlab::UrlSanitizer.valid?(location) && content
end
def content def content
return @content if defined?(@content) return @content if defined?(@content)
@content = strong_memoize(:content) do @content = strong_memoize(:content) do
begin begin
HTTParty.get(location) Gitlab::HTTP.get(location)
rescue HTTParty::Error, Timeout::Error rescue Gitlab::HTTP::Error, Timeout::Error, SocketError, Gitlab::HTTP::BlockedUrlError
false nil
end
end end
end end
def error_message
"Remote file '#{location}' is not valid."
end end
end end
end end
......
require 'fast_spec_helper' require 'spec_helper'
describe Gitlab::Ci::External::File::Local do describe Gitlab::Ci::External::File::Local do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:local_file) { described_class.new(location, { project: project, sha: '12345' }) } let(:local_file) { described_class.new(location, { project: project, sha: '12345' }) }
describe "#valid?" do describe '#valid?' do
context 'when is a valid local path' do context 'when is a valid local path' do
let(:location) { '/vendor/gitlab-ci-yml/existent-file.yml' } let(:location) { '/vendor/gitlab-ci-yml/existent-file.yml' }
before do before do
allow_any_instance_of(described_class).to receive(:local_file_content).and_return("image: 'ruby2:2'") allow_any_instance_of(described_class).to receive(:fetch_local_content).and_return("image: 'ruby2:2'")
end end
it 'should return true' do it 'should return true' do
...@@ -25,7 +25,17 @@ describe Gitlab::Ci::External::File::Local do ...@@ -25,7 +25,17 @@ describe Gitlab::Ci::External::File::Local do
end end
end end
describe "#content" do context 'when is not a yaml file' do
let(:location) { '/config/application.rb' }
it 'should return false' do
expect(local_file.valid?).to be_falsy
end
end
end
describe '#content' do
context 'with a a valid file' do
let(:local_file_content) do let(:local_file_content) do
<<~HEREDOC <<~HEREDOC
before_script: before_script:
...@@ -36,18 +46,31 @@ describe Gitlab::Ci::External::File::Local do ...@@ -36,18 +46,31 @@ describe Gitlab::Ci::External::File::Local do
- bundle install --jobs $(nproc) "${FLAGS[@]}" - bundle install --jobs $(nproc) "${FLAGS[@]}"
HEREDOC HEREDOC
end end
let(:location) { '/vendor/gitlab-ci-yml/existent-file.yml' }
context 'with a local file' do
let(:location) { '/vendor/gitlab-ci-yml/non-existent-file.yml' }
before do before do
allow_any_instance_of(described_class).to receive(:local_file_content).and_return(local_file_content) allow_any_instance_of(described_class).to receive(:fetch_local_content).and_return(local_file_content)
end end
it 'should return the content of the file' do it 'should return the content of the file' do
expect(local_file.content).to eq(local_file_content) expect(local_file.content).to eq(local_file_content)
end end
end end
context 'with an invalid file' do
let(:location) { '/vendor/gitlab-ci-yml/non-existent-file.yml' }
it 'should be nil' do
expect(local_file.content).to be_nil
end
end
end
describe '#error_message' do
let(:location) { '/vendor/gitlab-ci-yml/non-existent-file.yml' }
it 'should return an error message' do
expect(local_file.error_message).to eq("Local file '#{location}' is not valid.")
end end
end end
end end
require 'fast_spec_helper' require 'spec_helper'
describe Gitlab::Ci::External::File::Remote do describe Gitlab::Ci::External::File::Remote do
let(:remote_file) { described_class.new(location) } let(:remote_file) { described_class.new(location) }
...@@ -25,7 +25,7 @@ describe Gitlab::Ci::External::File::Remote do ...@@ -25,7 +25,7 @@ describe Gitlab::Ci::External::File::Remote do
end end
end end
context 'when is not a valid remote url' do context 'with an irregular url' do
let(:location) { 'not-valid://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } let(:location) { 'not-valid://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
it 'should return false' do it 'should return false' do
...@@ -35,13 +35,29 @@ describe Gitlab::Ci::External::File::Remote do ...@@ -35,13 +35,29 @@ describe Gitlab::Ci::External::File::Remote do
context 'with a timeout' do context 'with a timeout' do
before do before do
allow(HTTParty).to receive(:get).and_raise(Timeout::Error) allow(Gitlab::HTTP).to receive(:get).and_raise(Timeout::Error)
end end
it 'should be falsy' do it 'should be falsy' do
expect(remote_file.valid?).to be_falsy expect(remote_file.valid?).to be_falsy
end end
end end
context 'when is not a yaml file' do
let(:location) { 'https://asdasdasdaj48ggerexample.com' }
it 'should be falsy' do
expect(remote_file.valid?).to be_falsy
end
end
context 'with an internal url' do
let(:location) { 'http://localhost:8080' }
it 'should be falsy' do
expect(remote_file.valid?).to be_falsy
end
end
end end
describe "#content" do describe "#content" do
...@@ -57,12 +73,40 @@ describe Gitlab::Ci::External::File::Remote do ...@@ -57,12 +73,40 @@ describe Gitlab::Ci::External::File::Remote do
context 'with a timeout' do context 'with a timeout' do
before do before do
allow(HTTParty).to receive(:get).and_raise(Timeout::Error) allow(Gitlab::HTTP).to receive(:get).and_raise(Timeout::Error)
end end
it 'should be falsy' do it 'should be falsy' do
expect(remote_file.content).to be_falsy expect(remote_file.content).to be_falsy
end end
end end
context 'with an invalid remote url' do
let(:location) { 'https://asdasdasdaj48ggerexample.com' }
before do
WebMock.stub_request(:get, location).to_raise(SocketError.new('Some HTTP error'))
end
it 'should be nil' do
expect(remote_file.content).to be_nil
end
end
context 'with an internal url' do
let(:location) { 'http://localhost:8080' }
it 'should be nil' do
expect(remote_file.content).to be_nil
end
end
end
describe "#error_message" do
let(:location) { 'not-valid://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
it 'should return an error message' do
expect(remote_file.error_message).to eq("Remote file '#{location}' is not valid.")
end
end end
end end
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