Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append (comment) when a link points at a comment rather than the whole issue #23734

Merged
merged 27 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bab57d6
add comment to render and init tippy
HesterG Mar 24, 2023
895d34d
skip files changed hash comment
HesterG Mar 24, 2023
ea92068
add new line
HesterG Mar 24, 2023
43563af
comments
HesterG Mar 24, 2023
12d9df9
handle path including pr or issue
HesterG Mar 24, 2023
0cd84ed
adjust condition
HesterG Mar 24, 2023
259ff80
Merge branch 'go-gitea:main' into issue-commet-render-23671
HesterG Mar 24, 2023
60643cb
Merge branch 'go-gitea:main' into issue-commet-render-23671
HesterG Mar 27, 2023
e5d109d
add popup when preview changes
HesterG Mar 27, 2023
a2b91d7
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 27, 2023
13f0515
move if to attachTippyToRefIssues
HesterG Mar 27, 2023
76dbc97
move filesChangedm
HesterG Mar 27, 2023
b3839f2
remove unnecessary ifs and fix order
HesterG Mar 27, 2023
bb58eda
try add locale
HesterG Mar 28, 2023
262ef11
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 28, 2023
d5f70c6
pass language to metas
HesterG Mar 28, 2023
c682983
use context key for locale
wxiaoguang Mar 28, 2023
4919558
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 28, 2023
42a57bf
fine tune variable name and comments
wxiaoguang Mar 28, 2023
8f471ea
Update modules/markup/html.go
wxiaoguang Mar 28, 2023
fad4ae7
Merge branch 'main' into issue-commet-render-23671
silverwind Mar 28, 2023
ab7231b
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 29, 2023
434364a
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 29, 2023
bbea3ba
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 30, 2023
9007732
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 31, 2023
c0bde72
Merge branch 'main' into issue-commet-render-23671
HesterG Apr 3, 2023
b6fb404
Merge branch 'main' into issue-commet-render-23671
lunny Apr 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,9 @@ func (ctx *Context) Value(key interface{}) interface{} {
if key == git.RepositoryContextKey && ctx.Repo != nil {
return ctx.Repo.GitRepo
}

if key == translation.ContextKey && ctx.Locale != nil {
return ctx.Locale
}
return ctx.Req.Context().Value(key)
}

Expand Down
44 changes: 37 additions & 7 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"code.gitea.io/gitea/modules/regexplru"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/templates/vars"
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/modules/util"

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

// regexp for full links to hash comment in pull request files changed tab
var filesChangedFullPattern *regexp.Regexp

// Once for to prevent races
var filesChangedFullPatternOnce sync.Once

func getIssueFullPattern() *regexp.Regexp {
issueFullPatternOnce.Do(func() {
// example: https://domain/org/repo/pulls/27#hash
issueFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) +
`[\w_.-]+/[\w_.-]+/(?:issues|pulls)/((?:\w{1,10}-)?[1-9][0-9]*)([\?|#](\S+)?)?\b`)
})
return issueFullPattern
}

func getFilesChangedFullPattern() *regexp.Regexp {
filesChangedFullPatternOnce.Do(func() {
// example: https://domain/org/repo/pulls/27/files#hash
filesChangedFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) +
`[\w_.-]+/[\w_.-]+/pulls/((?:\w{1,10}-)?[1-9][0-9]*)/files([\?|#](\S+)?)?\b`)
})
return filesChangedFullPattern
}

