Skip to content

Commit 1ad902d

Browse files
Morlinestlafriks
authored andcommitted
Fix implementation of repo Home func (#2601)
* Fix implementation of repo Home func * Make fixture changes for testing
1 parent bae9cbc commit 1ad902d

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

models/fixtures/repo_unit.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
-
22
id: 1
33
repo_id: 1
4-
type: 1
5-
index: 0
4+
type: 4
5+
index: 3
66
config: "{}"
77
created_unix: 946684810
88

99
-
1010
id: 2
1111
repo_id: 1
12-
type: 2
13-
index: 1
14-
config: "{\"EnableTimetracker\":true,\"AllowOnlyContributorsToTrackTime\":true}"
12+
type: 5
13+
index: 4
14+
config: "{}"
1515
created_unix: 946684810
1616

1717
-
1818
id: 3
1919
repo_id: 1
20-
type: 3
21-
index: 2
20+
type: 1
21+
index: 0
2222
config: "{}"
2323
created_unix: 946684810
2424

2525
-
2626
id: 4
2727
repo_id: 1
28-
type: 4
29-
index: 3
30-
config: "{}"
28+
type: 2
29+
index: 1
30+
config: "{\"EnableTimetracker\":true,\"AllowOnlyContributorsToTrackTime\":true}"
3131
created_unix: 946684810
3232

3333
-
3434
id: 5
3535
repo_id: 1
36-
type: 5
37-
index: 4
36+
type: 3
37+
index: 2
3838
config: "{}"
3939
created_unix: 946684810
4040

models/unit.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ func (u *Unit) CanDisable() bool {
6060
return true
6161
}
6262

63+
// IsLessThan compares order of two units
64+
func (u Unit) IsLessThan(unit Unit) bool {
65+
if (u.Type == UnitTypeExternalTracker || u.Type == UnitTypeExternalWiki) && unit.Type != UnitTypeExternalTracker && unit.Type != UnitTypeExternalWiki {
66+
return false
67+
}
68+
return u.Idx < unit.Idx
69+
}
70+
6371
// Enumerate all the units
6472
var (
6573
UnitCode = Unit{

routers/repo/view.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,21 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
264264
// Home render repository home page
265265
func Home(ctx *context.Context) {
266266
if len(ctx.Repo.Repository.Units) > 0 {
267-
tp := ctx.Repo.Repository.Units[0].Type
268-
if tp == models.UnitTypeCode {
269-
renderCode(ctx)
270-
return
267+
var firstUnit *models.Unit
268+
for _, repoUnit := range ctx.Repo.Repository.Units {
269+
if repoUnit.Type == models.UnitTypeCode {
270+
renderCode(ctx)
271+
return
272+
}
273+
274+
unit, ok := models.Units[repoUnit.Type]
275+
if ok && (firstUnit == nil || !firstUnit.IsLessThan(unit)) {
276+
firstUnit = &unit
277+
}
271278
}
272279

273-
unit, ok := models.Units[tp]
274-
if ok {
275-
ctx.Redirect(setting.AppSubURL + fmt.Sprintf("/%s%s",
276-
ctx.Repo.Repository.FullName(), unit.URI))
280+
if firstUnit != nil {
281+
ctx.Redirect(fmt.Sprintf("%s/%s%s", setting.AppSubURL, ctx.Repo.Repository.FullName(), firstUnit.URI))
277282
return
278283
}
279284
}

0 commit comments

Comments
 (0)