diff --git a/common/lib/dependabot/clients/gitlab_with_retries.rb b/common/lib/dependabot/clients/gitlab_with_retries.rb index e5e5c29eebe1..1f7423e8e1d2 100644 --- a/common/lib/dependabot/clients/gitlab_with_retries.rb +++ b/common/lib/dependabot/clients/gitlab_with_retries.rb @@ -7,6 +7,11 @@ module Clients class GitlabWithRetries RETRYABLE_ERRORS = [Gitlab::Error::BadGateway].freeze + class ContentEncoding + BASE64 = "base64" + TEXT = "text" + end + ####################### # Constructor methods # ####################### @@ -60,6 +65,24 @@ def initialize(max_retries: 3, **args) @client = ::Gitlab::Client.new(args) end + # Create commit in gitlab repo with correctly mapped file actions + # + # @param [String] repo + # @param [String] branch_name + # @param [String] commit_message + # @param [Array] files + # @param [Hash] options + # @return [Gitlab::ObjectifiedHash] + def create_commit(repo, branch_name, commit_message, files, **options) + @client.create_commit( + repo, + branch_name, + commit_message, + file_actions(files), + **options + ) + end + def method_missing(method_name, *args, &block) retry_connection_failures do if @client.respond_to?(method_name) @@ -85,6 +108,47 @@ def retry_connection_failures retry_attempt <= @max_retries ? retry : raise end end + + private + + # Array of file actions for a commit + # + # @param [Array] files + # @return [Array] + def file_actions(files) + files.map do |file| + { + action: file_action(file), + encoding: file_encoding(file), + file_path: file.type == "symlink" ? file.symlink_target : file.path, + content: file.content + } + end + end + + # Single file action + # + # @param [Dependabot::DependencyFile] file + # @return [String] + def file_action(file) + if file.operation == Dependabot::DependencyFile::Operation::DELETE + "delete" + elsif file.operation == Dependabot::DependencyFile::Operation::CREATE + "create" + else + "update" + end + end + + # Encoding option for gitlab commit operation + # + # @param [Dependabot::DependencyFile] file + # @return [String] + def file_encoding(file) + return ContentEncoding::BASE64 if file.content_encoding == Dependabot::DependencyFile::ContentEncoding::BASE64 + + ContentEncoding::TEXT + end end end end diff --git a/common/lib/dependabot/pull_request_creator/gitlab.rb b/common/lib/dependabot/pull_request_creator/gitlab.rb index 9ff8707f60cd..2be27b731b01 100644 --- a/common/lib/dependabot/pull_request_creator/gitlab.rb +++ b/common/lib/dependabot/pull_request_creator/gitlab.rb @@ -99,32 +99,10 @@ def create_commit source.repo, branch_name, commit_message, - file_actions + files ) end - def file_actions - files.map do |file| - { - action: file_action(file), - file_path: file.type == "symlink" ? file.symlink_target : file.path, - content: file.content, - encoding: file.content_encoding == Dependabot::DependencyFile::ContentEncoding::BASE64 ? "base64" : "text" - } - end - end - - # @param [DependencyFile] file - def file_action(file) - if file.operation == Dependabot::DependencyFile::Operation::DELETE - "delete" - elsif file.operation == Dependabot::DependencyFile::Operation::CREATE - "create" - else - "update" - end - end - def create_submodule_update_commit file = files.first diff --git a/common/lib/dependabot/pull_request_updater/gitlab.rb b/common/lib/dependabot/pull_request_updater/gitlab.rb index 2fd0eeb11ae0..719703f78f54 100644 --- a/common/lib/dependabot/pull_request_updater/gitlab.rb +++ b/common/lib/dependabot/pull_request_updater/gitlab.rb @@ -68,33 +68,11 @@ def create_commit source.repo, merge_request.source_branch, commit_being_updated.title, - file_actions, + files, force: true, start_branch: merge_request.target_branch ) end - - def file_actions - files.map do |file| - { - action: file_action(file), - file_path: file.type == "symlink" ? file.symlink_target : file.path, - content: file.content, - encoding: file.content_encoding == Dependabot::DependencyFile::ContentEncoding::BASE64 ? "base64" : "text" - } - end - end - - # @param [DependencyFile] file - def file_action(file) - if file.operation == Dependabot::DependencyFile::Operation::DELETE - "delete" - elsif file.operation == Dependabot::DependencyFile::Operation::CREATE - "create" - else - "update" - end - end end end end