Cannot view MR when commit doesn't have parents
Zendesk ticket: https://gitlab.zendesk.com/agent/tickets/28374
In a certain scenario, a commit in a merge request will not have any parents so source_sha_parent
method bombs out. I'm not exactly sure how the customer got in to this scenario. Did the merge request somehow alter the first commit in the repository so there was no parent?
The method is:
def source_sha_parent
source_project.repository.commit(first_commit.sha).parents.first.sha
end
The problem is source_project.repository.commit(first_commit.sha).parents
returns an empty array in this case. Calling .first
causes the failure.
Started GET "/group/project/merge_requests/2" for 127.0.0.1 at 2016-06-27 12:36:06 +0000
Processing by Projects::MergeRequestsController#show as HTML
Parameters: {"namespace_id"=>"group", "project_id"=>"SSO", "id"=>"2"}
Completed 500 Internal Server Error in 532ms (ActiveRecord: 40.2ms)
ActionView::Template::Error (undefined method `sha' for nil:NilClass):
39: = render "projects/merge_requests/show/how_to_merge"
40: = render "projects/merge_requests/widget/show.html.haml"
41:
42: - if @merge_request.source_branch_exists? && @merge_request.mergeable? && @merge_request.can_be_merged_by?(current_user)
43: .light.prepend-top-default
44: You can also accept this merge request manually using the
45: = succeed '.' do
app/models/merge_request.rb:613:in `source_sha_parent'
app/models/merge_request.rb:617:in `ff_merge_possible?'
app/models/merge_request.rb:621:in `must_be_rebased?'
app/models/merge_request.rb:271:in `mergeable?'
app/views/projects/merge_requests/_show.html.haml:42:in `_app_views_projects_merge_requests__show_html_haml__1273117016591977417_70667180'
app/views/projects/merge_requests/show.html.haml:1:in `_app_views_projects_merge_requests_show_html_haml__4208630700998909260_70617480'
app/controllers/projects/merge_requests_controller.rb:59:in `show'
lib/gitlab/middleware/go.rb:16:in `call'
lib/gitlab/middleware/readonly_geo.rb:29:in `call'
Potential fix
Gracefully check for empty parents in source_sha_parent
.
def source_sha_parent
parents = source_project.repository.commit(first_commit.sha).parents
return nil if parents.empty?
parents.first.sha
end
cc/ @DouweM @stanhu I'm curious about your thoughts on how this MR could have gotten in to this scenario.