From 5bea7a82a239fd6c39ce880e61b20cce26f86da7 Mon Sep 17 00:00:00 2001 From: Andrejs Cunskis Date: Sat, 19 Aug 2023 12:21:32 +0300 Subject: [PATCH 1/2] Fix encoding option value for gitlab commit creation --- common/lib/dependabot/pull_request_creator/gitlab.rb | 2 +- common/lib/dependabot/pull_request_updater/gitlab.rb | 2 +- .../dependabot/pull_request_creator/gitlab_spec.rb | 10 +++++----- .../dependabot/pull_request_updater/gitlab_spec.rb | 10 +++++----- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/common/lib/dependabot/pull_request_creator/gitlab.rb b/common/lib/dependabot/pull_request_creator/gitlab.rb index 2b11092ae604..9ff8707f60cd 100644 --- a/common/lib/dependabot/pull_request_creator/gitlab.rb +++ b/common/lib/dependabot/pull_request_creator/gitlab.rb @@ -109,7 +109,7 @@ def file_actions action: file_action(file), file_path: file.type == "symlink" ? file.symlink_target : file.path, content: file.content, - encoding: file.content_encoding + encoding: file.content_encoding == Dependabot::DependencyFile::ContentEncoding::BASE64 ? "base64" : "text" } end end diff --git a/common/lib/dependabot/pull_request_updater/gitlab.rb b/common/lib/dependabot/pull_request_updater/gitlab.rb index 027499f3fb8b..2fd0eeb11ae0 100644 --- a/common/lib/dependabot/pull_request_updater/gitlab.rb +++ b/common/lib/dependabot/pull_request_updater/gitlab.rb @@ -80,7 +80,7 @@ def file_actions action: file_action(file), file_path: file.type == "symlink" ? file.symlink_target : file.path, content: file.content, - encoding: file.content_encoding + encoding: file.content_encoding == Dependabot::DependencyFile::ContentEncoding::BASE64 ? "base64" : "text" } end end diff --git a/common/spec/dependabot/pull_request_creator/gitlab_spec.rb b/common/spec/dependabot/pull_request_creator/gitlab_spec.rb index ae8aafb5480a..d4794ff86ea9 100644 --- a/common/spec/dependabot/pull_request_creator/gitlab_spec.rb +++ b/common/spec/dependabot/pull_request_creator/gitlab_spec.rb @@ -149,25 +149,25 @@ action: "update", file_path: gemfile.path, content: gemfile.content, - encoding: "utf-8" + encoding: "text" }, { action: "update", file_path: gemfile_lock.path, content: gemfile_lock.content, - encoding: "utf-8" + encoding: "text" }, { action: "create", file_path: created_file.path, content: created_file.content, - encoding: "utf-8" + encoding: "text" }, { action: "delete", file_path: deleted_file.path, content: "", - encoding: "utf-8" + encoding: "text" } ] } @@ -309,7 +309,7 @@ action: "update", file_path: files[0].symlink_target, content: files[0].content, - encoding: "utf-8" + encoding: "text" } ] } diff --git a/common/spec/dependabot/pull_request_updater/gitlab_spec.rb b/common/spec/dependabot/pull_request_updater/gitlab_spec.rb index 50fcfd9fc461..502ae8c8272c 100644 --- a/common/spec/dependabot/pull_request_updater/gitlab_spec.rb +++ b/common/spec/dependabot/pull_request_updater/gitlab_spec.rb @@ -158,25 +158,25 @@ action: "update", file_path: gemfile.path, content: gemfile.content, - encoding: "utf-8" + encoding: "text" }, { action: "update", file_path: gemfile_lock.path, content: gemfile_lock.content, - encoding: "utf-8" + encoding: "text" }, { action: "create", file_path: created_file.path, content: created_file.content, - encoding: "utf-8" + encoding: "text" }, { action: "delete", file_path: deleted_file.path, content: "", - encoding: "utf-8" + encoding: "text" } ], force: true, @@ -260,7 +260,7 @@ action: "update", file_path: files[0].symlink_target, content: files[0].content, - encoding: "utf-8" + encoding: "text" } ], force: true, From 819fd215e9fc392956eae3c33348f3e0e29e04e0 Mon Sep 17 00:00:00 2001 From: Andrejs Cunskis Date: Mon, 21 Aug 2023 20:10:20 +0300 Subject: [PATCH 2/2] Move file mapping logic to gitlab proxy client class --- .../dependabot/clients/gitlab_with_retries.rb | 64 +++++++++++++++++++ .../dependabot/pull_request_creator/gitlab.rb | 24 +------ .../dependabot/pull_request_updater/gitlab.rb | 24 +------ 3 files changed, 66 insertions(+), 46 deletions(-) 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