Redirect trees to blobs and vice versa
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
Merge request reports
Activity
Created by: codecov[bot]
Codecov Report
Merging #10193 into master will decrease coverage by
0.02%
. The diff coverage is10.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%)
#typescript 34.19% <23.33%> (-0.01%)
#unit 43.45% <10.25%> (-0.03%)
Impacted Files Coverage Δ cmd/frontend/internal/app/ui/router.go 53.87% <0.00%> (ø)
shared/src/panel/views/FileLocations.tsx 80.76% <ø> (-0.72%)
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%)
web/src/util/url.ts 55.35% <50.00%> (-0.79%)
shared/src/util/url.ts 89.85% <100.00%> (+0.09%)
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:
- Include inputs/outputs,
- reference to this PR,
- assign to me.
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).