# HG changeset patch # User olivier # Date 1475241267 -7200 # Fri Sep 30 15:14:27 2016 +0200 # Node ID 79a189f1725546f9eb803831056015f23b10fb63 # Parent da5e814314dfe7b63431556479ed37f3bf85b5d0 When issues are from multiple projects, set project to be the lowest common parent of all listed issues instead of nil diff -r da5e814314df -r 79a189f17255 app/controllers/application_controller.rb --- a/app/controllers/application_controller.rb Thu Sep 22 14:31:27 2016 +0200 +++ b/app/controllers/application_controller.rb Fri Sep 30 15:14:27 2016 +0200 @@ -294,6 +294,21 @@ render_404 end + # Find the first common parent of two projects + def find_common_parent(project1, project2) + return nil if project1.nil? || project2.nil? + return project1 if project1.id == project2.id + p1_ancestors = [project1.id] + while !project1.parent_id.nil? + p1_ancestors << project1.parent_id + project1 = Project.find(project1.parent_id) + end + while !project2.parent_id.nil? && !p1_ancestors.include?(project2.parent_id) + project2 = Project.find(project2.parent_id) + end + project2.parent_id.nil? ? nil : Project.find(project2.parent_id) + end + # Finds and sets @project based on @object.project def find_project_from_association render_404 unless @object.present? @@ -337,7 +352,14 @@ raise ActiveRecord::RecordNotFound if @issues.empty? raise Unauthorized unless @issues.all?(&:visible?) @projects = @issues.collect(&:project).compact.uniq - @project = @projects.first if @projects.size == 1 + # Get the highest common parent project in the project tree + common_parent = @projects.first + Project.project_tree(@projects) do |project, level| + if level == 0 + common_parent = find_common_parent(common_parent, project) + end + end + @project = common_parent rescue ActiveRecord::RecordNotFound render_404 end