Commit 964742f6 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Ensure merge request is created with valid diff object

* Add merge_request_diff validation to merge_request model
* Improve initialize of merge_request_diff object
* Rename some merge_request_diff methods for clarity
Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 988836bc
...@@ -15,7 +15,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -15,7 +15,7 @@ class MergeRequest < ActiveRecord::Base
serialize :merge_params, Hash serialize :merge_params, Hash
after_create :create_merge_request_diff, unless: :importing? before_validation :ensure_merge_request_diff, on: :create, unless: :importing?
after_update :update_merge_request_diff after_update :update_merge_request_diff
delegate :commits, :real_size, to: :merge_request_diff, prefix: nil delegate :commits, :real_size, to: :merge_request_diff, prefix: nil
...@@ -95,6 +95,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -95,6 +95,7 @@ class MergeRequest < ActiveRecord::Base
validates :merge_user, presence: true, if: :merge_when_build_succeeds? validates :merge_user, presence: true, if: :merge_when_build_succeeds?
validate :validate_branches, unless: [:allow_broken, :importing?] validate :validate_branches, unless: [:allow_broken, :importing?]
validate :validate_fork validate :validate_fork
validates_associated :merge_request_diff, on: :create, unless: [:allow_broken, :importing?]
scope :by_branch, ->(branch_name) { where("(source_branch LIKE :branch) OR (target_branch LIKE :branch)", branch: branch_name) } scope :by_branch, ->(branch_name) { where("(source_branch LIKE :branch) OR (target_branch LIKE :branch)", branch: branch_name) }
scope :cared, ->(user) { where('assignee_id = :user OR author_id = :user', user: user.id) } scope :cared, ->(user) { where('assignee_id = :user OR author_id = :user', user: user.id) }
...@@ -286,6 +287,10 @@ class MergeRequest < ActiveRecord::Base ...@@ -286,6 +287,10 @@ class MergeRequest < ActiveRecord::Base
end end
end end
def ensure_merge_request_diff
merge_request_diff || merge_request_diffs.build
end
def create_merge_request_diff def create_merge_request_diff
merge_request_diffs.create merge_request_diffs.create
end end
......
...@@ -22,28 +22,31 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -22,28 +22,31 @@ class MergeRequestDiff < ActiveRecord::Base
serialize :st_commits serialize :st_commits
serialize :st_diffs serialize :st_diffs
validates :head_commit_sha, presence: true
validates :start_commit_sha, presence: true validates :start_commit_sha, presence: true
validates :head_commit_sha, presence: true
validates :base_commit_sha, presence: true validates :base_commit_sha, presence: true
after_initialize :ensure_head_commit_sha, if: :persisted? after_initialize :set_diff_range, unless: :importing?
before_create :set_diff_range, unless: :importing? after_create :save_git_content, unless: :importing?
after_create :reload_content, unless: :importing?
after_save :keep_around_commits, unless: :importing? after_save :keep_around_commits, unless: :importing?
def ensure_head_commit_sha
self.head_commit_sha ||= last_commit.sha
end
def set_diff_range def set_diff_range
if persisted?
# Workaround for old MergeRequestDiff object
# that does not have head_commit_sha in the database
self.head_commit_sha ||= last_commit.sha
else
self.start_commit_sha ||= merge_request.target_branch_sha self.start_commit_sha ||= merge_request.target_branch_sha
self.head_commit_sha ||= merge_request.source_branch_sha self.head_commit_sha ||= merge_request.source_branch_sha
self.base_commit_sha ||= find_base_sha self.base_commit_sha ||= find_base_sha
end end
end
def reload_content # Collect information about commits and diff from repository
reload_commits # and save it to the database as serialized data
reload_diffs def save_git_content
save_commits
save_diffs
end end
def size def size
...@@ -130,9 +133,9 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -130,9 +133,9 @@ class MergeRequestDiff < ActiveRecord::Base
array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash), merge_request.source_project) } array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash), merge_request.source_project) }
end end
# Reload all commits related to current merge request from repo # Load all commits related to current merge request diff from repo
# and save it as array of hashes in st_commits db field # and save it as array of hashes in st_commits db field
def reload_commits def save_commits
new_attributes = {} new_attributes = {}
commits = compare.commits commits = compare.commits
...@@ -165,9 +168,9 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -165,9 +168,9 @@ class MergeRequestDiff < ActiveRecord::Base
end end
end end
# Reload diffs between branches related to current merge request from repo # Load diffs between branches related to current merge request diff from repo
# and save it as array of hashes in st_diffs db field # and save it as array of hashes in st_diffs db field
def reload_diffs def save_diffs
new_attributes = {} new_attributes = {}
new_diffs = [] new_diffs = []
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment