Welcome to mirror list, hosted at ThFree Co, Russian Federation.

signed_commit.rb « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 410e71f51a19b2f8cfaad64c98642682cdef1b85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true

module Gitlab
  class SignedCommit
    include Gitlab::Utils::StrongMemoize

    delegate :id, to: :@commit

    def initialize(commit)
      @commit = commit

      if commit.project
        repo = commit.project.repository.raw_repository
        @signature_data = Gitlab::Git::Commit.extract_signature_lazily(repo, commit.sha || commit.id)
      end

      lazy_signature
    end

    def signature
      return @signature if @signature

      cached_signature = lazy_signature&.itself

      return @signature = cached_signature if cached_signature.present?

      @signature = create_cached_signature!
    end

    def update_signature!(cached_signature)
      cached_signature.update!(attributes)
      @signature = cached_signature
    end

    def signature_text
      strong_memoize(:signature_text) do
        @signature_data.itself ? @signature_data[0] : nil
      end
    end

    def signed_text
      strong_memoize(:signed_text) do
        @signature_data.itself ? @signature_data[1] : nil
      end
    end

    private

    def signature_class
      raise NotImplementedError, '`signature_class` must be implmented by subclass`'
    end

    def lazy_signature
      BatchLoader.for(@commit.sha).batch do |shas, loader|
        signature_class.by_commit_sha(shas).each do |signature|
          loader.call(signature.commit_sha, signature)
        end
      end
    end

    def create_cached_signature!
      return if attributes.nil?

      return signature_class.new(attributes) if Gitlab::Database.read_only?

      signature_class.safe_create!(attributes)
    end
  end
end