Skip to content

Commit 43b4c38

Browse files
KN4CK3Rwxiaoguangtechknowlogick
authored
Use absolute links in feeds (#21229) (#21265)
Backport of #21229 Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent e79a107 commit 43b4c38

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

Diff for: models/action.go

+5
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ func (a *Action) GetRepoLink() string {
218218
return path.Join(setting.AppSubURL, "/", url.PathEscape(a.GetRepoUserName()), url.PathEscape(a.GetRepoName()))
219219
}
220220

221+
// GetRepoAbsoluteLink returns the absolute link to action repository.
222+
func (a *Action) GetRepoAbsoluteLink() string {
223+
return setting.AppURL + url.PathEscape(a.GetRepoUserName()) + "/" + url.PathEscape(a.GetRepoName())
224+
}
225+
221226
// GetRepositoryFromMatch returns a *repo_model.Repository from a username and repo strings
222227
func GetRepositoryFromMatch(ownerName, repoName string) (*repo_model.Repository, error) {
223228
var err error

Diff for: models/action_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"code.gitea.io/gitea/models/db"
12+
issue_model "code.gitea.io/gitea/models/issues"
1213
repo_model "code.gitea.io/gitea/models/repo"
1314
"code.gitea.io/gitea/models/unittest"
1415
user_model "code.gitea.io/gitea/models/user"
@@ -19,20 +20,23 @@ import (
1920

2021
func TestAction_GetRepoPath(t *testing.T) {
2122
assert.NoError(t, unittest.PrepareTestDatabase())
22-
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{}).(*repo_model.Repository)
23+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
2324
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
2425
action := &Action{RepoID: repo.ID}
2526
assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath())
2627
}
2728

2829
func TestAction_GetRepoLink(t *testing.T) {
2930
assert.NoError(t, unittest.PrepareTestDatabase())
30-
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{}).(*repo_model.Repository)
31+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
3132
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
32-
action := &Action{RepoID: repo.ID}
33+
comment := unittest.AssertExistsAndLoadBean(t, &issue_model.Comment{ID: 2}).(*issue_model.Comment)
34+
action := &Action{RepoID: repo.ID, CommentID: comment.ID}
3335
setting.AppSubURL = "/suburl"
3436
expected := path.Join(setting.AppSubURL, owner.Name, repo.Name)
3537
assert.Equal(t, expected, action.GetRepoLink())
38+
assert.Equal(t, repo.HTMLURL(), action.GetRepoAbsoluteLink())
39+
assert.Equal(t, comment.HTMLURL(), action.GetCommentLink())
3640
}
3741

