Skip to content
Snippets Groups Projects

Redirect trees to blobs and vice versa

Merged Administrator requested to merge redirect-tree-blob into master

Created by: felixfbecker

Extensions in code insights want to link to file paths and directories to support drilling into the details. Given how our URLs are structured, they currently need to know whether the file path is a directory or file to construct the URL. This information is not always available, and would require making a lot of API requests to determine eagerly.

We can be helpful by instead just redirecting a tree to a blob and vice versa if the file/directory turned out to be a directory/file. This also makes us return a proper 404 on not found files. Redirect is implemented both in the client and the backend (to support internal and external links).

Please give extra care to my Go code :smile:

Merge request reports

Approval is optional

Merged by avatar (Jul 26, 2025 10:28am UTC)

Merge details

  • Changes merged into master with 7146f81c.
  • Deleted the source branch.

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Created by: codecov[bot]

    Codecov Report

    Merging #10193 into master will decrease coverage by 0.02%. The diff coverage is 10.25%.

    @@            Coverage Diff             @@
    ##           master   #10193      +/-   ##
    ==========================================
    - Coverage   43.47%   43.45%   -0.03%     
    ==========================================
      Files        1394     1394              
      Lines       76850    76898      +48     
      Branches     7017     6868     -149     
    ==========================================
      Hits        33414    33414              
    - Misses      40323    40370      +47     
    - Partials     3113     3114       +1     
    Flag Coverage Δ
    #go 46.51% <2.08%> (-0.04%) :arrow_down:
    #typescript 34.19% <23.33%> (-0.01%) :arrow_down:
    #unit 43.45% <10.25%> (-0.03%) :arrow_down:
    Impacted Files Coverage Δ
    cmd/frontend/internal/app/ui/router.go 53.87% <0.00%> (ø)
    shared/src/panel/views/FileLocations.tsx 80.76% <ø> (-0.72%) :arrow_down:
    web/src/repo/TreePage.tsx 0.00% <0.00%> (ø)
    web/src/repo/blob/BlobPage.tsx 0.00% <0.00%> (ø)
    web/src/repo/blob/GoToRawAction.tsx 0.00% <0.00%> (ø)
    web/src/repo/routes.tsx 0.00% <0.00%> (ø)
    cmd/frontend/internal/app/ui/handlers.go 32.00% <2.12%> (-10.97%) :arrow_down:
    web/src/util/url.ts 55.35% <50.00%> (-0.79%) :arrow_down:
    shared/src/util/url.ts 89.85% <100.00%> (+0.09%) :arrow_up:
  • Created by: slimsag

    LGTM but I note there is no test coverage for the backend change, can you at least file an issue for that and have e.g. @unknwon help out here?

  • Created by: felixfbecker

    I'd like to have tests for this too, but how to write those while mocking the relevant API here is beyond my expertise... @unknwon would you be willing to help? Or anyone else from @sourcegraph/core-services?

  • Created by: felixfbecker

    Also, should I use SafeRedirectURL() here? !10167 (merged)

  • Created by: unknwon

    Also, should I use SafeRedirectURL() here? #10167 (files)

    I think yes, good catch! We can't be 100% sure at all time and it never hurts to sanitize the redirect URL.

    I'd like to have tests for this too, but how to write those while mocking the relevant API here is beyond my expertise... @unknwon would you be willing to help?

    Could you list out some inputs/outputs you think that are worthing testing? (sorry not familiar with the set of URLs here) Then I'll set up the corresponding test code to ensure the behavior. If this PR is a major blocker of your other on-going work, feel free to merge as you see fit and open an issue to:

    1. Include inputs/outputs,
    2. reference to this PR,
    3. assign to me.
  • Created by: unknwon

    BTW another pro tip in case you're not aware of: with multiple suggestions you want to accept, you can go to Files changed tab and batch apply all of them in one commit. Though doesn't change anything in our workflow, just FYI :D

  • Created by: felixfbecker

    @unknwon here are the redirects I'd like to test:

    From To
    /repo/-/tree/some/file.go /repo/-/blob/some/file.go
    /repo/-/blob/some/dir /repo/-/tree/some/dir
    /repo@rev/-/tree/some/file.go /repo@rev/-/blob/some/file.go
    /repo@rev/-/blob/some/dir /repo@rev/-/tree/some/dir
    /repo/-/tree /repo
    /repo/-/blob /repo
    /repo@rev/-/tree /repo@rev
    /repo@rev/-/blob /repo@rev
    /repo/-/tree/some/dir No redirect
    /repo/-/blob/some/file.go No redirect
    /repo No redirect

    I assume that trailing slash variants don't need to be tested because we globally redirect trailing slashes to non-trailing slashes (before this handler is called).

Please register or sign in to reply
Loading