Skip to content

Commit f25fd5c

Browse files
guillep2ktechknowlogick
authored andcommitted
Fix team links in HTML rendering (#9127)
* Fix team links in HTML rendering * Fix check and lint
1 parent 7523314 commit f25fd5c

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

models/repo.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ type Repository struct {
173173
*Mirror `xorm:"-"`
174174
Status RepositoryStatus `xorm:"NOT NULL DEFAULT 0"`
175175

176-
ExternalMetas map[string]string `xorm:"-"`
177-
Units []*RepoUnit `xorm:"-"`
176+
RenderingMetas map[string]string `xorm:"-"`
177+
Units []*RepoUnit `xorm:"-"`
178178

179179
IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"`
180180
ForkID int64 `xorm:"INDEX"`
@@ -559,27 +559,39 @@ func (repo *Repository) mustOwnerName(e Engine) string {
559559

560560
// ComposeMetas composes a map of metas for properly rendering issue links and external issue trackers.
561561
func (repo *Repository) ComposeMetas() map[string]string {
562-
if repo.ExternalMetas == nil {
563-
repo.ExternalMetas = map[string]string{
562+
if repo.RenderingMetas == nil {
563+
metas := map[string]string{
564564
"user": repo.MustOwner().Name,
565565
"repo": repo.Name,
566566
"repoPath": repo.RepoPath(),
567567
}
568+
568569
unit, err := repo.GetUnit(UnitTypeExternalTracker)
569-
if err != nil {
570-
return repo.ExternalMetas
570+
if err == nil {
571+
metas["format"] = unit.ExternalTrackerConfig().ExternalTrackerFormat
572+
switch unit.ExternalTrackerConfig().ExternalTrackerStyle {
573+
case markup.IssueNameStyleAlphanumeric:
574+
metas["style"] = markup.IssueNameStyleAlphanumeric
575+
default:
576+
metas["style"] = markup.IssueNameStyleNumeric
577+
}
571578
}
572579

573-
repo.ExternalMetas["format"] = unit.ExternalTrackerConfig().ExternalTrackerFormat
574-
switch unit.ExternalTrackerConfig().ExternalTrackerStyle {
575-
case markup.IssueNameStyleAlphanumeric:
576-
repo.ExternalMetas["style"] = markup.IssueNameStyleAlphanumeric
577-
default:
578-
repo.ExternalMetas["style"] = markup.IssueNameStyleNumeric
580+
if repo.Owner.IsOrganization() {
581+
teams := make([]string, 0, 5)
582+
_ = x.Table("team_repo").
583+
Join("INNER", "team", "team.id = team_repo.team_id").
584+
Where("team_repo.repo_id = ?", repo.ID).
585+
Select("team.lower_name").
586+
OrderBy("team.lower_name").
587+
Find(&teams)
588+
metas["teams"] = "," + strings.Join(teams, ",") + ","
589+
metas["org"] = repo.Owner.LowerName
579590
}
580591

592+
repo.RenderingMetas = metas
581593
}
582-
return repo.ExternalMetas
594+
return repo.RenderingMetas
583595
}
584596

585597
// DeleteWiki removes the actual and local copy of repository wiki.

models/repo_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import (
1717
"github.com/stretchr/testify/assert"
1818
)
1919

20-
func TestRepo(t *testing.T) {
20+
func TestMetas(t *testing.T) {
21+
assert.NoError(t, PrepareTestDatabase())
22+
2123
repo := &Repository{Name: "testRepo"}
2224
repo.Owner = &User{Name: "testOwner"}
2325

@@ -36,7 +38,7 @@ func TestRepo(t *testing.T) {
3638

3739
testSuccess := func(expectedStyle string) {
3840
repo.Units = []*RepoUnit{&externalTracker}
39-
repo.ExternalMetas = nil
41+
repo.RenderingMetas = nil
4042
metas := repo.ComposeMetas()
4143
assert.Equal(t, expectedStyle, metas["style"])
4244
assert.Equal(t, "testRepo", metas["repo"])
@@ -51,6 +53,15 @@ func TestRepo(t *testing.T) {
5153

5254
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleNumeric
5355
testSuccess(markup.IssueNameStyleNumeric)
56+
57+
repo, err := GetRepositoryByID(3)
58+
assert.NoError(t, err)
59+
60+
metas = repo.ComposeMetas()
61+
assert.Contains(t, metas, "org")
62+
assert.Contains(t, metas, "teams")
63+
assert.Equal(t, metas["org"], "user3")
64+
assert.Equal(t, metas["teams"], ",owners,team1,")
5465
}
5566

5667
func TestGetRepositoryCount(t *testing.T) {

modules/markup/html.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,20 @@ func replaceContentList(node *html.Node, i, j int, newNodes []*html.Node) {
432432
}
433433
}
434434

435-
func mentionProcessor(_ *postProcessCtx, node *html.Node) {
435+
func mentionProcessor(ctx *postProcessCtx, node *html.Node) {
436436
// We replace only the first mention; other mentions will be addressed later
437437
found, loc := references.FindFirstMentionBytes([]byte(node.Data))
438438
if !found {
439439
return
440440
}
441441
mention := node.Data[loc.Start:loc.End]
442-
replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mention[1:]), mention, "mention"))
442+
var teams string
443+
teams, ok := ctx.metas["teams"]
444+
if ok && strings.Contains(teams, ","+strings.ToLower(mention[1:])+",") {
445+
replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, "org", ctx.metas["org"], "teams", mention[1:]), mention, "mention"))
446+
} else {
447+
replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mention[1:]), mention, "mention"))
448+
}
443449
}
444450

445451
func shortLinkProcessor(ctx *postProcessCtx, node *html.Node) {

0 commit comments

Comments
 (0)