From b8bc14ac64a39e0ddfb90cb15ae1bb9fcc717e2f Mon Sep 17 00:00:00 2001 From: Shinya Maeda Date: Sat, 16 Feb 2019 15:25:32 +0900 Subject: Introduce new only keywords for gitlab-ci.yml Add more --- lib/gitlab/ci/build/policy/branches.rb | 33 ++++++++++++++++++++++++ lib/gitlab/ci/build/policy/events.rb | 29 +++++++++++++++++++++ lib/gitlab/ci/build/policy/merge_requests.rb | 33 ++++++++++++++++++++++++ lib/gitlab/ci/build/policy/project_paths.rb | 33 ++++++++++++++++++++++++ lib/gitlab/ci/build/policy/protected_branches.rb | 33 ++++++++++++++++++++++++ lib/gitlab/ci/build/policy/tags.rb | 33 ++++++++++++++++++++++++ 6 files changed, 194 insertions(+) create mode 100644 lib/gitlab/ci/build/policy/branches.rb create mode 100644 lib/gitlab/ci/build/policy/events.rb create mode 100644 lib/gitlab/ci/build/policy/merge_requests.rb create mode 100644 lib/gitlab/ci/build/policy/project_paths.rb create mode 100644 lib/gitlab/ci/build/policy/protected_branches.rb create mode 100644 lib/gitlab/ci/build/policy/tags.rb diff --git a/lib/gitlab/ci/build/policy/branches.rb b/lib/gitlab/ci/build/policy/branches.rb new file mode 100644 index 00000000000..90885087f5e --- /dev/null +++ b/lib/gitlab/ci/build/policy/branches.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Gitlab + module Ci + module Build + module Policy + class Branches < Policy::Specification + def initialize(branches) + @patterns = Array(branches) + end + + def satisfied_by?(pipeline, seed = nil) + return false unless pipeline.branch? + + @patterns.any? do |pattern| + matches_pattern?(pattern, pipeline.ref) + end + end + + private + + def matches_pattern?(pattern, subject) + if pattern.first == "/" && pattern.last == "/" + Regexp.new(pattern[1...-1]) =~ subject + else + pattern == subject + end + end + end + end + end + end +end diff --git a/lib/gitlab/ci/build/policy/events.rb b/lib/gitlab/ci/build/policy/events.rb new file mode 100644 index 00000000000..8d2fcfbc5ae --- /dev/null +++ b/lib/gitlab/ci/build/policy/events.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Gitlab + module Ci + module Build + module Policy + class Events < Policy::Specification + def initialize(events) + @events = Array(events) + end + + def satisfied_by?(pipeline, seed = nil) + return false unless pipeline.source + + @events.any? do |event| + matches_event?(event, pipeline.source) + end + end + + private + + def matches_event?(event, source) + event == source || event == source.pluralize + end + end + end + end + end +end diff --git a/lib/gitlab/ci/build/policy/merge_requests.rb b/lib/gitlab/ci/build/policy/merge_requests.rb new file mode 100644 index 00000000000..5d9947f15a0 --- /dev/null +++ b/lib/gitlab/ci/build/policy/merge_requests.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Gitlab + module Ci + module Build + module Policy + class Branches < Policy::Specification + def initialize(branches) + @patterns = Array(branches) + end + + def satisfied_by?(pipeline, seed = nil) + return false unless pipeline.merge_request? + + @patterns.any? do |pattern| + matches_pattern?(pattern, pipeline.ref) + end + end + + private + + def matches_pattern?(pattern, subject) + if pattern.first == "/" && pattern.last == "/" + Regexp.new(pattern[1...-1]) =~ subject + else + pattern == subject + end + end + end + end + end + end +end diff --git a/lib/gitlab/ci/build/policy/project_paths.rb b/lib/gitlab/ci/build/policy/project_paths.rb new file mode 100644 index 00000000000..d3478cb2f17 --- /dev/null +++ b/lib/gitlab/ci/build/policy/project_paths.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Gitlab + module Ci + module Build + module Policy + class ProjectPaths < Policy::Specification + def initialize(project_paths) + @patterns = Array(project_paths) + end + + def satisfied_by?(pipeline, seed = nil) + return false unless pipeline.project && !pipeline.project.pending_delete? + + @patterns.any? do |pattern| + matches_pattern?(pattern, pipeline.project_full_path) + end + end + + private + + def matches_pattern?(pattern, subject) + if pattern.first == "/" && pattern.last == "/" + Regexp.new(pattern[1...-1]) =~ subject + else + pattern == subject + end + end + end + end + end + end +end diff --git a/lib/gitlab/ci/build/policy/protected_branches.rb b/lib/gitlab/ci/build/policy/protected_branches.rb new file mode 100644 index 00000000000..5bbdaf4b416 --- /dev/null +++ b/lib/gitlab/ci/build/policy/protected_branches.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Gitlab + module Ci + module Build + module Policy + class ProtectedBranches < Policy::Specification + def initialize(protected_branches) + @patterns = Array(protected_branches) + end + + def satisfied_by?(pipeline, seed = nil) + return false unless pipeline.protected? + + @patterns.any? do |pattern| + matches_pattern?(pattern, pipeline.ref) + end + end + + private + + def matches_pattern?(pattern, subject) + if pattern.first == "/" && pattern.last == "/" + Regexp.new(pattern[1...-1]) =~ subject + else + pattern == subject + end + end + end + end + end + end +end diff --git a/lib/gitlab/ci/build/policy/tags.rb b/lib/gitlab/ci/build/policy/tags.rb new file mode 100644 index 00000000000..c7d097e28fe --- /dev/null +++ b/lib/gitlab/ci/build/policy/tags.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Gitlab + module Ci + module Build + module Policy + class Tags < Policy::Specification + def initialize(patterns) + @patterns = Array(patterns) + end + + def satisfied_by?(pipeline, seed = nil) + return false unless pipeline.tag? + + @patterns.any? do |pattern| + matches_pattern?(pattern, pipeline.ref) + end + end + + private + + def matches_pattern?(pattern, subject) + if pattern.first == "/" && pattern.last == "/" + Regexp.new(pattern[1...-1]) =~ subject + else + pattern == subject + end + end + end + end + end + end +end -- cgit v1.2.1