Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
855a6142a76f
Commit
855a6142
authored
Dec 01, 2015
by
Robert Speicher
Browse files
Add custom UrlValidator
parent
bf87c67554e9
Changes
10
Hide whitespace changes
Inline
Side-by-side
app/models/application_setting.rb
View file @
855a6142
...
...
@@ -43,8 +43,8 @@
validates
:home_page_url
,
allow_blank:
true
,
format:
{
with:
/\A
#{
URI
.
regexp
(
%w(http https)
)
}
\z/
,
message:
"should be a valid url"
}
,
url:
true
,
if: :home_page_url_column_exist
validates
:after_sign_out_path
,
allow_blank:
true
,
...
...
@@ -47,8 +47,8 @@
if: :home_page_url_column_exist
validates
:after_sign_out_path
,
allow_blank:
true
,
format:
{
with:
/\A
#{
URI
.
regexp
(
%w(http https)
)
}
\z/
,
message:
"should be a valid url"
}
url:
true
validates
:admin_notification_email
,
allow_blank:
true
,
...
...
app/models/ci/web_hook.rb
View file @
855a6142
...
...
@@ -20,8 +20,7 @@
# HTTParty timeout
default_timeout
10
validates
:url
,
presence:
true
,
format:
{
with:
URI
::
regexp
(
%w(http https)
),
message:
"should be a valid url"
}
validates
:url
,
presence:
true
,
url:
true
def
execute
(
data
)
parsed_url
=
URI
.
parse
(
url
)
...
...
app/models/hooks/web_hook.rb
View file @
855a6142
...
...
@@ -31,8 +31,7 @@
# HTTParty timeout
default_timeout
Gitlab
.
config
.
gitlab
.
webhook_timeout
validates
:url
,
presence:
true
,
format:
{
with:
/\A
#{
URI
.
regexp
(
%w(http https)
)
}
\z/
,
message:
"should be a valid url"
}
validates
:url
,
presence:
true
,
url:
true
def
execute
(
data
,
hook_name
)
parsed_url
=
URI
.
parse
(
url
)
...
...
app/models/project.rb
View file @
855a6142
...
...
@@ -152,7 +152,7 @@
validates_uniqueness_of
:name
,
scope: :namespace_id
validates_uniqueness_of
:path
,
scope: :namespace_id
validates
:import_url
,
format:
{
with:
/\A
#{
URI
.
regexp
(
%w(ssh git http https)
)
}
\z/
,
message:
'should be a valid url'
},
url:
{
protocols:
%w(ssh git http https)
},
if: :external_import?
validates
:star_count
,
numericality:
{
greater_than_or_equal_to:
0
}
validate
:check_limit
,
on: :create
...
...
app/models/project_services/bamboo_service.rb
View file @
855a6142
...
...
@@ -23,10 +23,7 @@
prop_accessor
:bamboo_url
,
:build_key
,
:username
,
:password
validates
:bamboo_url
,
presence:
true
,
format:
{
with:
/\A
#{
URI
.
regexp
}
\z/
},
if: :activated?
validates
:bamboo_url
,
presence:
true
,
url:
true
,
if: :activated?
validates
:build_key
,
presence:
true
,
if: :activated?
validates
:username
,
presence:
true
,
...
...
@@ -84,7 +81,7 @@
def
supported_events
%w(push)
end
def
build_info
(
sha
)
url
=
URI
.
parse
(
"
#{
bamboo_url
}
/rest/api/latest/result?label=
#{
sha
}
"
)
...
...
app/models/project_services/drone_ci_service.rb
View file @
855a6142
...
...
@@ -19,5 +19,5 @@
#
class
DroneCiService
<
CiService
prop_accessor
:drone_url
,
:token
,
:enable_ssl_verification
...
...
@@ -23,10 +23,7 @@
prop_accessor
:drone_url
,
:token
,
:enable_ssl_verification
validates
:drone_url
,
presence:
true
,
format:
{
with:
/\A
#{
URI
.
regexp
(
%w(http https)
)
}
\z/
,
message:
"should be a valid url"
},
if: :activated?
validates
:token
,
presence:
true
,
if: :activated?
validates
:drone_url
,
presence:
true
,
url:
true
,
if: :activated?
validates
:token
,
presence:
true
,
if: :activated?
after_save
:compose_service_hook
,
if: :activated?
...
...
@@ -58,11 +55,11 @@
end
def
merge_request_status_path
(
iid
,
sha
=
nil
,
ref
=
nil
)
url
=
[
drone_url
,
"gitlab/
#{
project
.
namespace
.
path
}
/
#{
project
.
path
}
/pulls/
#{
iid
}
"
,
url
=
[
drone_url
,
"gitlab/
#{
project
.
namespace
.
path
}
/
#{
project
.
path
}
/pulls/
#{
iid
}
"
,
"?access_token=
#{
token
}
"
]
URI
.
join
(
*
url
).
to_s
end
def
commit_status_path
(
sha
,
ref
)
...
...
@@ -63,11 +60,11 @@
"?access_token=
#{
token
}
"
]
URI
.
join
(
*
url
).
to_s
end
def
commit_status_path
(
sha
,
ref
)
url
=
[
drone_url
,
"gitlab/
#{
project
.
namespace
.
path
}
/
#{
project
.
path
}
/commits/
#{
sha
}
"
,
url
=
[
drone_url
,
"gitlab/
#{
project
.
namespace
.
path
}
/
#{
project
.
path
}
/commits/
#{
sha
}
"
,
"?branch=
#{
URI
::
encode
(
ref
.
to_s
)
}
&access_token=
#{
token
}
"
]
URI
.
join
(
*
url
).
to_s
...
...
@@ -114,10 +111,10 @@
end
def
merge_request_page
(
iid
,
sha
,
ref
)
url
=
[
drone_url
,
url
=
[
drone_url
,
"gitlab/
#{
project
.
namespace
.
path
}
/
#{
project
.
path
}
/redirect/pulls/
#{
iid
}
"
]
URI
.
join
(
*
url
).
to_s
end
def
commit_page
(
sha
,
ref
)
...
...
@@ -118,11 +115,11 @@
"gitlab/
#{
project
.
namespace
.
path
}
/
#{
project
.
path
}
/redirect/pulls/
#{
iid
}
"
]
URI
.
join
(
*
url
).
to_s
end
def
commit_page
(
sha
,
ref
)
url
=
[
drone_url
,
"gitlab/
#{
project
.
namespace
.
path
}
/
#{
project
.
path
}
/redirect/commits/
#{
sha
}
"
,
url
=
[
drone_url
,
"gitlab/
#{
project
.
namespace
.
path
}
/
#{
project
.
path
}
/redirect/commits/
#{
sha
}
"
,
"?branch=
#{
URI
::
encode
(
ref
.
to_s
)
}
"
]
URI
.
join
(
*
url
).
to_s
...
...
@@ -163,6 +160,6 @@
end
def
push_valid?
(
data
)
opened_merge_requests
=
project
.
merge_requests
.
opened
.
where
(
source_project_id:
project
.
id
,
opened_merge_requests
=
project
.
merge_requests
.
opened
.
where
(
source_project_id:
project
.
id
,
source_branch:
Gitlab
::
Git
.
ref_name
(
data
[
:ref
]))
...
...
@@ -167,6 +164,6 @@
source_branch:
Gitlab
::
Git
.
ref_name
(
data
[
:ref
]))
opened_merge_requests
.
empty?
&&
data
[
:total_commits_count
]
>
0
&&
opened_merge_requests
.
empty?
&&
data
[
:total_commits_count
]
>
0
&&
!
Gitlab
::
Git
.
blank_ref?
(
data
[
:after
])
end
...
...
app/models/project_services/external_wiki_service.rb
View file @
855a6142
...
...
@@ -22,10 +22,8 @@
include
HTTParty
prop_accessor
:external_wiki_url
validates
:external_wiki_url
,
presence:
true
,
format:
{
with:
/\A
#{
URI
.
regexp
}
\z/
},
if: :activated?
validates
:external_wiki_url
,
presence:
true
,
url:
true
,
if: :activated?
def
title
'External Wiki'
...
...
app/models/project_services/teamcity_service.rb
View file @
855a6142
...
...
@@ -23,9 +23,7 @@
prop_accessor
:teamcity_url
,
:build_type
,
:username
,
:password
validates
:teamcity_url
,
presence:
true
,
format:
{
with:
/\A
#{
URI
.
regexp
}
\z/
},
if: :activated?
validates
:teamcity_url
,
presence:
true
,
url:
true
,
if: :activated?
validates
:build_type
,
presence:
true
,
if: :activated?
validates
:username
,
presence:
true
,
...
...
@@ -29,6 +27,7 @@
validates
:build_type
,
presence:
true
,
if: :activated?
validates
:username
,
presence:
true
,
if:
->
(
service
)
{
service
.
password?
},
if: :activated?
if:
->
(
service
)
{
service
.
password?
},
if: :activated?
validates
:password
,
presence:
true
,
...
...
@@ -33,6 +32,7 @@
validates
:password
,
presence:
true
,
if:
->
(
service
)
{
service
.
username?
},
if: :activated?
if:
->
(
service
)
{
service
.
username?
},
if: :activated?
attr_accessor
:response
...
...
app/validators/url_validator.rb
0 → 100644
View file @
855a6142
# UrlValidator
#
# Custom validator for URLs.
#
# By default, only URLs for the HTTP(S) protocols will be considered valid.
# Provide a `:protocols` option to configure accepted protocols.
#
# Example:
#
# class User < ActiveRecord::Base
# validates :personal_url, url: true
#
# validates :ftp_url, url: { protocols: %w(ftp) }
#
# validates :git_url, url: { protocols: %w(http https ssh git) }
# end
#
class
UrlValidator
<
ActiveModel
::
EachValidator
def
validate_each
(
record
,
attribute
,
value
)
unless
valid_url?
(
value
)
record
.
errors
.
add
(
attribute
,
"must be a valid URL"
)
end
end
private
def
default_options
@default_options
||=
{
protocols:
%w(http https)
}
end
def
valid_url?
(
value
)
options
=
default_options
.
merge
(
self
.
options
)
value
=~
/\A
#{
URI
.
regexp
(
options
[
:protocols
])
}
\z/
end
end
spec/models/application_setting_spec.rb
View file @
855a6142
...
...
@@ -36,6 +36,22 @@
it
{
expect
(
setting
).
to
be_valid
}
describe
'validations'
do
let
(
:http
)
{
'http://example.com'
}
let
(
:https
)
{
'https://example.com'
}
let
(
:ftp
)
{
'ftp://example.com'
}
it
{
is_expected
.
to
allow_value
(
nil
).
for
(
:home_page_url
)
}
it
{
is_expected
.
to
allow_value
(
http
).
for
(
:home_page_url
)
}
it
{
is_expected
.
to
allow_value
(
https
).
for
(
:home_page_url
)
}
it
{
is_expected
.
not_to
allow_value
(
ftp
).
for
(
:home_page_url
)
}
it
{
is_expected
.
to
allow_value
(
nil
).
for
(
:after_sign_out_path
)
}
it
{
is_expected
.
to
allow_value
(
http
).
for
(
:after_sign_out_path
)
}
it
{
is_expected
.
to
allow_value
(
https
).
for
(
:after_sign_out_path
)
}
it
{
is_expected
.
not_to
allow_value
(
ftp
).
for
(
:after_sign_out_path
)
}
end
context
'restricted signup domains'
do
it
'set single domain'
do
setting
.
restricted_signup_domains_raw
=
'example.com'
...
...
Write
Preview
Supports
Markdown
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