Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
797046e3
Commit
797046e3
authored
Sep 08, 2018
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reconcile differences in lib/gitlab/ci/external/file
parent
6a8133b9
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
134 additions
and
43 deletions
+134
-43
lib/gitlab/ci/external/file/base.rb
lib/gitlab/ci/external/file/base.rb
+27
-0
lib/gitlab/ci/external/file/local.rb
lib/gitlab/ci/external/file/local.rb
+10
-9
lib/gitlab/ci/external/file/remote.rb
lib/gitlab/ci/external/file/remote.rb
+8
-12
spec/lib/gitlab/ci/external/file/local_spec.rb
spec/lib/gitlab/ci/external/file/local_spec.rb
+41
-18
spec/lib/gitlab/ci/external/file/remote_spec.rb
spec/lib/gitlab/ci/external/file/remote_spec.rb
+48
-4
No files found.
lib/gitlab/ci/external/file/base.rb
0 → 100644
View file @
797046e3
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
lib/gitlab/ci/external/file/local.rb
View file @
797046e3
...
...
@@ -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
...
...
lib/gitlab/ci/external/file/remote.rb
View file @
797046e3
...
...
@@ -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
::
BlockedUrl
Error
nil
end
end
end
def
error_message
"Remote file '
#{
location
}
' is not valid."
end
end
end
end
...
...
spec/lib/gitlab/ci/external/file/local_spec.rb
View file @
797046e3
require
'
fast_
spec_helper'
require
'spec_helper'
describe
Gitlab
::
Ci
::
External
::
File
::
Local
do
let
(
:project
)
{
create
(
:project
,
:repository
)
}
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
let
(
:location
)
{
'/vendor/gitlab-ci-yml/existent-file.yml'
}
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
it
'should return true'
do
...
...
@@ -25,29 +25,52 @@ describe Gitlab::Ci::External::File::Local do
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
<<~
HEREDOC
before_script:
- apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs
- ruby -v
- which ruby
- gem install bundler --no-ri --no-rdoc
- bundle install --jobs $(nproc) "${FLAGS[@]}"
before_script:
- apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs
- ruby -v
- which ruby
- gem install bundler --no-ri --no-rdoc
- bundle install --jobs $(nproc) "${FLAGS[@]}"
HEREDOC
end
let
(
:location
)
{
'/vendor/gitlab-ci-yml/existent-file.yml'
}
before
do
allow_any_instance_of
(
described_class
).
to
receive
(
:fetch_local_content
).
and_return
(
local_file_content
)
end
context
'with a local file'
do
let
(
:location
)
{
'/vendor/gitlab-ci-yml/non-existent-file.yml'
}
it
'should return the content of the file'
do
expect
(
local_file
.
content
).
to
eq
(
local_file_content
)
end
end
before
do
allow_any_instance_of
(
described_class
).
to
receive
(
:local_file_content
).
and_return
(
local_file_content
)
end
context
'with an invalid file'
do
let
(
:location
)
{
'/vendor/gitlab-ci-yml/non-existent-file.yml'
}
it
'should return the content of the file'
do
expect
(
local_file
.
content
).
to
eq
(
local_file_content
)
end
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
spec/lib/gitlab/ci/external/file/remote_spec.rb
View file @
797046e3
require
'
fast_
spec_helper'
require
'spec_helper'
describe
Gitlab
::
Ci
::
External
::
File
::
Remote
do
let
(
:remote_file
)
{
described_class
.
new
(
location
)
}
...
...
@@ -25,7 +25,7 @@ describe Gitlab::Ci::External::File::Remote do
end
end
context
'w
hen is not a valid remote
url'
do
context
'w
ith an irregular
url'
do
let
(
:location
)
{
'not-valid://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml'
}
it
'should return false'
do
...
...
@@ -35,13 +35,29 @@ describe Gitlab::Ci::External::File::Remote do
context
'with a timeout'
do
before
do
allow
(
HTTParty
).
to
receive
(
:get
).
and_raise
(
Timeout
::
Error
)
allow
(
Gitlab
::
HTTP
).
to
receive
(
:get
).
and_raise
(
Timeout
::
Error
)
end
it
'should be falsy'
do
expect
(
remote_file
.
valid?
).
to
be_falsy
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
describe
"#content"
do
...
...
@@ -57,12 +73,40 @@ describe Gitlab::Ci::External::File::Remote do
context
'with a timeout'
do
before
do
allow
(
HTTParty
).
to
receive
(
:get
).
and_raise
(
Timeout
::
Error
)
allow
(
Gitlab
::
HTTP
).
to
receive
(
:get
).
and_raise
(
Timeout
::
Error
)
end
it
'should be falsy'
do
expect
(
remote_file
.
content
).
to
be_falsy
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment