Browse Source

Fix implementation of repo Home func (#2601)

* Fix implementation of repo Home func

* Make fixture changes for testing
Morlinest 1 year ago
parent
commit
1ad902d529
3 changed files with 33 additions and 20 deletions
  1. 12 12
      models/fixtures/repo_unit.yml
  2. 8 0
      models/unit.go
  3. 13 8
      routers/repo/view.go

+ 12 - 12
models/fixtures/repo_unit.yml

@@ -1,40 +1,40 @@
1 1
 -
2 2
   id: 1
3 3
   repo_id: 1
4
-  type: 1
5
-  index: 0
4
+  type: 4
5
+  index: 3
6 6
   config: "{}"
7 7
   created_unix: 946684810
8 8
 
9 9
 -
10 10
   id: 2
11 11
   repo_id: 1
12
-  type: 2
13
-  index: 1
14
-  config: "{\"EnableTimetracker\":true,\"AllowOnlyContributorsToTrackTime\":true}"
12
+  type: 5
13
+  index: 4
14
+  config: "{}"
15 15
   created_unix: 946684810
16 16
 
17 17
 -
18 18
   id: 3
19 19
   repo_id: 1
20
-  type: 3
21
-  index: 2
20
+  type: 1
21
+  index: 0
22 22
   config: "{}"
23 23
   created_unix: 946684810
24 24
 
25 25
 -
26 26
   id: 4
27 27
   repo_id: 1
28
-  type: 4
29
-  index: 3
30
-  config: "{}"
28
+  type: 2
29
+  index: 1
30
+  config: "{\"EnableTimetracker\":true,\"AllowOnlyContributorsToTrackTime\":true}"
31 31
   created_unix: 946684810
32 32
 
33 33
 -
34 34
   id: 5
35 35
   repo_id: 1
36
-  type: 5
37
-  index: 4
36
+  type: 3
37
+  index: 2
38 38
   config: "{}"
39 39
   created_unix: 946684810
40 40
 

+ 8 - 0
models/unit.go

@@ -60,6 +60,14 @@ func (u *Unit) CanDisable() bool {
60 60
 	return true
61 61
 }
62 62
 
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
+
63 71
 // Enumerate all the units
64 72
 var (
65 73
 	UnitCode = Unit{

+ 13 - 8
routers/repo/view.go

@@ -264,16 +264,21 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
264 264
 // Home render repository home page
265 265
 func Home(ctx *context.Context) {
266 266
 	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
+			}
271 278
 		}
272 279
 
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))
277 282
 			return
278 283
 		}
279 284
 	}