// CustomLinkURLSchemes allows for additional schemes to be detected when parsing links within text
func CustomLinkURLSchemes(schemes []string) {
schemes = append(schemes, "http", "https")
Expand Down Expand Up @@ -793,15 +810,30 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
if ctx.Metas == nil {
return
}

next := node.NextSibling
for node != nil && node != next {
m := getIssueFullPattern().FindStringSubmatchIndex(node.Data)
if m == nil {
return
}

mDiffView := getFilesChangedFullPattern().FindStringSubmatchIndex(node.Data)
// leave it as it is if the link is from "Files Changed" tab in PR Diff View https://domain/org/repo/pulls/27/files
if mDiffView != nil {
return
}

link := node.Data[m[0]:m[1]]
id := "#" + node.Data[m[2]:m[3]]
text := "#" + node.Data[m[2]:m[3]]
// if m[4] and m[5] is not -1, then link is to a comment
// indicate that in the text by appending (comment)
if m[4] != -1 && m[5] != -1 {
if locale, ok := ctx.Ctx.Value(translation.ContextKey).(translation.Locale); ok {
text += " " + locale.Tr("repo.from_comment")
} else {
text += " (comment)"
}
}

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

if matchOrg == ctx.Metas["user"] && matchRepo == ctx.Metas["repo"] {
// TODO if m[4]:m[5] is not nil, then link is to a comment,
// and we should indicate that in the text somehow
replaceContent(node, m[0], m[1], createLink(link, id, "ref-issue"))
replaceContent(node, m[0], m[1], createLink(link, text, "ref-issue"))
} else {
orgRepoID := matchOrg + "/" + matchRepo + id
replaceContent(node, m[0], m[1], createLink(link, orgRepoID, "ref-issue"))
text = matchOrg + "/" + matchRepo + text
replaceContent(node, m[0], m[1], createLink(link, text, "ref-issue"))
}
node = node.NextSibling.NextSibling
}
Expand Down
8 changes: 6 additions & 2 deletions modules/markup/html_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,17 @@ func TestRender_FullIssueURLs(t *testing.T) {
test("Look here http://localhost:3000/person/repo/issues/4",
`Look here <a href="http://localhost:3000/person/repo/issues/4" class="ref-issue">person/repo#4</a>`)
test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
`<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4</a>`)
`<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4 (comment)</a>`)
test("http://localhost:3000/gogits/gogs/issues/4",
`<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a>`)
test("http://localhost:3000/gogits/gogs/issues/4 test",
`<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a> test`)
test("http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-123 test",
`<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&amp;b=2#comment-123" class="ref-issue">#4</a> test`)
`<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&amp;b=2#comment-123" class="ref-issue">#4 (comment)</a> test`)
test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24",
"http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24")
test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files",
"http://localhost:3000/testOrg/testOrgRepo/pulls/2/files")
}

func TestRegExp_sha1CurrentPattern(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions modules/translation/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
"golang.org/x/text/language"
)

type contextKey struct{}

var ContextKey interface{} = &contextKey{}

// Locale represents an interface to translation
type Locale interface {
Language() string
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@ download_file = Download file
normal_view = Normal View
line = line
lines = lines
from_comment = (comment)

editor.add_file = Add File
editor.new_file = New File
Expand Down
3 changes: 3 additions & 0 deletions web_src/js/features/comp/MarkupContentPreview.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import $ from 'jquery';
import {initMarkupContent} from '../../markup/content.js';
import {attachRefIssueContextPopup} from '../contextpopup.js';

const {csrfToken} = window.config;

Expand All @@ -16,6 +17,8 @@ export function initCompMarkupContentPreviewTab($form) {
}, (data) => {
const $previewPanel = $form.find(`.tab[data-tab="${$tabMenu.data('preview')}"]`);
$previewPanel.html(data);
const refIssues = $previewPanel.find('p .ref-issue');
attachRefIssueContextPopup(refIssues);
initMarkupContent();
});
});
Expand Down
5 changes: 4 additions & 1 deletion web_src/js/features/contextpopup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import {createTippy} from '../modules/tippy.js';

export function initContextPopups() {
const refIssues = $('.ref-issue');
if (!refIssues.length) return;
attachRefIssueContextPopup(refIssues);
}

export function attachRefIssueContextPopup(refIssues) {
if (!refIssues.length) return;
refIssues.each(function () {
if ($(this).hasClass('ref-external-issue')) {
return;
Expand Down
3 changes: 3 additions & 0 deletions web_src/js/features/repo-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {initCompReactionSelector} from './comp/ReactionSelector.js';
import {initRepoSettingBranches} from './repo-settings.js';
import {initRepoPullRequestMergeForm} from './repo-issue-pr-form.js';
import {hideElem, showElem} from '../utils/dom.js';
import {attachRefIssueContextPopup} from './contextpopup.js';

const {csrfToken} = window.config;

Expand Down Expand Up @@ -439,6 +440,8 @@ async function onEditContent(event) {
} else {
$renderContent.html(data.content);
$rawContent.text($textarea.val());
const refIssues = $renderContent.find('p .ref-issue');
attachRefIssueContextPopup(refIssues);
}
const $content = $segment;
if (!$content.find('.dropzone-attachments').length) {
Expand Down