Skip to content

Commit

Permalink
Move file mapping logic to gitlab proxy client class
Browse files Browse the repository at this point in the history
  • Loading branch information
andrcuns committed Aug 21, 2023
1 parent c9789c4 commit 8938afd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 46 deletions.
64 changes: 64 additions & 0 deletions common/lib/dependabot/clients/gitlab_with_retries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ module Clients
class GitlabWithRetries
RETRYABLE_ERRORS = [Gitlab::Error::BadGateway].freeze

class ContentEncoding
BASE64 = "base64"
TEXT = "text"
end

#######################
# Constructor methods #
#######################
Expand Down Expand Up @@ -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<Dependabot::DependencyFile>] 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)
Expand All @@ -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<Dependabot::DependencyFile>] files
# @return [Array<Hash>]
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
24 changes: 1 addition & 23 deletions common/lib/dependabot/pull_request_creator/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 1 addition & 23 deletions common/lib/dependabot/pull_request_updater/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8938afd

Please sign in to comment.