class Dependabot::PullRequestUpdater::Gitlab

Attributes

base_commit[R]
credentials[R]
files[R]
old_commit[R]
pull_request_number[R]
source[R]

Public Class Methods

new(source:, base_commit:, old_commit:, files:, credentials:, pull_request_number:) click to toggle source
# File lib/dependabot/pull_request_updater/gitlab.rb, line 13
def initialize(source:, base_commit:, old_commit:, files:,
               credentials:, pull_request_number:)
  @source              = source
  @base_commit         = base_commit
  @old_commit          = old_commit
  @files               = files
  @credentials         = credentials
  @pull_request_number = pull_request_number
end

Public Instance Methods

update() click to toggle source
# File lib/dependabot/pull_request_updater/gitlab.rb, line 23
def update
  return unless merge_request_exists?
  return unless branch_exists?(merge_request.source_branch)

  create_commit
  merge_request.source_branch
end

Private Instance Methods

branch_exists?(name) click to toggle source
# File lib/dependabot/pull_request_updater/gitlab.rb, line 55
def branch_exists?(name)
  gitlab_client_for_source.branch(source.repo, name)
rescue ::Gitlab::Error::NotFound
  false
end
commit_being_updated() click to toggle source
# File lib/dependabot/pull_request_updater/gitlab.rb, line 61
def commit_being_updated
  gitlab_client_for_source.commit(source.repo, old_commit)
end
create_commit() click to toggle source
# File lib/dependabot/pull_request_updater/gitlab.rb, line 76
def create_commit
  actions = files.map do |file|
    {
      action: file_action(file),
      file_path: file.type == "symlink" ? file.symlink_target : file.path,
      content: file.content
    }
  end

  gitlab_client_for_source.create_commit(
    source.repo,
    merge_request.source_branch,
    commit_being_updated.title,
    actions,
    force: true,
    start_branch: merge_request.target_branch
  )
end
file_action(file) click to toggle source

@param [DependencyFile] file

# File lib/dependabot/pull_request_updater/gitlab.rb, line 66
def file_action(file)
  if file.operation == Dependabot::DependencyFile::Operation::DELETE
    "delete"
  elsif file.operation == Dependabot::DependencyFile::Operation::CREATE
    "create"
  else
    "update"
  end
end
gitlab_client_for_source() click to toggle source
# File lib/dependabot/pull_request_updater/gitlab.rb, line 47
def gitlab_client_for_source
  @gitlab_client_for_source ||=
    Dependabot::Clients::GitlabWithRetries.for_source(
      source: source,
      credentials: credentials
    )
end
merge_request() click to toggle source
# File lib/dependabot/pull_request_updater/gitlab.rb, line 40
def merge_request
  @merge_request ||= gitlab_client_for_source.merge_request(
    source.repo,
    pull_request_number
  )
end
merge_request_exists?() click to toggle source
# File lib/dependabot/pull_request_updater/gitlab.rb, line 33
def merge_request_exists?
  merge_request
  true
rescue ::Gitlab::Error::NotFound
  false
end