Class: Gitlab::Changelog::Committer

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/changelog/committer.rb

Overview

A class used for committing a release's changelog to a Git repository.

Instance Method Summary collapse

Constructor Details

#initialize(project, user) ⇒ Committer

Returns a new instance of Committer.


7
8
9
10
# File 'lib/gitlab/changelog/committer.rb', line 7

def initialize(project, user)
  @project = project
  @user = user
end

Instance Method Details

#blob_content(file, commit = nil) ⇒ Object


62
63
64
65
66
# File 'lib/gitlab/changelog/committer.rb', line 62

def blob_content(file, commit = nil)
  return unless commit

  @project.repository.blob_at(commit.sha, file)&.data
end

#commit(release:, file:, branch:, message:) ⇒ Object

Commits a release's changelog to a file on a branch.

The `release` argument is a `Gitlab::Changelog::Release` for which to update the changelog.

The `file` argument specifies the path to commit the changes to.

The `branch` argument specifies the branch to commit the changes on.

The `message` argument specifies the commit message to use.


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/changelog/committer.rb', line 22

def commit(release:, file:, branch:, message:)
  # When retrying, we need to reprocess the existing changelog from
  # scratch, otherwise we may end up throwing away changes. As such, all
  # the logic is contained within the retry block.
  Retriable.retriable(on: Error) do
    commit = Gitlab::Git::Commit.last_for_path(
      @project.repository,
      branch,
      file,
      literal_pathspec: true
    )

    content = blob_content(file, commit)

    # If the release has already been added (e.g. concurrently by another
    # API call), we don't want to add it again.
    break if content&.match?(release.header_start_pattern)

    service = Files::MultiService.new(
      @project,
      @user,
      commit_message: message,
      branch_name: branch,
      start_branch: branch,
      actions: [
        {
          action: content ? 'update' : 'create',
          content: Generator.new(content.to_s).add(release),
          file_path: file,
          last_commit_id: commit&.sha
        }
      ]
    )

    result = service.execute

    raise Error, result[:message] if result[:status] != :success
  end
end