2020-01-29 10:08:59 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MergeRequestContextCommit < ApplicationRecord
|
|
|
|
include CachedCommit
|
|
|
|
include ShaAttribute
|
|
|
|
|
|
|
|
belongs_to :merge_request
|
|
|
|
has_many :diff_files, class_name: 'MergeRequestContextCommitDiffFile'
|
|
|
|
|
|
|
|
sha_attribute :sha
|
|
|
|
|
|
|
|
validates :sha, presence: true
|
|
|
|
validates :sha, uniqueness: { message: 'has already been added' }
|
|
|
|
|
2021-06-11 14:10:13 -04:00
|
|
|
serialize :trailers, Serializers::Json # rubocop:disable Cop/ActiveRecordSerialize
|
2021-01-20 10:10:29 -05:00
|
|
|
validates :trailers, json_schema: { filename: 'git_trailers' }
|
|
|
|
|
2020-08-04 14:09:49 -04:00
|
|
|
# Sort by committed date in descending order to ensure latest commits comes on the top
|
|
|
|
scope :order_by_committed_date_desc, -> { order('committed_date DESC') }
|
|
|
|
|
2020-01-29 10:08:59 -05:00
|
|
|
# delete all MergeRequestContextCommit & MergeRequestContextCommitDiffFile for given merge_request & commit SHAs
|
|
|
|
def self.delete_bulk(merge_request, commits)
|
|
|
|
commit_ids = commits.map(&:sha)
|
|
|
|
merge_request.merge_request_context_commits.where(sha: commit_ids).delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
# create MergeRequestContextCommit by given commit sha and it's diff file record
|
2020-10-01 11:10:05 -04:00
|
|
|
def self.bulk_insert(rows, **args)
|
2021-11-15 10:10:57 -05:00
|
|
|
ApplicationRecord.legacy_bulk_insert('merge_request_context_commits', rows, **args) # rubocop:disable Gitlab/BulkInsert
|
2020-01-29 10:08:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_commit
|
|
|
|
# Here we are storing the commit sha because to_hash removes the sha parameter and we lose
|
|
|
|
# the reference, this happens because we are storing the ID in db and the Commit class replaces
|
|
|
|
# id with sha and removes it, so in our case it will be some incremented integer which is not
|
|
|
|
# what we want
|
|
|
|
commit_hash = attributes.except('id').to_hash
|
|
|
|
commit_hash['id'] = sha
|
|
|
|
Commit.from_hash(commit_hash, merge_request.target_project)
|
|
|
|
end
|
|
|
|
end
|