Skip to content

Commit ca905b8

Browse files
authored
Append (comment) when a link points at a comment rather than the whole issue (#23734)
Close #23671 For the feature mentioned above, this PR append ' (comment)' to the rendered html if it is a hashcomment. After the PR, type in the following ``` pull request from other repo: http://localhost:3000/testOrg/testOrgRepo/pulls/2 pull request from this repo: http://localhost:3000/aaa/testA/pulls/2 issue comment from this repo: http://localhost:3000/aaa/testA/issues/1#issuecomment-18 http://localhost:3000/aaa/testA/pulls/2#issue-9 issue comment from other repo: http://localhost:3000/testOrg/testOrgRepo/pulls/2#issuecomment-24 http://localhost:3000/testOrg/testOrgRepo/pulls/2#issue ``` Gives: <img width="687" alt="截屏2023-03-27 13 53 06" src="https://user-images.githubusercontent.com/17645053/227852387-2b218e0d-3468-4d90-ad81-d702ddd17fd2.png"> Other than the above feature, this PR also includes two other changes: 1 Right now, the render of links from file changed tab in pull request might not be very proper, for example, if type in the following. (not sure if this is an issue or design, if not an issue, I will revert the changes). example on [try.gitea.io](https://try.gitea.io/HesterG/testrepo/pulls/1) ``` https://try.gitea.io/HesterG/testrepo/pulls/1/files#issuecomment-162725 https://try.gitea.io/HesterG/testrepo/pulls/1/files ``` it will render the following <img width="899" alt="截屏2023-03-24 15 41 37" src="https://user-images.githubusercontent.com/17645053/227456117-5eccedb7-9118-4540-929d-aee9a76de852.png"> In this PR, skip processing the link into a ref issue if it is a link from files changed tab in pull request After: type in following ``` hash comment on files changed tab: http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24 files changed link: http://localhost:3000/testOrg/testOrgRepo/pulls/2/files ``` Gives <img width="708" alt="截屏2023-03-27 22 09 02" src="https://user-images.githubusercontent.com/17645053/227964273-5dc06c50-3713-489c-b05d-d95367d0ab0f.png"> 2 Right now, after editing the comment area, there will not be tippys attached to `ref-issue`; and no tippy attached on preview as well. example: https://user-images.githubusercontent.com/17645053/227850540-5ae34e2d-b1d7-4d0d-9726-7701bf825d1f.mov In this PR, in frontend, make sure tippy is added after editing the comment, and to the comment on preview tab After: https://user-images.githubusercontent.com/17645053/227853777-06f56b4c-1148-467c-b6f7-f79418e67504.mov
1 parent 977ef21 commit ca905b8

File tree

8 files changed

+61
-11
lines changed

8 files changed

+61
-11
lines changed

modules/context/context.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,9 @@ func (ctx *Context) Value(key interface{}) interface{} {
628628
if key == git.RepositoryContextKey && ctx.Repo != nil {
629629
return ctx.Repo.GitRepo
630630
}
631-
631+
if key == translation.ContextKey && ctx.Locale != nil {
632+
return ctx.Locale
633+
}
632634
return ctx.Req.Context().Value(key)
633635
}
634636

modules/markup/html.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"code.gitea.io/gitea/modules/regexplru"
2323
"code.gitea.io/gitea/modules/setting"
2424
"code.gitea.io/gitea/modules/templates/vars"
25+
"code.gitea.io/gitea/modules/translation"
2526
"code.gitea.io/gitea/modules/util"
2627

2728
"golang.org/x/net/html"
@@ -97,14 +98,30 @@ var issueFullPattern *regexp.Regexp
9798
// Once for to prevent races
9899
var issueFullPatternOnce sync.Once
99100

101+
// regexp for full links to hash comment in pull request files changed tab
102+
var filesChangedFullPattern *regexp.Regexp
103+
104+
// Once for to prevent races
105+
var filesChangedFullPatternOnce sync.Once
106+
100107
func getIssueFullPattern() *regexp.Regexp {
101108
issueFullPatternOnce.Do(func() {
109+
// example: https://domain/org/repo/pulls/27#hash
102110
issueFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) +
103111
`[\w_.-]+/[\w_.-]+/(?:issues|pulls)/((?:\w{1,10}-)?[1-9][0-9]*)([\?|#](\S+)?)?\b`)
104112
})
105113
return issueFullPattern
106114
}
107115

