summaryrefslogtreecommitdiff
path: root/qa/qa/factory/resource/branch.rb
blob: b05d1e252ecb58ca6fda3de9907e746c2f092865 (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
module QA
  module Factory
    module Resource
      class Branch < Factory::Base
        attr_accessor :project, :branch_name,
                      :allow_to_push, :allow_to_merge, :protected

        attribute :project do
          Factory::Resource::Project.fabricate! do |resource|
            resource.name = 'protected-branch-project'
          end
        end

        def initialize
          @branch_name = 'test/branch'
          @allow_to_push = true
          @allow_to_merge = true
          @protected = false
        end

        def fabricate!
          project.visit!

          Factory::Repository::ProjectPush.fabricate! do |resource|
            resource.project = project
            resource.file_name = 'kick-off.txt'
            resource.commit_message = 'First commit'
          end

          branch = Factory::Repository::ProjectPush.fabricate! do |resource|
            resource.project = project
            resource.file_name = 'README.md'
            resource.commit_message = 'Add readme'
            resource.branch_name = 'master'
            resource.new_branch = false
            resource.remote_branch = @branch_name
          end

          Page::Project::Show.perform do |page|
            page.wait { page.has_content?(branch_name) }
          end

          # The upcoming process will make it access the Protected Branches page,
          # select the already created branch and protect it according
          # to `allow_to_push` variable.
          return branch unless @protected

          Page::Project::Menu.perform(&:click_repository_settings)

          Page::Project::Settings::Repository.perform do |setting|
            setting.expand_protected_branches do |page|
              page.select_branch(branch_name)

              if allow_to_push
                page.allow_devs_and_maintainers_to_push
              else
                page.allow_no_one_to_push
              end

              if allow_to_merge
                page.allow_devs_and_maintainers_to_merge
              else
                page.allow_no_one_to_merge
              end

              page.wait(reload: false) do
                !page.first('.btn-success').disabled?
              end

              page.protect_branch
            end
          end
        end
      end
    end
  end
end