From 20310a833b46bbf7bd89ef28b111bfb7f137d103 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 16:49:29 +0800 Subject: [PATCH 01/27] Use link in UI which returned a relative url but not html_url which contains an absolute url --- models/activities/notification.go | 16 ++++++++++++++++ models/issues/comment.go | 18 ++++++++++++++---- models/repo/release.go | 5 +++++ modules/structs/repo.go | 1 + routers/web/repo/repo.go | 19 ++++++++++--------- templates/code/searchresults.tmpl | 6 +++--- templates/mail/issue/assigned.tmpl | 2 +- templates/mail/issue/default.tmpl | 6 +++--- templates/mail/release.tmpl | 4 ++-- templates/package/shared/list.tmpl | 4 ++-- templates/package/view.tmpl | 6 +++--- templates/repo/branch/list.tmpl | 8 ++++---- templates/repo/diff/box.tmpl | 4 ++-- templates/repo/diff/comment_form.tmpl | 4 ++-- templates/repo/editor/edit.tmpl | 4 ++-- templates/repo/header.tmpl | 2 +- templates/repo/issue/comment_tab.tmpl | 4 ++-- templates/repo/issue/view_content.tmpl | 2 +- .../repo/issue/view_content/comments.tmpl | 6 +++--- .../repo/issue/view_content/context_menu.tmpl | 4 ++-- templates/repo/issue/view_content/pull.tmpl | 4 ++-- .../view_content/pull_merge_instruction.tmpl | 2 +- templates/repo/projects/view.tmpl | 2 +- templates/repo/release/new.tmpl | 2 +- templates/repo/view_file.tmpl | 6 +++--- templates/repo/wiki/new.tmpl | 4 ++-- templates/shared/issuelist.tmpl | 10 +++++----- templates/user/dashboard/repolist.tmpl | 2 +- .../user/notification/notification_div.tmpl | 6 +++--- 29 files changed, 98 insertions(+), 65 deletions(-) diff --git a/models/activities/notification.go b/models/activities/notification.go index 9aa4b87628b1f..e039104665950 100644 --- a/models/activities/notification.go +++ b/models/activities/notification.go @@ -459,6 +459,22 @@ func (n *Notification) HTMLURL() string { return "" } +// Link formats a URL-string to the notification +func (n *Notification) Link() string { + switch n.Source { + case NotificationSourceIssue, NotificationSourcePullRequest: + if n.Comment != nil { + return n.Comment.Link() + } + return n.Issue.Link() + case NotificationSourceCommit: + return n.Repository.Link() + "/commit/" + url.PathEscape(n.CommitID) + case NotificationSourceRepository: + return n.Repository.Link() + } + return "" +} + // APIURL formats a URL-string to the notification func (n *Notification) APIURL() string { return setting.AppURL + "api/v1/notifications/threads/" + strconv.FormatInt(n.ID, 10) diff --git a/models/issues/comment.go b/models/issues/comment.go index 0aaa870b46590..b46bc48281293 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -376,6 +376,15 @@ func (c *Comment) AfterDelete() { // HTMLURL formats a URL-string to the issue-comment func (c *Comment) HTMLURL() string { + return c.link(c.Issue.HTMLURL()) +} + +// Link formats a URL-string to the issue-comment +func (c *Comment) Link() string { + return c.link(c.Issue.Link()) +} + +func (c *Comment) link(baseURL string) string { err := c.LoadIssue(db.DefaultContext) if err != nil { // Silently dropping errors :unamused: log.Error("LoadIssue(%d): %v", c.IssueID, err) @@ -386,21 +395,22 @@ func (c *Comment) HTMLURL() string { log.Error("loadRepo(%d): %v", c.Issue.RepoID, err) return "" } + if c.Type == CommentTypeCode { if c.ReviewID == 0 { - return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag()) + return fmt.Sprintf("%s/files#%s", baseURL, c.HashTag()) } if c.Review == nil { if err := c.LoadReview(); err != nil { log.Warn("LoadReview(%d): %v", c.ReviewID, err) - return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag()) + return fmt.Sprintf("%s/files#%s", baseURL, c.HashTag()) } } if c.Review.Type <= ReviewTypePending { - return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag()) + return fmt.Sprintf("%s/files#%s", baseURL, c.HashTag()) } } - return fmt.Sprintf("%s#%s", c.Issue.HTMLURL(), c.HashTag()) + return fmt.Sprintf("%s#%s", baseURL, c.HashTag()) } // APIURL formats a API-string to the issue-comment diff --git a/models/repo/release.go b/models/repo/release.go index da6235988a70a..73f5a6420cf05 100644 --- a/models/repo/release.go +++ b/models/repo/release.go @@ -131,6 +131,11 @@ func (r *Release) HTMLURL() string { return r.Repo.HTMLURL() + "/releases/tag/" + util.PathEscapeSegments(r.TagName) } +// Link the url for a release on the web UI. release must have attributes loaded +func (r *Release) Link() string { + return r.Repo.Link() + "/releases/tag/" + util.PathEscapeSegments(r.TagName) +} + // IsReleaseExist returns true if release with given tag name already exists. func IsReleaseExist(ctx context.Context, repoID int64, tagName string) (bool, error) { if len(tagName) == 0 { diff --git a/modules/structs/repo.go b/modules/structs/repo.go index 16f3d9dd26b7b..ee4bec4df793a 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -63,6 +63,7 @@ type Repository struct { Language string `json:"language"` LanguagesURL string `json:"languages_url"` HTMLURL string `json:"html_url"` + Link string `json:"link"` SSHURL string `json:"ssh_url"` CloneURL string `json:"clone_url"` OriginalURL string `json:"original_url"` diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 0a8a1eb4e8ba6..3cecfc15fc982 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -561,15 +561,16 @@ func SearchRepo(ctx *context.Context) { results := make([]*api.Repository, len(repos)) for i, repo := range repos { results[i] = &api.Repository{ - ID: repo.ID, - FullName: repo.FullName(), - Fork: repo.IsFork, - Private: repo.IsPrivate, - Template: repo.IsTemplate, - Mirror: repo.IsMirror, - Stars: repo.NumStars, - HTMLURL: repo.HTMLURL(), - Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate, + ID: repo.ID, + FullName: repo.FullName(), + Fork: repo.IsFork, + Private: repo.IsPrivate, + Template: repo.IsTemplate, + Mirror: repo.IsMirror, + Stars: repo.NumStars, + HTMLURL: repo.HTMLURL(), + Link: repo.Link(), + Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate, } } diff --git a/templates/code/searchresults.tmpl b/templates/code/searchresults.tmpl index e21a50e1f12df..06821ca1fb8a3 100644 --- a/templates/code/searchresults.tmpl +++ b/templates/code/searchresults.tmpl @@ -13,13 +13,13 @@

- {{$repo.FullName}} + {{$repo.FullName}} {{if $repo.IsArchived}} {{$.locale.Tr "repo.desc.archived"}} {{end}} - {{.Filename}} - {{$.locale.Tr "repo.diff.view_file"}} + {{$.locale.Tr "repo.diff.view_file"}}

@@ -28,7 +28,7 @@ {{range .LineNumbers}} - {{.}} + {{.}} {{end}} {{.FormattedLines | Safe}} diff --git a/templates/mail/issue/assigned.tmpl b/templates/mail/issue/assigned.tmpl index 05bed6902221d..232a41b56f8ab 100644 --- a/templates/mail/issue/assigned.tmpl +++ b/templates/mail/issue/assigned.tmpl @@ -8,7 +8,7 @@ {{.Subject}} -{{$repo_url := printf "%s" (Escape .Issue.Repo.HTMLURL) (Escape .Issue.Repo.FullName)}} +{{$repo_url := printf "%s" (Escape .Issue.Repo.Link) (Escape .Issue.Repo.FullName)}} {{$link := printf "#%d" (Escape .Link) .Issue.Index}}

diff --git a/templates/mail/issue/default.tmpl b/templates/mail/issue/default.tmpl index d9f7aff4cce39..35e49d3a3e5f2 100644 --- a/templates/mail/issue/default.tmpl +++ b/templates/mail/issue/default.tmpl @@ -20,11 +20,11 @@ {{if eq .ActionName "push"}}

