Skip to content

Commit

Permalink
Merge pull request dependabot#7850 from andrcuns/fix-encoding
Browse files Browse the repository at this point in the history
Fix encoding option value for gitlab commit creation
  • Loading branch information
deivid-rodriguez committed Aug 22, 2023
2 parents 08ca9ef + 819fd21 commit 9b0cb77
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 56 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
}
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
}
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
10 changes: 5 additions & 5 deletions common/spec/dependabot/pull_request_creator/gitlab_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down Expand Up @@ -309,7 +309,7 @@
action: "update",
file_path: files[0].symlink_target,
content: files[0].content,
encoding: "utf-8"
encoding: "text"
}
]
}
Expand Down
10 changes: 5 additions & 5 deletions common/spec/dependabot/pull_request_updater/gitlab_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -260,7 +260,7 @@
action: "update",
file_path: files[0].symlink_target,
content: files[0].content,
encoding: "utf-8"
encoding: "text"
}
],
force: true,
Expand Down

0 comments on commit 9b0cb77

Please sign in to comment.