Skip to content

Commit f52fe82

Browse files
KN4CK3Rwxiaoguangtechknowlogick
authored
Use absolute links in feeds (#21229)
fixes #20864 Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent acee32c commit f52fe82

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

models/activities/action.go

Lines changed: 5 additions & 0 deletions
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
// GetCommentLink returns link to action comment.
222227
func (a *Action) GetCommentLink() string {
223228
return a.getCommentLink(db.DefaultContext)

models/activities/action_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
activities_model "code.gitea.io/gitea/models/activities"
1212
"code.gitea.io/gitea/models/db"
13+
issue_model "code.gitea.io/gitea/models/issues"
1314
repo_model "code.gitea.io/gitea/models/repo"
1415
"code.gitea.io/gitea/models/unittest"
1516
user_model "code.gitea.io/gitea/models/user"
@@ -20,20 +21,23 @@ import (
2021

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

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

3943
func TestGetFeeds(t *testing.T) {

routers/web/feed/convert.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@ import (
2424
)
2525

2626
func toBranchLink(act *activities_model.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 *activities_model.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 *activities_model.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 *activities_model.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 *activities_model.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 *activities_model.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 activities_model.Actio
7979
title = act.ActUser.DisplayName() + " "
8080
switch act.OpType {
8181
case activities_model.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 activities_model.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 activities_model.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 activities_model.ActionCreateIssue:
9595
link.Href = toIssueLink(act)
@@ -98,11 +98,11 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio
9898
link.Href = toPullLink(act)
9999
title += ctx.TrHTMLEscapeArgs("action.create_pull_request", link.Href, act.GetIssueInfos()[0], act.ShortRepoPath())
100100
case activities_model.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 activities_model.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 activities_model.ActionCommentIssue:
107107
issueLink := toIssueLink(act)
108108
if link.Href == "#" {
@@ -140,26 +140,26 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio
140140
}
141141
title += ctx.TrHTMLEscapeArgs("action.reopen_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
142142
case activities_model.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 activities_model.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 activities_model.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 activities_model.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 activities_model.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 activities_model.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 activities_model.Actio
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 activities_model.ActionPullReviewDismissed:
179179
pullLink := toPullLink(act)
180180
title += ctx.TrHTMLEscapeArgs("action.review_dismissed", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(), act.GetIssueInfos()[1])
181181
case activities_model.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 activities_model.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 activities_model.Actio
193193
switch act.OpType {
194194
case activities_model.ActionCommitRepo, activities_model.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 activities_model.Actio
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 activities_model.ActionCreateIssue, activities_model.ActionCreatePullRequest:

0 commit comments

Comments
 (0)