1# frozen_string_literal: true
2module CommitSignature
3  extend ActiveSupport::Concern
4
5  included do
6    include ShaAttribute
7
8    sha_attribute :commit_sha
9
10    enum verification_status: {
11      unverified: 0,
12      verified: 1,
13      same_user_different_email: 2,
14      other_user: 3,
15      unverified_key: 4,
16      unknown_key: 5,
17      multiple_signatures: 6
18    }
19
20    belongs_to :project, class_name: 'Project', foreign_key: 'project_id', optional: false
21
22    validates :commit_sha, presence: true
23    validates :project_id, presence: true
24
25    scope :by_commit_sha, ->(shas) { where(commit_sha: shas) }
26  end
27
28  class_methods do
29    def safe_create!(attributes)
30      create_with(attributes)
31        .safe_find_or_create_by!(commit_sha: attributes[:commit_sha])
32    end
33
34    # Find commits that are lacking a signature in the database at present
35    def unsigned_commit_shas(commit_shas)
36      return [] if commit_shas.empty?
37
38      signed = by_commit_sha(commit_shas).pluck(:commit_sha)
39      commit_shas - signed
40    end
41  end
42
43  def commit
44    project.commit(commit_sha)
45  end
46
47  def user
48    commit.committer
49  end
50end
51