summaryrefslogtreecommitdiff
path: root/lib/gitlab/signed_tag.rb
blob: 3b22cb7622d7cc87cb8a46366e10f39bad26678f (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
# frozen_string_literal: true

module Gitlab
  class SignedTag
    include Gitlab::Utils::StrongMemoize

    def initialize(repository, tag)
      @repository = repository
      @tag = tag

      if Feature.enabled?(:get_tag_signatures)
        @signature_data = Gitlab::Git::Tag.extract_signature_lazily(repository, tag.id) if repository
      else
        @signature_data = [signature_text_of_message.b, signed_text_of_message.b]
      end
    end

    def signature
      return unless @tag.has_signature?
    end

    def signature_text
      @signature_data&.fetch(0)
    end

    def signed_text
      @signature_data&.fetch(1)
    end

    private

    def signature_text_of_message
      @tag.message.slice(@tag.message.index("-----BEGIN SIGNED MESSAGE-----")..-1)
    rescue StandardError
      nil
    end

    def signed_text_of_message
      %{object #{@tag.target_commit.id}
type commit
tag #{@tag.name}
tagger #{@tag.tagger.name} <#{@tag.tagger.email}> #{@tag.tagger.date.seconds} #{@tag.tagger.timezone}

#{@tag.message.gsub(/-----BEGIN SIGNED MESSAGE-----(.*)-----END SIGNED MESSAGE-----/m, "")}}
    end
  end
end