Searching code in public repos causes 500 error
Created by: karlhungus
To reproduce:
- Create two users test1, test2
- Create project as test 1, push some code to it, make it public
- login as test 2, go to test1's project, search for something inside the project
Logs show:
NoMethodError (undefined method `empty_repo?' for nil:NilClass):
app/contexts/search_context.rb:20:in `execute'
app/controllers/search_controller.rb:17:in `show'
Think the problem is in SearchController.rb
project_ids = current_user.authorized_projects.map(&:id)
if group_id.present?
@group = Group.find(group_id)
group_project_ids = @group.projects.map(&:id)
project_ids.select! { |id| group_project_ids.include?(id)}
elsif project_id.present?
@project = Project.find(params[:project_id])
project_ids.select! { |id| id == project_id.to_i}
end
result = SearchContext.new(project_ids, params).execute
The project_ids
is probably empty, which gets called here in SearchContext.rb
projects = Project.where(id: project_ids)
result[:projects] = projects.search(query).limit(20)
# Search inside single project
project = projects.first if projects.length == 1
if params[:search_code].present?
result[:blobs] = project.repository.search_files(query, params[:repository_ref]) unless project.empty_repo?
Think the project_id's needs to include public projects, ala api's projects.rb
:
get "/search/:query" do
ids = current_user.authorized_projects.map(&:id)
projects = Project.where("(id in (?) OR public = true) AND (name LIKE (?))", ids, "%#{params[:query]}%")
present paginate(projects), with: Entities::Project
end
I guess a guard on if params[:search_code].present?
to if params[:search_code].present? && project