116+
func getFilesChangedFullPattern() *regexp.Regexp {
117+
filesChangedFullPatternOnce.Do(func() {
118+
// example: https://domain/org/repo/pulls/27/files#hash
119+
filesChangedFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) +
120+
`[\w_.-]+/[\w_.-]+/pulls/((?:\w{1,10}-)?[1-9][0-9]*)/files([\?|#](\S+)?)?\b`)
121+
})
122+
return filesChangedFullPattern
123+
}
124+
108125
// CustomLinkURLSchemes allows for additional schemes to be detected when parsing links within text
109126
func CustomLinkURLSchemes(schemes []string) {
110127
schemes = append(schemes, "http", "https")
@@ -793,15 +810,30 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
793810
if ctx.Metas == nil {
794811
return
795812
}
796-
797813
next := node.NextSibling
798814
for node != nil && node != next {
799815
m := getIssueFullPattern().FindStringSubmatchIndex(node.Data)
800816
if m == nil {
801817
return
802818
}
819+
820+
mDiffView := getFilesChangedFullPattern().FindStringSubmatchIndex(node.Data)
821+
// leave it as it is if the link is from "Files Changed" tab in PR Diff View https://domain/org/repo/pulls/27/files
822+
if mDiffView != nil {
823+
return
824+
}
825+
803826
link := node.Data[m[0]:m[1]]
804-
id := "#" + node.Data[m[2]:m[3]]
827+
text := "#" + node.Data[m[2]:m[3]]
828+
// if m[4] and m[5] is not -1, then link is to a comment
829+
// indicate that in the text by appending (comment)
830+
if m[4] != -1 && m[5] != -1 {
831+
if locale, ok := ctx.Ctx.Value(translation.ContextKey).(translation.Locale); ok {
832+
text += " " + locale.Tr("repo.from_comment")
833+
} else {
834+
text += " (comment)"
835+
}
836+
}
805837

806838
// extract repo and org name from matched link like
807839
// http://localhost:3000/gituser/myrepo/issues/1
@@ -810,12 +842,10 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
810842
matchRepo := linkParts[len(linkParts)-3]
811843

812844
if matchOrg == ctx.Metas["user"] && matchRepo == ctx.Metas["repo"] {
813-
// TODO if m[4]:m[5] is not nil, then link is to a comment,
814-
// and we should indicate that in the text somehow
815-
replaceContent(node, m[0], m[1], createLink(link, id, "ref-issue"))
845+
replaceContent(node, m[0], m[1], createLink(link, text, "ref-issue"))
816846
} else {
817-
orgRepoID := matchOrg + "/" + matchRepo + id
818-
replaceContent(node, m[0], m[1], createLink(link, orgRepoID, "ref-issue"))
847+
text = matchOrg + "/" + matchRepo + text
848+
replaceContent(node, m[0], m[1], createLink(link, text, "ref-issue"))
819849
}
820850
node = node.NextSibling.NextSibling
821851
}

modules/markup/html_internal_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,17 @@ func TestRender_FullIssueURLs(t *testing.T) {
330330
test("Look here http://localhost:3000/person/repo/issues/4",
331331
`Look here <a href="http://localhost:3000/person/repo/issues/4" class="ref-issue">person/repo#4</a>`)
332332
test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
333-
`<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4</a>`)
333+
`<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4 (comment)</a>`)
334334
test("http://localhost:3000/gogits/gogs/issues/4",
335335
`<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a>`)
336336
test("http://localhost:3000/gogits/gogs/issues/4 test",
337337
`<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a> test`)
338338
test("http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-123 test",
339-
`<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&amp;b=2#comment-123" class="ref-issue">#4</a> test`)
339+
`<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&amp;b=2#comment-123" class="ref-issue">#4 (comment)</a> test`)
340+
test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24",
341+
"http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24")
342+
test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files",
343+
"http://localhost:3000/testOrg/testOrgRepo/pulls/2/files")
340344
}
341345

342346
func TestRegExp_sha1CurrentPattern(t *testing.T) {

modules/translation/translation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import (
1818
"golang.org/x/text/language"
1919
)
2020

21+
type contextKey struct{}
22+
23+
var ContextKey interface{} = &contextKey{}
24+
2125
// Locale represents an interface to translation
2226
type Locale interface {
2327
Language() string

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@ download_file = Download file
11041104
normal_view = Normal View
11051105
line = line
11061106
lines = lines
1107+
from_comment = (comment)
11071108

11081109
editor.add_file = Add File
11091110
editor.new_file = New File

web_src/js/features/comp/MarkupContentPreview.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import $ from 'jquery';
22
import {initMarkupContent} from '../../markup/content.js';
3+
import {attachRefIssueContextPopup} from '../contextpopup.js';
34

45
const {csrfToken} = window.config;
56

@@ -16,6 +17,8 @@ export function initCompMarkupContentPreviewTab($form) {
1617
}, (data) => {
1718
const $previewPanel = $form.find(`.tab[data-tab="${$tabMenu.data('preview')}"]`);
1819
$previewPanel.html(data);
20+
const refIssues = $previewPanel.find('p .ref-issue');
21+
attachRefIssueContextPopup(refIssues);
1922
initMarkupContent();
2023
});
2124
});

web_src/js/features/contextpopup.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import {createTippy} from '../modules/tippy.js';
66

77
export function initContextPopups() {
88
const refIssues = $('.ref-issue');
9-
if (!refIssues.length) return;
9+
attachRefIssueContextPopup(refIssues);
10+
}
1011

12+
export function attachRefIssueContextPopup(refIssues) {
13+
if (!refIssues.length) return;
1114
refIssues.each(function () {
1215
if ($(this).hasClass('ref-external-issue')) {
1316
return;

web_src/js/features/repo-legacy.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {initCompReactionSelector} from './comp/ReactionSelector.js';
2626
import {initRepoSettingBranches} from './repo-settings.js';
2727
import {initRepoPullRequestMergeForm} from './repo-issue-pr-form.js';
2828
import {hideElem, showElem} from '../utils/dom.js';
29+
import {attachRefIssueContextPopup} from './contextpopup.js';
2930

3031
const {csrfToken} = window.config;
3132

@@ -439,6 +440,8 @@ async function onEditContent(event) {
439440
} else {
440441
$renderContent.html(data.content);
441442
$rawContent.text($textarea.val());
443+
const refIssues = $renderContent.find('p .ref-issue');
444+
attachRefIssueContextPopup(refIssues);
442445
}
443446
const $content = $segment;
444447
if (!$content.find('.dropzone-attachments').length) {

0 commit comments

Comments
 (0)