diff options
Diffstat (limited to 'qa/qa/resource/repository/commit.rb')
-rw-r--r-- | qa/qa/resource/repository/commit.rb | 99 |
1 files changed, 53 insertions, 46 deletions
diff --git a/qa/qa/resource/repository/commit.rb b/qa/qa/resource/repository/commit.rb index f5dba164de6..54093a5c195 100644 --- a/qa/qa/resource/repository/commit.rb +++ b/qa/qa/resource/repository/commit.rb @@ -22,42 +22,7 @@ module QA 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 + @actions = [] end # If `actions` are specified, it performs the actions to create, @@ -72,32 +37,74 @@ module QA end def api_get_path - api_post_path + "/projects/#{CGI.escape(project.path_with_namespace)}/repository/commits" end def api_post_path - "/projects/#{CGI.escape(project.path_with_namespace)}/repository/commits" + api_get_path 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, + branch: branch || project.default_branch, + author_email: author_email || api_client.user&.email || Runtime::User.default_email, + author_name: author_name || api_client.user&.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 + # Add files + # Pass in array of new files like, example: + # [{ "file_path": "foo/bar", "content": "some content" }] + # + # @param [Array<Hash>] files + # @return [void] + def add_files(files) + validate_files!(files) + + actions.push(*files.map { |file| file.merge({ action: "create" }) }) + end + + # Update files + # Pass in array of files and it's contents, example: + # [{ "file_path": "foo/bar", "content": "some content" }] + # + # @param [Array<Hash>] files + # @return [void] + def update_files(files) + validate_files!(files) + + actions.push(*files.map { |file| file.merge({ action: "update" }) }) + end + + # Add all files from directory + # + # @param [Pathname] dir + # @return [void] + 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 "directory" + add_directory(child) + when "file" + files_to_add.push({ file_path: child.basename, content: child.read }) + else + continue + end + end + + add_files(files_to_add) end private + attr_reader :actions + def validate_files!(files) if !files.is_a?(Array) || files.empty? || |