{{if .Comment.IsForcePush}} - {{$oldCommitUrl := printf "%s/commit/%s" .Comment.Issue.PullRequest.BaseRepo.HTMLURL .Comment.OldCommit}} + {{$oldCommitUrl := printf "%s/commit/%s" .Comment.Issue.PullRequest.BaseRepo.Link .Comment.OldCommit}} {{$oldShortSha := ShortSha .Comment.OldCommit}} {{$oldCommitLink := printf "%[2]s" (Escape $oldCommitUrl) (Escape $oldShortSha)}} - {{$newCommitUrl := printf "%s/commit/%s" .Comment.Issue.PullRequest.BaseRepo.HTMLURL .Comment.NewCommit}} + {{$newCommitUrl := printf "%s/commit/%s" .Comment.Issue.PullRequest.BaseRepo.Link .Comment.NewCommit}} {{$newShortSha := ShortSha .Comment.NewCommit}} {{$newCommitLink := printf "%[2]s" (Escape $newCommitUrl) (Escape $newShortSha)}} @@ -72,7 +72,7 @@

    {{range .Comment.Commits}}
  • - + {{ShortSha .ID.String}} - {{.Summary}}
  • diff --git a/templates/mail/release.tmpl b/templates/mail/release.tmpl index b2acdce8b2b48..42504335231a4 100644 --- a/templates/mail/release.tmpl +++ b/templates/mail/release.tmpl @@ -11,8 +11,8 @@ -{{$release_url := printf "%s" (.Release.HTMLURL | Escape) (.Release.TagName | Escape)}} -{{$repo_url := printf "%s" (.Release.Repo.HTMLURL | Escape) (.Release.Repo.FullName | Escape)}} +{{$release_url := printf "%s" (.Release.Link | Escape) (.Release.TagName | Escape)}} +{{$repo_url := printf "%s" (.Release.Repo.Link | Escape) (.Release.Repo.FullName | Escape)}}

    {{.locale.Tr "mail.release.new.text" .Release.Publisher.Name $release_url $repo_url | Str2html}} diff --git a/templates/package/shared/list.tmpl b/templates/package/shared/list.tmpl index 37c47cef335d5..a3ffb03edeab5 100644 --- a/templates/package/shared/list.tmpl +++ b/templates/package/shared/list.tmpl @@ -37,7 +37,7 @@ {{$hasRepositoryAccess = index $.RepositoryAccessMap .Repository.ID}} {{end}} {{if $hasRepositoryAccess}} - {{$.locale.Tr "packages.published_by_in" $timeStr .Creator.HomeLink (.Creator.GetDisplayName | Escape) .Repository.HTMLURL (.Repository.FullName | Escape) | Safe}} + {{$.locale.Tr "packages.published_by_in" $timeStr .Creator.HomeLink (.Creator.GetDisplayName | Escape) .Repository.Link (.Repository.FullName | Escape) | Safe}} {{else}} {{$.locale.Tr "packages.published_by" $timeStr .Creator.HomeLink (.Creator.GetDisplayName | Escape) | Safe}} {{end}} @@ -50,7 +50,7 @@ {{svg "octicon-package" 32}}

    {{.locale.Tr "packages.empty"}}

    {{if and .Repository .CanWritePackages}} - {{$packagesUrl := URLJoin .Owner.HTMLURL "-" "packages"}} + {{$packagesUrl := URLJoin .Owner.HomeLink "-" "packages"}}

    {{.locale.Tr "packages.empty.repo" $packagesUrl | Safe}}

    {{end}}

    {{.locale.Tr "packages.empty.documentation" | Safe}}

    diff --git a/templates/package/view.tmpl b/templates/package/view.tmpl index a5b2a2ef68a39..5d3b01f2bcf4c 100644 --- a/templates/package/view.tmpl +++ b/templates/package/view.tmpl @@ -11,7 +11,7 @@
    {{$timeStr := TimeSinceUnix .PackageDescriptor.Version.CreatedUnix $.locale}} {{if .HasRepositoryAccess}} - {{.locale.Tr "packages.published_by_in" $timeStr .PackageDescriptor.Creator.HomeLink (.PackageDescriptor.Creator.GetDisplayName | Escape) .PackageDescriptor.Repository.HTMLURL (.PackageDescriptor.Repository.FullName | Escape) | Safe}} + {{.locale.Tr "packages.published_by_in" $timeStr .PackageDescriptor.Creator.HomeLink (.PackageDescriptor.Creator.GetDisplayName | Escape) .PackageDescriptor.Repository.Link (.PackageDescriptor.Repository.FullName | Escape) | Safe}} {{else}} {{.locale.Tr "packages.published_by" $timeStr .PackageDescriptor.Creator.HomeLink (.PackageDescriptor.Creator.GetDisplayName | Escape) | Safe}} {{end}} @@ -38,7 +38,7 @@
    {{svg .PackageDescriptor.Package.Type.SVGName 16 "mr-3"}} {{.PackageDescriptor.Package.Type.Name}}
    {{if .HasRepositoryAccess}} -
    {{svg "octicon-repo" 16 "mr-3"}} {{.PackageDescriptor.Repository.FullName}}
    +
    {{svg "octicon-repo" 16 "mr-3"}} {{.PackageDescriptor.Repository.FullName}}
    {{end}}
    {{svg "octicon-calendar" 16 "mr-3"}} {{TimeSinceUnix .PackageDescriptor.Version.CreatedUnix $.locale}}
    {{svg "octicon-download" 16 "mr-3"}} {{.PackageDescriptor.Version.DownloadCount}}
    @@ -85,7 +85,7 @@
    {{if .HasRepositoryAccess}} -
    {{svg "octicon-issue-opened" 16 "mr-3"}} {{.locale.Tr "repo.issues"}}
    +
    {{svg "octicon-issue-opened" 16 "mr-3"}} {{.locale.Tr "repo.issues"}}
    {{end}} {{if .CanWritePackages}}
    {{svg "octicon-tools" 16 "mr-3"}} {{.locale.Tr "repo.settings"}}
    diff --git a/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl index abce6591bd434..d9b677bdfc428 100644 --- a/templates/repo/branch/list.tmpl +++ b/templates/repo/branch/list.tmpl @@ -96,13 +96,13 @@ {{end}} {{else}} - {{if not .LatestPullRequest.IsSameRepo}}{{.LatestPullRequest.BaseRepo.FullName}}{{end}}#{{.LatestPullRequest.Issue.Index}} + {{if not .LatestPullRequest.IsSameRepo}}{{.LatestPullRequest.BaseRepo.FullName}}{{end}}#{{.LatestPullRequest.Issue.Index}} {{if .LatestPullRequest.HasMerged}} - {{svg "octicon-git-merge" 16 "mr-2"}}{{$.locale.Tr "repo.pulls.merged"}} + {{svg "octicon-git-merge" 16 "mr-2"}}{{$.locale.Tr "repo.pulls.merged"}} {{else if .LatestPullRequest.Issue.IsClosed}} - {{svg "octicon-git-pull-request" 16 "mr-2"}}{{$.locale.Tr "repo.issues.closed_title"}} + {{svg "octicon-git-pull-request" 16 "mr-2"}}{{$.locale.Tr "repo.issues.closed_title"}} {{else}} - {{svg "octicon-git-pull-request" 16 "mr-2"}}{{$.locale.Tr "repo.issues.open_title"}} + {{svg "octicon-git-pull-request" 16 "mr-2"}}{{$.locale.Tr "repo.issues.open_title"}} {{end}} {{end}} diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index 15eff81099bbd..af6a4fc069aae 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -150,7 +150,7 @@ {{end}}
    {{else}} - +
    {{if $.IsSplitStyle}} {{template "repo/diff/section_split" dict "file" . "root" $}} {{else}} @@ -190,7 +190,7 @@
    diff --git a/templates/repo/diff/comment_form.tmpl b/templates/repo/diff/comment_form.tmpl index 9325c754cedb6..f51e8407212d4 100644 --- a/templates/repo/diff/comment_form.tmpl +++ b/templates/repo/diff/comment_form.tmpl @@ -1,5 +1,5 @@ {{if and $.root.SignedUserID (not $.Repository.IsArchived)}} -
    + {{$.root.CsrfTokenHtml}} @@ -11,7 +11,7 @@
    diff --git a/templates/repo/editor/edit.tmpl b/templates/repo/editor/edit.tmpl index ebe56ec477fe8..8a585c56cb988 100644 --- a/templates/repo/editor/edit.tmpl +++ b/templates/repo/editor/edit.tmpl @@ -31,13 +31,13 @@
    diff --git a/templates/repo/issue/view_content.tmpl b/templates/repo/issue/view_content.tmpl index 0cb74e8182904..005c57b4c08f3 100644 --- a/templates/repo/issue/view_content.tmpl +++ b/templates/repo/issue/view_content.tmpl @@ -198,7 +198,7 @@
    diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index e9b7172d1b324..43901975e6729 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -123,7 +123,7 @@ {{template "shared/user/avatarlink" .Poster}} {{template "shared/user/authorlink" .Poster}} - {{$link := printf "%s/commit/%s" $.Repository.HTMLURL ($.Issue.PullRequest.MergedCommitID|PathEscape)}} + {{$link := printf "%s/commit/%s" $.Repository.Link ($.Issue.PullRequest.MergedCommitID|PathEscape)}} {{if eq $.Issue.PullRequest.Status 3}} {{$.locale.Tr "repo.issues.manually_pull_merged_at" ($link|Escape) (ShortSha $.Issue.PullRequest.MergedCommitID) ($.BaseTarget|Escape) $createdStr | Str2html}} {{else}} @@ -329,7 +329,7 @@
    {{svg "octicon-plus"}} - + {{if eq .DependentIssue.RepoID .Issue.RepoID}} #{{.DependentIssue.Index}} {{.DependentIssue.Title}} {{else}} @@ -352,7 +352,7 @@
    {{svg "octicon-trash"}} - + {{if eq .DependentIssue.RepoID .Issue.RepoID}} #{{.DependentIssue.Index}} {{.DependentIssue.Title}} {{else}} diff --git a/templates/repo/issue/view_content/context_menu.tmpl b/templates/repo/issue/view_content/context_menu.tmpl index 45dd08bf61035..a687a8364da77 100644 --- a/templates/repo/issue/view_content/context_menu.tmpl +++ b/templates/repo/issue/view_content/context_menu.tmpl @@ -6,9 +6,9 @@
    {{end}} {{end}} diff --git a/templates/repo/wiki/new.tmpl b/templates/repo/wiki/new.tmpl index 26c19c0a43f06..3085e03711e78 100644 --- a/templates/repo/wiki/new.tmpl +++ b/templates/repo/wiki/new.tmpl @@ -21,11 +21,11 @@
    - +
    diff --git a/templates/shared/issuelist.tmpl b/templates/shared/issuelist.tmpl index e8ad8406cd2d5..7ac29d27ab0ac 100644 --- a/templates/shared/issuelist.tmpl +++ b/templates/shared/issuelist.tmpl @@ -35,7 +35,7 @@
    - {{RenderEmoji .Title | RenderCodeBlock}} + {{RenderEmoji .Title | RenderCodeBlock}} {{if .IsPull}} {{if (index $.CommitStatuses .PullRequest.ID)}} {{template "repo/commit_statuses" dict "Status" (index $.CommitLastStatus .PullRequest.ID) "Statuses" (index $.CommitStatuses .PullRequest.ID) "root" $}} @@ -48,7 +48,7 @@
    - + {{if eq $.listType "dashboard"}} {{.Repo.FullName}}#{{.Index}} {{else}} @@ -66,7 +66,7 @@ {{if .IsPull}}
    - + {{/* inline to remove the spaces between spans */}} {{if ne .RepoID .PullRequest.BaseRepoID}}{{.PullRequest.BaseRepo.OwnerName}}:{{end}}{{.PullRequest.BaseBranch}} @@ -74,7 +74,7 @@ {{svg "gitea-double-chevron-left" 12 "mx-1"}} {{if .PullRequest.HeadRepo}}
    {{if .NumComments}} - + {{svg "octicon-comment" 16 "mr-2"}}{{.NumComments}} {{end}} diff --git a/templates/user/dashboard/repolist.tmpl b/templates/user/dashboard/repolist.tmpl index 620cc322f0d39..5d304c5efda70 100644 --- a/templates/user/dashboard/repolist.tmpl +++ b/templates/user/dashboard/repolist.tmpl @@ -127,7 +127,7 @@
    • - +
      ${repo.full_name} diff --git a/templates/user/notification/notification_div.tmpl b/templates/user/notification/notification_div.tmpl index 18071549a0c59..2a1541ef7d66f 100644 --- a/templates/user/notification/notification_div.tmpl +++ b/templates/user/notification/notification_div.tmpl @@ -35,7 +35,7 @@ {{$issue := .Issue}} {{$repo := .Repository}} - + {{if eq .Status 3}} {{svg "octicon-pin"}} {{else if not $issue}} @@ -58,8 +58,8 @@ {{end}} {{end}} - - + + {{if $issue}} #{{$issue.Index}} - {{$issue.Title}} {{else}} From 9d372b4a0c9c1539b1a1de680711369e0f1a04ef Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 17:10:01 +0800 Subject: [PATCH 02/27] Add an absolute url from the current request domain but not setting --- templates/base/head_script.tmpl | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/base/head_script.tmpl b/templates/base/head_script.tmpl index c4ac18a86ee5e..0b8a9a9ea0767 100644 --- a/templates/base/head_script.tmpl +++ b/templates/base/head_script.tmpl @@ -8,6 +8,7 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly. window.config = { appUrl: '{{AppUrl}}', appSubUrl: '{{AppSubUrl}}', + reqSubUrl: window.location.protocol + '//' + window.location.host +'{{AppSubUrl}}', assetVersionEncoded: encodeURIComponent('{{AssetVersion}}'), // will be used in URL construction directly assetUrlPrefix: '{{AssetUrlPrefix}}', runModeIsProd: {{.RunModeIsProd}}, From 9aaef39a41fa622f219e6c978f1fa56de401e797 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 17:12:35 +0800 Subject: [PATCH 03/27] Fix lint --- routers/web/repo/repo.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 3cecfc15fc982..ae52046dd8054 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -561,16 +561,16 @@ func SearchRepo(ctx *context.Context) { results := make([]*api.Repository, len(repos)) for i, repo := range repos { results[i] = &api.Repository{ - ID: repo.ID, - FullName: repo.FullName(), - Fork: repo.IsFork, - Private: repo.IsPrivate, - Template: repo.IsTemplate, - Mirror: repo.IsMirror, - Stars: repo.NumStars, - HTMLURL: repo.HTMLURL(), - Link: repo.Link(), - Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate, + ID: repo.ID, + FullName: repo.FullName(), + Fork: repo.IsFork, + Private: repo.IsPrivate, + Template: repo.IsTemplate, + Mirror: repo.IsMirror, + Stars: repo.NumStars, + HTMLURL: repo.HTMLURL(), + Link: repo.Link(), + Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate, } } From c1a9695156b89379c72499f7e62dd8a17f69d379 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 17:25:03 +0800 Subject: [PATCH 04/27] proper name --- templates/base/head_script.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/base/head_script.tmpl b/templates/base/head_script.tmpl index 0b8a9a9ea0767..3ccc8d7181198 100644 --- a/templates/base/head_script.tmpl +++ b/templates/base/head_script.tmpl @@ -8,7 +8,7 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly. window.config = { appUrl: '{{AppUrl}}', appSubUrl: '{{AppSubUrl}}', - reqSubUrl: window.location.protocol + '//' + window.location.host +'{{AppSubUrl}}', + reqAppUrl: window.location.protocol + '//' + window.location.host +'{{AppSubUrl}}', assetVersionEncoded: encodeURIComponent('{{AssetVersion}}'), // will be used in URL construction directly assetUrlPrefix: '{{AssetUrlPrefix}}', runModeIsProd: {{.RunModeIsProd}}, From 08c9d23ef46734d8bed81e05363b428813d1e929 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 17:32:35 +0800 Subject: [PATCH 05/27] Fix bug --- templates/code/searchresults.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/code/searchresults.tmpl b/templates/code/searchresults.tmpl index 06821ca1fb8a3..f9b17aee417d9 100644 --- a/templates/code/searchresults.tmpl +++ b/templates/code/searchresults.tmpl @@ -19,7 +19,7 @@ {{end}} - {{.Filename}} - {{$.locale.Tr "repo.diff.view_file"}} + {{$.locale.Tr "repo.diff.view_file"}}
      From 2bcf3c49643f547e8d24ebb2ca539f192fba76a7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 17:43:13 +0800 Subject: [PATCH 06/27] convert action's comment url to relative url --- models/activities/action.go | 41 ++++++++++++++++++++++++++-- models/activities/action_test.go | 2 +- models/issues/pull.go | 12 ++++---- routers/web/feed/convert.go | 2 +- routers/web/repo/pull.go | 4 +-- templates/repo/issue/view_title.tmpl | 8 +++--- 6 files changed, 53 insertions(+), 16 deletions(-) diff --git a/models/activities/action.go b/models/activities/action.go index 80c117dc9572a..a317bc4e4b7fd 100644 --- a/models/activities/action.go +++ b/models/activities/action.go @@ -223,6 +223,43 @@ func (a *Action) GetRepoAbsoluteLink() string { return setting.AppURL + url.PathEscape(a.GetRepoUserName()) + "/" + url.PathEscape(a.GetRepoName()) } +// GetCommentHTMLURL returns link to action comment. +func (a *Action) GetCommentHTMLURL() string { + return a.getCommentHTMLURL(db.DefaultContext) +} + +func (a *Action) getCommentHTMLURL(ctx context.Context) string { + if a == nil { + return "#" + } + if a.Comment == nil && a.CommentID != 0 { + a.Comment, _ = issues_model.GetCommentByID(ctx, a.CommentID) + } + if a.Comment != nil { + return a.Comment.HTMLURL() + } + if len(a.GetIssueInfos()) == 0 { + return "#" + } + // Return link to issue + issueIDString := a.GetIssueInfos()[0] + issueID, err := strconv.ParseInt(issueIDString, 10, 64) + if err != nil { + return "#" + } + + issue, err := issues_model.GetIssueByID(ctx, issueID) + if err != nil { + return "#" + } + + if err = issue.LoadRepo(ctx); err != nil { + return "#" + } + + return issue.HTMLURL() +} + // GetCommentLink returns link to action comment. func (a *Action) GetCommentLink() string { return a.getCommentLink(db.DefaultContext) @@ -236,7 +273,7 @@ func (a *Action) getCommentLink(ctx context.Context) string { a.Comment, _ = issues_model.GetCommentByID(ctx, a.CommentID) } if a.Comment != nil { - return a.Comment.HTMLURL() + return a.Comment.Link() } if len(a.GetIssueInfos()) == 0 { return "#" @@ -257,7 +294,7 @@ func (a *Action) getCommentLink(ctx context.Context) string { return "#" } - return issue.HTMLURL() + return issue.Link() } // GetBranch returns the action's repository branch. diff --git a/models/activities/action_test.go b/models/activities/action_test.go index 29312bd482b1d..f37e58f685d4e 100644 --- a/models/activities/action_test.go +++ b/models/activities/action_test.go @@ -36,7 +36,7 @@ func TestAction_GetRepoLink(t *testing.T) { expected := path.Join(setting.AppSubURL, owner.Name, repo.Name) assert.Equal(t, expected, action.GetRepoLink()) assert.Equal(t, repo.HTMLURL(), action.GetRepoAbsoluteLink()) - assert.Equal(t, comment.HTMLURL(), action.GetCommentLink()) + assert.Equal(t, comment.HTMLURL(), action.GetCommentHTMLURL()) } func TestGetFeeds(t *testing.T) { diff --git a/models/issues/pull.go b/models/issues/pull.go index 9105dd4d3a6e6..89738775d9ea0 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -718,8 +718,8 @@ func GetPullRequestsByHeadBranch(ctx context.Context, headBranch string, headRep return prs, nil } -// GetBaseBranchHTMLURL returns the HTML URL of the base branch -func (pr *PullRequest) GetBaseBranchHTMLURL() string { +// GetBaseBranchLink returns the relative HTML URL of the base branch +func (pr *PullRequest) GetBaseBranchLink() string { if err := pr.LoadBaseRepo(db.DefaultContext); err != nil { log.Error("LoadBaseRepo: %v", err) return "" @@ -727,11 +727,11 @@ func (pr *PullRequest) GetBaseBranchHTMLURL() string { if pr.BaseRepo == nil { return "" } - return pr.BaseRepo.HTMLURL() + "/src/branch/" + util.PathEscapeSegments(pr.BaseBranch) + return pr.BaseRepo.Link() + "/src/branch/" + util.PathEscapeSegments(pr.BaseBranch) } -// GetHeadBranchHTMLURL returns the HTML URL of the head branch -func (pr *PullRequest) GetHeadBranchHTMLURL() string { +// GetHeadBranchLink returns the relative HTML URL of the head branch +func (pr *PullRequest) GetHeadBranchLink() string { if pr.Flow == PullRequestFlowAGit { return "" } @@ -743,7 +743,7 @@ func (pr *PullRequest) GetHeadBranchHTMLURL() string { if pr.HeadRepo == nil { return "" } - return pr.HeadRepo.HTMLURL() + "/src/branch/" + util.PathEscapeSegments(pr.HeadBranch) + return pr.HeadRepo.Link() + "/src/branch/" + util.PathEscapeSegments(pr.HeadBranch) } // UpdateAllowEdits update if PR can be edited from maintainers diff --git a/routers/web/feed/convert.go b/routers/web/feed/convert.go index e0e481d1a5618..3e82dca8f98ff 100644 --- a/routers/web/feed/convert.go +++ b/routers/web/feed/convert.go @@ -72,7 +72,7 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio var content, desc, title string - link := &feeds.Link{Href: act.GetCommentLink()} + link := &feeds.Link{Href: act.GetCommentHTMLURL()} // title title = act.ActUser.DisplayName() + " " diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 682bc64ca29d3..84d7edb285502 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -326,8 +326,8 @@ func setMergeTarget(ctx *context.Context, pull *issues_model.PullRequest) { ctx.Data["HeadTarget"] = pull.MustHeadUserName(ctx) + "/" + pull.HeadRepo.Name + ":" + pull.HeadBranch } ctx.Data["BaseTarget"] = pull.BaseBranch - ctx.Data["HeadBranchHTMLURL"] = pull.GetHeadBranchHTMLURL() - ctx.Data["BaseBranchHTMLURL"] = pull.GetBaseBranchHTMLURL() + ctx.Data["HeadBranchLink"] = pull.GetHeadBranchLink() + ctx.Data["BaseBranchLink"] = pull.GetBaseBranchLink() } // PrepareMergedViewPullInfo show meta information for a merged pull request view page diff --git a/templates/repo/issue/view_title.tmpl b/templates/repo/issue/view_title.tmpl index 58f4722e28a7b..74fe1ff24833e 100644 --- a/templates/repo/issue/view_title.tmpl +++ b/templates/repo/issue/view_title.tmpl @@ -35,13 +35,13 @@ {{if .Issue.IsPull}} {{$headHref := .HeadTarget|Escape}} - {{if .HeadBranchHTMLURL}} - {{$headHref = printf "%s" (.HeadBranchHTMLURL | Escape) $headHref}} + {{if .HeadBranchLink}} + {{$headHref = printf "%s" (.HeadBranchLink | Escape) $headHref}} {{end}} {{$headHref = printf "%s %s" $headHref (.locale.Tr "copy_branch") (.HeadTarget | Escape) (svg "octicon-copy" 14)}} {{$baseHref := .BaseTarget|Escape}} - {{if .BaseBranchHTMLURL}} - {{$baseHref = printf "%s" (.BaseBranchHTMLURL | Escape) $baseHref}} + {{if .BaseBranchLink}} + {{$baseHref = printf "%s" (.BaseBranchLink | Escape) $baseHref}} {{end}} {{if .Issue.PullRequest.HasMerged}} {{$mergedStr:= TimeSinceUnix .Issue.PullRequest.MergedUnix $.locale}} From fc89f23837310b412df26c6081f69efe1a2d3f58 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 17:44:57 +0800 Subject: [PATCH 07/27] fix link --- templates/swagger/v1_json.tmpl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index ddafc146a159c..2fa33200247b4 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -18345,6 +18345,10 @@ "type": "string", "x-go-name": "LanguagesURL" }, + "link": { + "type": "string", + "x-go-name": "Link" + }, "mirror": { "type": "boolean", "x-go-name": "Mirror" @@ -20232,4 +20236,4 @@ "TOTPHeader": [] } ] -} +} \ No newline at end of file From 5b331cc24ad2af73328280eb16d538fc97c7ad9f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 17:51:21 +0800 Subject: [PATCH 08/27] Fix link --- models/issues/comment.go | 6 +++--- templates/repo/issue/view_content/comments.tmpl | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index b46bc48281293..22ff02e9e77d8 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -738,8 +738,8 @@ func (c *Comment) UnsignedLine() uint64 { return uint64(c.Line) } -// CodeCommentURL returns the url to a comment in code -func (c *Comment) CodeCommentURL() string { +// CodeCommentLink returns the url to a comment in code +func (c *Comment) CodeCommentLink() string { err := c.LoadIssue(db.DefaultContext) if err != nil { // Silently dropping errors :unamused: log.Error("LoadIssue(%d): %v", c.IssueID, err) @@ -750,7 +750,7 @@ func (c *Comment) CodeCommentURL() string { log.Error("loadRepo(%d): %v", c.Issue.RepoID, err) return "" } - return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag()) + return fmt.Sprintf("%s/files#%s", c.Issue.Link(), c.HashTag()) } // LoadPushCommits Load push commits diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 43901975e6729..6c140deef7b26 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -476,7 +476,7 @@ {{$resolveDoer := (index $comms 0).ResolveDoer}} {{$isNotPending := (not (eq (index $comms 0).Review.Type 0))}}
      - {{$filename}} + {{$filename}} {{if $invalid}} {{$.locale.Tr "repo.issues.review.outdated"}} From 9062c7c5bb60167ab459be43fc0c0f7ab08c512f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 18:05:15 +0800 Subject: [PATCH 09/27] Revert 2 places --- templates/repo/issue/view_content/pull.tmpl | 2 +- templates/repo/issue/view_content/pull_merge_instruction.tmpl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/repo/issue/view_content/pull.tmpl b/templates/repo/issue/view_content/pull.tmpl index c4eb51791cb81..c4e770f1b617f 100644 --- a/templates/repo/issue/view_content/pull.tmpl +++ b/templates/repo/issue/view_content/pull.tmpl @@ -343,7 +343,7 @@ (() => { const defaultMergeTitle = {{.DefaultMergeMessage}}; const defaultSquashMergeTitle = {{.DefaultSquashMergeMessage}}; - const defaultMergeMessage = 'Reviewed-on: ' + {{$.Issue.Link}} + '\n' + {{$approvers}}; + const defaultMergeMessage = 'Reviewed-on: ' + {{$.Issue.HTMLURL}} + '\n' + {{$approvers}}; const mergeForm = { 'baseLink': {{.Link}}, 'textCancel': {{$.locale.Tr "cancel"}}, diff --git a/templates/repo/issue/view_content/pull_merge_instruction.tmpl b/templates/repo/issue/view_content/pull_merge_instruction.tmpl index e82c6b82df66b..816f25cbcf901 100644 --- a/templates/repo/issue/view_content/pull_merge_instruction.tmpl +++ b/templates/repo/issue/view_content/pull_merge_instruction.tmpl @@ -5,7 +5,7 @@
      {{if eq $.Issue.PullRequest.Flow 0}}
      git checkout -b {{if ne $.Issue.PullRequest.HeadRepo.ID $.Issue.PullRequest.BaseRepo.ID}}{{$.Issue.PullRequest.HeadRepo.OwnerName}}-{{end}}{{$.Issue.PullRequest.HeadBranch}} {{$.Issue.PullRequest.BaseBranch}}
      -
      git pull {{if ne $.Issue.PullRequest.HeadRepo.ID $.Issue.PullRequest.BaseRepo.ID}}{{$.Issue.PullRequest.HeadRepo.Link}}{{else}}origin{{end}} {{$.Issue.PullRequest.HeadBranch}}
      +
      git pull {{if ne $.Issue.PullRequest.HeadRepo.ID $.Issue.PullRequest.BaseRepo.ID}}{{$.Issue.PullRequest.HeadRepo.HTMLURL}}{{else}}origin{{end}} {{$.Issue.PullRequest.HeadBranch}}
      {{else}}
      git fetch origin {{$.Issue.PullRequest.GetGitRefName}}:{{$.Issue.PullRequest.HeadBranch}}
      {{end}} From 10af35801462bdd9096feb23d1da192a05596f6e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 19:36:50 +0800 Subject: [PATCH 10/27] fix fmt --- templates/swagger/v1_json.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 2fa33200247b4..3a56b35bcb642 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -20236,4 +20236,4 @@ "TOTPHeader": [] } ] -} \ No newline at end of file +} From 7f699297ccff18d715eaaeb24edac4d5b9fc2760 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 20:02:01 +0800 Subject: [PATCH 11/27] Fix bug --- models/issues/comment.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index 22ff02e9e77d8..efe4ec50e46ea 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -376,15 +376,21 @@ func (c *Comment) AfterDelete() { // HTMLURL formats a URL-string to the issue-comment func (c *Comment) HTMLURL() string { + err := c.LoadIssue(db.DefaultContext) + if err != nil { // Silently dropping errors :unamused: + log.Error("LoadIssue(%d): %v", c.IssueID, err) + return "" + } + err = c.Issue.LoadRepo(db.DefaultContext) + if err != nil { // Silently dropping errors :unamused: + log.Error("loadRepo(%d): %v", c.Issue.RepoID, err) + return "" + } return c.link(c.Issue.HTMLURL()) } // Link formats a URL-string to the issue-comment func (c *Comment) Link() string { - return c.link(c.Issue.Link()) -} - -func (c *Comment) link(baseURL string) string { err := c.LoadIssue(db.DefaultContext) if err != nil { // Silently dropping errors :unamused: log.Error("LoadIssue(%d): %v", c.IssueID, err) @@ -395,7 +401,10 @@ func (c *Comment) link(baseURL string) string { log.Error("loadRepo(%d): %v", c.Issue.RepoID, err) return "" } + return c.link(c.Issue.Link()) +} +func (c *Comment) link(baseURL string) string { if c.Type == CommentTypeCode { if c.ReviewID == 0 { return fmt.Sprintf("%s/files#%s", baseURL, c.HashTag()) From f66f7d5feb4f8e19f8dad32ec520d778086ea79f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 21:18:57 +0800 Subject: [PATCH 12/27] Fix test --- templates/repo/projects/view.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/projects/view.tmpl b/templates/repo/projects/view.tmpl index 1937c82d16255..bcae151f04d37 100644 --- a/templates/repo/projects/view.tmpl +++ b/templates/repo/projects/view.tmpl @@ -242,7 +242,7 @@ {{end}}
      {{range .Assignees}} - {{avatar . 28 "mini mr-3"}} + {{avatar . 28 "mini mr-3"}} {{end}}
      From 58cf943ce196864252932b8758143907e89c10de Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Nov 2022 23:36:49 +0800 Subject: [PATCH 13/27] use currentAppUrl --- templates/base/head_script.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/base/head_script.tmpl b/templates/base/head_script.tmpl index 3ccc8d7181198..9d5ebd825c391 100644 --- a/templates/base/head_script.tmpl +++ b/templates/base/head_script.tmpl @@ -8,7 +8,7 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly. window.config = { appUrl: '{{AppUrl}}', appSubUrl: '{{AppSubUrl}}', - reqAppUrl: window.location.protocol + '//' + window.location.host +'{{AppSubUrl}}', + currentAppUrl: window.location.protocol + '//' + window.location.host +'{{AppSubUrl}}', assetVersionEncoded: encodeURIComponent('{{AssetVersion}}'), // will be used in URL construction directly assetUrlPrefix: '{{AssetUrlPrefix}}', runModeIsProd: {{.RunModeIsProd}}, From 1deb827adb74646401d49486c324e3af9adb6afd Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 1 Dec 2022 09:33:24 +0800 Subject: [PATCH 14/27] use a function --- templates/base/head_script.tmpl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/templates/base/head_script.tmpl b/templates/base/head_script.tmpl index 9d5ebd825c391..81fe41f1d9cad 100644 --- a/templates/base/head_script.tmpl +++ b/templates/base/head_script.tmpl @@ -8,7 +8,9 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly. window.config = { appUrl: '{{AppUrl}}', appSubUrl: '{{AppSubUrl}}', - currentAppUrl: window.location.protocol + '//' + window.location.host +'{{AppSubUrl}}', + getCurAbsUrl: function(relUrl) { + return window.location.protocol + '//' + window.location.host +'{{AppSubUrl}}' + relUrl; + }, assetVersionEncoded: encodeURIComponent('{{AssetVersion}}'), // will be used in URL construction directly assetUrlPrefix: '{{AssetUrlPrefix}}', runModeIsProd: {{.RunModeIsProd}}, From eaed1c9a7ac39668f213fbefa90bc5bc15dbfaf9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 20 Dec 2022 23:40:28 +0800 Subject: [PATCH 15/27] follow @silverwind review --- templates/base/head_script.tmpl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/templates/base/head_script.tmpl b/templates/base/head_script.tmpl index 81fe41f1d9cad..d53e8454dadf8 100644 --- a/templates/base/head_script.tmpl +++ b/templates/base/head_script.tmpl @@ -8,9 +8,7 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly. window.config = { appUrl: '{{AppUrl}}', appSubUrl: '{{AppSubUrl}}', - getCurAbsUrl: function(relUrl) { - return window.location.protocol + '//' + window.location.host +'{{AppSubUrl}}' + relUrl; - }, + getCurAbsUrl: (relUrl) => `${window.location.origin}{{AppSubUrl}}${relUrl}`, assetVersionEncoded: encodeURIComponent('{{AssetVersion}}'), // will be used in URL construction directly assetUrlPrefix: '{{AssetUrlPrefix}}', runModeIsProd: {{.RunModeIsProd}}, From 445eab03f4b0d71508b139142a6a691d7517cdab Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 21 Dec 2022 20:16:53 +0800 Subject: [PATCH 16/27] Fix copy comment link --- templates/base/head_script.tmpl | 2 +- templates/repo/issue/view_content/context_menu.tmpl | 2 +- web_src/js/features/clipboard.js | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/templates/base/head_script.tmpl b/templates/base/head_script.tmpl index d53e8454dadf8..715f19940ac27 100644 --- a/templates/base/head_script.tmpl +++ b/templates/base/head_script.tmpl @@ -8,7 +8,7 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly. window.config = { appUrl: '{{AppUrl}}', appSubUrl: '{{AppSubUrl}}', - getCurAbsUrl: (relUrl) => `${window.location.origin}{{AppSubUrl}}${relUrl}`, + getCurAbsUrl: (relUrl) => `${window.location.origin}`+'{{AppSubUrl}}'+`${relUrl}`, assetVersionEncoded: encodeURIComponent('{{AssetVersion}}'), // will be used in URL construction directly assetUrlPrefix: '{{AssetUrlPrefix}}', runModeIsProd: {{.RunModeIsProd}}, diff --git a/templates/repo/issue/view_content/context_menu.tmpl b/templates/repo/issue/view_content/context_menu.tmpl index 20827f231744f..c8cd5530f390c 100644 --- a/templates/repo/issue/view_content/context_menu.tmpl +++ b/templates/repo/issue/view_content/context_menu.tmpl @@ -10,7 +10,7 @@ {{else}} {{$referenceUrl = Printf "%s/files#%s" .ctx.Issue.Link .item.HashTag}} {{end}} -
      {{.ctx.locale.Tr "repo.issues.context.copy_link"}}
      +
      {{.ctx.locale.Tr "repo.issues.context.copy_link"}}
      {{.ctx.locale.Tr "repo.issues.context.quote_reply"}}
      {{if not .ctx.UnitIssuesGlobalDisabled}}
      {{.ctx.locale.Tr "repo.issues.context.reference_issue"}}
      diff --git a/web_src/js/features/clipboard.js b/web_src/js/features/clipboard.js index 75b96cb7811c4..41b792295de36 100644 --- a/web_src/js/features/clipboard.js +++ b/web_src/js/features/clipboard.js @@ -50,7 +50,11 @@ export default function initGlobalCopyToClipboardListener() { // in case , so we just search // up to 3 levels for performance for (let i = 0; i < 3 && target; i++) { - const text = target.getAttribute('data-clipboard-text') || document.querySelector(target.getAttribute('data-clipboard-target'))?.value; + let txt = target.getAttribute('data-clipboard-text'); + if (txt && target.getAttribute('data-clipboard-text-type') === 'link') { + txt = window.config.getCurAbsUrl(txt); + } + const text = txt || document.querySelector(target.getAttribute('data-clipboard-target'))?.value; if (text) { e.preventDefault(); From 5cfc482ad23044541733cac5cbd76505e47b818d Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 22 Dec 2022 22:13:58 +0800 Subject: [PATCH 17/27] follow @silverwind review --- templates/base/head_script.tmpl | 1 - web_src/js/features/clipboard.js | 3 ++- web_src/js/utils.js | 6 ++++++ web_src/js/utils.test.js | 6 ++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/templates/base/head_script.tmpl b/templates/base/head_script.tmpl index 715f19940ac27..c4ac18a86ee5e 100644 --- a/templates/base/head_script.tmpl +++ b/templates/base/head_script.tmpl @@ -8,7 +8,6 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly. window.config = { appUrl: '{{AppUrl}}', appSubUrl: '{{AppSubUrl}}', - getCurAbsUrl: (relUrl) => `${window.location.origin}`+'{{AppSubUrl}}'+`${relUrl}`, assetVersionEncoded: encodeURIComponent('{{AssetVersion}}'), // will be used in URL construction directly assetUrlPrefix: '{{AssetUrlPrefix}}', runModeIsProd: {{.RunModeIsProd}}, diff --git a/web_src/js/features/clipboard.js b/web_src/js/features/clipboard.js index 41b792295de36..59e7bb7513a76 100644 --- a/web_src/js/features/clipboard.js +++ b/web_src/js/features/clipboard.js @@ -1,4 +1,5 @@ import {showTemporaryTooltip} from '../modules/tippy.js'; +import {getCurAbsUrl} from '../utils.js'; const {copy_success, copy_error} = window.config.i18n; @@ -52,7 +53,7 @@ export default function initGlobalCopyToClipboardListener() { for (let i = 0; i < 3 && target; i++) { let txt = target.getAttribute('data-clipboard-text'); if (txt && target.getAttribute('data-clipboard-text-type') === 'link') { - txt = window.config.getCurAbsUrl(txt); + txt = getCurAbsUrl(txt); } const text = txt || document.querySelector(target.getAttribute('data-clipboard-target'))?.value; diff --git a/web_src/js/utils.js b/web_src/js/utils.js index 01c076aebac21..b3424c0589499 100644 --- a/web_src/js/utils.js +++ b/web_src/js/utils.js @@ -133,3 +133,9 @@ export function convertImage(blob, mime) { } }); } + +const {appSubUrl} = window.config; + +export function getCurAbsUrl(relUrl) { + return `${window.location.origin}${appSubUrl}${relUrl}`; +} diff --git a/web_src/js/utils.test.js b/web_src/js/utils.test.js index 1df0caa211030..b2b6f8c3422a5 100644 --- a/web_src/js/utils.test.js +++ b/web_src/js/utils.test.js @@ -2,6 +2,7 @@ import {expect, test} from 'vitest'; import { basename, extname, isObject, uniq, stripTags, joinPaths, parseIssueHref, prettyNumber, parseUrl, translateMonth, translateDay, blobToDataURI, + getCurAbsUrl, } from './utils.js'; test('basename', () => { @@ -136,3 +137,8 @@ test('blobToDataURI', async () => { const blob = new Blob([JSON.stringify({test: true})], {type: 'application/json'}); expect(await blobToDataURI(blob)).toEqual('data:application/json;base64,eyJ0ZXN0Ijp0cnVlfQ=='); }); + +test('getCurAbsUrl', () => { + expect(getCurAbsUrl('')).toEqual('http://localhost:3000'); + expect(getCurAbsUrl('/path')).toEqual('http://localhost:3000/path'); +}); From f36e90013e31dd1c876e4486a5c1b4e3629b3225 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 22 Dec 2022 23:07:11 +0800 Subject: [PATCH 18/27] fix frontend test --- web_src/js/test/setup.js | 1 + web_src/js/utils.test.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/web_src/js/test/setup.js b/web_src/js/test/setup.js index 7c208eb0d28f0..e0e2c71e29236 100644 --- a/web_src/js/test/setup.js +++ b/web_src/js/test/setup.js @@ -2,4 +2,5 @@ window.config = { csrfToken: 'test-csrf-token-123456', pageData: {}, i18n: {}, + appSubUrl: '', }; diff --git a/web_src/js/utils.test.js b/web_src/js/utils.test.js index b2b6f8c3422a5..6dd82d2678fc2 100644 --- a/web_src/js/utils.test.js +++ b/web_src/js/utils.test.js @@ -140,5 +140,5 @@ test('blobToDataURI', async () => { test('getCurAbsUrl', () => { expect(getCurAbsUrl('')).toEqual('http://localhost:3000'); - expect(getCurAbsUrl('/path')).toEqual('http://localhost:3000/path'); + expect(getCurAbsUrl('/user/repo')).toEqual('http://localhost:3000/user/repo'); }); From f09fb8a271cd679d2bc9074bb58a110e1e4aff47 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 22 Dec 2022 23:24:05 +0800 Subject: [PATCH 19/27] fix package link --- models/packages/descriptor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/packages/descriptor.go b/models/packages/descriptor.go index 34f1cad87dc45..b2120c6f3047e 100644 --- a/models/packages/descriptor.go +++ b/models/packages/descriptor.go @@ -62,7 +62,7 @@ type PackageFileDescriptor struct { // PackageWebLink returns the package web link func (pd *PackageDescriptor) PackageWebLink() string { - return fmt.Sprintf("%s/-/packages/%s/%s", pd.Owner.HTMLURL(), string(pd.Package.Type), url.PathEscape(pd.Package.LowerName)) + return fmt.Sprintf("%s/-/packages/%s/%s", pd.Owner.HomeLink(), string(pd.Package.Type), url.PathEscape(pd.Package.LowerName)) } // FullWebLink returns the package version web link From 9df63f5805dc10ea9202a464e802246fd4490e7c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 24 Dec 2022 00:09:14 +0800 Subject: [PATCH 20/27] improve js code --- templates/repo/issue/view_content/context_menu.tmpl | 2 +- web_src/js/utils.js | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/templates/repo/issue/view_content/context_menu.tmpl b/templates/repo/issue/view_content/context_menu.tmpl index c8cd5530f390c..17c3268f22b3f 100644 --- a/templates/repo/issue/view_content/context_menu.tmpl +++ b/templates/repo/issue/view_content/context_menu.tmpl @@ -10,7 +10,7 @@ {{else}} {{$referenceUrl = Printf "%s/files#%s" .ctx.Issue.Link .item.HashTag}} {{end}} -
      {{.ctx.locale.Tr "repo.issues.context.copy_link"}}
      +
      {{.ctx.locale.Tr "repo.issues.context.copy_link"}}
      {{.ctx.locale.Tr "repo.issues.context.quote_reply"}}
      {{if not .ctx.UnitIssuesGlobalDisabled}}
      {{.ctx.locale.Tr "repo.issues.context.reference_issue"}}
      diff --git a/web_src/js/utils.js b/web_src/js/utils.js index b3424c0589499..2573a501f48e8 100644 --- a/web_src/js/utils.js +++ b/web_src/js/utils.js @@ -134,8 +134,9 @@ export function convertImage(blob, mime) { }); } -const {appSubUrl} = window.config; - export function getCurAbsUrl(relUrl) { - return `${window.location.origin}${appSubUrl}${relUrl}`; + if (relUrl.startsWith('http://') || relUrl.startsWith('https://')) { + return relUrl; + } + return `${window.location.origin}${relUrl}`; } From 49799bbae17b1c881261238f4b70f5f6be7738e6 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 24 Dec 2022 09:09:19 +0800 Subject: [PATCH 21/27] Update web_src/js/features/clipboard.js Co-authored-by: silverwind --- web_src/js/features/clipboard.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/features/clipboard.js b/web_src/js/features/clipboard.js index 1ab2d726d325b..6844db7cafddb 100644 --- a/web_src/js/features/clipboard.js +++ b/web_src/js/features/clipboard.js @@ -52,7 +52,7 @@ export function initGlobalCopyToClipboardListener() { // up to 3 levels for performance for (let i = 0; i < 3 && target; i++) { let txt = target.getAttribute('data-clipboard-text'); - if (txt && target.getAttribute('data-clipboard-text-type') === 'link') { + if (txt && target.getAttribute('data-clipboard-text-type') === 'url') { txt = getCurAbsUrl(txt); } const text = txt || document.querySelector(target.getAttribute('data-clipboard-target'))?.value; From e3dc4522b4de5172ed15bf507bf9136c8f753222 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 24 Dec 2022 09:09:30 +0800 Subject: [PATCH 22/27] Update templates/repo/issue/view_content/context_menu.tmpl Co-authored-by: silverwind --- templates/repo/issue/view_content/context_menu.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/issue/view_content/context_menu.tmpl b/templates/repo/issue/view_content/context_menu.tmpl index 17c3268f22b3f..b96fd86af5d00 100644 --- a/templates/repo/issue/view_content/context_menu.tmpl +++ b/templates/repo/issue/view_content/context_menu.tmpl @@ -10,7 +10,7 @@ {{else}} {{$referenceUrl = Printf "%s/files#%s" .ctx.Issue.Link .item.HashTag}} {{end}} -
      {{.ctx.locale.Tr "repo.issues.context.copy_link"}}
      +
      {{.ctx.locale.Tr "repo.issues.context.copy_link"}}
      {{.ctx.locale.Tr "repo.issues.context.quote_reply"}}
      {{if not .ctx.UnitIssuesGlobalDisabled}}
      {{.ctx.locale.Tr "repo.issues.context.reference_issue"}}
      From 93bdd1a44f668b227d25b76981ffe8d920a6365e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 28 Jan 2023 15:27:29 +0800 Subject: [PATCH 23/27] refactor link method --- models/issues/comment.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index c5343ff940ddb..b2f1f6c8dd8f8 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -391,7 +391,7 @@ func (c *Comment) HTMLURL() string { log.Error("loadRepo(%d): %v", c.Issue.RepoID, err) return "" } - return c.link(c.Issue.HTMLURL()) + return c.Issue.HTMLURL() + c.hashLink() } // Link formats a URL-string to the issue-comment @@ -406,25 +406,25 @@ func (c *Comment) Link() string { log.Error("loadRepo(%d): %v", c.Issue.RepoID, err) return "" } - return c.link(c.Issue.Link()) + return c.Issue.Link() + c.hashLink() } -func (c *Comment) link(baseURL string) string { +func (c *Comment) hashLink() string { if c.Type == CommentTypeCode { if c.ReviewID == 0 { - return fmt.Sprintf("%s/files#%s", baseURL, c.HashTag()) + return "/files#" + c.HashTag() } if c.Review == nil { if err := c.LoadReview(); err != nil { log.Warn("LoadReview(%d): %v", c.ReviewID, err) - return fmt.Sprintf("%s/files#%s", baseURL, c.HashTag()) + return "/files#" + c.HashTag() } } if c.Review.Type <= ReviewTypePending { - return fmt.Sprintf("%s/files#%s", baseURL, c.HashTag()) + return "/files#" + c.HashTag() } } - return fmt.Sprintf("%s#%s", baseURL, c.HashTag()) + return "#" + c.HashTag() } // APIURL formats a API-string to the issue-comment From 8f2aebd083bc409e921c4e3a853172f3a6b78fbb Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 4 Feb 2023 11:03:33 +0800 Subject: [PATCH 24/27] follow @delvh suggestion --- models/activities/notification.go | 2 +- models/issues/comment.go | 2 +- models/issues/issue.go | 2 +- models/issues/pull.go | 4 ++-- models/project/project.go | 1 + models/repo/release.go | 2 +- models/repo/repo.go | 2 +- web_src/js/features/clipboard.js | 4 ++-- web_src/js/utils.js | 2 +- web_src/js/utils.test.js | 8 ++++---- 10 files changed, 15 insertions(+), 14 deletions(-) diff --git a/models/activities/notification.go b/models/activities/notification.go index 406f0f0fa2931..0b920710a4f7d 100644 --- a/models/activities/notification.go +++ b/models/activities/notification.go @@ -459,7 +459,7 @@ func (n *Notification) HTMLURL() string { return "" } -// Link formats a URL-string to the notification +// Link formats a relative URL-string to the notification func (n *Notification) Link() string { switch n.Source { case NotificationSourceIssue, NotificationSourcePullRequest: diff --git a/models/issues/comment.go b/models/issues/comment.go index b2f1f6c8dd8f8..833c57bf8cb64 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -394,7 +394,7 @@ func (c *Comment) HTMLURL() string { return c.Issue.HTMLURL() + c.hashLink() } -// Link formats a URL-string to the issue-comment +// Link formats a relative URL-string to the issue-comment func (c *Comment) Link() string { err := c.LoadIssue(db.DefaultContext) if err != nil { // Silently dropping errors :unamused: diff --git a/models/issues/issue.go b/models/issues/issue.go index dc9e5c5acd18a..5762e10b19941 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -419,7 +419,7 @@ func (issue *Issue) HTMLURL() string { return fmt.Sprintf("%s/%s/%d", issue.Repo.HTMLURL(), path, issue.Index) } -// Link returns the Link URL to this issue. +// Link returns the issue's relative URL. func (issue *Issue) Link() string { var path string if issue.IsPull { diff --git a/models/issues/pull.go b/models/issues/pull.go index 1826ff6752eae..929422538b7e7 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -695,7 +695,7 @@ func GetPullRequestsByHeadBranch(ctx context.Context, headBranch string, headRep return prs, nil } -// GetBaseBranchLink returns the relative HTML URL of the base branch +// GetBaseBranchLink returns the relative URL of the base branch func (pr *PullRequest) GetBaseBranchLink() string { if err := pr.LoadBaseRepo(db.DefaultContext); err != nil { log.Error("LoadBaseRepo: %v", err) @@ -707,7 +707,7 @@ func (pr *PullRequest) GetBaseBranchLink() string { return pr.BaseRepo.Link() + "/src/branch/" + util.PathEscapeSegments(pr.BaseBranch) } -// GetHeadBranchLink returns the relative HTML URL of the head branch +// GetHeadBranchLink returns the relative URL of the head branch func (pr *PullRequest) GetHeadBranchLink() string { if pr.Flow == PullRequestFlowAGit { return "" diff --git a/models/project/project.go b/models/project/project.go index 273823ac9d347..9074fd0c15c7d 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -116,6 +116,7 @@ func (p *Project) LoadRepo(ctx context.Context) (err error) { return err } +// Link returns the project's relative URL. func (p *Project) Link() string { if p.OwnerID > 0 { err := p.LoadOwner(db.DefaultContext) diff --git a/models/repo/release.go b/models/repo/release.go index 2163111603cac..abf91bc4bbab0 100644 --- a/models/repo/release.go +++ b/models/repo/release.go @@ -130,7 +130,7 @@ func (r *Release) HTMLURL() string { return r.Repo.HTMLURL() + "/releases/tag/" + util.PathEscapeSegments(r.TagName) } -// Link the url for a release on the web UI. release must have attributes loaded +// Link the relative url for a release on the web UI. release must have attributes loaded func (r *Release) Link() string { return r.Repo.Link() + "/releases/tag/" + util.PathEscapeSegments(r.TagName) } diff --git a/models/repo/repo.go b/models/repo/repo.go index e5e1ac43b41fd..69c1e8f8edfd2 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -481,7 +481,7 @@ func (repo *Repository) RepoPath() string { return RepoPath(repo.OwnerName, repo.Name) } -// Link returns the repository link +// Link returns the repository relative url func (repo *Repository) Link() string { return setting.AppSubURL + "/" + url.PathEscape(repo.OwnerName) + "/" + url.PathEscape(repo.Name) } diff --git a/web_src/js/features/clipboard.js b/web_src/js/features/clipboard.js index 6844db7cafddb..07c439504ed7c 100644 --- a/web_src/js/features/clipboard.js +++ b/web_src/js/features/clipboard.js @@ -1,5 +1,5 @@ import {showTemporaryTooltip} from '../modules/tippy.js'; -import {getCurAbsUrl} from '../utils.js'; +import {toAbsoluteUrl} from '../utils.js'; const {copy_success, copy_error} = window.config.i18n; @@ -53,7 +53,7 @@ export function initGlobalCopyToClipboardListener() { for (let i = 0; i < 3 && target; i++) { let txt = target.getAttribute('data-clipboard-text'); if (txt && target.getAttribute('data-clipboard-text-type') === 'url') { - txt = getCurAbsUrl(txt); + txt = toAbsoluteUrl(txt); } const text = txt || document.querySelector(target.getAttribute('data-clipboard-target'))?.value; diff --git a/web_src/js/utils.js b/web_src/js/utils.js index 2573a501f48e8..b9cd69e15aa63 100644 --- a/web_src/js/utils.js +++ b/web_src/js/utils.js @@ -134,7 +134,7 @@ export function convertImage(blob, mime) { }); } -export function getCurAbsUrl(relUrl) { +export function toAbsoluteUrl(relUrl) { if (relUrl.startsWith('http://') || relUrl.startsWith('https://')) { return relUrl; } diff --git a/web_src/js/utils.test.js b/web_src/js/utils.test.js index 6dd82d2678fc2..4efe916231d4f 100644 --- a/web_src/js/utils.test.js +++ b/web_src/js/utils.test.js @@ -2,7 +2,7 @@ import {expect, test} from 'vitest'; import { basename, extname, isObject, uniq, stripTags, joinPaths, parseIssueHref, prettyNumber, parseUrl, translateMonth, translateDay, blobToDataURI, - getCurAbsUrl, + toAbsoluteUrl, } from './utils.js'; test('basename', () => { @@ -138,7 +138,7 @@ test('blobToDataURI', async () => { expect(await blobToDataURI(blob)).toEqual('data:application/json;base64,eyJ0ZXN0Ijp0cnVlfQ=='); }); -test('getCurAbsUrl', () => { - expect(getCurAbsUrl('')).toEqual('http://localhost:3000'); - expect(getCurAbsUrl('/user/repo')).toEqual('http://localhost:3000/user/repo'); +test('toAbsoluteUrl', () => { + expect(toAbsoluteUrl('')).toEqual('http://localhost:3000'); + expect(toAbsoluteUrl('/user/repo')).toEqual('http://localhost:3000/user/repo'); }); From d79bfb444deb1567c24fadfda478e402ee3ab02e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 4 Feb 2023 11:18:56 +0800 Subject: [PATCH 25/27] follow @delvh suggestion --- models/activities/action.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/models/activities/action.go b/models/activities/action.go index 9ba010e933d84..87228cc8347cb 100644 --- a/models/activities/action.go +++ b/models/activities/action.go @@ -228,13 +228,19 @@ func (a *Action) GetCommentHTMLURL() string { return a.getCommentHTMLURL(db.DefaultContext) } +func (a *Action) loadComment(ctx context.Context) (err error) { + if a.CommentID == 0 || a.Comment != nil { + return nil + } + a.Comment, err = issues_model.GetCommentByID(ctx, a.CommentID) + return +} + func (a *Action) getCommentHTMLURL(ctx context.Context) string { if a == nil { return "#" } - if a.Comment == nil && a.CommentID != 0 { - a.Comment, _ = issues_model.GetCommentByID(ctx, a.CommentID) - } + _ = a.loadComment(ctx) if a.Comment != nil { return a.Comment.HTMLURL() } @@ -269,9 +275,7 @@ func (a *Action) getCommentLink(ctx context.Context) string { if a == nil { return "#" } - if a.Comment == nil && a.CommentID != 0 { - a.Comment, _ = issues_model.GetCommentByID(ctx, a.CommentID) - } + _ = a.loadComment(ctx) if a.Comment != nil { return a.Comment.Link() } From 2ece9aaabb8e3b388726f0a58a5287090078a464 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 4 Feb 2023 22:00:44 +0800 Subject: [PATCH 26/27] merge main branch and follow @wxiaoguang's suggestion --- templates/repo/issue/view_content/comments.tmpl | 4 ++-- templates/repo/view_file.tmpl | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index a72281a12c203..39fbc638ce12c 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -329,7 +329,7 @@
      {{svg "octicon-plus"}} - + {{if eq .DependentIssue.RepoID .Issue.RepoID}} #{{.DependentIssue.Index}} {{.DependentIssue.Title}} {{else}} @@ -352,7 +352,7 @@
      {{svg "octicon-trash"}} - + {{if eq .DependentIssue.RepoID .Issue.RepoID}} #{{.DependentIssue.Index}} {{.DependentIssue.Title}} {{else}} diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index f83585f4968c6..85ff0120b22c3 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -110,7 +110,8 @@