summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMatija Čupić <matteeyah@gmail.com>2018-09-08 12:09:14 +0200
committerMatija Čupić <matteeyah@gmail.com>2018-09-08 12:09:14 +0200
commit797046e358bcf0fcd8cab413fcccad1614180aa9 (patch)
tree174dfc2e3851e8e4d2d74f6bfac7774295d488ee /lib
parent6a8133b943a8e06571f5497bea0f36c236b2bf90 (diff)
downloadgitlab-ce-797046e358bcf0fcd8cab413fcccad1614180aa9.tar.gz
Reconcile differences in lib/gitlab/ci/external/file
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/ci/external/file/base.rb27
-rw-r--r--lib/gitlab/ci/external/file/local.rb19
-rw-r--r--lib/gitlab/ci/external/file/remote.rb20
3 files changed, 45 insertions, 21 deletions
diff --git a/lib/gitlab/ci/external/file/base.rb b/lib/gitlab/ci/external/file/base.rb
new file mode 100644
index 00000000000..4591b3ec82e
--- /dev/null
+++ b/lib/gitlab/ci/external/file/base.rb
@@ -0,0 +1,27 @@
+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
diff --git a/lib/gitlab/ci/external/file/local.rb b/lib/gitlab/ci/external/file/local.rb
index 27e827222e5..829cc9b3d19 100644
--- a/lib/gitlab/ci/external/file/local.rb
+++ b/lib/gitlab/ci/external/file/local.rb
@@ -2,27 +2,28 @@ module Gitlab
module Ci
module External
module File
- class Local
- attr_reader :location, :project, :branch_name
+ class Local < Base
+ attr_reader :location, :project, :sha
def initialize(location, opts = {})
- @location = location
+ super
+
@project = opts.fetch(:project)
@sha = opts.fetch(:sha)
end
- def valid?
- local_file_content
+ def content
+ @content ||= fetch_local_content
end
- def content
- local_file_content
+ def error_message
+ "Local file '#{location}' is not valid."
end
private
- def local_file_content
- @local_file_content ||= project.repository.blob_data_at(sha, location)
+ def fetch_local_content
+ project.repository.blob_data_at(sha, location)
end
end
end
diff --git a/lib/gitlab/ci/external/file/remote.rb b/lib/gitlab/ci/external/file/remote.rb
index e728e3de77d..b4294231265 100644
--- a/lib/gitlab/ci/external/file/remote.rb
+++ b/lib/gitlab/ci/external/file/remote.rb
@@ -2,29 +2,25 @@ module Gitlab
module Ci
module External
module File
- class Remote
+ class Remote < Base
include Gitlab::Utils::StrongMemoize
attr_reader :location
- def initialize(location, opts = {})
- @location = location
- end
-
- def valid?
- ::Gitlab::UrlSanitizer.valid?(location) && content
- end
-
def content
return @content if defined?(@content)
@content = strong_memoize(:content) do
begin
- HTTParty.get(location)
- rescue HTTParty::Error, Timeout::Error
- false
+ Gitlab::HTTP.get(location)
+ rescue Gitlab::HTTP::Error, Timeout::Error, SocketError, Gitlab::HTTP::BlockedUrlError
+ nil
end
end
end
+
+ def error_message
+ "Remote file '#{location}' is not valid."
+ end
end
end
end