File: commit_signature.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (63 lines) | stat: -rw-r--r-- 1,663 bytes parent folder | download
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
# frozen_string_literal: true

module CommitSignature
  extend ActiveSupport::Concern

  included do
    include ShaAttribute
    include EachBatch

    sha_attribute :commit_sha

    enum verification_status: Enums::CommitSignature.verification_statuses

    belongs_to :project, class_name: 'Project', foreign_key: 'project_id', optional: false

    validates :commit_sha, presence: true
    validates :project_id, presence: true

    scope :by_commit_sha, ->(shas) { where(commit_sha: shas) }
  end

  class_methods do
    def safe_create!(attributes)
      create_with(attributes)
        .safe_find_or_create_by!(commit_sha: attributes[:commit_sha])
    end

    # Find commits that are lacking a signature in the database at present
    def unsigned_commit_shas(commit_shas)
      return [] if commit_shas.empty?

      signed = by_commit_sha(commit_shas).pluck(:commit_sha)
      commit_shas - signed
    end
  end

  def commit
    project.commit(commit_sha)
  end

  def signed_by_user
    raise NoMethodError, 'must implement `signed_by_user` method'
  end

  def reverified_status
    return verification_status unless Feature.enabled?(:check_for_mailmapped_commit_emails, project)
    return verification_status unless verified_signature_type? || verified_system?

    verified_emails = signed_by_user&.verified_emails
    if verified_emails&.exclude?(commit.author_email)
      'unverified_author_email'
    else
      verification_status
    end
  end

  private

  # We are omitting x509 signature types until https://gitlab.com/gitlab-org/gitlab/-/issues/498188 is complete
  def verified_signature_type?
    verified? && (ssh? || gpg?)
  end
end