summaryrefslogtreecommitdiff
path: root/spec/factories/protected_branches.rb
blob: bac1cf2159656dc4d7c6f6c3193e83cca24e1f0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true

FactoryBot.define do
  factory :protected_branch do
    sequence(:name) { |n| "protected_branch_#{n}" }
    project

    transient do
      default_push_level { true }
      default_merge_level { true }
      default_access_level { true }
    end

    trait :create_branch_on_repository do
      association :project, factory: [:project, :repository]

      transient do
        repository_branch_name { name }
      end

      after(:create) do |protected_branch, evaluator|
        project = protected_branch.project

        project.repository.create_branch(evaluator.repository_branch_name, project.default_branch_or_main)
      end
    end

    trait :developers_can_push do
      transient do
        default_push_level { false }
      end

      after(:build) do |protected_branch|
        protected_branch.push_access_levels.new(access_level: Gitlab::Access::DEVELOPER)
      end
    end

    trait :developers_can_merge do
      transient do
        default_merge_level { false }
      end

      after(:build) do |protected_branch|
        protected_branch.merge_access_levels.new(access_level: Gitlab::Access::DEVELOPER)
      end
    end

    trait :no_one_can_push do
      transient do
        default_push_level { false }
      end

      after(:build) do |protected_branch|
        protected_branch.push_access_levels.new(access_level: Gitlab::Access::NO_ACCESS)
      end
    end

    trait :maintainers_can_push do
      transient do
        default_push_level { false }
      end

      after(:build) do |protected_branch|
        protected_branch.push_access_levels.new(access_level: Gitlab::Access::MAINTAINER)
      end
    end

    after(:build) do |protected_branch, evaluator|
      if evaluator.default_access_level && evaluator.default_push_level
        protected_branch.push_access_levels.new(access_level: Gitlab::Access::MAINTAINER)
      end

      if evaluator.default_access_level && evaluator.default_merge_level
        protected_branch.merge_access_levels.new(access_level: Gitlab::Access::MAINTAINER)
      end
    end

    trait :no_one_can_merge do
      after(:create) do |protected_branch|
        protected_branch.merge_access_levels.first.update!(access_level: Gitlab::Access::NO_ACCESS)
      end
    end
  end
end