3842
func TestGetFeeds(t *testing.T) {

Diff for: routers/web/feed/convert.go

+31-31
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@ import (
2424
)
2525

2626
func toBranchLink(act *models.Action) string {
27-
return act.GetRepoLink() + "/src/branch/" + util.PathEscapeSegments(act.GetBranch())
27+
return act.GetRepoAbsoluteLink() + "/src/branch/" + util.PathEscapeSegments(act.GetBranch())
2828
}
2929

3030
func toTagLink(act *models.Action) string {
31-
return act.GetRepoLink() + "/src/tag/" + util.PathEscapeSegments(act.GetTag())
31+
return act.GetRepoAbsoluteLink() + "/src/tag/" + util.PathEscapeSegments(act.GetTag())
3232
}
3333

3434
func toIssueLink(act *models.Action) string {
35-
return act.GetRepoLink() + "/issues/" + url.PathEscape(act.GetIssueInfos()[0])
35+
return act.GetRepoAbsoluteLink() + "/issues/" + url.PathEscape(act.GetIssueInfos()[0])
3636
}
3737

3838
func toPullLink(act *models.Action) string {
39-
return act.GetRepoLink() + "/pulls/" + url.PathEscape(act.GetIssueInfos()[0])
39+
return act.GetRepoAbsoluteLink() + "/pulls/" + url.PathEscape(act.GetIssueInfos()[0])
4040
}
4141

4242
func toSrcLink(act *models.Action) string {
43-
return act.GetRepoLink() + "/src/" + util.PathEscapeSegments(act.GetBranch())
43+
return act.GetRepoAbsoluteLink() + "/src/" + util.PathEscapeSegments(act.GetBranch())
4444
}
4545

4646
func toReleaseLink(act *models.Action) string {
47-
return act.GetRepoLink() + "/releases/tag/" + util.PathEscapeSegments(act.GetBranch())
47+
return act.GetRepoAbsoluteLink() + "/releases/tag/" + util.PathEscapeSegments(act.GetBranch())
4848
}
4949

5050
// renderMarkdown creates a minimal markdown render context from an action.
@@ -79,17 +79,17 @@ func feedActionsToFeedItems(ctx *context.Context, actions models.ActionList) (it
7979
title = act.ActUser.DisplayName() + " "
8080
switch act.OpType {
8181
case models.ActionCreateRepo:
82-
title += ctx.TrHTMLEscapeArgs("action.create_repo", act.GetRepoLink(), act.ShortRepoPath())
83-
link.Href = act.GetRepoLink()
82+
title += ctx.TrHTMLEscapeArgs("action.create_repo", act.GetRepoAbsoluteLink(), act.ShortRepoPath())
83+
link.Href = act.GetRepoAbsoluteLink()
8484
case models.ActionRenameRepo:
85-
title += ctx.TrHTMLEscapeArgs("action.rename_repo", act.GetContent(), act.GetRepoLink(), act.ShortRepoPath())
86-
link.Href = act.GetRepoLink()
85+
title += ctx.TrHTMLEscapeArgs("action.rename_repo", act.GetContent(), act.GetRepoAbsoluteLink(), act.ShortRepoPath())
86+
link.Href = act.GetRepoAbsoluteLink()
8787
case models.ActionCommitRepo:
8888
link.Href = toBranchLink(act)
8989
if len(act.Content) != 0 {
90-
title += ctx.TrHTMLEscapeArgs("action.commit_repo", act.GetRepoLink(), link.Href, act.GetBranch(), act.ShortRepoPath())
90+
title += ctx.TrHTMLEscapeArgs("action.commit_repo", act.GetRepoAbsoluteLink(), link.Href, act.GetBranch(), act.ShortRepoPath())
9191
} else {
92-
title += ctx.TrHTMLEscapeArgs("action.create_branch", act.GetRepoLink(), link.Href, act.GetBranch(), act.ShortRepoPath())
92+
title += ctx.TrHTMLEscapeArgs("action.create_branch", act.GetRepoAbsoluteLink(), link.Href, act.GetBranch(), act.ShortRepoPath())
9393
}
9494
case models.ActionCreateIssue:
9595
link.Href = toIssueLink(act)
@@ -98,11 +98,11 @@ func feedActionsToFeedItems(ctx *context.Context, actions models.ActionList) (it
9898
link.Href = toPullLink(act)
9999
title += ctx.TrHTMLEscapeArgs("action.create_pull_request", link.Href, act.GetIssueInfos()[0], act.ShortRepoPath())
100100
case models.ActionTransferRepo:
101-
link.Href = act.GetRepoLink()
102-
title += ctx.TrHTMLEscapeArgs("action.transfer_repo", act.GetContent(), act.GetRepoLink(), act.ShortRepoPath())
101+
link.Href = act.GetRepoAbsoluteLink()
102+
title += ctx.TrHTMLEscapeArgs("action.transfer_repo", act.GetContent(), act.GetRepoAbsoluteLink(), act.ShortRepoPath())
103103
case models.ActionPushTag:
104104
link.Href = toTagLink(act)
105-
title += ctx.TrHTMLEscapeArgs("action.push_tag", act.GetRepoLink(), link.Href, act.GetTag(), act.ShortRepoPath())
105+
title += ctx.TrHTMLEscapeArgs("action.push_tag", act.GetRepoAbsoluteLink(), link.Href, act.GetTag(), act.ShortRepoPath())
106106
case models.ActionCommentIssue:
107107
issueLink := toIssueLink(act)
108108
if link.Href == "#" {
@@ -140,26 +140,26 @@ func feedActionsToFeedItems(ctx *context.Context, actions models.ActionList) (it
140140
}
141141
title += ctx.TrHTMLEscapeArgs("action.reopen_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
142142
case models.ActionDeleteTag:
143-
link.Href = act.GetRepoLink()
144-
title += ctx.TrHTMLEscapeArgs("action.delete_tag", act.GetRepoLink(), act.GetTag(), act.ShortRepoPath())
143+
link.Href = act.GetRepoAbsoluteLink()
144+
title += ctx.TrHTMLEscapeArgs("action.delete_tag", act.GetRepoAbsoluteLink(), act.GetTag(), act.ShortRepoPath())
145145
case models.ActionDeleteBranch:
146-
link.Href = act.GetRepoLink()
147-
title += ctx.TrHTMLEscapeArgs("action.delete_branch", act.GetRepoLink(), html.EscapeString(act.GetBranch()), act.ShortRepoPath())
146+
link.Href = act.GetRepoAbsoluteLink()
147+
title += ctx.TrHTMLEscapeArgs("action.delete_branch", act.GetRepoAbsoluteLink(), html.EscapeString(act.GetBranch()), act.ShortRepoPath())
148148
case models.ActionMirrorSyncPush:
149149
srcLink := toSrcLink(act)
150150
if link.Href == "#" {
151151
link.Href = srcLink
152152
}
153-
title += ctx.TrHTMLEscapeArgs("action.mirror_sync_push", act.GetRepoLink(), srcLink, act.GetBranch(), act.ShortRepoPath())
153+
title += ctx.TrHTMLEscapeArgs("action.mirror_sync_push", act.GetRepoAbsoluteLink(), srcLink, act.GetBranch(), act.ShortRepoPath())
154154
case models.ActionMirrorSyncCreate:
155155
srcLink := toSrcLink(act)
156156
if link.Href == "#" {
157157
link.Href = srcLink
158158
}
159-
title += ctx.TrHTMLEscapeArgs("action.mirror_sync_create", act.GetRepoLink(), srcLink, act.GetBranch(), act.ShortRepoPath())
159+
title += ctx.TrHTMLEscapeArgs("action.mirror_sync_create", act.GetRepoAbsoluteLink(), srcLink, act.GetBranch(), act.ShortRepoPath())
160160
case models.ActionMirrorSyncDelete:
161-
link.Href = act.GetRepoLink()
162-
title += ctx.TrHTMLEscapeArgs("action.mirror_sync_delete", act.GetRepoLink(), act.GetBranch(), act.ShortRepoPath())
161+
link.Href = act.GetRepoAbsoluteLink()
162+
title += ctx.TrHTMLEscapeArgs("action.mirror_sync_delete", act.GetRepoAbsoluteLink(), act.GetBranch(), act.ShortRepoPath())
163163
case models.ActionApprovePullRequest:
164164
pullLink := toPullLink(act)
165165
title += ctx.TrHTMLEscapeArgs("action.approve_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
@@ -174,16 +174,16 @@ func feedActionsToFeedItems(ctx *context.Context, actions models.ActionList) (it
174174
if link.Href == "#" {
175175
link.Href = releaseLink
176176
}
177-
title += ctx.TrHTMLEscapeArgs("action.publish_release", act.GetRepoLink(), releaseLink, act.ShortRepoPath(), act.Content)
177+
title += ctx.TrHTMLEscapeArgs("action.publish_release", act.GetRepoAbsoluteLink(), releaseLink, act.ShortRepoPath(), act.Content)
178178
case models.ActionPullReviewDismissed:
179179
pullLink := toPullLink(act)
180180
title += ctx.TrHTMLEscapeArgs("action.review_dismissed", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(), act.GetIssueInfos()[1])
181181
case models.ActionStarRepo:
182-
link.Href = act.GetRepoLink()
183-
title += ctx.TrHTMLEscapeArgs("action.starred_repo", act.GetRepoLink(), act.GetRepoPath())
182+
link.Href = act.GetRepoAbsoluteLink()
183+
title += ctx.TrHTMLEscapeArgs("action.starred_repo", act.GetRepoAbsoluteLink(), act.GetRepoPath())
184184
case models.ActionWatchRepo:
185-
link.Href = act.GetRepoLink()
186-
title += ctx.TrHTMLEscapeArgs("action.watched_repo", act.GetRepoLink(), act.GetRepoPath())
185+
link.Href = act.GetRepoAbsoluteLink()
186+
title += ctx.TrHTMLEscapeArgs("action.watched_repo", act.GetRepoAbsoluteLink(), act.GetRepoPath())
187187
default:
188188
return nil, fmt.Errorf("unknown action type: %v", act.OpType)
189189
}
@@ -193,14 +193,14 @@ func feedActionsToFeedItems(ctx *context.Context, actions models.ActionList) (it
193193
switch act.OpType {
194194
case models.ActionCommitRepo, models.ActionMirrorSyncPush:
195195
push := templates.ActionContent2Commits(act)
196-
repoLink := act.GetRepoLink()
196+
repoLink := act.GetRepoAbsoluteLink()
197197

198198
for _, commit := range push.Commits {
199199
if len(desc) != 0 {
200200
desc += "\n\n"
201201
}
202202
desc += fmt.Sprintf("<a href=\"%s\">%s</a>\n%s",
203-
html.EscapeString(fmt.Sprintf("%s/commit/%s", act.GetRepoLink(), commit.Sha1)),
203+
html.EscapeString(fmt.Sprintf("%s/commit/%s", act.GetRepoAbsoluteLink(), commit.Sha1)),
204204
commit.Sha1,
205205
templates.RenderCommitMessage(ctx, commit.Message, repoLink, nil),
206206
)
@@ -209,7 +209,7 @@ func feedActionsToFeedItems(ctx *context.Context, actions models.ActionList) (it
209209
if push.Len > 1 {
210210
link = &feeds.Link{Href: fmt.Sprintf("%s/%s", setting.AppSubURL, push.CompareURL)}
211211
} else if push.Len == 1 {
212-
link = &feeds.Link{Href: fmt.Sprintf("%s/commit/%s", act.GetRepoLink(), push.Commits[0].Sha1)}
212+
link = &feeds.Link{Href: fmt.Sprintf("%s/commit/%s", act.GetRepoAbsoluteLink(), push.Commits[0].Sha1)}
213213
}
214214

215215
case models.ActionCreateIssue, models.ActionCreatePullRequest:

0 commit comments

Comments
 (0)