summaryrefslogtreecommitdiff
path: root/qa/qa/resource/repository/commit.rb
blob: f5dba164de6938c247251cfdd9fb485482b0abd7 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# frozen_string_literal: true

module QA
  module Resource
    module Repository
      class Commit < Base
        attr_accessor :author_email,
                      :author_name,
                      :branch,
                      :commit_message,
                      :file_path,
                      :sha,
                      :start_branch

        attribute :short_id

        attribute :project do
          Project.fabricate! do |resource|
            resource.name = 'project-with-commit'
          end
        end

        def initialize
          @commit_message = 'QA Test - Commit message'
        end

        def add_files(files)
          validate_files!(files)

          @add_files = files
        end

        def add_directory(dir)
          raise "Must set directory as a Pathname" unless dir.is_a?(Pathname)

          files_to_add = []

          dir.each_child do |child|
            case child.ftype?
            when "file"
              files_to_add.append({
                file_path: child.to_s,
                content: child.read
              })
            when "directory"
              add_directory(child)
            else
              continue
            end
          end

          validate_files!(files_to_add)

          @add_files.merge(files_to_add)
        end

        def update_files(files)
          validate_files!(files)

          @update_files = files
        end

        # If `actions` are specified, it performs the actions to create,
        # update, or delete commits. If no actions are specified it
        # gets existing commits.
        def fabricate_via_api!
          return api_get if actions.empty?

          super
        rescue ResourceNotFoundError
          super
        end

        def api_get_path
          api_post_path
        end

        def api_post_path
          "/projects/#{CGI.escape(project.path_with_namespace)}/repository/commits"
        end

        def api_post_body
          {
            branch: @branch || project.default_branch,
            author_email: @author_email || Runtime::User.default_email,
            author_name: @author_name || Runtime::User.username,
            commit_message: commit_message,
            actions: actions
          }.merge(new_branch)
        end

        def actions
          pending_actions = []
          pending_actions << @add_files.map { |file| file.merge({ action: "create" }) } if @add_files
          pending_actions << @update_files.map { |file| file.merge({ action: "update" }) } if @update_files
          pending_actions.flatten
        end

        private

        def validate_files!(files)
          if !files.is_a?(Array) ||
              files.empty? ||
              files.any? { |file| !file.has_key?(:file_path) || !file.has_key?(:content) }
            raise ArgumentError, "Please provide an array of hashes e.g.: [{file_path: 'file1', content: 'foo'}]"
          end
        end

        def new_branch
          return {} unless start_branch

          {
            start_branch: start_branch
          }
        end
      end
    end
  end
end