summaryrefslogtreecommitdiff
path: root/app/models/x509_commit_signature.rb
blob: 57d809f7cfb69949434825587d4189e86fb33ee2 (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
# frozen_string_literal: true

class X509CommitSignature < ApplicationRecord
  include ShaAttribute

  sha_attribute :commit_sha

  enum verification_status: {
    unverified: 0,
    verified: 1
  }

  belongs_to :project, class_name: 'Project', foreign_key: 'project_id', optional: false
  belongs_to :x509_certificate, class_name: 'X509Certificate', foreign_key: 'x509_certificate_id', optional: false

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

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

  def self.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 self.unsigned_commit_shas(commit_shas)
    return [] if commit_shas.empty?

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

  def commit
    project.commit(commit_sha)
  end

  def x509_commit
    return unless commit

    Gitlab::X509::Commit.new(commit)
  end

  def user
    commit.committer
  end
end