Skip to content

GitLab

  • Menu
Projects Groups Snippets
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
  • G gitlabhq1
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 21
    • Issues 21
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 12
    • Merge requests 12
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Infrastructure Registry
  • Analytics
    • Analytics
    • CI/CD
    • Repository
    • Value stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • gpt
  • large_projects
  • gitlabhq1
  • Issues
  • #5349

Closed
Open
Created Oct 16, 2013 by Administrator@rootOwner

Searching code in public repos causes 500 error

Created by: karlhungus

To reproduce:

  1. Create two users test1, test2
  2. Create project as test 1, push some code to it, make it public
  3. 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

Assignee
Assign to
Time tracking