Code search returns 404 when querying across multiple repositories that have different default branches
Disclaimer: I could not search for existing bugs because GitLab issue searching was repeatedly failing with error (see bug #340124 (closed))
Summary
Code search returns 404 when querying across multiple repositories that have different default branches
Steps to reproduce
- Identify a GitLab subgroup that contains at least 2 repositories, one with default branch of
master
and one with default branch ofmain
(e.g. https://gitlab.com/gitlab-com) - Browse to a repository in this subgroup (e.g. https://gitlab.com/gitlab-com/early-career-professionals-tmrg)
- Search for the code that you are interested in (e.g. https://gitlab.com/search?search=%22Mission+Statement%22+Career&group_id=6543&project_id=33995506&scope=&search_code=true&snippets=false&repository_ref=main&nav_source=navbar)
- Remove the
Project
filter so that you are searching across the whole subgroup (e.g. https://gitlab.com/search?search=%22Mission+Statement%22+Career&group_id=6543&scope=&search_code=true&snippets=false&repository_ref=main) - Switch to
Code
search results (e.g. https://gitlab.com/search?group_id=6543&repository_ref=main&scope=blobs&search=%22Mission+Statement%22+Career&snippets=false) - Click on a search result link from a repository that has a different default branch (e.g. https://gitlab.com/gitlab-com/alliances/screenful/sandbox-projects/www-gitlab-com/-/blob/main/sites/handbook/source/handbook/engineering/security/women-in-security.html.md)
Example Project
Listed above in reproduction steps
What is the current bug behavior?
Link returns 404 because the branch name is incorrect
What is the expected correct behavior?
Link should use the default branch name of the project, so the URL provided by the code search page should be
https://gitlab.com/gitlab-com/alliances/screenful/sandbox-projects/www-gitlab-com/-/blob/master/sites/handbook/source/handbook/engineering/security/women-in-security.html.md
(note the change from main
to master
)
Relevant logs and/or screenshots
Output of checks
This bug happens on GitLab.com
Results of GitLab environment info
Expand for output related to GitLab environment info
(For installations with omnibus-gitlab package run and paste the output of: `sudo gitlab-rake gitlab:env:info`) (For installations from source run and paste the output of: `sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production`)
Results of GitLab application Check
Expand for output related to the GitLab application check
(For installations with omnibus-gitlab package run and paste the output of:
sudo gitlab-rake gitlab:check SANITIZE=true
)(For installations from source run and paste the output of:
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production SANITIZE=true
)(we will only investigate if the tests are passing)
Possible fixes
In API endpoint https://gitlab.com/search, the query parameter repository_ref=
should only be present when searching within a single project, because the query parameter repository_ref=main
for a sub-group search makes no sense when the sub-group contains repositories with a mix of default branches. Note that leaving repository_ref
blank fixes this issue, so I recommend clearing this query parameter when removing the Project
filter