mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-11-04 08:21:11 +00:00 
			
		
		
		
	[GITEA] Fix generated source URL on rendered files
- The permalink and 'Reference in New issue' URL of an renderable file (those where you can see the source and a rendered version of it, such as markdown) doesn't contain `?display=source`. This leads the issue that the URL doesn't have any effect, as by default the rendered version is shown and thus not the source. - Add `?display=source` to the permalink URL and to 'Reference in New Issue' if it's renderable file. - Add integration testing. (cherry picked from commit458f20f862) (cherry picked from commit5e630d7b10) Conflicts: models/fixtures/repo_unit.yml https://codeberg.org/forgejo/forgejo/pulls/1178
This commit is contained in:
		
					parent
					
						
							
								f3a6e1f121
							
						
					
				
			
			
				commit
				
					
						a10c6eb9c9
					
				
			
		
					 3 changed files with 52 additions and 2 deletions
				
			
		| 
						 | 
					@ -608,6 +608,20 @@
 | 
				
			||||||
  type: 1
 | 
					  type: 1
 | 
				
			||||||
  created_unix: 946684810
 | 
					  created_unix: 946684810
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# BEGIN Forgejo [GITEA] Fix generated source URL on rendered files
 | 
				
			||||||
 | 
					-
 | 
				
			||||||
 | 
					  id: 1091
 | 
				
			||||||
 | 
					  repo_id: 49
 | 
				
			||||||
 | 
					  type: 1
 | 
				
			||||||
 | 
					  created_unix: 946684810
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-
 | 
				
			||||||
 | 
					  id: 1092
 | 
				
			||||||
 | 
					  repo_id: 49
 | 
				
			||||||
 | 
					  type: 2
 | 
				
			||||||
 | 
					  created_unix: 946684810
 | 
				
			||||||
 | 
					# END Forgejo [GITEA] Fix generated source URL on rendered files
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-
 | 
					-
 | 
				
			||||||
  id: 91
 | 
					  id: 91
 | 
				
			||||||
  repo_id: 58
 | 
					  repo_id: 58
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -116,10 +116,10 @@
 | 
				
			||||||
				</table>
 | 
									</table>
 | 
				
			||||||
				<div class="code-line-menu ui vertical pointing menu tippy-target">
 | 
									<div class="code-line-menu ui vertical pointing menu tippy-target">
 | 
				
			||||||
					{{if $.Permission.CanRead $.UnitTypeIssues}}
 | 
										{{if $.Permission.CanRead $.UnitTypeIssues}}
 | 
				
			||||||
						<a class="item ref-in-new-issue" data-url-issue-new="{{.RepoLink}}/issues/new" data-url-param-body-link="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}" rel="nofollow noindex">{{.locale.Tr "repo.issues.context.reference_issue"}}</a>
 | 
											<a class="item ref-in-new-issue" data-url-issue-new="{{.RepoLink}}/issues/new" data-url-param-body-link="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}{{if $.HasSourceRenderedToggle}}?display=source{{end}}" rel="nofollow noindex">{{.locale.Tr "repo.issues.context.reference_issue"}}</a>
 | 
				
			||||||
					{{end}}
 | 
										{{end}}
 | 
				
			||||||
					<a class="item view_git_blame" href="{{.Repository.Link}}/blame/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}">{{.locale.Tr "repo.view_git_blame"}}</a>
 | 
										<a class="item view_git_blame" href="{{.Repository.Link}}/blame/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}">{{.locale.Tr "repo.view_git_blame"}}</a>
 | 
				
			||||||
					<a class="item copy-line-permalink" data-url="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}">{{.locale.Tr "repo.file_copy_permalink"}}</a>
 | 
										<a class="item copy-line-permalink" data-url="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}{{if $.HasSourceRenderedToggle}}?display=source{{end}}">{{.locale.Tr "repo.file_copy_permalink"}}</a>
 | 
				
			||||||
				</div>
 | 
									</div>
 | 
				
			||||||
				{{end}}
 | 
									{{end}}
 | 
				
			||||||
			{{end}}
 | 
								{{end}}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -408,3 +408,39 @@ func TestMarkDownReadmeImageSubfolder(t *testing.T) {
 | 
				
			||||||
	assert.True(t, exists, "Image not found in markdown file")
 | 
						assert.True(t, exists, "Image not found in markdown file")
 | 
				
			||||||
	assert.Equal(t, "/user2/repo1/media/branch/sub-home-md-img-check/docs/test-fake-img.jpg", src)
 | 
						assert.Equal(t, "/user2/repo1/media/branch/sub-home-md-img-check/docs/test-fake-img.jpg", src)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestGeneratedSourceLink(t *testing.T) {
 | 
				
			||||||
 | 
						defer tests.PrepareTestEnv(t)()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("Rendered file", func(t *testing.T) {
 | 
				
			||||||
 | 
							defer tests.PrintCurrentTest(t)()
 | 
				
			||||||
 | 
							req := NewRequest(t, "GET", "/user2/repo1/src/branch/master/README.md?display=source")
 | 
				
			||||||
 | 
							resp := MakeRequest(t, req, http.StatusOK)
 | 
				
			||||||
 | 
							doc := NewHTMLParser(t, resp.Body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							dataURL, exists := doc.doc.Find(".copy-line-permalink").Attr("data-url")
 | 
				
			||||||
 | 
							assert.True(t, exists)
 | 
				
			||||||
 | 
							assert.Equal(t, "/user2/repo1/src/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d/README.md?display=source", dataURL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							dataURL, exists = doc.doc.Find(".ref-in-new-issue").Attr("data-url-param-body-link")
 | 
				
			||||||
 | 
							assert.True(t, exists)
 | 
				
			||||||
 | 
							assert.Equal(t, "/user2/repo1/src/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d/README.md?display=source", dataURL)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("Non-Rendered file", func(t *testing.T) {
 | 
				
			||||||
 | 
							defer tests.PrintCurrentTest(t)()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							session := loginUser(t, "user27")
 | 
				
			||||||
 | 
							req := NewRequest(t, "GET", "/user27/repo49/src/branch/master/test/test.txt")
 | 
				
			||||||
 | 
							resp := session.MakeRequest(t, req, http.StatusOK)
 | 
				
			||||||
 | 
							doc := NewHTMLParser(t, resp.Body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							dataURL, exists := doc.doc.Find(".copy-line-permalink").Attr("data-url")
 | 
				
			||||||
 | 
							assert.True(t, exists)
 | 
				
			||||||
 | 
							assert.Equal(t, "/user27/repo49/src/commit/aacbdfe9e1c4b47f60abe81849045fa4e96f1d75/test/test.txt", dataURL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							dataURL, exists = doc.doc.Find(".ref-in-new-issue").Attr("data-url-param-body-link")
 | 
				
			||||||
 | 
							assert.True(t, exists)
 | 
				
			||||||
 | 
							assert.Equal(t, "/user27/repo49/src/commit/aacbdfe9e1c4b47f60abe81849045fa4e96f1d75/test/test.txt", dataURL)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue