2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
class GpgSignature < ApplicationRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
include ShaAttribute
|
|
|
|
|
|
|
|
sha_attribute :commit_sha
|
|
|
|
sha_attribute :gpg_key_primary_keyid
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
enum verification_status: {
|
|
|
|
unverified: 0,
|
|
|
|
verified: 1,
|
|
|
|
same_user_different_email: 2,
|
|
|
|
other_user: 3,
|
|
|
|
unverified_key: 4,
|
|
|
|
unknown_key: 5
|
|
|
|
}
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
belongs_to :project
|
|
|
|
belongs_to :gpg_key
|
2018-03-17 18:26:18 +05:30
|
|
|
belongs_to :gpg_key_subkey
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
validates :commit_sha, presence: true
|
|
|
|
validates :project_id, presence: true
|
|
|
|
validates :gpg_key_primary_keyid, presence: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def self.with_key_and_subkeys(gpg_key)
|
|
|
|
subkey_ids = gpg_key.subkeys.pluck(:id)
|
|
|
|
|
|
|
|
where(
|
|
|
|
arel_table[:gpg_key_id].eq(gpg_key.id).or(
|
|
|
|
arel_table[:gpg_key_subkey_id].in(subkey_ids)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def self.safe_create!(attributes)
|
|
|
|
create_with(attributes)
|
|
|
|
.safe_find_or_create_by!(commit_sha: attributes[:commit_sha])
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def gpg_key=(model)
|
|
|
|
case model
|
|
|
|
when GpgKey
|
|
|
|
super
|
|
|
|
when GpgKeySubkey
|
|
|
|
self.gpg_key_subkey = model
|
|
|
|
when NilClass
|
|
|
|
super
|
|
|
|
self.gpg_key_subkey = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def gpg_key
|
|
|
|
if gpg_key_id
|
|
|
|
super
|
|
|
|
elsif gpg_key_subkey_id
|
|
|
|
gpg_key_subkey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def gpg_key_primary_keyid
|
|
|
|
super&.upcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit
|
|
|
|
project.commit(commit_sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
def gpg_commit
|
2018-03-17 18:26:18 +05:30
|
|
|
return unless commit
|
|
|
|
|
|
|
|
Gitlab::Gpg::Commit.new(commit)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|