diff options
author | Duana Saskia <starkcoffee@users.noreply.github.com> | 2018-06-07 09:35:17 +0200 |
---|---|---|
committer | Duana Saskia <starkcoffee@users.noreply.github.com> | 2018-08-13 13:20:58 +0200 |
commit | ece6a1ea6ecffdbde5ff7d663f1ad1eb74f78aa6 (patch) | |
tree | 288e34ff932b6c84e1a1c5ead26cbd8f80ea12f5 /app/models/hooks | |
parent | 07356866b2ce85f4d724c96f14e129fbe6a56963 (diff) | |
download | gitlab-ce-ece6a1ea6ecffdbde5ff7d663f1ad1eb74f78aa6.tar.gz |
Filter project hooks by branch
Allow specificying a branch filter for a project hook and only trigger
a project hook if either the branch filter is blank or the branch matches.
Only supported for push_events for now.
Diffstat (limited to 'app/models/hooks')
-rw-r--r-- | app/models/hooks/active_hook_filter.rb | 38 | ||||
-rw-r--r-- | app/models/hooks/web_hook.rb | 1 |
2 files changed, 39 insertions, 0 deletions
diff --git a/app/models/hooks/active_hook_filter.rb b/app/models/hooks/active_hook_filter.rb new file mode 100644 index 00000000000..611af971163 --- /dev/null +++ b/app/models/hooks/active_hook_filter.rb @@ -0,0 +1,38 @@ +class ActiveHookFilter + def initialize(hook) + @hook = hook + end + + def matches?(hooks_scope, data) + return true if hooks_scope != :push_hooks + 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?('*') + end +end diff --git a/app/models/hooks/web_hook.rb b/app/models/hooks/web_hook.rb index f18aadefa5c..20f15c15277 100644 --- a/app/models/hooks/web_hook.rb +++ b/app/models/hooks/web_hook.rb @@ -9,6 +9,7 @@ class WebHook < ActiveRecord::Base allow_local_network: lambda(&:allow_local_requests?) } validates :token, format: { without: /\n/ } + validates :push_events_branch_filter, branch_filter: true def execute(data, hook_name) WebHookService.new(self, data, hook_name).execute |