Skip to content

Commit 769be87

Browse files
authored
Use link in UI which returned a relative url but not html_url which contains an absolute url (#21986)
partially fix #19345 This PR add some `Link` methods for different objects. The `Link` methods are not different from `HTMLURL`, they are lack of the absolute URL. And most of UI `HTMLURL` have been replaced to `Link` so that users can visit them from a different domain or IP. This PR also introduces a new javascript configuration `window.config.reqAppUrl` which is different from `appUrl` which is still an absolute url but the domain has been replaced to the current requested domain.
1 parent 189d5b7 commit 769be87

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+189
-81
lines changed

models/activities/action.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,49 @@ func (a *Action) GetRepoAbsoluteLink() string {
223223
return setting.AppURL + url.PathEscape(a.GetRepoUserName()) + "/" + url.PathEscape(a.GetRepoName())
224224
}
225225

226+
// GetCommentHTMLURL returns link to action comment.
227+
func (a *Action) GetCommentHTMLURL() string {
228+
return a.getCommentHTMLURL(db.DefaultContext)
229+
}
230+
231+
func (a *Action) loadComment(ctx context.Context) (err error) {
232+
if a.CommentID == 0 || a.Comment != nil {
233+
return nil
234+
}
235+
a.Comment, err = issues_model.GetCommentByID(ctx, a.CommentID)
236+
return err
237+
}
238+
239+
func (a *Action) getCommentHTMLURL(ctx context.Context) string {
240+
if a == nil {
241+
return "#"
242+
}
243+
_ = a.loadComment(ctx)
244+
if a.Comment != nil {
245+
return a.Comment.HTMLURL()
246+
}
247+
if len(a.GetIssueInfos()) == 0 {
248+
return "#"
249+
}
250+
// Return link to issue
251+
issueIDString := a.GetIssueInfos()[0]
252+
issueID, err := strconv.ParseInt(issueIDString, 10, 64)
253+
if err != nil {
254+
return "#"
255+
}
256+
257+
issue, err := issues_model.GetIssueByID(ctx, issueID)
258+
if err != nil {
259+
return "#"
260+
}
261+
262+
if err = issue.LoadRepo(ctx); err != nil {
263+
return "#"
264+
}
265+
266+
return issue.HTMLURL()
267+
}
268+
226269
// GetCommentLink returns link to action comment.
227270
func (a *Action) GetCommentLink() string {
228271
return a.getCommentLink(db.DefaultContext)
@@ -232,11 +275,9 @@ func (a *Action) getCommentLink(ctx context.Context) string {
232275
if a == nil {
233276
return "#"
234277
}
235-
if a.Comment == nil && a.CommentID != 0 {
236-
a.Comment, _ = issues_model.GetCommentByID(ctx, a.CommentID)
237-
}
278+
_ = a.loadComment(ctx)
238279
if a.Comment != nil {
239-
return a.Comment.HTMLURL()
280+
return a.Comment.Link()
240281
}
241282
if len(a.GetIssueInfos()) == 0 {
242283
return "#"
@@ -257,7 +298,7 @@ func (a *Action) getCommentLink(ctx context.Context) string {
257298
return "#"
258299
}
259300

260-
return issue.HTMLURL()
301+
return issue.Link()
261302
}
262303

263304
// GetBranch returns the action's repository branch.

models/activities/action_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestAction_GetRepoLink(t *testing.T) {
3636
expected := path.Join(setting.AppSubURL, owner.Name, repo.Name)
3737
assert.Equal(t, expected, action.GetRepoLink())
3838
assert.Equal(t, repo.HTMLURL(), action.GetRepoAbsoluteLink())
39-
assert.Equal(t, comment.HTMLURL(), action.GetCommentLink())
39+
assert.Equal(t, comment.HTMLURL(), action.GetCommentHTMLURL())
4040
}
4141

4242
func TestGetFeeds(t *testing.T) {

models/activities/notification.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,22 @@ func (n *Notification) HTMLURL() string {
459459
return ""
460460
}
461461

462+
// Link formats a relative URL-string to the notification
463+
func (n *Notification) Link() string {
464+
switch n.Source {
465+
case NotificationSourceIssue, NotificationSourcePullRequest:
466+
if n.Comment != nil {
467+
return n.Comment.Link()
468+
}
469+
return n.Issue.Link()
470+
case NotificationSourceCommit:
471+
return n.Repository.Link() + "/commit/" + url.PathEscape(n.CommitID)
472+
case NotificationSourceRepository:
473+
return n.Repository.Link()
474+
}
475+
return ""
476+
}
477+
462478
// APIURL formats a URL-string to the notification
463479
func (n *Notification) APIURL() string {
464480
return setting.AppURL + "api/v1/notifications/threads/" + strconv.FormatInt(n.ID, 10)

models/issues/comment.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,21 +391,40 @@ func (c *Comment) HTMLURL() string {
391391
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
392392
return ""
393393
}
394+
return c.Issue.HTMLURL() + c.hashLink()
395+
}
396+
397+
// Link formats a relative URL-string to the issue-comment
398+
func (c *Comment) Link() string {
399+
err := c.LoadIssue(db.DefaultContext)
400+
if err != nil { // Silently dropping errors :unamused:
401+
log.Error("LoadIssue(%d): %v", c.IssueID, err)
402+
return ""
403+
}
404+
err = c.Issue.LoadRepo(db.DefaultContext)
405+
if err != nil { // Silently dropping errors :unamused:
406+
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
407+
return ""
408+
}
409+
return c.Issue.Link() + c.hashLink()
410+
}
411+
412+
func (c *Comment) hashLink() string {
394413
if c.Type == CommentTypeCode {
395414
if c.ReviewID == 0 {
396-
return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag())
415+
return "/files#" + c.HashTag()
397416
}
398417
if c.Review == nil {
399418
if err := c.LoadReview(); err != nil {
400419
log.Warn("LoadReview(%d): %v", c.ReviewID, err)
401-
return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag())
420+
return "/files#" + c.HashTag()
402421
}
403422
}
404423
if c.Review.Type <= ReviewTypePending {
405-
return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag())
424+
return "/files#" + c.HashTag()
406425
}
407426
}
408-
return fmt.Sprintf("%s#%s", c.Issue.HTMLURL(), c.HashTag())
427+
return "#" + c.HashTag()
409428
}
410429

411430
// APIURL formats a API-string to the issue-comment
@@ -708,8 +727,8 @@ func (c *Comment) UnsignedLine() uint64 {
708727
return uint64(c.Line)
709728
}
710729

711-
// CodeCommentURL returns the url to a comment in code
712-
func (c *Comment) CodeCommentURL() string {
730+
// CodeCommentLink returns the url to a comment in code
731+
func (c *Comment) CodeCommentLink() string {
713732
err := c.LoadIssue(db.DefaultContext)
714733
if err != nil { // Silently dropping errors :unamused:
715734
log.Error("LoadIssue(%d): %v", c.IssueID, err)
@@ -720,7 +739,7 @@ func (c *Comment) CodeCommentURL() string {
720739
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
721740
return ""
722741
}
723-
return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag())
742+
return fmt.Sprintf("%s/files#%s", c.Issue.Link(), c.HashTag())
724743
}
725744

726745
// LoadPushCommits Load push commits

models/issues/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func (issue *Issue) HTMLURL() string {
419419
return fmt.Sprintf("%s/%s/%d", issue.Repo.HTMLURL(), path, issue.Index)
420420
}
421421

422-
// Link returns the Link URL to this issue.
422+
// Link returns the issue's relative URL.
423423
func (issue *Issue) Link() string {
424424
var path string
425425
if issue.IsPull {

models/issues/pull.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -759,20 +759,20 @@ func GetPullRequestsByHeadBranch(ctx context.Context, headBranch string, headRep
759759
return prs, nil
760760
}
761761

762-
// GetBaseBranchHTMLURL returns the HTML URL of the base branch
763-
func (pr *PullRequest) GetBaseBranchHTMLURL() string {
762+
// GetBaseBranchLink returns the relative URL of the base branch
763+
func (pr *PullRequest) GetBaseBranchLink() string {
764764
if err := pr.LoadBaseRepo(db.DefaultContext); err != nil {
765765
log.Error("LoadBaseRepo: %v", err)
766766
return ""
767767
}
768768
if pr.BaseRepo == nil {
769769
return ""
770770
}
771-
return pr.BaseRepo.HTMLURL() + "/src/branch/" + util.PathEscapeSegments(pr.BaseBranch)
771+
return pr.BaseRepo.Link() + "/src/branch/" + util.PathEscapeSegments(pr.BaseBranch)
772772
}
773773

774-
// GetHeadBranchHTMLURL returns the HTML URL of the head branch
775-
func (pr *PullRequest) GetHeadBranchHTMLURL() string {
774+
// GetHeadBranchLink returns the relative URL of the head branch
775+
func (pr *PullRequest) GetHeadBranchLink() string {
776776
if pr.Flow == PullRequestFlowAGit {
777777
return ""
778778
}
@@ -784,7 +784,7 @@ func (pr *PullRequest) GetHeadBranchHTMLURL() string {
784784
if pr.HeadRepo == nil {
785785
return ""
786786
}
787-
return pr.HeadRepo.HTMLURL() + "/src/branch/" + util.PathEscapeSegments(pr.HeadBranch)
787+
return pr.HeadRepo.Link() + "/src/branch/" + util.PathEscapeSegments(pr.HeadBranch)
788788
}
789789

790790
// UpdateAllowEdits update if PR can be edited from maintainers

models/packages/descriptor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type PackageFileDescriptor struct {
6565

6666
// PackageWebLink returns the package web link
6767
func (pd *PackageDescriptor) PackageWebLink() string {
68-
return fmt.Sprintf("%s/-/packages/%s/%s", pd.Owner.HTMLURL(), string(pd.Package.Type), url.PathEscape(pd.Package.LowerName))
68+
return fmt.Sprintf("%s/-/packages/%s/%s", pd.Owner.HomeLink(), string(pd.Package.Type), url.PathEscape(pd.Package.LowerName))
6969
}
7070

7171
// FullWebLink returns the package version web link

models/project/project.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func (p *Project) LoadRepo(ctx context.Context) (err error) {
116116
return err
117117
}
118118

119+
// Link returns the project's relative URL.
119120
func (p *Project) Link() string {
120121
if p.OwnerID > 0 {
121122
err := p.LoadOwner(db.DefaultContext)

models/repo/release.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ func (r *Release) HTMLURL() string {
130130
return r.Repo.HTMLURL() + "/releases/tag/" + util.PathEscapeSegments(r.TagName)
131131
}
132132

133+
// Link the relative url for a release on the web UI. release must have attributes loaded
134+
func (r *Release) Link() string {
135+
return r.Repo.Link() + "/releases/tag/" + util.PathEscapeSegments(r.TagName)
136+
}
137+
133138
// IsReleaseExist returns true if release with given tag name already exists.
134139
func IsReleaseExist(ctx context.Context, repoID int64, tagName string) (bool, error) {
135140
if len(tagName) == 0 {

models/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ func (repo *Repository) RepoPath() string {
480480
return RepoPath(repo.OwnerName, repo.Name)
481481
}
482482

483-
// Link returns the repository link
483+
// Link returns the repository relative url
484484
func (repo *Repository) Link() string {
485485
return setting.AppSubURL + "/" + url.PathEscape(repo.OwnerName) + "/" + url.PathEscape(repo.Name)
486486
}

modules/structs/repo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type Repository struct {
6363
Language string `json:"language"`
6464
LanguagesURL string `json:"languages_url"`
6565
HTMLURL string `json:"html_url"`
66+
Link string `json:"link"`
6667
SSHURL string `json:"ssh_url"`
6768
CloneURL string `json:"clone_url"`
6869
OriginalURL string `json:"original_url"`

routers/web/feed/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio
7373

7474
var content, desc, title string
7575

76-
link := &feeds.Link{Href: act.GetCommentLink()}
76+
link := &feeds.Link{Href: act.GetCommentHTMLURL()}
7777

7878
// title
7979
title = act.ActUser.DisplayName() + " "

routers/web/repo/pull.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ func setMergeTarget(ctx *context.Context, pull *issues_model.PullRequest) {
339339
ctx.Data["HeadTarget"] = pull.MustHeadUserName(ctx) + "/" + pull.HeadRepo.Name + ":" + pull.HeadBranch
340340
}
341341
ctx.Data["BaseTarget"] = pull.BaseBranch
342-
ctx.Data["HeadBranchHTMLURL"] = pull.GetHeadBranchHTMLURL()
343-
ctx.Data["BaseBranchHTMLURL"] = pull.GetBaseBranchHTMLURL()
342+
ctx.Data["HeadBranchLink"] = pull.GetHeadBranchLink()
343+
ctx.Data["BaseBranchLink"] = pull.GetBaseBranchLink()
344344
}
345345

346346
// PrepareMergedViewPullInfo show meta information for a merged pull request view page

routers/web/repo/repo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ func SearchRepo(ctx *context.Context) {
569569
Mirror: repo.IsMirror,
570570
Stars: repo.NumStars,
571571
HTMLURL: repo.HTMLURL(),
572+
Link: repo.Link(),
572573
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
573574
}
574575
}

templates/code/searchresults.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
<div class="diff-file-box diff-box file-content non-diff-file-content repo-search-result">
1414
<h4 class="ui top attached normal header">
1515
<span class="file">
16-
<a rel="nofollow" href="{{$repo.HTMLURL}}">{{$repo.FullName}}</a>
16+
<a rel="nofollow" href="{{$repo.Link}}">{{$repo.FullName}}</a>
1717
{{if $repo.IsArchived}}
1818
<span class="ui basic label">{{$.locale.Tr "repo.desc.archived"}}</span>
1919
{{end}}
2020
- {{.Filename}}
2121
</span>
22-
<a class="ui basic tiny button" rel="nofollow" href="{{$repo.HTMLURL}}/src/commit/{{$result.CommitID | PathEscape}}/{{.Filename | PathEscapeSegments}}">{{$.locale.Tr "repo.diff.view_file"}}</a>
22+
<a class="ui basic tiny button" rel="nofollow" href="{{$repo.Link}}/src/commit/{{$result.CommitID | PathEscape}}/{{.Filename | PathEscapeSegments}}">{{$.locale.Tr "repo.diff.view_file"}}</a>
2323
</h4>
2424
<div class="ui attached table segment">
2525
<div class="file-body file-code code-view">
@@ -28,7 +28,7 @@
2828
<tr>
2929
<td class="lines-num">
3030
{{range .LineNumbers}}
31-
<a href="{{$repo.HTMLURL}}/src/commit/{{$result.CommitID | PathEscape}}/{{$result.Filename | PathEscapeSegments}}#L{{.}}"><span>{{.}}</span></a>
31+
<a href="{{$repo.Link}}/src/commit/{{$result.CommitID | PathEscape}}/{{$result.Filename | PathEscapeSegments}}#L{{.}}"><span>{{.}}</span></a>
3232
{{end}}
3333
</td>
3434
<td class="lines-code chroma"><code class="code-inner">{{.FormattedLines | Safe}}</code></td>

templates/mail/issue/assigned.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<title>{{.Subject}}</title>
99
</head>
1010

11-
{{$repo_url := printf "<a href='%s'>%s</a>" (Escape .Issue.Repo.HTMLURL) (Escape .Issue.Repo.FullName)}}
11+
{{$repo_url := printf "<a href='%s'>%s</a>" (Escape .Issue.Repo.Link) (Escape .Issue.Repo.FullName)}}
1212
{{$link := printf "<a href='%s'>#%d</a>" (Escape .Link) .Issue.Index}}
1313
<body>
1414
<p>

templates/mail/issue/default.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
{{if eq .ActionName "push"}}
2121
<p>
2222
{{if .Comment.IsForcePush}}
23-
{{$oldCommitUrl := printf "%s/commit/%s" .Comment.Issue.PullRequest.BaseRepo.HTMLURL .Comment.OldCommit}}
23+
{{$oldCommitUrl := printf "%s/commit/%s" .Comment.Issue.PullRequest.BaseRepo.Link .Comment.OldCommit}}
2424
{{$oldShortSha := ShortSha .Comment.OldCommit}}
2525
{{$oldCommitLink := printf "<a href='%[1]s'><b>%[2]s</b></a>" (Escape $oldCommitUrl) (Escape $oldShortSha)}}
2626

27-
{{$newCommitUrl := printf "%s/commit/%s" .Comment.Issue.PullRequest.BaseRepo.HTMLURL .Comment.NewCommit}}
27+
{{$newCommitUrl := printf "%s/commit/%s" .Comment.Issue.PullRequest.BaseRepo.Link .Comment.NewCommit}}
2828
{{$newShortSha := ShortSha .Comment.NewCommit}}
2929
{{$newCommitLink := printf "<a href='%[1]s'><b>%[2]s</b></a>" (Escape $newCommitUrl) (Escape $newShortSha)}}
3030

@@ -72,7 +72,7 @@
7272
<ul>
7373
{{range .Comment.Commits}}
7474
<li>
75-
<a href="{{$.Comment.Issue.PullRequest.BaseRepo.HTMLURL}}/commit/{{.ID}}">
75+
<a href="{{$.Comment.Issue.PullRequest.BaseRepo.Link}}/commit/{{.ID}}">
7676
{{ShortSha .ID.String}}
7777
</a> - {{.Summary}}
7878
</li>

templates/mail/release.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
</head>
1313

14-
{{$release_url := printf "<a href='%s'>%s</a>" (.Release.HTMLURL | Escape) (.Release.TagName | Escape)}}
15-
{{$repo_url := printf "<a href='%s'>%s</a>" (.Release.Repo.HTMLURL | Escape) (.Release.Repo.FullName | Escape)}}
14+
{{$release_url := printf "<a href='%s'>%s</a>" (.Release.Link | Escape) (.Release.TagName | Escape)}}
15+
{{$repo_url := printf "<a href='%s'>%s</a>" (.Release.Repo.Link | Escape) (.Release.Repo.FullName | Escape)}}
1616
<body>
1717
<p>
1818
{{.locale.Tr "mail.release.new.text" .Release.Publisher.Name $release_url $repo_url | Str2html}}

templates/package/shared/list.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
{{$hasRepositoryAccess = index $.RepositoryAccessMap .Repository.ID}}
2929
{{end}}
3030
{{if $hasRepositoryAccess}}
31-
{{$.locale.Tr "packages.published_by_in" $timeStr .Creator.HomeLink (.Creator.GetDisplayName | Escape) .Repository.HTMLURL (.Repository.FullName | Escape) | Safe}}
31+
{{$.locale.Tr "packages.published_by_in" $timeStr .Creator.HomeLink (.Creator.GetDisplayName | Escape) .Repository.Link (.Repository.FullName | Escape) | Safe}}
3232
{{else}}
3333
{{$.locale.Tr "packages.published_by" $timeStr .Creator.HomeLink (.Creator.GetDisplayName | Escape) | Safe}}
3434
{{end}}
@@ -41,7 +41,7 @@
4141
{{svg "octicon-package" 32}}
4242
<h2>{{.locale.Tr "packages.empty"}}</h2>
4343
{{if and .Repository .CanWritePackages}}
44-
{{$packagesUrl := URLJoin .Owner.HTMLURL "-" "packages"}}
44+
{{$packagesUrl := URLJoin .Owner.HomeLink "-" "packages"}}
4545
<p>{{.locale.Tr "packages.empty.repo" $packagesUrl | Safe}}</p>
4646
{{end}}
4747
<p>{{.locale.Tr "packages.empty.documentation" | Safe}}</p>

0 commit comments

Comments
 (0)