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
Boxiang Sun
gitlab-ce
Commits
c3229760
Commit
c3229760
authored
Jul 25, 2018
by
Duana Saskia
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor ProtectedRefMatcher to be more generic
parent
ece6a1ea
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
28 deletions
+56
-28
app/models/concerns/protected_ref.rb
app/models/concerns/protected_ref.rb
+8
-2
app/models/hooks/active_hook_filter.rb
app/models/hooks/active_hook_filter.rb
+2
-26
app/models/ref_matcher.rb
app/models/ref_matcher.rb
+46
-0
No files found.
app/models/concerns/protected_ref.rb
View file @
c3229760
...
...
@@ -50,14 +50,20 @@ module ProtectedRef
.
map
(
&
:"
#{
action
}
_access_levels"
).
flatten
end
# Returns all protected refs that match the given ref name.
# This checks all records from the scope built up so far, and does
# _not_ return a relation.
#
# This method optionally takes in a list of `protected_refs` to search
# through, to avoid calling out to the database.
def
matching
(
ref_name
,
protected_refs:
nil
)
ProtectedRefMatcher
.
matching
(
self
,
ref_name
,
protected_refs:
protected_refs
)
(
protected_refs
||
self
.
all
).
select
{
|
protected_ref
|
protected_ref
.
matches?
(
ref_name
)
}
end
end
private
def
ref_matcher
@ref_matcher
||=
ProtectedRefMatcher
.
new
(
self
)
@ref_matcher
||=
RefMatcher
.
new
(
self
.
name
)
end
end
app/models/hooks/active_hook_filter.rb
View file @
c3229760
class
ActiveHookFilter
def
initialize
(
hook
)
@hook
=
hook
@push_events_filter_matcher
=
RefMatcher
.
new
(
@hook
.
push_events_branch_filter
)
end
def
matches?
(
hooks_scope
,
data
)
...
...
@@ -8,31 +9,6 @@ class ActiveHookFilter
return
true
if
@hook
.
push_events_branch_filter
.
blank?
branch_name
=
Gitlab
::
Git
.
branch_name
(
data
[
:ref
])
exact_match?
(
branch_name
)
||
wildcard_match?
(
branch_name
)
end
private
def
exact_match?
(
branch_name
)
@hook
.
push_events_branch_filter
==
branch_name
end
def
wildcard_match?
(
branch_name
)
return
false
unless
wildcard?
wildcard_regex
===
branch_name
end
def
wildcard_regex
@wildcard_regex
||=
begin
name
=
@hook
.
push_events_branch_filter
.
gsub
(
'*'
,
'STAR_DONT_ESCAPE'
)
quoted_name
=
Regexp
.
quote
(
name
)
regex_string
=
quoted_name
.
gsub
(
'STAR_DONT_ESCAPE'
,
'.*?'
)
/\A
#{
regex_string
}
\z/
end
end
def
wildcard?
@hook
.
push_events_branch_filter
&&
@hook
.
push_events_branch_filter
.
include?
(
'*'
)
@push_events_filter_matcher
.
matches?
(
branch_name
)
end
end
app/models/
protected_
ref_matcher.rb
→
app/models/ref_matcher.rb
View file @
c3229760
# frozen_string_literal: true
class
ProtectedRefMatcher
def
initialize
(
protected_ref
)
@protected_ref
=
protected_ref
end
# Returns all protected refs that match the given ref name.
# This checks all records from the scope built up so far, and does
# _not_ return a relation.
#
# This method optionally takes in a list of `protected_refs` to search
# through, to avoid calling out to the database.
def
self
.
matching
(
type
,
ref_name
,
protected_refs:
nil
)
(
protected_refs
||
type
.
all
).
select
{
|
protected_ref
|
protected_ref
.
matches?
(
ref_name
)
}
class
RefMatcher
def
initialize
(
ref_name_or_pattern
)
@ref_name_or_pattern
=
ref_name_or_pattern
end
# Returns all branches/tags (among the given list of refs [`Gitlab::Git::Branch`])
# that match the current protected ref.
def
matching
(
refs
)
refs
.
select
{
|
ref
|
@protected_ref
.
matches?
(
ref
.
name
)
}
refs
.
select
{
|
ref
|
matches?
(
ref
.
name
)
}
end
# Checks if the protected ref matches the given ref name.
def
matches?
(
ref_name
)
return
false
if
@
protected_ref
.
name
.
blank?
return
false
if
@
ref_name_or_pattern
.
blank?
exact_match?
(
ref_name
)
||
wildcard_match?
(
ref_name
)
end
# Checks if this protected ref contains a wildcard
def
wildcard?
@
protected_ref
.
name
&&
@protected_ref
.
name
.
include?
(
'*'
)
@
ref_name_or_pattern
&&
@ref_name_or_pattern
.
include?
(
'*'
)
end
protected
def
exact_match?
(
ref_name
)
@
protected_ref
.
name
==
ref_name
@
ref_name_or_pattern
==
ref_name
end
def
wildcard_match?
(
ref_name
)
...
...
@@ -47,7 +37,7 @@ class ProtectedRefMatcher
def
wildcard_regex
@wildcard_regex
||=
begin
name
=
@
protected_ref
.
name
.
gsub
(
'*'
,
'STAR_DONT_ESCAPE'
)
name
=
@
ref_name_or_pattern
.
gsub
(
'*'
,
'STAR_DONT_ESCAPE'
)
quoted_name
=
Regexp
.
quote
(
name
)
regex_string
=
quoted_name
.
gsub
(
'STAR_DONT_ESCAPE'
,
'.*?'
)
/\A
#{
regex_string
}
\z/
...
...
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