Commit 6ce25e7b authored by Douwe Maan's avatar Douwe Maan

Rename MergeRequest methods that return commits or shas to be more clear and consistent

parent 18a5bb05
...@@ -77,12 +77,8 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -77,12 +77,8 @@ class Projects::MergeRequestsController < Projects::ApplicationController
def diffs def diffs
apply_diff_view_cookie! apply_diff_view_cookie!
@commit = @merge_request.last_commit @commit = @merge_request.diff_head_commit
@base_commit = @merge_request.diff_base_commit @base_commit = @merge_request.diff_base_commit || @merge_request.likely_diff_base_commit
# MRs created before 8.4 don't have a diff_base_commit,
# but we need it for the "View file @ ..." link by deleted files
@base_commit ||= @merge_request.first_commit.parent || @merge_request.first_commit
@comments_target = { @comments_target = {
noteable_type: 'MergeRequest', noteable_type: 'MergeRequest',
...@@ -134,7 +130,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -134,7 +130,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@target_project = merge_request.target_project @target_project = merge_request.target_project
@source_project = merge_request.source_project @source_project = merge_request.source_project
@commits = @merge_request.compare_commits.reverse @commits = @merge_request.compare_commits.reverse
@commit = @merge_request.last_commit @commit = @merge_request.diff_head_commit
@base_commit = @merge_request.diff_base_commit @base_commit = @merge_request.diff_base_commit
@diffs = @merge_request.compare.diffs(diff_options) if @merge_request.compare @diffs = @merge_request.compare.diffs(diff_options) if @merge_request.compare
@diff_notes_disabled = true @diff_notes_disabled = true
...@@ -212,7 +208,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -212,7 +208,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
return return
end end
if params[:sha] != @merge_request.source_sha if params[:sha] != @merge_request.diff_head_sha
@status = :sha_mismatch @status = :sha_mismatch
return return
end end
...@@ -274,16 +270,16 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -274,16 +270,16 @@ class Projects::MergeRequestsController < Projects::ApplicationController
status ||= "preparing" status ||= "preparing"
else else
ci_service = @merge_request.source_project.ci_service ci_service = @merge_request.source_project.ci_service
status = ci_service.commit_status(merge_request.last_commit.sha, merge_request.source_branch) if ci_service status = ci_service.commit_status(merge_request.diff_head_sha, merge_request.source_branch) if ci_service
if ci_service.respond_to?(:commit_coverage) if ci_service.respond_to?(:commit_coverage)
coverage = ci_service.commit_coverage(merge_request.last_commit.sha, merge_request.source_branch) coverage = ci_service.commit_coverage(merge_request.diff_head_sha, merge_request.source_branch)
end end
end end
response = { response = {
title: merge_request.title, title: merge_request.title,
sha: merge_request.last_commit_short_sha, sha: merge_request.diff_head_commit.short_id,
status: status, status: status,
coverage: coverage coverage: coverage
} }
......
...@@ -27,7 +27,7 @@ module MergeRequestsHelper ...@@ -27,7 +27,7 @@ module MergeRequestsHelper
end end
def ci_build_details_path(merge_request) def ci_build_details_path(merge_request)
build_url = merge_request.source_project.ci_service.build_page(merge_request.last_commit.sha, merge_request.source_branch) build_url = merge_request.source_project.ci_service.build_page(merge_request.diff_head_sha, merge_request.source_branch)
return nil unless build_url return nil unless build_url
parsed_url = URI.parse(build_url) parsed_url = URI.parse(build_url)
......
...@@ -16,7 +16,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -16,7 +16,7 @@ class MergeRequest < ActiveRecord::Base
serialize :merge_params, Hash serialize :merge_params, Hash
after_create :create_merge_request_diff, unless: :importing after_create :create_merge_request_diff, unless: :importing?
after_update :update_merge_request_diff after_update :update_merge_request_diff
delegate :commits, :diffs, :real_size, to: :merge_request_diff, prefix: nil delegate :commits, :diffs, :real_size, to: :merge_request_diff, prefix: nil
...@@ -29,10 +29,6 @@ class MergeRequest < ActiveRecord::Base ...@@ -29,10 +29,6 @@ class MergeRequest < ActiveRecord::Base
# when creating new merge request # when creating new merge request
attr_accessor :can_be_created, :compare_commits, :compare attr_accessor :can_be_created, :compare_commits, :compare
# Temporary fields to store target_sha, and base_sha to
# compare when importing pull requests from GitHub
attr_accessor :base_target_sha, :head_source_sha
state_machine :state, initial: :opened do state_machine :state, initial: :opened do
event :close do event :close do
transition [:reopened, :opened] => :closed transition [:reopened, :opened] => :closed
...@@ -169,28 +165,88 @@ class MergeRequest < ActiveRecord::Base ...@@ -169,28 +165,88 @@ class MergeRequest < ActiveRecord::Base
reference reference
end end
def last_commit
merge_request_diff ? merge_request_diff.last_commit : compare_commits.last
end
def first_commit def first_commit
merge_request_diff ? merge_request_diff.first_commit : compare_commits.first merge_request_diff ? merge_request_diff.first_commit : compare_commits.first
end end
def last_commit
merge_request_diff ? merge_request_diff.last_commit : compare_commits.last
end
def diff_size def diff_size
merge_request_diff.size merge_request_diff.size
end end
def diff_base_commit def diff_base_commit
if merge_request_diff if persisted?
merge_request_diff.base_commit merge_request_diff.base_commit
elsif source_sha elsif diff_start_commit && diff_head_commit
self.target_project.merge_base_commit(self.source_sha, self.target_branch) self.target_project.merge_base_commit(diff_start_sha, diff_head_sha)
end
end
# MRs created before 8.4 don't store a MergeRequestDiff#base_commit_sha,
# but we need to get a commit for the "View file @ ..." link by deleted files,
# so we find the likely one if we can't get the actual one.
# This will not be the actual base commit if the target branch was merged into
# the source branch after the merge request was created, but it is good enough
# for the specific purpose of linking to a commit.
# It is not good enough for use in Gitlab::Git::DiffRefs, which need the
# true base commit.
def likely_diff_base_commit
first_commit.parent || first_commit
end
def diff_start_commit
if persisted?
merge_request_diff.start_commit
else
target_branch_head
end end
end end
def last_commit_short_sha def diff_head_commit
last_commit.short_id if persisted?
merge_request_diff.head_commit
else
source_branch_head
end
end
def diff_start_sha
diff_start_commit.try(:sha)
end
def diff_base_sha
diff_base_commit.try(:sha)
end
def diff_head_sha
diff_head_commit.try(:sha)
end
# When importing a pull request from GitHub, the old and new branches may no
# longer actually exist by those names, but we need to recreate the merge
# request diff with the right source and target shas.
# We use these attributes to force these to the intended values.
attr_writer :target_branch_sha, :source_branch_sha
def source_branch_head
source_branch_ref = @source_branch_sha || source_branch
source_project.repository.commit(source_branch) if source_branch_ref
end
def target_branch_head
target_branch_ref = @target_branch_sha || target_branch
target_project.repository.commit(target_branch) if target_branch_ref
end
def target_branch_sha
target_branch_head.try(:sha)
end
def source_branch_sha
source_branch_head.try(:sha)
end end
def validate_branches def validate_branches
...@@ -241,7 +297,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -241,7 +297,7 @@ class MergeRequest < ActiveRecord::Base
return unless unchecked? return unless unchecked?
can_be_merged = can_be_merged =
!broken? && project.repository.can_be_merged?(source_sha, target_branch) !broken? && project.repository.can_be_merged?(diff_head_sha, target_branch)
if can_be_merged if can_be_merged
mark_as_mergeable mark_as_mergeable
...@@ -293,7 +349,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -293,7 +349,7 @@ class MergeRequest < ActiveRecord::Base
!source_project.protected_branch?(source_branch) && !source_project.protected_branch?(source_branch) &&
!source_project.root_ref?(source_branch) && !source_project.root_ref?(source_branch) &&
Ability.abilities.allowed?(current_user, :push_code, source_project) && Ability.abilities.allowed?(current_user, :push_code, source_project) &&
last_commit == source_project.commit(source_branch) diff_head_commit == source_branch_head
end end
def should_remove_source_branch? def should_remove_source_branch?
...@@ -331,8 +387,8 @@ class MergeRequest < ActiveRecord::Base ...@@ -331,8 +387,8 @@ class MergeRequest < ActiveRecord::Base
work_in_progress: work_in_progress? work_in_progress: work_in_progress?
} }
if last_commit if diff_head_commit
attrs.merge!(last_commit: last_commit.hook_attrs) attrs.merge!(last_commit: diff_head_commit.hook_attrs)
end end
attributes.merge!(attrs) attributes.merge!(attrs)
...@@ -510,22 +566,6 @@ class MergeRequest < ActiveRecord::Base ...@@ -510,22 +566,6 @@ class MergeRequest < ActiveRecord::Base
end end
end end
def target_sha
return @base_target_sha if defined?(@base_target_sha)
target_project.repository.commit(target_branch).try(:sha)
end
def source_sha
return @head_source_sha if defined?(@head_source_sha)
last_commit.try(:sha) || source_tip.try(:sha)
end
def source_tip
source_branch && source_project.repository.commit(source_branch)
end
def fetch_ref def fetch_ref
target_project.repository.fetch_ref( target_project.repository.fetch_ref(
source_project.repository.path_to_repo, source_project.repository.path_to_repo,
...@@ -558,10 +598,10 @@ class MergeRequest < ActiveRecord::Base ...@@ -558,10 +598,10 @@ class MergeRequest < ActiveRecord::Base
def diverged_commits_count def diverged_commits_count
cache = Rails.cache.read(:"merge_request_#{id}_diverged_commits") cache = Rails.cache.read(:"merge_request_#{id}_diverged_commits")
if cache.blank? || cache[:source_sha] != source_sha || cache[:target_sha] != target_sha if cache.blank? || cache[:source_sha] != source_branch_sha || cache[:target_sha] != target_branch_sha
cache = { cache = {
source_sha: source_sha, source_sha: source_branch_sha,
target_sha: target_sha, target_sha: target_branch_sha,
diverged_commits_count: compute_diverged_commits_count diverged_commits_count: compute_diverged_commits_count
} }
Rails.cache.write(:"merge_request_#{id}_diverged_commits", cache) Rails.cache.write(:"merge_request_#{id}_diverged_commits", cache)
...@@ -571,9 +611,9 @@ class MergeRequest < ActiveRecord::Base ...@@ -571,9 +611,9 @@ class MergeRequest < ActiveRecord::Base
end end
def compute_diverged_commits_count def compute_diverged_commits_count
return 0 unless source_sha && target_sha return 0 unless source_branch_sha && target_branch_sha
Gitlab::Git::Commit.between(target_project.repository.raw_repository, source_sha, target_sha).size Gitlab::Git::Commit.between(target_project.repository.raw_repository, source_branch_sha, target_branch_sha).size
end end
private :compute_diverged_commits_count private :compute_diverged_commits_count
...@@ -582,13 +622,13 @@ class MergeRequest < ActiveRecord::Base ...@@ -582,13 +622,13 @@ class MergeRequest < ActiveRecord::Base
end end
def pipeline def pipeline
@pipeline ||= source_project.pipeline(last_commit.id, source_branch) if last_commit && source_project
end end
def diff_refs def diff_refs
return nil unless diff_base_commit return nil unless diff_base_commit
[diff_base_commit, last_commit] [diff_base_commit, last_commit]
@pipeline ||= source_project.pipeline(diff_head_sha, source_branch) if diff_head_sha && source_project
end end
def merge_commit def merge_commit
......
...@@ -7,7 +7,7 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -7,7 +7,7 @@ class MergeRequestDiff < ActiveRecord::Base
belongs_to :merge_request belongs_to :merge_request
delegate :head_source_sha, :target_branch, :source_branch, to: :merge_request, prefix: nil delegate :source_branch_sha, :target_branch_sha, :target_branch, :source_branch, to: :merge_request, prefix: nil
state_machine :state, initial: :empty do state_machine :state, initial: :empty do
state :collected state :collected
...@@ -40,8 +40,8 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -40,8 +40,8 @@ class MergeRequestDiff < ActiveRecord::Base
@diffs_no_whitespace ||= begin @diffs_no_whitespace ||= begin
compare = Gitlab::Git::Compare.new( compare = Gitlab::Git::Compare.new(
self.repository.raw_repository, self.repository.raw_repository,
self.base, self.target_branch_sha,
self.head, self.source_branch_sha,
) )
compare.diffs(options) compare.diffs(options)
end end
...@@ -63,13 +63,21 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -63,13 +63,21 @@ class MergeRequestDiff < ActiveRecord::Base
end end
def base_commit def base_commit
return nil unless self.base_commit_sha return unless self.base_commit_sha
merge_request.target_project.commit(self.base_commit_sha) merge_request.target_project.commit(self.base_commit_sha)
end end
def last_commit_short_sha def start_commit
@last_commit_short_sha ||= last_commit.short_id return unless self.start_commit_sha
merge_request.target_project.commit(self.start_commit_sha)
end
def head_commit
return last_commit unless self.head_commit_sha
merge_request.target_project.commit(self.head_commit_sha)
end end
def dump_commits(commits) def dump_commits(commits)
...@@ -166,23 +174,14 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -166,23 +174,14 @@ class MergeRequestDiff < ActiveRecord::Base
merge_request.target_project.repository merge_request.target_project.repository
end end
def source_sha def branch_base_commit
return head_source_sha if head_source_sha.present? return unless self.source_branch_sha && self.target_branch_sha
source_commit = merge_request.source_project.commit(source_branch)
source_commit.try(:sha)
end
def target_sha
merge_request.target_sha
end
def base merge_request.target_project.merge_base_commit(self.source_branch_sha, self.target_branch_sha)
self.target_sha || self.target_branch
end end
def head def branch_base_sha
self.source_sha branch_base_commit.try(:sha)
end end
def compare def compare
...@@ -193,8 +192,8 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -193,8 +192,8 @@ class MergeRequestDiff < ActiveRecord::Base
Gitlab::Git::Compare.new( Gitlab::Git::Compare.new(
self.repository.raw_repository, self.repository.raw_repository,
self.base, self.target_branch_sha,
self.head self.source_branch_sha
) )
end end
end end
......
...@@ -144,7 +144,7 @@ class JiraService < IssueTrackerService ...@@ -144,7 +144,7 @@ class JiraService < IssueTrackerService
commit_id = if entity.is_a?(Commit) commit_id = if entity.is_a?(Commit)
entity.id entity.id
elsif entity.is_a?(MergeRequest) elsif entity.is_a?(MergeRequest)
entity.last_commit.id entity.diff_head_sha
end end
commit_url = build_entity_url(:commit, commit_id) commit_url = build_entity_url(:commit, commit_id)
......
...@@ -34,7 +34,7 @@ module MergeRequests ...@@ -34,7 +34,7 @@ module MergeRequests
committer: committer committer: committer
} }
commit_id = repository.merge(current_user, merge_request.source_sha, merge_request.target_branch, options) commit_id = repository.merge(current_user, merge_request.diff_head_sha, merge_request.target_branch, options)
merge_request.update(merge_commit_sha: commit_id) merge_request.update(merge_commit_sha: commit_id)
rescue GitHooksService::PreReceiveError => e rescue GitHooksService::PreReceiveError => e
merge_request.update(merge_error: e.message) merge_request.update(merge_error: e.message)
......
...@@ -12,7 +12,7 @@ module MergeRequests ...@@ -12,7 +12,7 @@ module MergeRequests
merge_request.merge_when_build_succeeds = true merge_request.merge_when_build_succeeds = true
merge_request.merge_user = @current_user merge_request.merge_user = @current_user
SystemNoteService.merge_when_build_succeeds(merge_request, @project, @current_user, merge_request.last_commit) SystemNoteService.merge_when_build_succeeds(merge_request, @project, @current_user, merge_request.diff_head_commit)
end end
merge_request.save merge_request.save
......
...@@ -34,10 +34,10 @@ module MergeRequests ...@@ -34,10 +34,10 @@ module MergeRequests
def close_merge_requests def close_merge_requests
commit_ids = @commits.map(&:id) commit_ids = @commits.map(&:id)
merge_requests = @project.merge_requests.opened.where(target_branch: @branch_name).to_a merge_requests = @project.merge_requests.opened.where(target_branch: @branch_name).to_a
merge_requests = merge_requests.select(&:last_commit) merge_requests = merge_requests.select(&:diff_head_commit)
merge_requests = merge_requests.select do |merge_request| merge_requests = merge_requests.select do |merge_request|
commit_ids.include?(merge_request.last_commit.id) commit_ids.include?(merge_request.diff_head_sha)
end end
merge_requests.uniq.select(&:source_project).each do |merge_request| merge_requests.uniq.select(&:source_project).each do |merge_request|
...@@ -94,12 +94,10 @@ module MergeRequests ...@@ -94,12 +94,10 @@ module MergeRequests
merge_request = merge_requests_for_source_branch.first merge_request = merge_requests_for_source_branch.first
return unless merge_request return unless merge_request
last_commit = merge_request.last_commit
begin begin
# Since any number of commits could have been made to the restored branch, # Since any number of commits could have been made to the restored branch,
# find the common root to see what has been added. # find the common root to see what has been added.
common_ref = @project.repository.merge_base(last_commit.id, @newrev) common_ref = @project.repository.merge_base(merge_request.diff_head_sha, @newrev)
# If the a commit no longer exists in this repo, gitlab_git throws # If the a commit no longer exists in this repo, gitlab_git throws
# a Rugged::OdbError. This is fixed in https://gitlab.com/gitlab-org/gitlab_git/merge_requests/52 # a Rugged::OdbError. This is fixed in https://gitlab.com/gitlab-org/gitlab_git/merge_requests/52
@commits = @project.repository.commits_between(common_ref, @newrev) if common_ref @commits = @project.repository.commits_between(common_ref, @newrev) if common_ref
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
CI build CI build
= ci_label_for_status(status) = ci_label_for_status(status)
for for
- commit = @merge_request.last_commit - commit = @merge_request.diff_head_commit
= succeed "." do = succeed "." do
= link_to @pipeline.short_sha, namespace_project_commit_path(@merge_request.source_project.namespace, @merge_request.source_project, @pipeline.sha), class: "monospace" = link_to @pipeline.short_sha, namespace_project_commit_path(@merge_request.source_project.namespace, @merge_request.source_project, @pipeline.sha), class: "monospace"
%span.ci-coverage %span.ci-coverage
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
CI build CI build
= ci_label_for_status(status) = ci_label_for_status(status)
for for
- commit = @merge_request.last_commit - commit = @merge_request.diff_head_commit
= succeed "." do = succeed "." do
= link_to commit.short_id, namespace_project_commit_path(@merge_request.source_project.namespace, @merge_request.source_project, commit), class: "monospace" = link_to commit.short_id, namespace_project_commit_path(@merge_request.source_project.namespace, @merge_request.source_project, commit), class: "monospace"
%span.ci-coverage %span.ci-coverage
...@@ -33,12 +33,12 @@ ...@@ -33,12 +33,12 @@
.ci_widget .ci_widget
= icon("spinner spin") = icon("spinner spin")
Checking CI status for #{@merge_request.last_commit_short_sha}&hellip; Checking CI status for #{@merge_request.diff_head_commit.short_id}&hellip;
.ci_widget.ci-not_found{style: "display:none"} .ci_widget.ci-not_found{style: "display:none"}
= icon("times-circle") = icon("times-circle")
Could not find CI status for #{@merge_request.last_commit_short_sha}. Could not find CI status for #{@merge_request.diff_head_commit.short_id}.
.ci_widget.ci-error{style: "display:none"} .ci_widget.ci-error{style: "display:none"}
= icon("times-circle") = icon("times-circle")
Could not connect to the CI server. Please check your settings and try again. Could not connect to the CI server. Please check your settings and try again.
\ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
= form_for [:merge, @project.namespace.becomes(Namespace), @project, @merge_request], remote: true, method: :post, html: { class: 'accept-mr-form js-quick-submit js-requires-input' } do |f| = form_for [:merge, @project.namespace.becomes(Namespace), @project, @merge_request], remote: true, method: :post, html: { class: 'accept-mr-form js-quick-submit js-requires-input' } do |f|
= hidden_field_tag :authenticity_token, form_authenticity_token = hidden_field_tag :authenticity_token, form_authenticity_token
= hidden_field_tag :sha, @merge_request.source_sha = hidden_field_tag :sha, @merge_request.diff_head_sha
.accept-merge-holder.clearfix.js-toggle-container .accept-merge-holder.clearfix.js-toggle-container
.clearfix .clearfix
.accept-action .accept-action
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
- if remove_source_branch_button || user_can_cancel_automatic_merge - if remove_source_branch_button || user_can_cancel_automatic_merge
.clearfix.prepend-top-10 .clearfix.prepend-top-10
- if remove_source_branch_button - if remove_source_branch_button
= link_to merge_namespace_project_merge_request_path(@merge_request.target_project.namespace, @merge_request.target_project, @merge_request, merge_when_build_succeeds: true, should_remove_source_branch: true, sha: @merge_request.source_sha), remote: true, method: :post, class: "btn btn-grouped btn-primary btn-sm remove_source_branch" do = link_to merge_namespace_project_merge_request_path(@merge_request.target_project.namespace, @merge_request.target_project, @merge_request, merge_when_build_succeeds: true, should_remove_source_branch: true, sha: @merge_request.diff_head_sha), remote: true, method: :post, class: "btn btn-grouped btn-primary btn-sm remove_source_branch" do
= icon('times') = icon('times')
Remove Source Branch When Merged Remove Source Branch When Merged
......
...@@ -519,7 +519,7 @@ class Spinach::Features::ProjectMergeRequests < Spinach::FeatureSteps ...@@ -519,7 +519,7 @@ class Spinach::Features::ProjectMergeRequests < Spinach::FeatureSteps
step '"Bug NS-05" has CI status' do step '"Bug NS-05" has CI status' do
project = merge_request.source_project project = merge_request.source_project
project.enable_ci project.enable_ci
pipeline = create :ci_pipeline, project: project, sha: merge_request.last_commit.id, ref: merge_request.source_branch pipeline = create :ci_pipeline, project: project, sha: merge_request.diff_head_sha, ref: merge_request.source_branch
create :ci_build, pipeline: pipeline create :ci_build, pipeline: pipeline
end end
......
...@@ -233,8 +233,8 @@ module API ...@@ -233,8 +233,8 @@ module API
render_api_error!('Branch cannot be merged', 406) unless merge_request.mergeable? render_api_error!('Branch cannot be merged', 406) unless merge_request.mergeable?
if params[:sha] && merge_request.source_sha != params[:sha] if params[:sha] && merge_request.diff_head_sha != params[:sha]
render_api_error!("SHA does not match HEAD of source branch: #{merge_request.source_sha}", 409) render_api_error!("SHA does not match HEAD of source branch: #{merge_request.diff_head_sha}", 409)
end end
merge_params = { merge_params = {
......
...@@ -11,10 +11,10 @@ module Gitlab ...@@ -11,10 +11,10 @@ module Gitlab
description: description, description: description,
source_project: source_branch_project, source_project: source_branch_project,
source_branch: source_branch_name, source_branch: source_branch_name,
head_source_sha: source_branch_sha, source_branch_sha: source_branch_sha,
target_project: target_branch_project, target_project: target_branch_project,
target_branch: target_branch_name, target_branch: target_branch_name,
base_target_sha: target_branch_sha, target_branch_sha: target_branch_sha,
state: state, state: state,
milestone: milestone, milestone: milestone,
author_id: author_id, author_id: author_id,
......
...@@ -212,7 +212,7 @@ describe Projects::MergeRequestsController do ...@@ -212,7 +212,7 @@ describe Projects::MergeRequestsController do
context 'when the sha parameter matches the source SHA' do context 'when the sha parameter matches the source SHA' do
def merge_with_sha def merge_with_sha
post :merge, base_params.merge(sha: merge_request.source_sha) post :merge, base_params.merge(sha: merge_request.diff_head_sha)
end end
it 'returns :success' do it 'returns :success' do
...@@ -229,11 +229,11 @@ describe Projects::MergeRequestsController do ...@@ -229,11 +229,11 @@ describe Projects::MergeRequestsController do
context 'when merge_when_build_succeeds is passed' do context 'when merge_when_build_succeeds is passed' do
def merge_when_build_succeeds def merge_when_build_succeeds
post :merge, base_params.merge(sha: merge_request.source_sha, merge_when_build_succeeds: '1') post :merge, base_params.merge(sha: merge_request.diff_head_sha, merge_when_build_succeeds: '1')
end end
before do before do
create(:ci_empty_pipeline, project: project, sha: merge_request.source_sha, ref: merge_request.source_branch) create(:ci_empty_pipeline, project: project, sha: merge_request.diff_head_sha, ref: merge_request.source_branch)
end end
it 'returns :merge_when_build_succeeds' do it 'returns :merge_when_build_succeeds' do
......
...@@ -30,7 +30,7 @@ feature 'Merge request created from fork' do ...@@ -30,7 +30,7 @@ feature 'Merge request created from fork' do
given(:pipeline) do given(:pipeline) do
create(:ci_pipeline_with_two_job, project: fork_project, create(:ci_pipeline_with_two_job, project: fork_project,
sha: merge_request.last_commit.id, sha: merge_request.diff_head_sha,
ref: merge_request.source_branch) ref: merge_request.source_branch)
end end
......
...@@ -12,7 +12,7 @@ feature 'Merge When Build Succeeds', feature: true, js: true do ...@@ -12,7 +12,7 @@ feature 'Merge When Build Succeeds', feature: true, js: true do
end end
context "Active build for Merge Request" do context "Active build for Merge Request" do
let!(:pipeline) { create(:ci_pipeline, project: project, sha: merge_request.last_commit.id, ref: merge_request.source_branch) } let!(:pipeline) { create(:ci_pipeline, project: project, sha: merge_request.diff_head_sha, ref: merge_request.source_branch) }
let!(:ci_build) { create(:ci_build, pipeline: pipeline) } let!(:ci_build) { create(:ci_build, pipeline: pipeline) }
before do before do
...@@ -47,7 +47,7 @@ feature 'Merge When Build Succeeds', feature: true, js: true do ...@@ -47,7 +47,7 @@ feature 'Merge When Build Succeeds', feature: true, js: true do
merge_user: user, title: "MepMep", merge_when_build_succeeds: true) merge_user: user, title: "MepMep", merge_when_build_succeeds: true)
end end
let!(:pipeline) { create(:ci_pipeline, project: project, sha: merge_request.last_commit.id, ref: merge_request.source_branch) } let!(:pipeline) { create(:ci_pipeline, project: project, sha: merge_request.diff_head_sha, ref: merge_request.source_branch) }
let!(:ci_build) { create(:ci_build, pipeline: pipeline) } let!(:ci_build) { create(:ci_build, pipeline: pipeline) }
before do before do
......
...@@ -19,7 +19,7 @@ feature 'Only allow merge requests to be merged if the build succeeds', feature: ...@@ -19,7 +19,7 @@ feature 'Only allow merge requests to be merged if the build succeeds', feature:
end end
context 'when project has CI enabled' do context 'when project has CI enabled' do
let(:pipeline) { create(:ci_empty_pipeline, project: project, sha: merge_request.last_commit.id, ref: merge_request.source_branch) } let(:pipeline) { create(:ci_empty_pipeline, project: project, sha: merge_request.diff_head_sha, ref: merge_request.source_branch) }
context 'when merge requests can only be merged if the build succeeds' do context 'when merge requests can only be merged if the build succeeds' do
before do before do
......
...@@ -43,10 +43,10 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -43,10 +43,10 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
description: "*Created by: octocat*\n\nPlease pull these awesome changes", description: "*Created by: octocat*\n\nPlease pull these awesome changes",
source_project: project, source_project: project,
source_branch: 'feature', source_branch: 'feature',
head_source_sha: source_sha, source_branch_sha: source_sha,
target_project: project, target_project: project,
target_branch: 'master', target_branch: 'master',
base_target_sha: target_sha, target_branch_sha: target_sha,
state: 'opened', state: 'opened',
milestone: nil, milestone: nil,
author_id: project.creator_id, author_id: project.creator_id,
...@@ -70,10 +70,10 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -70,10 +70,10 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
description: "*Created by: octocat*\n\nPlease pull these awesome changes", description: "*Created by: octocat*\n\nPlease pull these awesome changes",
source_project: project, source_project: project,
source_branch: 'feature', source_branch: 'feature',
head_source_sha: source_sha, source_branch_sha: source_sha,
target_project: project, target_project: project,
target_branch: 'master', target_branch: 'master',
base_target_sha: target_sha, target_branch_sha: target_sha,
state: 'closed', state: 'closed',
milestone: nil, milestone: nil,
author_id: project.creator_id, author_id: project.creator_id,
...@@ -97,10 +97,10 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -97,10 +97,10 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
description: "*Created by: octocat*\n\nPlease pull these awesome changes", description: "*Created by: octocat*\n\nPlease pull these awesome changes",
source_project: project, source_project: project,
source_branch: 'feature', source_branch: 'feature',
head_source_sha: source_sha, source_branch_sha: source_sha,
target_project: project, target_project: project,
target_branch: 'master', target_branch: 'master',
base_target_sha: target_sha, target_branch_sha: target_sha,
state: 'merged', state: 'merged',
milestone: nil, milestone: nil,
author_id: project.creator_id, author_id: project.creator_id,
......
...@@ -125,7 +125,7 @@ describe Gitlab::ImportExport::ProjectTreeSaver, services: true do ...@@ -125,7 +125,7 @@ describe Gitlab::ImportExport::ProjectTreeSaver, services: true do
ci_pipeline = create(:ci_pipeline, ci_pipeline = create(:ci_pipeline,
project: project, project: project,
sha: merge_request.last_commit.id, sha: merge_request.diff_head_sha,
ref: merge_request.source_branch, ref: merge_request.source_branch,
statuses: [commit_status]) statuses: [commit_status])
......
...@@ -62,7 +62,7 @@ describe MergeRequest, models: true do ...@@ -62,7 +62,7 @@ describe MergeRequest, models: true do
end end
end end
describe '#target_sha' do describe '#target_branch_sha' do
context 'when the target branch does not exist anymore' do context 'when the target branch does not exist anymore' do
let(:project) { create(:project) } let(:project) { create(:project) }
...@@ -73,32 +73,32 @@ describe MergeRequest, models: true do ...@@ -73,32 +73,32 @@ describe MergeRequest, models: true do
end end
it 'returns nil' do it 'returns nil' do
expect(subject.target_sha).to be_nil expect(subject.target_branch_sha).to be_nil
end end
end end
end end
describe '#source_sha' do describe '#source_branch_sha' do
let(:last_branch_commit) { subject.source_project.repository.commit(subject.source_branch) } let(:last_branch_commit) { subject.source_project.repository.commit(subject.source_branch) }
context 'with diffs' do context 'with diffs' do
subject { create(:merge_request, :with_diffs) } subject { create(:merge_request, :with_diffs) }
it 'returns the sha of the source branch last commit' do it 'returns the sha of the source branch last commit' do
expect(subject.source_sha).to eq(last_branch_commit.sha) expect(subject.source_branch_sha).to eq(last_branch_commit.sha)
end end
end end
context 'without diffs' do context 'without diffs' do
subject { create(:merge_request, :without_diffs) } subject { create(:merge_request, :without_diffs) }
it 'returns the sha of the source branch last commit' do it 'returns the sha of the source branch last commit' do
expect(subject.source_sha).to eq(last_branch_commit.sha) expect(subject.source_branch_sha).to eq(last_branch_commit.sha)
end end
end end
context 'when the merge request is being created' do context 'when the merge request is being created' do
subject { build(:merge_request, source_branch: nil, compare_commits: []) } subject { build(:merge_request, source_branch: nil, compare_commits: []) }
it 'returns nil' do it 'returns nil' do
expect(subject.source_sha).to be_nil expect(subject.source_branch_sha).to be_nil
end end
end end
end end
...@@ -252,12 +252,14 @@ describe MergeRequest, models: true do ...@@ -252,12 +252,14 @@ describe MergeRequest, models: true do
end end
it "can be removed if the last commit is the head of the source branch" do it "can be removed if the last commit is the head of the source branch" do
allow(subject.source_project).to receive(:commit).and_return(subject.last_commit) allow(subject.source_project).to receive(:commit).and_return(subject.diff_head_commit)
expect(subject.can_remove_source_branch?(user)).to be_truthy expect(subject.can_remove_source_branch?(user)).to be_truthy
end end
it "cannot be removed if the last commit is not also the head of the source branch" do it "cannot be removed if the last commit is not also the head of the source branch" do
subject.source_branch = "lfs"
expect(subject.can_remove_source_branch?(user)).to be_falsey expect(subject.can_remove_source_branch?(user)).to be_falsey
end end
end end
...@@ -363,7 +365,7 @@ describe MergeRequest, models: true do ...@@ -363,7 +365,7 @@ describe MergeRequest, models: true do
and_return(2) and_return(2)
subject.diverged_commits_count subject.diverged_commits_count
allow(subject).to receive(:source_sha).and_return('123abc') allow(subject).to receive(:source_branch_sha).and_return('123abc')
subject.diverged_commits_count subject.diverged_commits_count
end end
...@@ -373,7 +375,7 @@ describe MergeRequest, models: true do ...@@ -373,7 +375,7 @@ describe MergeRequest, models: true do
and_return(2) and_return(2)
subject.diverged_commits_count subject.diverged_commits_count
allow(subject).to receive(:target_sha).and_return('123abc') allow(subject).to receive(:target_branch_sha).and_return('123abc')
subject.diverged_commits_count subject.diverged_commits_count
end end
end end
...@@ -392,11 +394,10 @@ describe MergeRequest, models: true do ...@@ -392,11 +394,10 @@ describe MergeRequest, models: true do
describe '#pipeline' do describe '#pipeline' do
describe 'when the source project exists' do describe 'when the source project exists' do
it 'returns the latest commit' do it 'returns the latest pipeline' do
commit = double(:commit, id: '123abc')
pipeline = double(:ci_pipeline, ref: 'master') pipeline = double(:ci_pipeline, ref: 'master')
allow(subject).to receive(:last_commit).and_return(commit) allow(subject).to receive(:diff_head_sha).and_return('123abc')
expect(subject.source_project).to receive(:pipeline). expect(subject.source_project).to receive(:pipeline).
with('123abc', 'master'). with('123abc', 'master').
...@@ -464,7 +465,7 @@ describe MergeRequest, models: true do ...@@ -464,7 +465,7 @@ describe MergeRequest, models: true do
context 'when it is not broken and has no conflicts' do context 'when it is not broken and has no conflicts' do
it 'is marked as mergeable' do it 'is marked as mergeable' do
allow(subject).to receive(:broken?) { false } allow(subject).to receive(:broken?) { false }
allow(project.repository).to receive(:can_be_merged?) { true } allow(project.repository).to receive(:can_be_merged?).and_return(true)
expect { subject.check_if_can_be_merged }.to change { subject.merge_status }.to('can_be_merged') expect { subject.check_if_can_be_merged }.to change { subject.merge_status }.to('can_be_merged')
end end
...@@ -481,7 +482,7 @@ describe MergeRequest, models: true do ...@@ -481,7 +482,7 @@ describe MergeRequest, models: true do
context 'when it has conflicts' do context 'when it has conflicts' do
before do before do
allow(subject).to receive(:broken?) { false } allow(subject).to receive(:broken?) { false }
allow(project.repository).to receive(:can_be_merged?) { false } allow(project.repository).to receive(:can_be_merged?).and_return(false)
end end
it 'becomes unmergeable' do it 'becomes unmergeable' do
......
...@@ -312,7 +312,7 @@ describe Project, models: true do ...@@ -312,7 +312,7 @@ describe Project, models: true do
it 'should update merge request commits with new one if pushed to source branch' do it 'should update merge request commits with new one if pushed to source branch' do
project.update_merge_requests(prev_commit_id, commit_id, "refs/heads/#{merge_request.source_branch}", key.user) project.update_merge_requests(prev_commit_id, commit_id, "refs/heads/#{merge_request.source_branch}", key.user)
merge_request.reload merge_request.reload
expect(merge_request.last_commit.id).to eq(commit_id) expect(merge_request.diff_head_sha).to eq(commit_id)
end end
end end
......
...@@ -12,8 +12,8 @@ describe Repository, models: true do ...@@ -12,8 +12,8 @@ describe Repository, models: true do
end end
let(:merge_commit) do let(:merge_commit) do
source_sha = repository.find_branch('feature').target source_sha = repository.find_branch('feature').target
merge_commit_id = repository.merge(user, source_sha, 'master', commit_options) merge_commit_sha = repository.merge(user, source_sha, 'master', commit_options)
repository.commit(merge_commit_id) repository.commit(merge_commit_sha)
end end
describe :branch_names_contains do describe :branch_names_contains do
......
...@@ -439,14 +439,14 @@ describe API::API, api: true do ...@@ -439,14 +439,14 @@ describe API::API, api: true do
end end
it "returns 409 if the SHA parameter doesn't match" do it "returns 409 if the SHA parameter doesn't match" do
put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user), sha: merge_request.source_sha.succ put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user), sha: merge_request.diff_head_sha.reverse
expect(response).to have_http_status(409) expect(response).to have_http_status(409)
expect(json_response['message']).to start_with('SHA does not match HEAD of source branch') expect(json_response['message']).to start_with('SHA does not match HEAD of source branch')
end end
it "succeeds if the SHA parameter matches" do it "succeeds if the SHA parameter matches" do
put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user), sha: merge_request.source_sha put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user), sha: merge_request.diff_head_sha
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
......
...@@ -213,7 +213,7 @@ describe SystemNoteService, services: true do ...@@ -213,7 +213,7 @@ describe SystemNoteService, services: true do
create(:merge_request, source_project: project, target_project: project) create(:merge_request, source_project: project, target_project: project)
end end
subject { described_class.merge_when_build_succeeds(noteable, project, author, noteable.last_commit) } subject { described_class.merge_when_build_succeeds(noteable, project, author, noteable.diff_head_commit) }
it_behaves_like 'a system note' it_behaves_like 'a system note'
......
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