From 1ad902d5298202d5be14fd5a9c8ed6ce781a23c8 Mon Sep 17 00:00:00 2001
From: Morlinest <Morlinest@users.noreply.github.com>
Date: Sun, 1 Oct 2017 15:50:56 +0200
Subject: [PATCH] Fix implementation of repo Home func (#2601)

* Fix implementation of repo Home func

* Make fixture changes for testing
---
 models/fixtures/repo_unit.yml | 24 ++++++++++++------------
 models/unit.go                |  8 ++++++++
 routers/repo/view.go          | 21 +++++++++++++--------
 3 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/models/fixtures/repo_unit.yml b/models/fixtures/repo_unit.yml
index 57cf35e198..ef06107928 100644
--- a/models/fixtures/repo_unit.yml
+++ b/models/fixtures/repo_unit.yml
@@ -1,40 +1,40 @@
 -
   id: 1
   repo_id: 1
-  type: 1
-  index: 0
+  type: 4
+  index: 3
   config: "{}"
   created_unix: 946684810
 
 -
   id: 2
   repo_id: 1
-  type: 2
-  index: 1
-  config: "{\"EnableTimetracker\":true,\"AllowOnlyContributorsToTrackTime\":true}"
+  type: 5
+  index: 4
+  config: "{}"
   created_unix: 946684810
 
 -
   id: 3
   repo_id: 1
-  type: 3
-  index: 2
+  type: 1
+  index: 0
   config: "{}"
   created_unix: 946684810
 
 -
   id: 4
   repo_id: 1
-  type: 4
-  index: 3
-  config: "{}"
+  type: 2
+  index: 1
+  config: "{\"EnableTimetracker\":true,\"AllowOnlyContributorsToTrackTime\":true}"
   created_unix: 946684810
 
 -
   id: 5
   repo_id: 1
-  type: 5
-  index: 4
+  type: 3
+  index: 2
   config: "{}"
   created_unix: 946684810
 
diff --git a/models/unit.go b/models/unit.go
index a14edcec0c..1d26359528 100644
--- a/models/unit.go
+++ b/models/unit.go
@@ -60,6 +60,14 @@ func (u *Unit) CanDisable() bool {
 	return true
 }
 
+// IsLessThan compares order of two units
+func (u Unit) IsLessThan(unit Unit) bool {
+	if (u.Type == UnitTypeExternalTracker || u.Type == UnitTypeExternalWiki) && unit.Type != UnitTypeExternalTracker && unit.Type != UnitTypeExternalWiki {
+		return false
+	}
+	return u.Idx < unit.Idx
+}
+
 // Enumerate all the units
 var (
 	UnitCode = Unit{
diff --git a/routers/repo/view.go b/routers/repo/view.go
index 64a2e2ea76..fa1087fe09 100644
--- a/routers/repo/view.go
+++ b/routers/repo/view.go
@@ -264,16 +264,21 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
 // Home render repository home page
 func Home(ctx *context.Context) {
 	if len(ctx.Repo.Repository.Units) > 0 {
-		tp := ctx.Repo.Repository.Units[0].Type
-		if tp == models.UnitTypeCode {
-			renderCode(ctx)
-			return
+		var firstUnit *models.Unit
+		for _, repoUnit := range ctx.Repo.Repository.Units {
+			if repoUnit.Type == models.UnitTypeCode {
+				renderCode(ctx)
+				return
+			}
+
+			unit, ok := models.Units[repoUnit.Type]
+			if ok && (firstUnit == nil || !firstUnit.IsLessThan(unit)) {
+				firstUnit = &unit
+			}
 		}
 
-		unit, ok := models.Units[tp]
-		if ok {
-			ctx.Redirect(setting.AppSubURL + fmt.Sprintf("/%s%s",
-				ctx.Repo.Repository.FullName(), unit.URI))
+		if firstUnit != nil {
+			ctx.Redirect(fmt.Sprintf("%s/%s%s", setting.AppSubURL, ctx.Repo.Repository.FullName(), firstUnit.URI))
 			return
 		}
 	}