2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class MergeRequestDiffCommit < ApplicationRecord
|
2021-01-27 07:09:01 -05:00
|
|
|
extend SuppressCompositePrimaryKeyWarning
|
|
|
|
|
2020-02-12 16:08:48 -05:00
|
|
|
include BulkInsertSafe
|
2017-06-16 10:00:58 -04:00
|
|
|
include ShaAttribute
|
2020-01-29 10:08:59 -05:00
|
|
|
include CachedCommit
|
2021-10-27 14:13:16 -04:00
|
|
|
include IgnorableColumns
|
2021-11-05 14:12:21 -04:00
|
|
|
include FromUnion
|
2021-10-27 14:13:16 -04:00
|
|
|
|
|
|
|
ignore_column %i[author_name author_email committer_name committer_email],
|
|
|
|
remove_with: '14.6',
|
|
|
|
remove_after: '2021-11-22'
|
2017-06-16 10:00:58 -04:00
|
|
|
|
|
|
|
belongs_to :merge_request_diff
|
|
|
|
|
2021-07-07 14:08:30 -04:00
|
|
|
# This relation is called `commit_author` and not `author`, as the project
|
|
|
|
# import/export logic treats relations named `author` as instances of the
|
|
|
|
# `User` class.
|
|
|
|
#
|
|
|
|
# NOTE: these columns are _not_ indexed, nor do they use foreign keys.
|
|
|
|
#
|
|
|
|
# This is deliberate, as creating these indexes on GitLab.com takes a _very_
|
|
|
|
# long time. In addition, there's no real need for them either based on how
|
|
|
|
# this data is used.
|
|
|
|
#
|
|
|
|
# For more information, refer to the following:
|
|
|
|
#
|
|
|
|
# - https://gitlab.com/gitlab-com/gl-infra/production/-/issues/5038#note_614592881
|
|
|
|
# - https://gitlab.com/gitlab-org/gitlab/-/merge_requests/63669
|
|
|
|
belongs_to :commit_author, class_name: 'MergeRequest::DiffCommitUser'
|
|
|
|
belongs_to :committer, class_name: 'MergeRequest::DiffCommitUser'
|
|
|
|
|
2017-06-16 10:00:58 -04:00
|
|
|
sha_attribute :sha
|
|
|
|
alias_attribute :id, :sha
|
|
|
|
|
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' }
|
|
|
|
|
2021-07-07 14:08:30 -04:00
|
|
|
scope :with_users, -> { preload(:commit_author, :committer) }
|
|
|
|
|
|
|
|
# A list of keys of which their values need to be trimmed before they can be
|
|
|
|
# inserted into the merge_request_diff_commit_users table.
|
|
|
|
TRIM_USER_KEYS =
|
|
|
|
%i[author_name author_email committer_name committer_email].freeze
|
|
|
|
|
2020-03-04 07:07:52 -05:00
|
|
|
# Deprecated; use `bulk_insert!` from `BulkInsertSafe` mixin instead.
|
|
|
|
# cf. https://gitlab.com/gitlab-org/gitlab/issues/207989 for progress
|
2017-06-16 10:00:58 -04:00
|
|
|
def self.create_bulk(merge_request_diff_id, commits)
|
2021-07-07 14:08:30 -04:00
|
|
|
commit_hashes, user_tuples = prepare_commits_for_bulk_insert(commits)
|
|
|
|
users = MergeRequest::DiffCommitUser.bulk_find_or_create(user_tuples)
|
|
|
|
|
|
|
|
rows = commit_hashes.map.with_index do |commit_hash, index|
|
2017-06-16 10:00:58 -04:00
|
|
|
sha = commit_hash.delete(:id)
|
2021-07-07 14:08:30 -04:00
|
|
|
author = users[[commit_hash[:author_name], commit_hash[:author_email]]]
|
|
|
|
committer =
|
|
|
|
users[[commit_hash[:committer_name], commit_hash[:committer_email]]]
|
2017-06-16 10:00:58 -04:00
|
|
|
|
2021-10-27 14:13:16 -04:00
|
|
|
# These fields are only used to determine the author/committer IDs, we
|
|
|
|
# don't store them in the DB.
|
|
|
|
commit_hash = commit_hash
|
|
|
|
.except(:author_name, :author_email, :committer_name, :committer_email)
|
|
|
|
|
2017-06-16 10:00:58 -04:00
|
|
|
commit_hash.merge(
|
2021-10-27 14:13:16 -04:00
|
|
|
commit_author_id: author.id,
|
|
|
|
committer_id: committer.id,
|
2017-06-16 10:00:58 -04:00
|
|
|
merge_request_diff_id: merge_request_diff_id,
|
|
|
|
relative_order: index,
|
2020-01-29 10:08:59 -05:00
|
|
|
sha: Gitlab::Database::ShaAttribute.serialize(sha), # rubocop:disable Cop/ActiveRecordSerialize
|
2017-10-30 19:21:56 -04:00
|
|
|
authored_date: Gitlab::Database.sanitize_timestamp(commit_hash[:authored_date]),
|
2021-01-20 10:10:29 -05:00
|
|
|
committed_date: Gitlab::Database.sanitize_timestamp(commit_hash[:committed_date]),
|
|
|
|
trailers: commit_hash.fetch(:trailers, {}).to_json
|
2017-06-16 10:00:58 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-11-15 10:10:57 -05:00
|
|
|
ApplicationRecord.legacy_bulk_insert(self.table_name, rows) # rubocop:disable Gitlab/BulkInsert
|
2017-06-16 10:00:58 -04:00
|
|
|
end
|
2021-01-29 13:09:17 -05:00
|
|
|
|
2021-07-07 14:08:30 -04:00
|
|
|
def self.prepare_commits_for_bulk_insert(commits)
|
|
|
|
user_tuples = Set.new
|
|
|
|
hashes = commits.map do |commit|
|
|
|
|
hash = commit.to_hash.except(:parent_ids)
|
|
|
|
|
|
|
|
TRIM_USER_KEYS.each do |key|
|
|
|
|
hash[key] = MergeRequest::DiffCommitUser.prepare(hash[key])
|
|
|
|
end
|
|
|
|
|
|
|
|
user_tuples << [hash[:author_name], hash[:author_email]]
|
|
|
|
user_tuples << [hash[:committer_name], hash[:committer_email]]
|
|
|
|
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
|
|
|
[hashes, user_tuples]
|
|
|
|
end
|
|
|
|
|
2021-01-29 13:09:17 -05:00
|
|
|
def self.oldest_merge_request_id_per_commit(project_id, shas)
|
|
|
|
# This method is defined here and not on MergeRequest, otherwise the SHA
|
|
|
|
# values used in the WHERE below won't be encoded correctly.
|
|
|
|
select(['merge_request_diff_commits.sha AS sha', 'min(merge_requests.id) AS merge_request_id'])
|
|
|
|
.joins(:merge_request_diff)
|
|
|
|
.joins(
|
|
|
|
'INNER JOIN merge_requests ' \
|
|
|
|
'ON merge_requests.latest_merge_request_diff_id = merge_request_diffs.id'
|
|
|
|
)
|
|
|
|
.where(sha: shas)
|
|
|
|
.where(
|
|
|
|
merge_requests: {
|
|
|
|
target_project_id: project_id,
|
|
|
|
state_id: MergeRequest.available_states[:merged]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.group(:sha)
|
|
|
|
end
|
2021-07-07 14:08:30 -04:00
|
|
|
|
|
|
|
def author_name
|
2021-10-28 08:10:22 -04:00
|
|
|
commit_author&.name
|
2021-07-07 14:08:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def author_email
|
2021-10-28 08:10:22 -04:00
|
|
|
commit_author&.email
|
2021-07-07 14:08:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def committer_name
|
2021-10-28 08:10:22 -04:00
|
|
|
committer&.name
|
2021-07-07 14:08:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def committer_email
|
2021-10-28 08:10:22 -04:00
|
|
|
committer&.email
|
2021-07-07 14:08:30 -04:00
|
|
|
end
|
2017-06-16 10:00:58 -04:00
|
|
|
end
|