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
}

0 commit comments

Comments
 (0)