diff options
Diffstat (limited to 'routers')
-rw-r--r-- | routers/admin/admin.go | 11 | ||||
-rw-r--r-- | routers/admin/auth.go | 12 | ||||
-rw-r--r-- | routers/admin/user.go | 29 | ||||
-rw-r--r-- | routers/api/v1/users.go | 5 | ||||
-rw-r--r-- | routers/dev/debug.go | 4 | ||||
-rw-r--r-- | routers/dev/template.go | 6 | ||||
-rw-r--r-- | routers/home.go (renamed from routers/dashboard.go) | 21 | ||||
-rw-r--r-- | routers/install.go | 17 | ||||
-rw-r--r-- | routers/org/org.go | 12 | ||||
-rw-r--r-- | routers/repo/branch.go | 4 | ||||
-rw-r--r-- | routers/repo/commit.go | 440 | ||||
-rw-r--r-- | routers/repo/download.go | 131 | ||||
-rw-r--r-- | routers/repo/http.go | 55 | ||||
-rw-r--r-- | routers/repo/issue.go | 2222 | ||||
-rw-r--r-- | routers/repo/pull.go | 4 | ||||
-rw-r--r-- | routers/repo/release.go | 434 | ||||
-rw-r--r-- | routers/repo/repo.go | 517 | ||||
-rw-r--r-- | routers/repo/setting.go | 718 | ||||
-rw-r--r-- | routers/repo/view.go | 203 | ||||
-rw-r--r-- | routers/user/auth.go | 442 | ||||
-rw-r--r-- | routers/user/home.go | 91 | ||||
-rw-r--r-- | routers/user/setting.go | 328 | ||||
-rw-r--r-- | routers/user/social.go | 8 | ||||
-rw-r--r-- | routers/user/user.go | 457 |
24 files changed, 3057 insertions, 3114 deletions
diff --git a/routers/admin/admin.go b/routers/admin/admin.go index eb4123435a..efdc803b95 100644 --- a/routers/admin/admin.go +++ b/routers/admin/admin.go @@ -10,7 +10,8 @@ import ( "strings" "time" - "github.com/go-martini/martini" + "github.com/Unknwon/com" + "github.com/Unknwon/macaron" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" @@ -126,7 +127,7 @@ func Dashboard(ctx *middleware.Context) { ctx.Data["PageIsDashboard"] = true // Run operation. - op, _ := base.StrTo(ctx.Query("op")).Int() + op, _ := com.StrTo(ctx.Query("op")).Int() if op > 0 { var err error var success string @@ -159,7 +160,7 @@ func Users(ctx *middleware.Context) { ctx.Data["Title"] = "User Management" ctx.Data["PageIsUsers"] = true - p := base.StrTo(ctx.Query("p")).MustInt() + p := com.StrTo(ctx.Query("p")).MustInt() if p < 1 { p = 1 } @@ -188,7 +189,7 @@ func Repositories(ctx *middleware.Context) { ctx.Data["Title"] = "Repository Management" ctx.Data["PageIsRepos"] = true - p := base.StrTo(ctx.Query("p")).MustInt() + p := com.StrTo(ctx.Query("p")).MustInt() if p < 1 { p = 1 } @@ -235,7 +236,7 @@ func Config(ctx *middleware.Context) { ctx.Data["OfflineMode"] = setting.OfflineMode ctx.Data["DisableRouterLog"] = setting.DisableRouterLog ctx.Data["RunUser"] = setting.RunUser - ctx.Data["RunMode"] = strings.Title(martini.Env) + ctx.Data["RunMode"] = strings.Title(macaron.Env) ctx.Data["RepoRootPath"] = setting.RepoRootPath ctx.Data["StaticRootPath"] = setting.StaticRootPath ctx.Data["LogRootPath"] = setting.LogRootPath diff --git a/routers/admin/auth.go b/routers/admin/auth.go index ff6c0325b6..44af962751 100644 --- a/routers/admin/auth.go +++ b/routers/admin/auth.go @@ -7,7 +7,7 @@ package admin import ( "strings" - "github.com/go-martini/martini" + "github.com/Unknwon/com" "github.com/go-xorm/core" "github.com/gogits/gogs/models" @@ -89,13 +89,13 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { ctx.Redirect("/admin/auths") } -func EditAuthSource(ctx *middleware.Context, params martini.Params) { +func EditAuthSource(ctx *middleware.Context) { ctx.Data["Title"] = "Edit Authentication" ctx.Data["PageIsAuths"] = true ctx.Data["LoginTypes"] = models.LoginTypes ctx.Data["SMTPAuths"] = models.SMTPAuths - id, err := base.StrTo(params["authid"]).Int64() + id, err := com.StrTo(ctx.Params(":authid")).Int64() if err != nil { ctx.Handle(404, "admin.auths.EditAuthSource", err) return @@ -168,11 +168,11 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { ctx.Redirect("/admin/auths") } -func DeleteAuthSource(ctx *middleware.Context, params martini.Params) { +func DeleteAuthSource(ctx *middleware.Context) { ctx.Data["Title"] = "Delete Authentication" ctx.Data["PageIsAuths"] = true - id, err := base.StrTo(params["authid"]).Int64() + id, err := com.StrTo(ctx.Params(":authid")).Int64() if err != nil { ctx.Handle(404, "admin.auths.DeleteAuth", err) return @@ -188,7 +188,7 @@ func DeleteAuthSource(ctx *middleware.Context, params martini.Params) { switch err { case models.ErrAuthenticationUserUsed: ctx.Flash.Error("This authentication still has used by some users, you should move them and then delete again.") - ctx.Redirect("/admin/auths/" + params["authid"]) + ctx.Redirect("/admin/auths/" + ctx.Params(":authid")) default: ctx.Handle(500, "admin.auths.DeleteAuth(DelLoginSource)", err) } diff --git a/routers/admin/user.go b/routers/admin/user.go index 2d22cfdd89..0355632b53 100644 --- a/routers/admin/user.go +++ b/routers/admin/user.go @@ -7,7 +7,7 @@ package admin import ( "strings" - "github.com/go-martini/martini" + "github.com/Unknwon/com" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" @@ -42,7 +42,7 @@ func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) { return } - if form.Password != form.RetypePasswd { + if form.Password != form.Retype { ctx.Data["Err_Password"] = true ctx.Data["Err_RetypePasswd"] = true ctx.RenderWithErr("Password and re-type password are not same.", "admin/users/new", &form) @@ -60,14 +60,13 @@ func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) { if len(form.LoginType) > 0 { // NOTE: need rewrite. fields := strings.Split(form.LoginType, "-") - tp, _ := base.StrTo(fields[0]).Int() + tp, _ := com.StrTo(fields[0]).Int() u.LoginType = models.LoginType(tp) - u.LoginSource, _ = base.StrTo(fields[1]).Int64() + u.LoginSource, _ = com.StrTo(fields[1]).Int64() u.LoginName = form.LoginName } - var err error - if u, err = models.CreateUser(u); err != nil { + if err := models.CreateUser(u); err != nil { switch err { case models.ErrUserAlreadyExist: ctx.RenderWithErr("Username has been already taken", USER_NEW, &form) @@ -87,11 +86,11 @@ func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) { ctx.Redirect("/admin/users") } -func EditUser(ctx *middleware.Context, params martini.Params) { +func EditUser(ctx *middleware.Context) { ctx.Data["Title"] = "Edit Account" ctx.Data["PageIsUsers"] = true - uid, err := base.StrTo(params["userid"]).Int() + uid, err := com.StrTo(ctx.Params(":userid")).Int() if err != nil { ctx.Handle(404, "admin.user.EditUser", err) return @@ -113,11 +112,11 @@ func EditUser(ctx *middleware.Context, params martini.Params) { ctx.HTML(200, USER_EDIT) } -func EditUserPost(ctx *middleware.Context, params martini.Params, form auth.AdminEditUserForm) { +func EditUserPost(ctx *middleware.Context, form auth.AdminEditUserForm) { ctx.Data["Title"] = "Edit Account" ctx.Data["PageIsUsers"] = true - uid, err := base.StrTo(params["userid"]).Int() + uid, err := com.StrTo(ctx.Params(":userid")).Int() if err != nil { ctx.Handle(404, "admin.user.EditUserPost", err) return @@ -134,7 +133,7 @@ func EditUserPost(ctx *middleware.Context, params martini.Params, form auth.Admi return } - if (form.Passwd != "") { + if form.Passwd != "" { u.Passwd = form.Passwd u.Rands = models.GetUserSalt() u.Salt = models.GetUserSalt() @@ -157,15 +156,15 @@ func EditUserPost(ctx *middleware.Context, params martini.Params, form auth.Admi ctx.Data["User"] = u ctx.Flash.Success("Account profile has been successfully updated.") - ctx.Redirect("/admin/users/" + params["userid"]) + ctx.Redirect("/admin/users/" + ctx.Params(":userid")) } -func DeleteUser(ctx *middleware.Context, params martini.Params) { +func DeleteUser(ctx *middleware.Context) { ctx.Data["Title"] = "Delete Account" ctx.Data["PageIsUsers"] = true //log.Info("delete") - uid, err := base.StrTo(params["userid"]).Int() + uid, err := com.StrTo(ctx.Params(":userid")).Int() if err != nil { ctx.Handle(404, "admin.user.DeleteUser", err) return @@ -181,7 +180,7 @@ func DeleteUser(ctx *middleware.Context, params martini.Params) { switch err { case models.ErrUserOwnRepos: ctx.Flash.Error("This account still has ownership of repository, owner has to delete or transfer them first.") - ctx.Redirect("/admin/users/" + params["userid"]) + ctx.Redirect("/admin/users/" + ctx.Params(":userid")) default: ctx.Handle(500, "admin.user.DeleteUser", err) } diff --git a/routers/api/v1/users.go b/routers/api/v1/users.go index e4da60aa7b..fe67033748 100644 --- a/routers/api/v1/users.go +++ b/routers/api/v1/users.go @@ -5,8 +5,9 @@ package v1 import ( + "github.com/Unknwon/com" + "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" ) @@ -17,7 +18,7 @@ type user struct { func SearchUsers(ctx *middleware.Context) { q := ctx.Query("q") - limit, err := base.StrTo(ctx.Query("limit")).Int() + limit, err := com.StrTo(ctx.Query("limit")).Int() if err != nil { limit = 10 } diff --git a/routers/dev/debug.go b/routers/dev/debug.go index 62022146ad..6ef40a6226 100644 --- a/routers/dev/debug.go +++ b/routers/dev/debug.go @@ -7,10 +7,10 @@ package dev import (
"net/http/pprof"
- "github.com/go-martini/martini"
+ "github.com/Unknwon/macaron"
)
-func RegisterDebugRoutes(r martini.Router) {
+func RegisterDebugRoutes(r *macaron.Macaron) {
r.Any("/debug/pprof/cmdline", pprof.Cmdline)
r.Any("/debug/pprof/profile", pprof.Profile)
r.Any("/debug/pprof/symbol", pprof.Symbol)
diff --git a/routers/dev/template.go b/routers/dev/template.go index da477c94c0..e4f2429ae9 100644 --- a/routers/dev/template.go +++ b/routers/dev/template.go @@ -5,15 +5,13 @@ package dev import ( - "github.com/go-martini/martini" - "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/setting" ) -func TemplatePreview(ctx *middleware.Context, params martini.Params) { +func TemplatePreview(ctx *middleware.Context) { ctx.Data["User"] = models.User{Name: "Unknown"} ctx.Data["AppName"] = setting.AppName ctx.Data["AppVer"] = setting.AppVer @@ -23,5 +21,5 @@ func TemplatePreview(ctx *middleware.Context, params martini.Params) { ctx.Data["ActiveCodeLives"] = setting.Service.ActiveCodeLives / 60 ctx.Data["ResetPwdCodeLives"] = setting.Service.ResetPwdCodeLives / 60 ctx.Data["CurDbValue"] = "" - ctx.HTML(200, base.TplName(params["_1"])) + ctx.HTML(200, base.TplName(ctx.Params("*"))) } diff --git a/routers/dashboard.go b/routers/home.go index 4ef4e54f49..770aca034b 100644 --- a/routers/dashboard.go +++ b/routers/home.go @@ -5,7 +5,6 @@ package routers import ( - "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/setting" @@ -23,33 +22,17 @@ func Home(ctx *middleware.Context) { } // Check auto-login. - userName := ctx.GetCookie(setting.CookieUserName) - if len(userName) != 0 { + uname := ctx.GetCookie(setting.CookieUserName) + if len(uname) != 0 { ctx.Redirect("/user/login") return } ctx.Data["PageIsHome"] = true - - // Show recent updated repositories for new visitors. - repos, err := models.GetRecentUpdatedRepositories() - if err != nil { - ctx.Handle(500, "dashboard.Home(GetRecentUpdatedRepositories)", err) - return - } - - for _, repo := range repos { - if err = repo.GetOwner(); err != nil { - ctx.Handle(500, "dashboard.Home(GetOwner)", err) - return - } - } - ctx.Data["Repos"] = repos ctx.HTML(200, HOME) } func NotFound(ctx *middleware.Context) { ctx.Data["Title"] = "Page Not Found" - ctx.Data["PageIsNotFound"] = true ctx.Handle(404, "home.NotFound", nil) } diff --git a/routers/install.go b/routers/install.go index bb3c16eae4..516d11e4aa 100644 --- a/routers/install.go +++ b/routers/install.go @@ -11,8 +11,9 @@ import ( "path" "strings" + "github.com/Unknwon/com" "github.com/Unknwon/goconfig" - "github.com/go-martini/martini" + "github.com/Unknwon/macaron" "github.com/go-xorm/xorm" "github.com/gogits/gogs/models" @@ -33,12 +34,12 @@ const ( func checkRunMode() { switch setting.Cfg.MustValue("", "RUN_MODE") { case "prod": - martini.Env = martini.Prod + macaron.Env = macaron.PROD setting.ProdMode = true case "test": - martini.Env = martini.Test + macaron.Env = macaron.TEST } - log.Info("Run Mode: %s", strings.Title(martini.Env)) + log.Info("Run Mode: %s", strings.Title(macaron.Env)) } func NewServices() { @@ -59,7 +60,7 @@ func GlobalInit() { if setting.InstallLock { if err := models.NewEngine(); err != nil { - log.Fatal("Fail to initialize ORM engine: %v", err) + log.Fatal(4, "Fail to initialize ORM engine: %v", err) } models.HasEngine = true @@ -210,8 +211,8 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) { setting.Cfg.SetValue("mailer", "USER", form.SmtpEmail) setting.Cfg.SetValue("mailer", "PASSWD", form.SmtpPasswd) - setting.Cfg.SetValue("service", "REGISTER_EMAIL_CONFIRM", base.ToStr(form.RegisterConfirm == "on")) - setting.Cfg.SetValue("service", "ENABLE_NOTIFY_MAIL", base.ToStr(form.MailNotify == "on")) + setting.Cfg.SetValue("service", "REGISTER_EMAIL_CONFIRM", com.ToStr(form.RegisterConfirm == "on")) + setting.Cfg.SetValue("service", "ENABLE_NOTIFY_MAIL", com.ToStr(form.MailNotify == "on")) } setting.Cfg.SetValue("", "RUN_MODE", "prod") @@ -227,7 +228,7 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) { GlobalInit() // Create admin account. - if _, err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd, + if err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd, IsAdmin: true, IsActive: true}); err != nil { if err != models.ErrUserAlreadyExist { setting.InstallLock = false diff --git a/routers/org/org.go b/routers/org/org.go index 337fe89eb7..4c8854974d 100644 --- a/routers/org/org.go +++ b/routers/org/org.go @@ -7,7 +7,7 @@ package org import ( "github.com/go-martini/martini" - "github.com/gogits/gogs/models" + "github.com/gogits/gogs-ng/models" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" @@ -114,11 +114,11 @@ func Dashboard(ctx *middleware.Context, params martini.Params) { return } - if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) - return - } - ctx.Data["Orgs"] = ctx.User.Orgs + // if err := ctx.User.GetOrganizations(); err != nil { + // ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) + // return + // } + // ctx.Data["Orgs"] = ctx.User.Orgs ctx.Data["ContextUser"] = org ctx.Data["MyRepos"], err = models.GetRepositories(org.Id, true) diff --git a/routers/repo/branch.go b/routers/repo/branch.go index 9bad7289b9..c340b2bf6b 100644 --- a/routers/repo/branch.go +++ b/routers/repo/branch.go @@ -5,8 +5,6 @@ package repo import ( - "github.com/go-martini/martini" - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" ) @@ -15,7 +13,7 @@ const ( BRANCH base.TplName = "repo/branch" ) -func Branches(ctx *middleware.Context, params martini.Params) { +func Branches(ctx *middleware.Context) { ctx.Data["Title"] = "Branches" ctx.Data["IsRepoToolbarBranches"] = true diff --git a/routers/repo/commit.go b/routers/repo/commit.go index aa5c22e417..71b483823b 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -4,222 +4,224 @@ package repo -import ( - "path" - - "github.com/go-martini/martini" - - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/base" - "github.com/gogits/gogs/modules/middleware" -) - -const ( - COMMITS base.TplName = "repo/commits" - DIFF base.TplName = "repo/diff" -) - -func Commits(ctx *middleware.Context, params martini.Params) { - ctx.Data["IsRepoToolbarCommits"] = true - - userName := ctx.Repo.Owner.Name - repoName := ctx.Repo.Repository.Name - - brs, err := ctx.Repo.GitRepo.GetBranches() - if err != nil { - ctx.Handle(500, "repo.Commits(GetBranches)", err) - return - } else if len(brs) == 0 { - ctx.Handle(404, "repo.Commits(GetBranches)", nil) - return - } - - commitsCount, err := ctx.Repo.Commit.CommitsCount() - if err != nil { - ctx.Handle(500, "repo.Commits(GetCommitsCount)", err) - return - } - - // Calculate and validate page number. - page, _ := base.StrTo(ctx.Query("p")).Int() - if page < 1 { - page = 1 - } - lastPage := page - 1 - if lastPage < 0 { - lastPage = 0 - } - nextPage := page + 1 - if nextPage*50 > commitsCount { - nextPage = 0 - } - - // Both `git log branchName` and `git log commitId` work. - ctx.Data["Commits"], err = ctx.Repo.Commit.CommitsByRange(page) - if err != nil { - ctx.Handle(500, "repo.Commits(CommitsByRange)", err) - return - } - - ctx.Data["Username"] = userName - ctx.Data["Reponame"] = repoName - ctx.Data["CommitCount"] = commitsCount - ctx.Data["LastPageNum"] = lastPage - ctx.Data["NextPageNum"] = nextPage - ctx.HTML(200, COMMITS) -} - -func SearchCommits(ctx *middleware.Context, params martini.Params) { - ctx.Data["IsSearchPage"] = true - ctx.Data["IsRepoToolbarCommits"] = true - - keyword := ctx.Query("q") - if len(keyword) == 0 { - ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName) - return - } - - userName := params["username"] - repoName := params["reponame"] - - brs, err := ctx.Repo.GitRepo.GetBranches() - if err != nil { - ctx.Handle(500, "repo.SearchCommits(GetBranches)", err) - return - } else if len(brs) == 0 { - ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil) - return - } - - commits, err := ctx.Repo.Commit.SearchCommits(keyword) - if err != nil { - ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err) - return - } - - ctx.Data["Keyword"] = keyword - ctx.Data["Username"] = userName - ctx.Data["Reponame"] = repoName - ctx.Data["CommitCount"] = commits.Len() - ctx.Data["Commits"] = commits - ctx.HTML(200, COMMITS) -} - -func Diff(ctx *middleware.Context, params martini.Params) { - ctx.Data["IsRepoToolbarCommits"] = true - - userName := ctx.Repo.Owner.Name - repoName := ctx.Repo.Repository.Name - commitId := ctx.Repo.CommitId - - commit := ctx.Repo.Commit - - diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId) - if err != nil { - ctx.Handle(404, "repo.Diff(GetDiff)", err) - return - } - - isImageFile := func(name string) bool { - blob, err := ctx.Repo.Commit.GetBlobByPath(name) - if err != nil { - return false - } - - dataRc, err := blob.Data() - if err != nil { - return false - } - buf := make([]byte, 1024) - n, _ := dataRc.Read(buf) - if n > 0 { - buf = buf[:n] - } - dataRc.Close() - _, isImage := base.IsImageFile(buf) - return isImage - } - - parents := make([]string, commit.ParentCount()) - for i := 0; i < commit.ParentCount(); i++ { - sha, err := commit.ParentId(i) - parents[i] = sha.String() - if err != nil { - ctx.Handle(404, "repo.Diff", err) - return - } - } - - ctx.Data["Username"] = userName - ctx.Data["Reponame"] = repoName - ctx.Data["IsImageFile"] = isImageFile - ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitId) - ctx.Data["Commit"] = commit - ctx.Data["Diff"] = diff - ctx.Data["Parents"] = parents - ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0 - ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId) - ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId) - ctx.HTML(200, DIFF) -} - -func FileHistory(ctx *middleware.Context, params martini.Params) { - ctx.Data["IsRepoToolbarCommits"] = true - - fileName := params["_1"] - if len(fileName) == 0 { - Commits(ctx, params) - return - } - - userName := ctx.Repo.Owner.Name - repoName := ctx.Repo.Repository.Name - branchName := params["branchname"] - - brs, err := ctx.Repo.GitRepo.GetBranches() - if err != nil { - ctx.Handle(500, "repo.FileHistory", err) - return - } else if len(brs) == 0 { - ctx.Handle(404, "repo.FileHistory", nil) - return - } - - commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName) - if err != nil { - ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err) - return - } else if commitsCount == 0 { - ctx.Handle(404, "repo.FileHistory", nil) - return - } - - // Calculate and validate page number. - page, _ := base.StrTo(ctx.Query("p")).Int() - if page < 1 { - page = 1 - } - lastPage := page - 1 - if lastPage < 0 { - lastPage = 0 - } - nextPage := page + 1 - if nextPage*50 > commitsCount { - nextPage = 0 - } - - ctx.Data["Commits"], err = ctx.Repo.GitRepo.CommitsByFileAndRange( - branchName, fileName, page) - if err != nil { - ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err) - return - } - - ctx.Data["Username"] = userName - ctx.Data["Reponame"] = repoName - ctx.Data["FileName"] = fileName - ctx.Data["CommitCount"] = commitsCount - ctx.Data["LastPageNum"] = lastPage - ctx.Data["NextPageNum"] = nextPage - ctx.HTML(200, COMMITS) -} +// import ( +// "path" + +// "github.com/Unknwon/com" +// "github.com/go-martini/martini" + +// "github.com/gogits/gogs/models" +// "github.com/gogits/gogs/modules/base" +// "github.com/gogits/gogs/modules/middleware" +// ) + +// const ( +// COMMITS base.TplName = "repo/commits" +// DIFF base.TplName = "repo/diff" +// ) + +// func Commits(ctx *middleware.Context, params martini.Params) { +// ctx.Data["IsRepoToolbarCommits"] = true + +// userName := ctx.Repo.Owner.Name +// repoName := ctx.Repo.Repository.Name + +// brs, err := ctx.Repo.GitRepo.GetBranches() +// if err != nil { +// ctx.Handle(500, "repo.Commits(GetBranches)", err) +// return +// } else if len(brs) == 0 { +// ctx.Handle(404, "repo.Commits(GetBranches)", nil) +// return +// } + +// commitsCount, err := ctx.Repo.Commit.CommitsCount() +// if err != nil { +// ctx.Handle(500, "repo.Commits(GetCommitsCount)", err) +// return +// } + +// // Calculate and validate page number. +// page, _ := com.StrTo(ctx.Query("p")).Int() +// if page < 1 { +// page = 1 +// } +// lastPage := page - 1 +// if lastPage < 0 { +// lastPage = 0 +// } +// nextPage := page + 1 +// if nextPage*50 > commitsCount { +// nextPage = 0 +// } + +// // Both `git log branchName` and `git log commitId` work. +// // ctx.Data["Commits"], err = ctx.Repo.Commit.CommitsByRange(page) +// // if err != nil { +// // ctx.Handle(500, "repo.Commits(CommitsByRange)", err) +// // return +// // } + +// ctx.Data["Username"] = userName +// ctx.Data["Reponame"] = repoName +// ctx.Data["CommitCount"] = commitsCount +// ctx.Data["LastPageNum"] = lastPage +// ctx.Data["NextPageNum"] = nextPage +// ctx.HTML(200, COMMITS) +// } + +// func SearchCommits(ctx *middleware.Context, params martini.Params) { +// ctx.Data["IsSearchPage"] = true +// ctx.Data["IsRepoToolbarCommits"] = true + +// keyword := ctx.Query("q") +// if len(keyword) == 0 { +// ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName) +// return +// } + +// userName := params["username"] +// repoName := params["reponame"] + +// brs, err := ctx.Repo.GitRepo.GetBranches() +// if err != nil { +// ctx.Handle(500, "repo.SearchCommits(GetBranches)", err) +// return +// } else if len(brs) == 0 { +// ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil) +// return +// } + +// // commits, err := ctx.Repo.Commit.SearchCommits(keyword) +// // if err != nil { +// // ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err) +// // return +// // } + +// ctx.Data["Keyword"] = keyword +// ctx.Data["Username"] = userName +// ctx.Data["Reponame"] = repoName +// // ctx.Data["CommitCount"] = commits.Len() +// // ctx.Data["Commits"] = commits +// ctx.HTML(200, COMMITS) +// } + +// func Diff(ctx *middleware.Context, params martini.Params) { +// ctx.Data["IsRepoToolbarCommits"] = true + +// userName := ctx.Repo.Owner.Name +// repoName := ctx.Repo.Repository.Name +// commitId := ctx.Repo.CommitId + +// commit := ctx.Repo.Commit + +// diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId) +// if err != nil { +// ctx.Handle(404, "repo.Diff(GetDiff)", err) +// return +// } + +// isImageFile := func(name string) bool { +// // blob, err := ctx.Repo.Commit.GetBlobByPath(name) +// // if err != nil { +// // return false +// // } + +// // dataRc, err := blob.Data() +// // if err != nil { +// // return false +// // } +// // buf := make([]byte, 1024) +// // n, _ := dataRc.Read(buf) +// // if n > 0 { +// // buf = buf[:n] +// // } +// // dataRc.Close() +// // _, isImage := base.IsImageFile(buf) +// // return isImage +// return false +// } + +// parents := make([]string, commit.ParentCount()) +// for i := 0; i < commit.ParentCount(); i++ { +// sha, err := commit.ParentId(i) +// parents[i] = sha.String() +// if err != nil { +// ctx.Handle(404, "repo.Diff", err) +// return +// } +// } + +// ctx.Data["Username"] = userName +// ctx.Data["Reponame"] = repoName +// ctx.Data["IsImageFile"] = isImageFile +// ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitId) +// ctx.Data["Commit"] = commit +// ctx.Data["Diff"] = diff +// ctx.Data["Parents"] = parents +// ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0 +// ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId) +// ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId) +// ctx.HTML(200, DIFF) +// } + +// func FileHistory(ctx *middleware.Context, params martini.Params) { +// ctx.Data["IsRepoToolbarCommits"] = true + +// fileName := params["_1"] +// if len(fileName) == 0 { +// Commits(ctx, params) +// return +// } + +// userName := ctx.Repo.Owner.Name +// repoName := ctx.Repo.Repository.Name +// branchName := params["branchname"] + +// brs, err := ctx.Repo.GitRepo.GetBranches() +// if err != nil { +// ctx.Handle(500, "repo.FileHistory", err) +// return +// } else if len(brs) == 0 { +// ctx.Handle(404, "repo.FileHistory", nil) +// return +// } + +// // commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName) +// // if err != nil { +// // ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err) +// // return +// // } else if commitsCount == 0 { +// // ctx.Handle(404, "repo.FileHistory", nil) +// // return +// // } + +// // Calculate and validate page number. +// // page, _ := base.StrTo(ctx.Query("p")).Int() +// // if page < 1 { +// // page = 1 +// // } +// // lastPage := page - 1 +// // if lastPage < 0 { +// // lastPage = 0 +// // } +// // nextPage := page + 1 +// // if nextPage*50 > commitsCount { +// // nextPage = 0 +// // } + +// // ctx.Data["Commits"], err = ctx.Repo.GitRepo.CommitsByFileAndRange( +// // branchName, fileName, page) +// // if err != nil { +// // ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err) +// // return +// // } + +// ctx.Data["Username"] = userName +// ctx.Data["Reponame"] = repoName +// ctx.Data["FileName"] = fileName +// // ctx.Data["CommitCount"] = commitsCount +// // ctx.Data["LastPageNum"] = lastPage +// // ctx.Data["NextPageNum"] = nextPage +// ctx.HTML(200, COMMITS) +// } diff --git a/routers/repo/download.go b/routers/repo/download.go index 7e20b0e095..42bce2b1ff 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -5,101 +5,50 @@ package repo import ( - "io" - "os" - "path/filepath" + // "io" + // "os" + // "path/filepath" - "github.com/Unknwon/com" - "github.com/go-martini/martini" + // "github.com/Unknwon/com" - "github.com/gogits/git" + // "github.com/gogits/git" - "github.com/gogits/gogs/modules/base" + // "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" ) -func SingleDownload(ctx *middleware.Context, params martini.Params) { - treename := params["_1"] - - blob, err := ctx.Repo.Commit.GetBlobByPath(treename) - if err != nil { - ctx.Handle(500, "repo.SingleDownload(GetBlobByPath)", err) - return - } - - dataRc, err := blob.Data() - if err != nil { - ctx.Handle(500, "repo.SingleDownload(Data)", err) - return - } - - buf := make([]byte, 1024) - n, _ := dataRc.Read(buf) - if n > 0 { - buf = buf[:n] - } - - defer func() { - dataRc.Close() - }() - - contentType, isTextFile := base.IsTextFile(buf) - _, isImageFile := base.IsImageFile(buf) - ctx.Res.Header().Set("Content-Type", contentType) - if !isTextFile && !isImageFile { - ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(treename)) - ctx.Res.Header().Set("Content-Transfer-Encoding", "binary") - } - ctx.Res.Write(buf) - io.Copy(ctx.Res, dataRc) -} - -func ZipDownload(ctx *middleware.Context, params martini.Params) { - commitId := ctx.Repo.CommitId - archivesPath := filepath.Join(ctx.Repo.GitRepo.Path, "archives/zip") - if !com.IsDir(archivesPath) { - if err := os.MkdirAll(archivesPath, 0755); err != nil { - ctx.Handle(500, "ZipDownload -> os.Mkdir(archivesPath)", err) - return - } - } - - archivePath := filepath.Join(archivesPath, commitId+".zip") - - if com.IsFile(archivePath) { - ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".zip") - return - } - - if err := ctx.Repo.Commit.CreateArchive(archivePath, git.AT_ZIP); err != nil { - ctx.Handle(500, "ZipDownload -> CreateArchive "+archivePath, err) - return - } - - ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".zip") -} - -func TarGzDownload(ctx *middleware.Context, params martini.Params) { - commitId := ctx.Repo.CommitId - archivesPath := filepath.Join(ctx.Repo.GitRepo.Path, "archives/targz") - if !com.IsDir(archivesPath) { - if err := os.MkdirAll(archivesPath, 0755); err != nil { - ctx.Handle(500, "TarGzDownload -> os.Mkdir(archivesPath)", err) - return - } - } - - archivePath := filepath.Join(archivesPath, commitId+".tar.gz") - - if com.IsFile(archivePath) { - ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".tar.gz") - return - } - - if err := ctx.Repo.Commit.CreateArchive(archivePath, git.AT_TARGZ); err != nil { - ctx.Handle(500, "TarGzDownload -> CreateArchive "+archivePath, err) - return - } - - ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".tar.gz") +func SingleDownload(ctx *middleware.Context) { + // treename := params["_1"] + + // blob, err := ctx.Repo.Commit.GetBlobByPath(treename) + // if err != nil { + // ctx.Handle(500, "repo.SingleDownload(GetBlobByPath)", err) + // return + // } + + // dataRc, err := blob.Data() + // if err != nil { + // ctx.Handle(500, "repo.SingleDownload(Data)", err) + // return + // } + + // buf := make([]byte, 1024) + // n, _ := dataRc.Read(buf) + // if n > 0 { + // buf = buf[:n] + // } + + // defer func() { + // dataRc.Close() + // }() + + // contentType, isTextFile := base.IsTextFile(buf) + // _, isImageFile := base.IsImageFile(buf) + // ctx.Res.Header().Set("Content-Type", contentType) + // if !isTextFile && !isImageFile { + // ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(treename)) + // ctx.Res.Header().Set("Content-Transfer-Encoding", "binary") + // } + // ctx.Res.Write(buf) + // io.Copy(ctx.Res, dataRc) } diff --git a/routers/repo/http.go b/routers/repo/http.go index d22c0f8781..4ffe5ec345 100644 --- a/routers/repo/http.go +++ b/routers/repo/http.go @@ -6,6 +6,8 @@ package repo import ( "bytes" + "encoding/base64" + "errors" "fmt" "io" "io/ioutil" @@ -19,16 +21,43 @@ import ( "strings" "time" - "github.com/go-martini/martini" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/setting" ) -func Http(ctx *middleware.Context, params martini.Params) { - username := params["username"] - reponame := params["reponame"] +func basicEncode(username, password string) string { + auth := username + ":" + password + return base64.StdEncoding.EncodeToString([]byte(auth)) +} + +func basicDecode(encoded string) (user string, name string, err error) { + var s []byte + s, err = base64.StdEncoding.DecodeString(encoded) + if err != nil { + return user, name, err + } + + a := strings.Split(string(s), ":") + if len(a) == 2 { + user, name = a[0], a[1] + } else { + err = errors.New("decode failed") + } + return user, name, err +} + +func authRequired(ctx *middleware.Context) { + ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=\".\"") + ctx.Data["ErrorMsg"] = "no basic auth and digit auth" + ctx.HTML(401, base.TplName("status/401")) +} + +func Http(ctx *middleware.Context) { + username := ctx.Params(":username") + reponame := ctx.Params(":reponame") if strings.HasSuffix(reponame, ".git") { reponame = reponame[:len(reponame)-4] } @@ -50,7 +79,7 @@ func Http(ctx *middleware.Context, params martini.Params) { if err == models.ErrUserNotExist { ctx.Handle(404, "repo.Http(GetUserByName)", nil) } else { - ctx.Handle(500, "repo.Http(GetUserByName)", nil) + ctx.Handle(500, "repo.Http(GetUserByName)", err) } return } @@ -60,7 +89,7 @@ func Http(ctx *middleware.Context, params martini.Params) { if err == models.ErrRepoNotExist { ctx.Handle(404, "repo.Http(GetRepositoryByName)", nil) } else { - ctx.Handle(500, "repo.Http(GetRepositoryByName)", nil) + ctx.Handle(500, "repo.Http(GetRepositoryByName)", err) } return } @@ -75,7 +104,6 @@ func Http(ctx *middleware.Context, params martini.Params) { if askAuth { baHead := ctx.Req.Header.Get("Authorization") if baHead == "" { - // ask auth authRequired(ctx) return } @@ -142,7 +170,7 @@ func Http(ctx *middleware.Context, params martini.Params) { if head[0] == '0' && head[1] == '0' { size, err := strconv.ParseInt(string(input[lastLine+2:lastLine+4]), 16, 32) if err != nil { - log.Error("%v", err) + log.Error(4, "%v", err) return } @@ -166,7 +194,6 @@ func Http(ctx *middleware.Context, params martini.Params) { } lastLine = lastLine + size } else { - //fmt.Println("ddddddddddd") break } } @@ -176,7 +203,7 @@ func Http(ctx *middleware.Context, params martini.Params) { config := Config{setting.RepoRootPath, "git", true, true, f} handler := HttpBackend(&config) - handler(ctx.ResponseWriter, ctx.Req) + handler(ctx.Resp, ctx.Req) } type route struct { @@ -229,7 +256,7 @@ func HttpBackend(config *Config) http.HandlerFunc { dir, err := getGitDir(config, m[1]) if err != nil { - log.GitLogger.Error(err.Error()) + log.GitLogger.Error(4, err.Error()) renderNotFound(w) return } @@ -283,7 +310,7 @@ func serviceRpc(rpc string, hr handler) { err := cmd.Run() if err != nil { - log.GitLogger.Error(err.Error()) + log.GitLogger.Error(4, err.Error()) return } @@ -366,7 +393,7 @@ func getGitDir(config *Config, fPath string) (string, error) { cwd, err := os.Getwd() if err != nil { - log.GitLogger.Error(err.Error()) + log.GitLogger.Error(4, err.Error()) return "", err } @@ -443,7 +470,7 @@ func gitCommand(gitBinPath, dir string, args ...string) []byte { out, err := command.Output() if err != nil { - log.GitLogger.Error(err.Error()) + log.GitLogger.Error(4, err.Error()) } return out diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 4465a39957..005596bdeb 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -4,1114 +4,1114 @@ package repo -import ( - "errors" - "fmt" - "io" - "io/ioutil" - "mime" - "net/url" - "strings" - "time" - - "github.com/Unknwon/com" - "github.com/go-martini/martini" - - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/auth" - "github.com/gogits/gogs/modules/base" - "github.com/gogits/gogs/modules/log" - "github.com/gogits/gogs/modules/mailer" - "github.com/gogits/gogs/modules/middleware" - "github.com/gogits/gogs/modules/setting" -) - -const ( - ISSUES base.TplName = "repo/issue/list" - ISSUE_CREATE base.TplName = "repo/issue/create" - ISSUE_VIEW base.TplName = "repo/issue/view" - - MILESTONE base.TplName = "repo/issue/milestone" - MILESTONE_NEW base.TplName = "repo/issue/milestone_new" - MILESTONE_EDIT base.TplName = "repo/issue/milestone_edit" -) - -var ( - ErrFileTypeForbidden = errors.New("File type is not allowed") - ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded") -) - -func Issues(ctx *middleware.Context) { - ctx.Data["Title"] = "Issues" - ctx.Data["IsRepoToolbarIssues"] = true - ctx.Data["IsRepoToolbarIssuesList"] = true - - viewType := ctx.Query("type") - types := []string{"assigned", "created_by", "mentioned"} - if !com.IsSliceContainsStr(types, viewType) { - viewType = "all" - } - - isShowClosed := ctx.Query("state") == "closed" - - if viewType != "all" && !ctx.IsSigned { - ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI)) - ctx.Redirect("/user/login") - return - } - - var assigneeId, posterId int64 - var filterMode int - switch viewType { - case "assigned": - assigneeId = ctx.User.Id - filterMode = models.FM_ASSIGN - case "created_by": - posterId = ctx.User.Id - filterMode = models.FM_CREATE - case "mentioned": - filterMode = models.FM_MENTION - } - - var mid int64 - midx, _ := base.StrTo(ctx.Query("milestone")).Int64() - if midx > 0 { - mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, midx) - if err != nil { - ctx.Handle(500, "issue.Issues(GetMilestoneByIndex): %v", err) - return - } - mid = mile.Id - } - - selectLabels := ctx.Query("labels") - labels, err := models.GetLabels(ctx.Repo.Repository.Id) - if err != nil { - ctx.Handle(500, "issue.Issues(GetLabels): %v", err) - return - } - for _, l := range labels { - l.CalOpenIssues() - } - ctx.Data["Labels"] = labels - - page, _ := base.StrTo(ctx.Query("page")).Int() - - // Get issues. - issues, err := models.GetIssues(assigneeId, ctx.Repo.Repository.Id, posterId, mid, page, - isShowClosed, selectLabels, ctx.Query("sortType")) - if err != nil { - ctx.Handle(500, "issue.Issues(GetIssues): %v", err) - return - } - - // Get issue-user pairs. - pairs, err := models.GetIssueUserPairs(ctx.Repo.Repository.Id, posterId, isShowClosed) - if err != nil { - ctx.Handle(500, "issue.Issues(GetIssueUserPairs): %v", err) - return - } - - // Get posters. - for i := range issues { - if err = issues[i].GetLabels(); err != nil { - ctx.Handle(500, "issue.Issues(GetLabels)", fmt.Errorf("[#%d]%v", issues[i].Id, err)) - return - } - - idx := models.PairsContains(pairs, issues[i].Id) - - if filterMode == models.FM_MENTION && (idx == -1 || !pairs[idx].IsMentioned) { - continue - } - - if idx > -1 { - issues[i].IsRead = pairs[idx].IsRead - } else { - issues[i].IsRead = true - } - - if err = issues[i].GetPoster(); err != nil { - ctx.Handle(500, "issue.Issues(GetPoster)", fmt.Errorf("[#%d]%v", issues[i].Id, err)) - return - } - } - - var uid int64 = -1 - if ctx.User != nil { - uid = ctx.User.Id - } - issueStats := models.GetIssueStats(ctx.Repo.Repository.Id, uid, isShowClosed, filterMode) - ctx.Data["IssueStats"] = issueStats - ctx.Data["SelectLabels"], _ = base.StrTo(selectLabels).Int64() - ctx.Data["ViewType"] = viewType - ctx.Data["Issues"] = issues - ctx.Data["IsShowClosed"] = isShowClosed - if isShowClosed { - ctx.Data["State"] = "closed" - ctx.Data["ShowCount"] = issueStats.ClosedCount - } else { - ctx.Data["ShowCount"] = issueStats.OpenCount - } - ctx.HTML(200, ISSUES) -} - -func CreateIssue(ctx *middleware.Context, params martini.Params) { - ctx.Data["Title"] = "Create issue" - ctx.Data["IsRepoToolbarIssues"] = true - ctx.Data["IsRepoToolbarIssuesList"] = false - ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled - - var err error - // Get all milestones. - ctx.Data["OpenMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, false) - if err != nil { - ctx.Handle(500, "issue.ViewIssue(GetMilestones.1): %v", err) - return - } - ctx.Data["ClosedMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, true) - if err != nil { - ctx.Handle(500, "issue.ViewIssue(GetMilestones.2): %v", err) - return - } - - us, err := models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/")) - if err != nil { - ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err) - return - } - - ctx.Data["AllowedTypes"] = setting.AttachmentAllowedTypes - ctx.Data["Collaborators"] = us - - ctx.HTML(200, ISSUE_CREATE) -} - -func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { - send := func(status int, data interface{}, err error) { - if err != nil { - log.Error("issue.CreateIssuePost(?): %s", err.Error()) - - ctx.JSON(status, map[string]interface{}{ - "ok": false, - "status": status, - "error": err.Error(), - }) - } else { - ctx.JSON(status, map[string]interface{}{ - "ok": true, - "status": status, - "data": data, - }) - } - } - - var err error - // Get all milestones. - _, err = models.GetMilestones(ctx.Repo.Repository.Id, false) - if err != nil { - send(500, nil, err) - return - } - _, err = models.GetMilestones(ctx.Repo.Repository.Id, true) - if err != nil { - send(500, nil, err) - return - } - - _, err = models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/")) - if err != nil { - send(500, nil, err) - return - } - - if ctx.HasError() { - send(400, nil, errors.New(ctx.Flash.ErrorMsg)) - return - } - - // Only collaborators can assign. - if !ctx.Repo.IsOwner { - form.AssigneeId = 0 - } - issue := &models.Issue{ - RepoId: ctx.Repo.Repository.Id, - Index: int64(ctx.Repo.Repository.NumIssues) + 1, - Name: form.IssueName, - PosterId: ctx.User.Id, - MilestoneId: form.MilestoneId, - AssigneeId: form.AssigneeId, - LabelIds: form.Labels, - Content: form.Content, - } - if err := models.NewIssue(issue); err != nil { - send(500, nil, err) - return - } else if err := models.NewIssueUserPairs(issue.RepoId, issue.Id, ctx.Repo.Owner.Id, - ctx.User.Id, form.AssigneeId, ctx.Repo.Repository.Name); err != nil { - send(500, nil, err) - return - } - - if setting.AttachmentEnabled { - uploadFiles(ctx, issue.Id, 0) - } - - // Update mentions. - ms := base.MentionPattern.FindAllString(issue.Content, -1) - if len(ms) > 0 { - for i := range ms { - ms[i] = ms[i][1:] - } - - if err := models.UpdateMentions(ms, issue.Id); err != nil { - send(500, nil, err) - return - } - } - - act := &models.Action{ - ActUserId: ctx.User.Id, - ActUserName: ctx.User.Name, - ActEmail: ctx.User.Email, - OpType: models.OP_CREATE_ISSUE, - Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name), - RepoId: ctx.Repo.Repository.Id, - RepoUserName: ctx.Repo.Owner.Name, - RepoName: ctx.Repo.Repository.Name, - RefName: ctx.Repo.BranchName, - IsPrivate: ctx.Repo.Repository.IsPrivate, - } - // Notify watchers. - if err := models.NotifyWatchers(act); err != nil { - send(500, nil, err) - return - } - - // Mail watchers and mentions. - if setting.Service.EnableNotifyMail { - tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue) - if err != nil { - send(500, nil, err) - return - } - - tos = append(tos, ctx.User.LowerName) - newTos := make([]string, 0, len(ms)) - for _, m := range ms { - if com.IsSliceContainsStr(tos, m) { - continue - } - - newTos = append(newTos, m) - } - if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, - ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { - send(500, nil, err) - return - } - } - log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id) - - send(200, fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index), nil) -} - -func checkLabels(labels, allLabels []*models.Label) { - for _, l := range labels { - for _, l2 := range allLabels { - if l.Id == l2.Id { - l2.IsChecked = true - break - } - } - } -} - -func ViewIssue(ctx *middleware.Context, params martini.Params) { - ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled - - idx, _ := base.StrTo(params["index"]).Int64() - if idx == 0 { - ctx.Handle(404, "issue.ViewIssue", nil) - return - } - - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx) - if err != nil { - if err == models.ErrIssueNotExist { - ctx.Handle(404, "issue.ViewIssue(GetIssueByIndex)", err) - } else { - ctx.Handle(500, "issue.ViewIssue(GetIssueByIndex)", err) - } - return - } - - // Get labels. - if err = issue.GetLabels(); err != nil { - ctx.Handle(500, "issue.ViewIssue(GetLabels)", err) - return - } - labels, err := models.GetLabels(ctx.Repo.Repository.Id) - if err != nil { - ctx.Handle(500, "issue.ViewIssue(GetLabels.2)", err) - return - } - checkLabels(issue.Labels, labels) - ctx.Data["Labels"] = labels - - // Get assigned milestone. - if issue.MilestoneId > 0 { - ctx.Data["Milestone"], err = models.GetMilestoneById(issue.MilestoneId) - if err != nil { - if err == models.ErrMilestoneNotExist { - log.Warn("issue.ViewIssue(GetMilestoneById): %v", err) - } else { - ctx.Handle(500, "issue.ViewIssue(GetMilestoneById)", err) - return - } - } - } - - // Get all milestones. - ctx.Data["OpenMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, false) - if err != nil { - ctx.Handle(500, "issue.ViewIssue(GetMilestones.1): %v", err) - return - } - ctx.Data["ClosedMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, true) - if err != nil { - ctx.Handle(500, "issue.ViewIssue(GetMilestones.2): %v", err) - return - } - - // Get all collaborators. - ctx.Data["Collaborators"], err = models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/")) - if err != nil { - ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err) - return - } - - if ctx.IsSigned { - // Update issue-user. - if err = models.UpdateIssueUserPairByRead(ctx.User.Id, issue.Id); err != nil { - ctx.Handle(500, "issue.ViewIssue(UpdateIssueUserPairByRead): %v", err) - return - } - } - - // Get poster and Assignee. - if err = issue.GetPoster(); err != nil { - ctx.Handle(500, "issue.ViewIssue(GetPoster): %v", err) - return - } else if err = issue.GetAssignee(); err != nil { - ctx.Handle(500, "issue.ViewIssue(GetAssignee): %v", err) - return - } - issue.RenderedContent = string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)) - - // Get comments. - comments, err := models.GetIssueComments(issue.Id) - if err != nil { - ctx.Handle(500, "issue.ViewIssue(GetIssueComments): %v", err) - return - } - - // Get posters. - for i := range comments { - u, err := models.GetUserById(comments[i].PosterId) - if err != nil { - ctx.Handle(500, "issue.ViewIssue(GetUserById.2): %v", err) - return - } - comments[i].Poster = u - - if comments[i].Type == models.COMMENT { - comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), ctx.Repo.RepoLink)) - } - } - - ctx.Data["AllowedTypes"] = setting.AttachmentAllowedTypes - - ctx.Data["Title"] = issue.Name - ctx.Data["Issue"] = issue - ctx.Data["Comments"] = comments - ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner || (ctx.IsSigned && issue.PosterId == ctx.User.Id) - ctx.Data["IsRepoToolbarIssues"] = true - ctx.Data["IsRepoToolbarIssuesList"] = false - ctx.HTML(200, ISSUE_VIEW) -} - -func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { - idx, _ := base.StrTo(params["index"]).Int64() - if idx <= 0 { - ctx.Error(404) - return - } - - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx) - if err != nil { - if err == models.ErrIssueNotExist { - ctx.Handle(404, "issue.UpdateIssue", err) - } else { - ctx.Handle(500, "issue.UpdateIssue(GetIssueByIndex)", err) - } - return - } - - if ctx.User.Id != issue.PosterId && !ctx.Repo.IsOwner { - ctx.Error(403) - return - } - - issue.Name = form.IssueName - issue.MilestoneId = form.MilestoneId - issue.AssigneeId = form.AssigneeId - issue.LabelIds = form.Labels - issue.Content = form.Content - // try get content from text, ignore conflict with preview ajax - if form.Content == "" { - issue.Content = ctx.Query("text") - } - if err = models.UpdateIssue(issue); err != nil { - ctx.Handle(500, "issue.UpdateIssue(UpdateIssue)", err) - return - } - - ctx.JSON(200, map[string]interface{}{ - "ok": true, - "title": issue.Name, - "content": string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)), - }) -} - -func UpdateIssueLabel(ctx *middleware.Context, params martini.Params) { - if !ctx.Repo.IsOwner { - ctx.Error(403) - return - } - - idx, _ := base.StrTo(params["index"]).Int64() - if idx <= 0 { - ctx.Error(404) - return - } - - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx) - if err != nil { - if err == models.ErrIssueNotExist { - ctx.Handle(404, "issue.UpdateIssueLabel(GetIssueByIndex)", err) - } else { - ctx.Handle(500, "issue.UpdateIssueLabel(GetIssueByIndex)", err) - } - return - } - - isAttach := ctx.Query("action") == "attach" - labelStrId := ctx.Query("id") - labelId, _ := base.StrTo(labelStrId).Int64() - label, err := models.GetLabelById(labelId) - if err != nil { - if err == models.ErrLabelNotExist { - ctx.Handle(404, "issue.UpdateIssueLabel(GetLabelById)", err) - } else { - ctx.Handle(500, "issue.UpdateIssueLabel(GetLabelById)", err) - } - return - } - - isHad := strings.Contains(issue.LabelIds, "$"+labelStrId+"|") - isNeedUpdate := false - if isAttach { - if !isHad { - issue.LabelIds += "$" + labelStrId + "|" - isNeedUpdate = true - } - } else { - if isHad { - issue.LabelIds = strings.Replace(issue.LabelIds, "$"+labelStrId+"|", "", -1) - isNeedUpdate = true - } - } - - if isNeedUpdate { - if err = models.UpdateIssue(issue); err != nil { - ctx.Handle(500, "issue.UpdateIssueLabel(UpdateIssue)", err) - return - } - - if isAttach { - label.NumIssues++ - if issue.IsClosed { - label.NumClosedIssues++ - } - } else { - label.NumIssues-- - if issue.IsClosed { - label.NumClosedIssues-- - } - } - if err = models.UpdateLabel(label); err != nil { - ctx.Handle(500, "issue.UpdateIssueLabel(UpdateLabel)", err) - return - } - } - ctx.JSON(200, map[string]interface{}{ - "ok": true, - }) -} - -func UpdateIssueMilestone(ctx *middleware.Context) { - if !ctx.Repo.IsOwner { - ctx.Error(403) - return - } - - issueId, err := base.StrTo(ctx.Query("issue")).Int64() - if err != nil { - ctx.Error(404) - return - } - - issue, err := models.GetIssueById(issueId) - if err != nil { - if err == models.ErrIssueNotExist { - ctx.Handle(404, "issue.UpdateIssueMilestone(GetIssueById)", err) - } else { - ctx.Handle(500, "issue.UpdateIssueMilestone(GetIssueById)", err) - } - return - } - - oldMid := issue.MilestoneId - mid, _ := base.StrTo(ctx.Query("milestone")).Int64() - if oldMid == mid { - ctx.JSON(200, map[string]interface{}{ - "ok": true, - }) - return - } - - // Not check for invalid milestone id and give responsibility to owners. - issue.MilestoneId = mid - if err = models.ChangeMilestoneAssign(oldMid, mid, issue); err != nil { - ctx.Handle(500, "issue.UpdateIssueMilestone(ChangeMilestoneAssign)", err) - return - } else if err = models.UpdateIssue(issue); err != nil { - ctx.Handle(500, "issue.UpdateIssueMilestone(UpdateIssue)", err) - return - } - - ctx.JSON(200, map[string]interface{}{ - "ok": true, - }) -} - -func UpdateAssignee(ctx *middleware.Context) { - if !ctx.Repo.IsOwner { - ctx.Error(403) - return - } - - issueId, err := base.StrTo(ctx.Query("issue")).Int64() - if err != nil { - ctx.Error(404) - return - } - - issue, err := models.GetIssueById(issueId) - if err != nil { - if err == models.ErrIssueNotExist { - ctx.Handle(404, "issue.UpdateAssignee(GetIssueById)", err) - } else { - ctx.Handle(500, "issue.UpdateAssignee(GetIssueById)", err) - } - return - } - - aid, _ := base.StrTo(ctx.Query("assigneeid")).Int64() - // Not check for invalid assignne id and give responsibility to owners. - issue.AssigneeId = aid - if err = models.UpdateIssueUserPairByAssignee(aid, issue.Id); err != nil { - ctx.Handle(500, "issue.UpdateAssignee(UpdateIssueUserPairByAssignee): %v", err) - return - } else if err = models.UpdateIssue(issue); err != nil { - ctx.Handle(500, "issue.UpdateAssignee(UpdateIssue)", err) - return - } - - ctx.JSON(200, map[string]interface{}{ - "ok": true, - }) -} - -func uploadFiles(ctx *middleware.Context, issueId, commentId int64) { - if !setting.AttachmentEnabled { - return - } - - allowedTypes := strings.Split(setting.AttachmentAllowedTypes, "|") - attachments := ctx.Req.MultipartForm.File["attachments"] - - if len(attachments) > setting.AttachmentMaxFiles { - ctx.Handle(400, "issue.Comment", ErrTooManyFiles) - return - } - - for _, header := range attachments { - file, err := header.Open() - - if err != nil { - ctx.Handle(500, "issue.Comment(header.Open)", err) - return - } - - defer file.Close() - - allowed := false - fileType := mime.TypeByExtension(header.Filename) - - for _, t := range allowedTypes { - t := strings.Trim(t, " ") - - if t == "*/*" || t == fileType { - allowed = true - break - } - } - - if !allowed { - ctx.Handle(400, "issue.Comment", ErrFileTypeForbidden) - return - } - - out, err := ioutil.TempFile(setting.AttachmentPath, "attachment_") - - if err != nil { - ctx.Handle(500, "issue.Comment(ioutil.TempFile)", err) - return - } - - defer out.Close() - - _, err = io.Copy(out, file) - - if err != nil { - ctx.Handle(500, "issue.Comment(io.Copy)", err) - return - } - - _, err = models.CreateAttachment(issueId, commentId, header.Filename, out.Name()) - - if err != nil { - ctx.Handle(500, "issue.Comment(io.Copy)", err) - return - } - } -} - -func Comment(ctx *middleware.Context, params martini.Params) { - send := func(status int, data interface{}, err error) { - if err != nil { - log.Error("issue.Comment(?): %s", err.Error()) - - ctx.JSON(status, map[string]interface{}{ - "ok": false, - "status": status, - "error": err.Error(), - }) - } else { - ctx.JSON(status, map[string]interface{}{ - "ok": true, - "status": status, - "data": data, - }) - } - } - - index, err := base.StrTo(ctx.Query("issueIndex")).Int64() - if err != nil { - send(404, nil, err) - return - } - - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, index) - if err != nil { - if err == models.ErrIssueNotExist { - send(404, nil, err) - } else { - send(200, nil, err) - } - - return - } - - // Check if issue owner changes the status of issue. - var newStatus string - if ctx.Repo.IsOwner || issue.PosterId == ctx.User.Id { - newStatus = ctx.Query("change_status") - } - if len(newStatus) > 0 { - if (strings.Contains(newStatus, "Reopen") && issue.IsClosed) || - (strings.Contains(newStatus, "Close") && !issue.IsClosed) { - issue.IsClosed = !issue.IsClosed - if err = models.UpdateIssue(issue); err != nil { - send(500, nil, err) - return - } else if err = models.UpdateIssueUserPairsByStatus(issue.Id, issue.IsClosed); err != nil { - send(500, nil, err) - return - } - - // Change open/closed issue counter for the associated milestone - if issue.MilestoneId > 0 { - if err = models.ChangeMilestoneIssueStats(issue); err != nil { - send(500, nil, err) - } - } - - cmtType := models.CLOSE - if !issue.IsClosed { - cmtType = models.REOPEN - } - - if _, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, cmtType, "", nil); err != nil { - send(200, nil, err) - return - } - log.Trace("%s Issue(%d) status changed: %v", ctx.Req.RequestURI, issue.Id, !issue.IsClosed) - } - } - - var comment *models.Comment - - var ms []string - content := ctx.Query("content") - // Fix #321. Allow empty comments, as long as we have attachments. - if len(content) > 0 || len(ctx.Req.MultipartForm.File["attachments"]) > 0 { - switch params["action"] { - case "new": - if comment, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.COMMENT, content, nil); err != nil { - send(500, nil, err) - return - } - - // Update mentions. - ms = base.MentionPattern.FindAllString(issue.Content, -1) - if len(ms) > 0 { - for i := range ms { - ms[i] = ms[i][1:] - } - - if err := models.UpdateMentions(ms, issue.Id); err != nil { - send(500, nil, err) - return - } - } - - log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id) - default: - ctx.Handle(404, "issue.Comment", err) - return - } - } - - if comment != nil { - uploadFiles(ctx, issue.Id, comment.Id) - } - - // Notify watchers. - act := &models.Action{ - ActUserId: ctx.User.Id, - ActUserName: ctx.User.LowerName, - ActEmail: ctx.User.Email, - OpType: models.OP_COMMENT_ISSUE, - Content: fmt.Sprintf("%d|%s", issue.Index, strings.Split(content, "\n")[0]), - RepoId: ctx.Repo.Repository.Id, - RepoUserName: ctx.Repo.Owner.LowerName, - RepoName: ctx.Repo.Repository.LowerName, - } - if err = models.NotifyWatchers(act); err != nil { - send(500, nil, err) - return - } - - // Mail watchers and mentions. - if setting.Service.EnableNotifyMail { - issue.Content = content - tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue) - if err != nil { - send(500, nil, err) - return - } - - tos = append(tos, ctx.User.LowerName) - newTos := make([]string, 0, len(ms)) - for _, m := range ms { - if com.IsSliceContainsStr(tos, m) { - continue - } - - newTos = append(newTos, m) - } - if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, - ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { - send(500, nil, err) - return - } - } - - send(200, fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index), nil) -} - -func NewLabel(ctx *middleware.Context, form auth.CreateLabelForm) { - if ctx.HasError() { - Issues(ctx) - return - } - - l := &models.Label{ - RepoId: ctx.Repo.Repository.Id, - Name: form.Title, - Color: form.Color, - } - if err := models.NewLabel(l); err != nil { - ctx.Handle(500, "issue.NewLabel(NewLabel)", err) - return - } - ctx.Redirect(ctx.Repo.RepoLink + "/issues") -} - -func UpdateLabel(ctx *middleware.Context, params martini.Params, form auth.CreateLabelForm) { - id, _ := base.StrTo(ctx.Query("id")).Int64() - if id == 0 { - ctx.Error(404) - return - } - - l := &models.Label{ - Id: id, - Name: form.Title, - Color: form.Color, - } - if err := models.UpdateLabel(l); err != nil { - ctx.Handle(500, "issue.UpdateLabel(UpdateLabel)", err) - return - } - ctx.Redirect(ctx.Repo.RepoLink + "/issues") -} - -func DeleteLabel(ctx *middleware.Context) { - removes := ctx.Query("remove") - if len(strings.TrimSpace(removes)) == 0 { - ctx.JSON(200, map[string]interface{}{ - "ok": true, - }) - return - } - - strIds := strings.Split(removes, ",") - for _, strId := range strIds { - if err := models.DeleteLabel(ctx.Repo.Repository.Id, strId); err != nil { - ctx.Handle(500, "issue.DeleteLabel(DeleteLabel)", err) - return - } - } - - ctx.JSON(200, map[string]interface{}{ - "ok": true, - }) -} - -func Milestones(ctx *middleware.Context) { - ctx.Data["Title"] = "Milestones" - ctx.Data["IsRepoToolbarIssues"] = true - ctx.Data["IsRepoToolbarIssuesList"] = true - - isShowClosed := ctx.Query("state") == "closed" - - miles, err := models.GetMilestones(ctx.Repo.Repository.Id, isShowClosed) - if err != nil { - ctx.Handle(500, "issue.Milestones(GetMilestones)", err) - return - } - for _, m := range miles { - m.RenderedContent = string(base.RenderSpecialLink([]byte(m.Content), ctx.Repo.RepoLink)) - m.CalOpenIssues() - } - ctx.Data["Milestones"] = miles - - if isShowClosed { - ctx.Data["State"] = "closed" - } else { - ctx.Data["State"] = "open" - } - ctx.HTML(200, MILESTONE) -} - -func NewMilestone(ctx *middleware.Context) { - ctx.Data["Title"] = "New Milestone" - ctx.Data["IsRepoToolbarIssues"] = true - ctx.Data["IsRepoToolbarIssuesList"] = true - ctx.HTML(200, MILESTONE_NEW) -} - -func NewMilestonePost(ctx *middleware.Context, form auth.CreateMilestoneForm) { - ctx.Data["Title"] = "New Milestone" - ctx.Data["IsRepoToolbarIssues"] = true - ctx.Data["IsRepoToolbarIssuesList"] = true - - if ctx.HasError() { - ctx.HTML(200, MILESTONE_NEW) - return - } - - var deadline time.Time - var err error - if len(form.Deadline) == 0 { - form.Deadline = "12/31/9999" - } - deadline, err = time.Parse("01/02/2006", form.Deadline) - if err != nil { - ctx.Handle(500, "issue.NewMilestonePost(time.Parse)", err) - return - } - - mile := &models.Milestone{ - RepoId: ctx.Repo.Repository.Id, - Index: int64(ctx.Repo.Repository.NumMilestones) + 1, - Name: form.Title, - Content: form.Content, - Deadline: deadline, - } - if err = models.NewMilestone(mile); err != nil { - ctx.Handle(500, "issue.NewMilestonePost(NewMilestone)", err) - return - } - - ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones") -} - -func UpdateMilestone(ctx *middleware.Context, params martini.Params) { - ctx.Data["Title"] = "Update Milestone" - ctx.Data["IsRepoToolbarIssues"] = true - ctx.Data["IsRepoToolbarIssuesList"] = true - - idx, _ := base.StrTo(params["index"]).Int64() - if idx == 0 { - ctx.Handle(404, "issue.UpdateMilestone", nil) - return - } - - mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, idx) - if err != nil { - if err == models.ErrMilestoneNotExist { - ctx.Handle(404, "issue.UpdateMilestone(GetMilestoneByIndex)", err) - } else { - ctx.Handle(500, "issue.UpdateMilestone(GetMilestoneByIndex)", err) - } - return - } - - action := params["action"] - if len(action) > 0 { - switch action { - case "open": - if mile.IsClosed { - if err = models.ChangeMilestoneStatus(mile, false); err != nil { - ctx.Handle(500, "issue.UpdateMilestone(ChangeMilestoneStatus)", err) - return - } - } - case "close": - if !mile.IsClosed { - mile.ClosedDate = time.Now() - if err = models.ChangeMilestoneStatus(mile, true); err != nil { - ctx.Handle(500, "issue.UpdateMilestone(ChangeMilestoneStatus)", err) - return - } - } - case "delete": - if err = models.DeleteMilestone(mile); err != nil { - ctx.Handle(500, "issue.UpdateMilestone(DeleteMilestone)", err) - return - } - } - ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones") - return - } - - mile.DeadlineString = mile.Deadline.UTC().Format("01/02/2006") - if mile.DeadlineString == "12/31/9999" { - mile.DeadlineString = "" - } - ctx.Data["Milestone"] = mile - - ctx.HTML(200, MILESTONE_EDIT) -} - -func UpdateMilestonePost(ctx *middleware.Context, params martini.Params, form auth.CreateMilestoneForm) { - ctx.Data["Title"] = "Update Milestone" - ctx.Data["IsRepoToolbarIssues"] = true - ctx.Data["IsRepoToolbarIssuesList"] = true - - idx, _ := base.StrTo(params["index"]).Int64() - if idx == 0 { - ctx.Handle(404, "issue.UpdateMilestonePost", nil) - return - } - - mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, idx) - if err != nil { - if err == models.ErrMilestoneNotExist { - ctx.Handle(404, "issue.UpdateMilestonePost(GetMilestoneByIndex)", err) - } else { - ctx.Handle(500, "issue.UpdateMilestonePost(GetMilestoneByIndex)", err) - } - return - } - - if ctx.HasError() { - ctx.HTML(200, MILESTONE_EDIT) - return - } - - var deadline time.Time - if len(form.Deadline) == 0 { - form.Deadline = "12/31/9999" - } - deadline, err = time.Parse("01/02/2006", form.Deadline) - if err != nil { - ctx.Handle(500, "issue.UpdateMilestonePost(time.Parse)", err) - return - } - - mile.Name = form.Title - mile.Content = form.Content - mile.Deadline = deadline - if err = models.UpdateMilestone(mile); err != nil { - ctx.Handle(500, "issue.UpdateMilestonePost(UpdateMilestone)", err) - return - } - - ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones") -} - -func IssueGetAttachment(ctx *middleware.Context, params martini.Params) { - id, err := base.StrTo(params["id"]).Int64() - - if err != nil { - ctx.Handle(400, "issue.IssueGetAttachment(base.StrTo.Int64)", err) - return - } - - attachment, err := models.GetAttachmentById(id) - - if err != nil { - ctx.Handle(404, "issue.IssueGetAttachment(models.GetAttachmentById)", err) - return - } - - // Fix #312. Attachments with , in their name are not handled correctly by Google Chrome. - // We must put the name in " manually. - ctx.ServeFile(attachment.Path, "\""+attachment.Name+"\"") -} +// import ( +// "errors" +// // "fmt" +// // "io" +// // "io/ioutil" +// // "mime" +// "net/url" +// // "strings" +// // "time" + +// "github.com/Unknwon/com" +// "github.com/go-martini/martini" + +// // "github.com/gogits/gogs-ng/models" +// "github.com/gogits/gogs/modules/auth" +// "github.com/gogits/gogs/modules/base" +// // "github.com/gogits/gogs/modules/log" +// // "github.com/gogits/gogs/modules/mailer" +// "github.com/gogits/gogs/modules/middleware" +// // "github.com/gogits/gogs/modules/setting" +// ) + +// const ( +// ISSUES base.TplName = "repo/issue/list" +// ISSUE_CREATE base.TplName = "repo/issue/create" +// ISSUE_VIEW base.TplName = "repo/issue/view" + +// MILESTONE base.TplName = "repo/issue/milestone" +// MILESTONE_NEW base.TplName = "repo/issue/milestone_new" +// MILESTONE_EDIT base.TplName = "repo/issue/milestone_edit" +// ) + +// var ( +// ErrFileTypeForbidden = errors.New("File type is not allowed") +// ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded") +// ) + +// func Issues(ctx *middleware.Context) { +// ctx.Data["Title"] = "Issues" +// ctx.Data["IsRepoToolbarIssues"] = true +// ctx.Data["IsRepoToolbarIssuesList"] = true + +// viewType := ctx.Query("type") +// types := []string{"assigned", "created_by", "mentioned"} +// if !com.IsSliceContainsStr(types, viewType) { +// viewType = "all" +// } + +// isShowClosed := ctx.Query("state") == "closed" + +// if viewType != "all" && !ctx.IsSigned { +// ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI)) +// ctx.Redirect("/user/login") +// return +// } + +// // var assigneeId, posterId int64 +// // var filterMode int +// // switch viewType { +// // case "assigned": +// // assigneeId = ctx.User.Id +// // filterMode = models.FM_ASSIGN +// // case "created_by": +// // posterId = ctx.User.Id +// // filterMode = models.FM_CREATE +// // case "mentioned": +// // filterMode = models.FM_MENTION +// // } + +// // var mid int64 +// // midx, _ := com.StrTo(ctx.Query("milestone")).Int64() +// // if midx > 0 { +// // mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, midx) +// // if err != nil { +// // ctx.Handle(500, "issue.Issues(GetMilestoneByIndex): %v", err) +// // return +// // } +// // mid = mile.Id +// // } + +// // selectLabels := ctx.Query("labels") +// // labels, err := models.GetLabels(ctx.Repo.Repository.Id) +// // if err != nil { +// // ctx.Handle(500, "issue.Issues(GetLabels): %v", err) +// // return +// // } +// // for _, l := range labels { +// // l.CalOpenIssues() +// // } +// // ctx.Data["Labels"] = labels + +// page, _ := com.StrTo(ctx.Query("page")).Int() + +// // Get issues. +// // issues, err := models.GetIssues(assigneeId, ctx.Repo.Repository.Id, posterId, mid, page, +// // isShowClosed, selectLabels, ctx.Query("sortType")) +// // if err != nil { +// // ctx.Handle(500, "issue.Issues(GetIssues): %v", err) +// // return +// // } + +// // Get issue-user pairs. +// // pairs, err := models.GetIssueUserPairs(ctx.Repo.Repository.Id, posterId, isShowClosed) +// // if err != nil { +// // ctx.Handle(500, "issue.Issues(GetIssueUserPairs): %v", err) +// // return +// // } + +// // Get posters. +// // for i := range issues { +// // if err = issues[i].GetLabels(); err != nil { +// // ctx.Handle(500, "issue.Issues(GetLabels)", fmt.Errorf("[#%d]%v", issues[i].Id, err)) +// // return +// // } + +// // idx := models.PairsContains(pairs, issues[i].Id) + +// // if filterMode == models.FM_MENTION && (idx == -1 || !pairs[idx].IsMentioned) { +// // continue +// // } + +// // if idx > -1 { +// // issues[i].IsRead = pairs[idx].IsRead +// // } else { +// // issues[i].IsRead = true +// // } + +// // if err = issues[i].GetPoster(); err != nil { +// // ctx.Handle(500, "issue.Issues(GetPoster)", fmt.Errorf("[#%d]%v", issues[i].Id, err)) +// // return +// // } +// // } + +// // var uid int64 = -1 +// // if ctx.User != nil { +// // uid = ctx.User.Id +// // } +// // issueStats := models.GetIssueStats(ctx.Repo.Repository.Id, uid, isShowClosed, filterMode) +// // ctx.Data["IssueStats"] = issueStats +// // ctx.Data["SelectLabels"], _ = com.StrTo(selectLabels).Int64() +// // ctx.Data["ViewType"] = viewType +// // ctx.Data["Issues"] = issues +// // ctx.Data["IsShowClosed"] = isShowClosed +// // if isShowClosed { +// // ctx.Data["State"] = "closed" +// // ctx.Data["ShowCount"] = issueStats.ClosedCount +// // } else { +// // ctx.Data["ShowCount"] = issueStats.OpenCount +// // } +// // ctx.HTML(200, ISSUES) +// } + +// func CreateIssue(ctx *middleware.Context, params martini.Params) { +// // ctx.Data["Title"] = "Create issue" +// // ctx.Data["IsRepoToolbarIssues"] = true +// // ctx.Data["IsRepoToolbarIssuesList"] = false +// // ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled + +// // var err error +// // // Get all milestones. +// // ctx.Data["OpenMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, false) +// // if err != nil { +// // ctx.Handle(500, "issue.ViewIssue(GetMilestones.1): %v", err) +// // return +// // } +// // ctx.Data["ClosedMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, true) +// // if err != nil { +// // ctx.Handle(500, "issue.ViewIssue(GetMilestones.2): %v", err) +// // return +// // } + +// // us, err := models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/")) +// // if err != nil { +// // ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err) +// // return +// // } + +// // ctx.Data["AllowedTypes"] = setting.AttachmentAllowedTypes +// // ctx.Data["Collaborators"] = us + +// // ctx.HTML(200, ISSUE_CREATE) +// } + +// func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { +// // send := func(status int, data interface{}, err error) { +// // if err != nil { +// // log.Error(4, "issue.CreateIssuePost(?): %s", err.Error()) + +// // ctx.JSON(status, map[string]interface{}{ +// // "ok": false, +// // "status": status, +// // "error": err.Error(), +// // }) +// // } else { +// // ctx.JSON(status, map[string]interface{}{ +// // "ok": true, +// // "status": status, +// // "data": data, +// // }) +// // } +// // } + +// // var err error +// // // Get all milestones. +// // _, err = models.GetMilestones(ctx.Repo.Repository.Id, false) +// // if err != nil { +// // send(500, nil, err) +// // return +// // } +// // _, err = models.GetMilestones(ctx.Repo.Repository.Id, true) +// // if err != nil { +// // send(500, nil, err) +// // return +// // } + +// // _, err = models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/")) +// // if err != nil { +// // send(500, nil, err) +// // return +// // } + +// // if ctx.HasError() { +// // send(400, nil, errors.New(ctx.Flash.ErrorMsg)) +// // return +// // } + +// // // Only collaborators can assign. +// // if !ctx.Repo.IsOwner { +// // form.AssigneeId = 0 +// // } +// // issue := &models.Issue{ +// // RepoId: ctx.Repo.Repository.Id, +// // Index: int64(ctx.Repo.Repository.NumIssues) + 1, +// // Name: form.IssueName, +// // PosterId: ctx.User.Id, +// // MilestoneId: form.MilestoneId, +// // AssigneeId: form.AssigneeId, +// // LabelIds: form.Labels, +// // Content: form.Content, +// // } +// // if err := models.NewIssue(issue); err != nil { +// // send(500, nil, err) +// // return +// // } else if err := models.NewIssueUserPairs(issue.RepoId, issue.Id, ctx.Repo.Owner.Id, +// // ctx.User.Id, form.AssigneeId, ctx.Repo.Repository.Name); err != nil { +// // send(500, nil, err) +// // return +// // } + +// // if setting.AttachmentEnabled { +// // uploadFiles(ctx, issue.Id, 0) +// // } + +// // // Update mentions. +// // ms := base.MentionPattern.FindAllString(issue.Content, -1) +// // if len(ms) > 0 { +// // for i := range ms { +// // ms[i] = ms[i][1:] +// // } + +// // if err := models.UpdateMentions(ms, issue.Id); err != nil { +// // send(500, nil, err) +// // return +// // } +// // } + +// // act := &models.Action{ +// // ActUserId: ctx.User.Id, +// // ActUserName: ctx.User.Name, +// // ActEmail: ctx.User.Email, +// // OpType: models.OP_CREATE_ISSUE, +// // Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name), +// // RepoId: ctx.Repo.Repository.Id, +// // RepoUserName: ctx.Repo.Owner.Name, +// // RepoName: ctx.Repo.Repository.Name, +// // RefName: ctx.Repo.BranchName, +// // IsPrivate: ctx.Repo.Repository.IsPrivate, +// // } +// // // Notify watchers. +// // if err := models.NotifyWatchers(act); err != nil { +// // send(500, nil, err) +// // return +// // } + +// // // Mail watchers and mentions. +// // if setting.Service.EnableNotifyMail { +// // tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue) +// // if err != nil { +// // send(500, nil, err) +// // return +// // } + +// // tos = append(tos, ctx.User.LowerName) +// // newTos := make([]string, 0, len(ms)) +// // for _, m := range ms { +// // if com.IsSliceContainsStr(tos, m) { +// // continue +// // } + +// // newTos = append(newTos, m) +// // } +// // if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, +// // ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { +// // send(500, nil, err) +// // return +// // } +// // } +// // log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id) + +// // send(200, fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index), nil) +// } + +// // func checkLabels(labels, allLabels []*models.Label) { +// // for _, l := range labels { +// // for _, l2 := range allLabels { +// // if l.Id == l2.Id { +// // l2.IsChecked = true +// // break +// // } +// // } +// // } +// // } + +// // func ViewIssue(ctx *middleware.Context, params martini.Params) { +// // ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled + +// // idx, _ := base.StrTo(params["index"]).Int64() +// // if idx == 0 { +// // ctx.Handle(404, "issue.ViewIssue", nil) +// // return +// // } + +// // issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx) +// // if err != nil { +// // if err == models.ErrIssueNotExist { +// // ctx.Handle(404, "issue.ViewIssue(GetIssueByIndex)", err) +// // } else { +// // ctx.Handle(500, "issue.ViewIssue(GetIssueByIndex)", err) +// // } +// // return +// // } + +// // // Get labels. +// // if err = issue.GetLabels(); err != nil { +// // ctx.Handle(500, "issue.ViewIssue(GetLabels)", err) +// // return +// // } +// // labels, err := models.GetLabels(ctx.Repo.Repository.Id) +// // if err != nil { +// // ctx.Handle(500, "issue.ViewIssue(GetLabels.2)", err) +// // return +// // } +// // checkLabels(issue.Labels, labels) +// // ctx.Data["Labels"] = labels + +// // // Get assigned milestone. +// // if issue.MilestoneId > 0 { +// // ctx.Data["Milestone"], err = models.GetMilestoneById(issue.MilestoneId) +// // if err != nil { +// // if err == models.ErrMilestoneNotExist { +// // log.Warn("issue.ViewIssue(GetMilestoneById): %v", err) +// // } else { +// // ctx.Handle(500, "issue.ViewIssue(GetMilestoneById)", err) +// // return +// // } +// // } +// // } + +// // // Get all milestones. +// // ctx.Data["OpenMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, false) +// // if err != nil { +// // ctx.Handle(500, "issue.ViewIssue(GetMilestones.1): %v", err) +// // return +// // } +// // ctx.Data["ClosedMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, true) +// // if err != nil { +// // ctx.Handle(500, "issue.ViewIssue(GetMilestones.2): %v", err) +// // return +// // } + +// // // Get all collaborators. +// // ctx.Data["Collaborators"], err = models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/")) +// // if err != nil { +// // ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err) +// // return +// // } + +// // if ctx.IsSigned { +// // // Update issue-user. +// // if err = models.UpdateIssueUserPairByRead(ctx.User.Id, issue.Id); err != nil { +// // ctx.Handle(500, "issue.ViewIssue(UpdateIssueUserPairByRead): %v", err) +// // return +// // } +// // } + +// // // Get poster and Assignee. +// // if err = issue.GetPoster(); err != nil { +// // ctx.Handle(500, "issue.ViewIssue(GetPoster): %v", err) +// // return +// // } else if err = issue.GetAssignee(); err != nil { +// // ctx.Handle(500, "issue.ViewIssue(GetAssignee): %v", err) +// // return +// // } +// // issue.RenderedContent = string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)) + +// // // Get comments. +// // comments, err := models.GetIssueComments(issue.Id) +// // if err != nil { +// // ctx.Handle(500, "issue.ViewIssue(GetIssueComments): %v", err) +// // return +// // } + +// // // Get posters. +// // for i := range comments { +// // u, err := models.GetUserById(comments[i].PosterId) +// // if err != nil { +// // ctx.Handle(500, "issue.ViewIssue(GetUserById.2): %v", err) +// // return +// // } +// // comments[i].Poster = u + +// // if comments[i].Type == models.COMMENT { +// // comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), ctx.Repo.RepoLink)) +// // } +// // } + +// // ctx.Data["AllowedTypes"] = setting.AttachmentAllowedTypes + +// // ctx.Data["Title"] = issue.Name +// // ctx.Data["Issue"] = issue +// // ctx.Data["Comments"] = comments +// // ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner || (ctx.IsSigned && issue.PosterId == ctx.User.Id) +// // ctx.Data["IsRepoToolbarIssues"] = true +// // ctx.Data["IsRepoToolbarIssuesList"] = false +// // ctx.HTML(200, ISSUE_VIEW) +// // } + +// // func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { +// // idx, _ := base.StrTo(params["index"]).Int64() +// // if idx <= 0 { +// // ctx.Error(404) +// // return +// // } + +// // issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx) +// // if err != nil { +// // if err == models.ErrIssueNotExist { +// // ctx.Handle(404, "issue.UpdateIssue", err) +// // } else { +// // ctx.Handle(500, "issue.UpdateIssue(GetIssueByIndex)", err) +// // } +// // return +// // } + +// // if ctx.User.Id != issue.PosterId && !ctx.Repo.IsOwner { +// // ctx.Error(403) +// // return +// // } + +// // issue.Name = form.IssueName +// // issue.MilestoneId = form.MilestoneId +// // issue.AssigneeId = form.AssigneeId +// // issue.LabelIds = form.Labels +// // issue.Content = form.Content +// // // try get content from text, ignore conflict with preview ajax +// // if form.Content == "" { +// // issue.Content = ctx.Query("text") +// // } +// // if err = models.UpdateIssue(issue); err != nil { +// // ctx.Handle(500, "issue.UpdateIssue(UpdateIssue)", err) +// // return +// // } + +// // ctx.JSON(200, map[string]interface{}{ +// // "ok": true, +// // "title": issue.Name, +// // "content": string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)), +// // }) +// // } + +// // func UpdateIssueLabel(ctx *middleware.Context, params martini.Params) { +// // if !ctx.Repo.IsOwner { +// // ctx.Error(403) +// // return +// // } + +// // idx, _ := base.StrTo(params["index"]).Int64() +// // if idx <= 0 { +// // ctx.Error(404) +// // return +// // } + +// // issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx) +// // if err != nil { +// // if err == models.ErrIssueNotExist { +// // ctx.Handle(404, "issue.UpdateIssueLabel(GetIssueByIndex)", err) +// // } else { +// // ctx.Handle(500, "issue.UpdateIssueLabel(GetIssueByIndex)", err) +// // } +// // return +// // } + +// // isAttach := ctx.Query("action") == "attach" +// // labelStrId := ctx.Query("id") +// // labelId, _ := base.StrTo(labelStrId).Int64() +// // label, err := models.GetLabelById(labelId) +// // if err != nil { +// // if err == models.ErrLabelNotExist { +// // ctx.Handle(404, "issue.UpdateIssueLabel(GetLabelById)", err) +// // } else { +// // ctx.Handle(500, "issue.UpdateIssueLabel(GetLabelById)", err) +// // } +// // return +// // } + +// // isHad := strings.Contains(issue.LabelIds, "$"+labelStrId+"|") +// // isNeedUpdate := false +// // if isAttach { +// // if !isHad { +// // issue.LabelIds += "$" + labelStrId + "|" +// // isNeedUpdate = true +// // } +// // } else { +// // if isHad { +// // issue.LabelIds = strings.Replace(issue.LabelIds, "$"+labelStrId+"|", "", -1) +// // isNeedUpdate = true +// // } +// // } + +// // if isNeedUpdate { +// // if err = models.UpdateIssue(issue); err != nil { +// // ctx.Handle(500, "issue.UpdateIssueLabel(UpdateIssue)", err) +// // return +// // } + +// // if isAttach { +// // label.NumIssues++ +// // if issue.IsClosed { +// // label.NumClosedIssues++ +// // } +// // } else { +// // label.NumIssues-- +// // if issue.IsClosed { +// // label.NumClosedIssues-- +// // } +// // } +// // if err = models.UpdateLabel(label); err != nil { +// // ctx.Handle(500, "issue.UpdateIssueLabel(UpdateLabel)", err) +// // return +// // } +// // } +// // ctx.JSON(200, map[string]interface{}{ +// // "ok": true, +// // }) +// // } + +// // func UpdateIssueMilestone(ctx *middleware.Context) { +// // if !ctx.Repo.IsOwner { +// // ctx.Error(403) +// // return +// // } + +// // issueId, err := base.StrTo(ctx.Query("issue")).Int64() +// // if err != nil { +// // ctx.Error(404) +// // return +// // } + +// // issue, err := models.GetIssueById(issueId) +// // if err != nil { +// // if err == models.ErrIssueNotExist { +// // ctx.Handle(404, "issue.UpdateIssueMilestone(GetIssueById)", err) +// // } else { +// // ctx.Handle(500, "issue.UpdateIssueMilestone(GetIssueById)", err) +// // } +// // return +// // } + +// // oldMid := issue.MilestoneId +// // mid, _ := base.StrTo(ctx.Query("milestone")).Int64() +// // if oldMid == mid { +// // ctx.JSON(200, map[string]interface{}{ +// // "ok": true, +// // }) +// // return +// // } + +// // // Not check for invalid milestone id and give responsibility to owners. +// // issue.MilestoneId = mid +// // if err = models.ChangeMilestoneAssign(oldMid, mid, issue); err != nil { +// // ctx.Handle(500, "issue.UpdateIssueMilestone(ChangeMilestoneAssign)", err) +// // return +// // } else if err = models.UpdateIssue(issue); err != nil { +// // ctx.Handle(500, "issue.UpdateIssueMilestone(UpdateIssue)", err) +// // return +// // } + +// // ctx.JSON(200, map[string]interface{}{ +// // "ok": true, +// // }) +// // } + +// // func UpdateAssignee(ctx *middleware.Context) { +// // if !ctx.Repo.IsOwner { +// // ctx.Error(403) +// // return +// // } + +// // issueId, err := base.StrTo(ctx.Query("issue")).Int64() +// // if err != nil { +// // ctx.Error(404) +// // return +// // } + +// // issue, err := models.GetIssueById(issueId) +// // if err != nil { +// // if err == models.ErrIssueNotExist { +// // ctx.Handle(404, "issue.UpdateAssignee(GetIssueById)", err) +// // } else { +// // ctx.Handle(500, "issue.UpdateAssignee(GetIssueById)", err) +// // } +// // return +// // } + +// // aid, _ := base.StrTo(ctx.Query("assigneeid")).Int64() +// // // Not check for invalid assignne id and give responsibility to owners. +// // issue.AssigneeId = aid +// // if err = models.UpdateIssueUserPairByAssignee(aid, issue.Id); err != nil { +// // ctx.Handle(500, "issue.UpdateAssignee(UpdateIssueUserPairByAssignee): %v", err) +// // return +// // } else if err = models.UpdateIssue(issue); err != nil { +// // ctx.Handle(500, "issue.UpdateAssignee(UpdateIssue)", err) +// // return +// // } + +// // ctx.JSON(200, map[string]interface{}{ +// // "ok": true, +// // }) +// // } + +// // func uploadFiles(ctx *middleware.Context, issueId, commentId int64) { +// // if !setting.AttachmentEnabled { +// // return +// // } + +// // allowedTypes := strings.Split(setting.AttachmentAllowedTypes, "|") +// // attachments := ctx.Req.MultipartForm.File["attachments"] + +// // if len(attachments) > setting.AttachmentMaxFiles { +// // ctx.Handle(400, "issue.Comment", ErrTooManyFiles) +// // return +// // } + +// // for _, header := range attachments { +// // file, err := header.Open() + +// // if err != nil { +// // ctx.Handle(500, "issue.Comment(header.Open)", err) +// // return +// // } + +// // defer file.Close() + +// // allowed := false +// // fileType := mime.TypeByExtension(header.Filename) + +// // for _, t := range allowedTypes { +// // t := strings.Trim(t, " ") + +// // if t == "*/*" || t == fileType { +// // allowed = true +// // break +// // } +// // } + +// // if !allowed { +// // ctx.Handle(400, "issue.Comment", ErrFileTypeForbidden) +// // return +// // } + +// // out, err := ioutil.TempFile(setting.AttachmentPath, "attachment_") + +// // if err != nil { +// // ctx.Handle(500, "issue.Comment(ioutil.TempFile)", err) +// // return +// // } + +// // defer out.Close() + +// // _, err = io.Copy(out, file) + +// // if err != nil { +// // ctx.Handle(500, "issue.Comment(io.Copy)", err) +// // return +// // } + +// // _, err = models.CreateAttachment(issueId, commentId, header.Filename, out.Name()) + +// // if err != nil { +// // ctx.Handle(500, "issue.Comment(io.Copy)", err) +// // return +// // } +// // } +// // } + +// // func Comment(ctx *middleware.Context, params martini.Params) { +// // send := func(status int, data interface{}, err error) { +// // if err != nil { +// // log.Error("issue.Comment(?): %s", err.Error()) + +// // ctx.JSON(status, map[string]interface{}{ +// // "ok": false, +// // "status": status, +// // "error": err.Error(), +// // }) +// // } else { +// // ctx.JSON(status, map[string]interface{}{ +// // "ok": true, +// // "status": status, +// // "data": data, +// // }) +// // } +// // } + +// // index, err := base.StrTo(ctx.Query("issueIndex")).Int64() +// // if err != nil { +// // send(404, nil, err) +// // return +// // } + +// // issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, index) +// // if err != nil { +// // if err == models.ErrIssueNotExist { +// // send(404, nil, err) +// // } else { +// // send(200, nil, err) +// // } + +// // return +// // } + +// // // Check if issue owner changes the status of issue. +// // var newStatus string +// // if ctx.Repo.IsOwner || issue.PosterId == ctx.User.Id { +// // newStatus = ctx.Query("change_status") +// // } +// // if len(newStatus) > 0 { +// // if (strings.Contains(newStatus, "Reopen") && issue.IsClosed) || +// // (strings.Contains(newStatus, "Close") && !issue.IsClosed) { +// // issue.IsClosed = !issue.IsClosed +// // if err = models.UpdateIssue(issue); err != nil { +// // send(500, nil, err) +// // return +// // } else if err = models.UpdateIssueUserPairsByStatus(issue.Id, issue.IsClosed); err != nil { +// // send(500, nil, err) +// // return +// // } + +// // // Change open/closed issue counter for the associated milestone +// // if issue.MilestoneId > 0 { +// // if err = models.ChangeMilestoneIssueStats(issue); err != nil { +// // send(500, nil, err) +// // } +// // } + +// // cmtType := models.CLOSE +// // if !issue.IsClosed { +// // cmtType = models.REOPEN +// // } + +// // if _, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, cmtType, "", nil); err != nil { +// // send(200, nil, err) +// // return +// // } +// // log.Trace("%s Issue(%d) status changed: %v", ctx.Req.RequestURI, issue.Id, !issue.IsClosed) +// // } +// // } + +// // var comment *models.Comment + +// // var ms []string +// // content := ctx.Query("content") +// // // Fix #321. Allow empty comments, as long as we have attachments. +// // if len(content) > 0 || len(ctx.Req.MultipartForm.File["attachments"]) > 0 { +// // switch params["action"] { +// // case "new": +// // if comment, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.COMMENT, content, nil); err != nil { +// // send(500, nil, err) +// // return +// // } + +// // // Update mentions. +// // ms = base.MentionPattern.FindAllString(issue.Content, -1) +// // if len(ms) > 0 { +// // for i := range ms { +// // ms[i] = ms[i][1:] +// // } + +// // if err := models.UpdateMentions(ms, issue.Id); err != nil { +// // send(500, nil, err) +// // return +// // } +// // } + +// // log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id) +// // default: +// // ctx.Handle(404, "issue.Comment", err) +// // return +// // } +// // } + +// // if comment != nil { +// // uploadFiles(ctx, issue.Id, comment.Id) +// // } + +// // // Notify watchers. +// // act := &models.Action{ +// // ActUserId: ctx.User.Id, +// // ActUserName: ctx.User.LowerName, +// // ActEmail: ctx.User.Email, +// // OpType: models.OP_COMMENT_ISSUE, +// // Content: fmt.Sprintf("%d|%s", issue.Index, strings.Split(content, "\n")[0]), +// // RepoId: ctx.Repo.Repository.Id, +// // RepoUserName: ctx.Repo.Owner.LowerName, +// // RepoName: ctx.Repo.Repository.LowerName, +// // } +// // if err = models.NotifyWatchers(act); err != nil { +// // send(500, nil, err) +// // return +// // } + +// // // Mail watchers and mentions. +// // if setting.Service.EnableNotifyMail { +// // issue.Content = content +// // tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue) +// // if err != nil { +// // send(500, nil, err) +// // return +// // } + +// // tos = append(tos, ctx.User.LowerName) +// // newTos := make([]string, 0, len(ms)) +// // for _, m := range ms { +// // if com.IsSliceContainsStr(tos, m) { +// // continue +// // } + +// // newTos = append(newTos, m) +// // } +// // if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, +// // ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { +// // send(500, nil, err) +// // return +// // } +// // } + +// // send(200, fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index), nil) +// // } + +// // func NewLabel(ctx *middleware.Context, form auth.CreateLabelForm) { +// // if ctx.HasError() { +// // Issues(ctx) +// // return +// // } + +// // l := &models.Label{ +// // RepoId: ctx.Repo.Repository.Id, +// // Name: form.Title, +// // Color: form.Color, +// // } +// // if err := models.NewLabel(l); err != nil { +// // ctx.Handle(500, "issue.NewLabel(NewLabel)", err) +// // return +// // } +// // ctx.Redirect(ctx.Repo.RepoLink + "/issues") +// // } + +// // func UpdateLabel(ctx *middleware.Context, params martini.Params, form auth.CreateLabelForm) { +// // id, _ := base.StrTo(ctx.Query("id")).Int64() +// // if id == 0 { +// // ctx.Error(404) +// // return +// // } + +// // l := &models.Label{ +// // Id: id, +// // Name: form.Title, +// // Color: form.Color, +// // } +// // if err := models.UpdateLabel(l); err != nil { +// // ctx.Handle(500, "issue.UpdateLabel(UpdateLabel)", err) +// // return +// // } +// // ctx.Redirect(ctx.Repo.RepoLink + "/issues") +// // } + +// // func DeleteLabel(ctx *middleware.Context) { +// // removes := ctx.Query("remove") +// // if len(strings.TrimSpace(removes)) == 0 { +// // ctx.JSON(200, map[string]interface{}{ +// // "ok": true, +// // }) +// // return +// // } + +// // strIds := strings.Split(removes, ",") +// // for _, strId := range strIds { +// // if err := models.DeleteLabel(ctx.Repo.Repository.Id, strId); err != nil { +// // ctx.Handle(500, "issue.DeleteLabel(DeleteLabel)", err) +// // return +// // } +// // } + +// // ctx.JSON(200, map[string]interface{}{ +// // "ok": true, +// // }) +// // } + +// // func Milestones(ctx *middleware.Context) { +// // ctx.Data["Title"] = "Milestones" +// // ctx.Data["IsRepoToolbarIssues"] = true +// // ctx.Data["IsRepoToolbarIssuesList"] = true + +// // isShowClosed := ctx.Query("state") == "closed" + +// // miles, err := models.GetMilestones(ctx.Repo.Repository.Id, isShowClosed) +// // if err != nil { +// // ctx.Handle(500, "issue.Milestones(GetMilestones)", err) +// // return +// // } +// // for _, m := range miles { +// // m.RenderedContent = string(base.RenderSpecialLink([]byte(m.Content), ctx.Repo.RepoLink)) +// // m.CalOpenIssues() +// // } +// // ctx.Data["Milestones"] = miles + +// // if isShowClosed { +// // ctx.Data["State"] = "closed" +// // } else { +// // ctx.Data["State"] = "open" +// // } +// // ctx.HTML(200, MILESTONE) +// // } + +// // func NewMilestone(ctx *middleware.Context) { +// // ctx.Data["Title"] = "New Milestone" +// // ctx.Data["IsRepoToolbarIssues"] = true +// // ctx.Data["IsRepoToolbarIssuesList"] = true +// // ctx.HTML(200, MILESTONE_NEW) +// // } + +// // func NewMilestonePost(ctx *middleware.Context, form auth.CreateMilestoneForm) { +// // ctx.Data["Title"] = "New Milestone" +// // ctx.Data["IsRepoToolbarIssues"] = true +// // ctx.Data["IsRepoToolbarIssuesList"] = true + +// // if ctx.HasError() { +// // ctx.HTML(200, MILESTONE_NEW) +// // return +// // } + +// // var deadline time.Time +// // var err error +// // if len(form.Deadline) == 0 { +// // form.Deadline = "12/31/9999" +// // } +// // deadline, err = time.Parse("01/02/2006", form.Deadline) +// // if err != nil { +// // ctx.Handle(500, "issue.NewMilestonePost(time.Parse)", err) +// // return +// // } + +// // mile := &models.Milestone{ +// // RepoId: ctx.Repo.Repository.Id, +// // Index: int64(ctx.Repo.Repository.NumMilestones) + 1, +// // Name: form.Title, +// // Content: form.Content, +// // Deadline: deadline, +// // } +// // if err = models.NewMilestone(mile); err != nil { +// // ctx.Handle(500, "issue.NewMilestonePost(NewMilestone)", err) +// // return +// // } + +// // ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones") +// // } + +// // func UpdateMilestone(ctx *middleware.Context, params martini.Params) { +// // ctx.Data["Title"] = "Update Milestone" +// // ctx.Data["IsRepoToolbarIssues"] = true +// // ctx.Data["IsRepoToolbarIssuesList"] = true + +// // idx, _ := base.StrTo(params["index"]).Int64() +// // if idx == 0 { +// // ctx.Handle(404, "issue.UpdateMilestone", nil) +// // return +// // } + +// // mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, idx) +// // if err != nil { +// // if err == models.ErrMilestoneNotExist { +// // ctx.Handle(404, "issue.UpdateMilestone(GetMilestoneByIndex)", err) +// // } else { +// // ctx.Handle(500, "issue.UpdateMilestone(GetMilestoneByIndex)", err) +// // } +// // return +// // } + +// // action := params["action"] +// // if len(action) > 0 { +// // switch action { +// // case "open": +// // if mile.IsClosed { +// // if err = models.ChangeMilestoneStatus(mile, false); err != nil { +// // ctx.Handle(500, "issue.UpdateMilestone(ChangeMilestoneStatus)", err) +// // return +// // } +// // } +// // case "close": +// // if !mile.IsClosed { +// // mile.ClosedDate = time.Now() +// // if err = models.ChangeMilestoneStatus(mile, true); err != nil { +// // ctx.Handle(500, "issue.UpdateMilestone(ChangeMilestoneStatus)", err) +// // return +// // } +// // } +// // case "delete": +// // if err = models.DeleteMilestone(mile); err != nil { +// // ctx.Handle(500, "issue.UpdateMilestone(DeleteMilestone)", err) +// // return +// // } +// // } +// // ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones") +// // return +// // } + +// // mile.DeadlineString = mile.Deadline.UTC().Format("01/02/2006") +// // if mile.DeadlineString == "12/31/9999" { +// // mile.DeadlineString = "" +// // } +// // ctx.Data["Milestone"] = mile + +// // ctx.HTML(200, MILESTONE_EDIT) +// // } + +// // func UpdateMilestonePost(ctx *middleware.Context, params martini.Params, form auth.CreateMilestoneForm) { +// // ctx.Data["Title"] = "Update Milestone" +// // ctx.Data["IsRepoToolbarIssues"] = true +// // ctx.Data["IsRepoToolbarIssuesList"] = true + +// // idx, _ := base.StrTo(params["index"]).Int64() +// // if idx == 0 { +// // ctx.Handle(404, "issue.UpdateMilestonePost", nil) +// // return +// // } + +// // mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, idx) +// // if err != nil { +// // if err == models.ErrMilestoneNotExist { +// // ctx.Handle(404, "issue.UpdateMilestonePost(GetMilestoneByIndex)", err) +// // } else { +// // ctx.Handle(500, "issue.UpdateMilestonePost(GetMilestoneByIndex)", err) +// // } +// // return +// // } + +// // if ctx.HasError() { +// // ctx.HTML(200, MILESTONE_EDIT) +// // return +// // } + +// // var deadline time.Time +// // if len(form.Deadline) == 0 { +// // form.Deadline = "12/31/9999" +// // } +// // deadline, err = time.Parse("01/02/2006", form.Deadline) +// // if err != nil { +// // ctx.Handle(500, "issue.UpdateMilestonePost(time.Parse)", err) +// // return +// // } + +// // mile.Name = form.Title +// // mile.Content = form.Content +// // mile.Deadline = deadline +// // if err = models.UpdateMilestone(mile); err != nil { +// // ctx.Handle(500, "issue.UpdateMilestonePost(UpdateMilestone)", err) +// // return +// // } + +// // ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones") +// // } + +// // func IssueGetAttachment(ctx *middleware.Context, params martini.Params) { +// // id, err := base.StrTo(params["id"]).Int64() + +// // if err != nil { +// // ctx.Handle(400, "issue.IssueGetAttachment(base.StrTo.Int64)", err) +// // return +// // } + +// // attachment, err := models.GetAttachmentById(id) + +// // if err != nil { +// // ctx.Handle(404, "issue.IssueGetAttachment(models.GetAttachmentById)", err) +// // return +// // } + +// // // Fix #312. Attachments with , in their name are not handled correctly by Google Chrome. +// // // We must put the name in " manually. +// // ctx.ServeFile(attachment.Path, "\""+attachment.Name+"\"") +// // } diff --git a/routers/repo/pull.go b/routers/repo/pull.go index db208f9fbc..d379a54eaa 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -5,8 +5,6 @@ package repo import ( - "github.com/go-martini/martini" - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" ) @@ -15,7 +13,7 @@ const ( PULLS base.TplName = "repo/pulls" ) -func Pulls(ctx *middleware.Context, params martini.Params) { +func Pulls(ctx *middleware.Context) { ctx.Data["IsRepoToolbarPulls"] = true ctx.HTML(200, PULLS) } diff --git a/routers/repo/release.go b/routers/repo/release.go index a901436fc0..509796fb19 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -5,13 +5,13 @@ package repo import ( - "github.com/go-martini/martini" + // "github.com/go-martini/martini" - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/auth" + // "github.com/gogits/gogs/models" + // "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" - "github.com/gogits/gogs/modules/log" - "github.com/gogits/gogs/modules/middleware" + // "github.com/gogits/gogs/modules/log" + // "github.com/gogits/gogs/modules/middleware" ) const ( @@ -20,215 +20,215 @@ const ( RELEASE_EDIT base.TplName = "repo/release/edit" ) -func Releases(ctx *middleware.Context) { - ctx.Data["Title"] = "Releases" - ctx.Data["IsRepoToolbarReleases"] = true - ctx.Data["IsRepoReleaseNew"] = false - rawTags, err := ctx.Repo.GitRepo.GetTags() - if err != nil { - ctx.Handle(500, "release.Releases(GetTags)", err) - return - } - - rels, err := models.GetReleasesByRepoId(ctx.Repo.Repository.Id) - if err != nil { - ctx.Handle(500, "release.Releases(GetReleasesByRepoId)", err) - return - } - - commitsCount, err := ctx.Repo.Commit.CommitsCount() - if err != nil { - ctx.Handle(500, "release.Releases(CommitsCount)", err) - return - } - - // Temproray cache commits count of used branches to speed up. - countCache := make(map[string]int) - - tags := make([]*models.Release, len(rawTags)) - for i, rawTag := range rawTags { - for _, rel := range rels { - if rel.IsDraft && !ctx.Repo.IsOwner { - continue - } - if rel.TagName == rawTag { - rel.Publisher, err = models.GetUserById(rel.PublisherId) - if err != nil { - ctx.Handle(500, "release.Releases(GetUserById)", err) - return - } - // Get corresponding target if it's not the current branch. - if ctx.Repo.BranchName != rel.Target { - // Get count if not exists. - if _, ok := countCache[rel.Target]; !ok { - commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rel.TagName) - if err != nil { - ctx.Handle(500, "release.Releases(GetCommitOfTag)", err) - return - } - countCache[rel.Target], err = commit.CommitsCount() - if err != nil { - ctx.Handle(500, "release.Releases(CommitsCount2)", err) - return - } - } - rel.NumCommitsBehind = countCache[rel.Target] - rel.NumCommits - } else { - rel.NumCommitsBehind = commitsCount - rel.NumCommits - } - - rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink) - tags[i] = rel - break - } - } - - if tags[i] == nil { - commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rawTag) - if err != nil { - ctx.Handle(500, "release.Releases(GetCommitOfTag2)", err) - return - } - - tags[i] = &models.Release{ - Title: rawTag, - TagName: rawTag, - Sha1: commit.Id.String(), - } - - tags[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.Id.String()) - if err != nil { - ctx.Handle(500, "release.Releases(CommitsCount)", err) - return - } - tags[i].NumCommitsBehind = commitsCount - tags[i].NumCommits - } - } - models.SortReleases(tags) - ctx.Data["Releases"] = tags - ctx.HTML(200, RELEASES) -} - -func NewRelease(ctx *middleware.Context) { - if !ctx.Repo.IsOwner { - ctx.Handle(403, "release.ReleasesNew", nil) - return - } - - ctx.Data["Title"] = "New Release" - ctx.Data["IsRepoToolbarReleases"] = true - ctx.Data["IsRepoReleaseNew"] = true - ctx.HTML(200, RELEASE_NEW) -} - -func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) { - if !ctx.Repo.IsOwner { - ctx.Handle(403, "release.ReleasesNew", nil) - return - } - - ctx.Data["Title"] = "New Release" - ctx.Data["IsRepoToolbarReleases"] = true - ctx.Data["IsRepoReleaseNew"] = true - - if ctx.HasError() { - ctx.HTML(200, RELEASE_NEW) - return - } - - commitsCount, err := ctx.Repo.Commit.CommitsCount() - if err != nil { - ctx.Handle(500, "release.ReleasesNewPost(CommitsCount)", err) - return - } - - if !ctx.Repo.GitRepo.IsBranchExist(form.Target) { - ctx.RenderWithErr("Target branch does not exist", "release/new", &form) - return - } - - rel := &models.Release{ - RepoId: ctx.Repo.Repository.Id, - PublisherId: ctx.User.Id, - Title: form.Title, - TagName: form.TagName, - Target: form.Target, - Sha1: ctx.Repo.Commit.Id.String(), - NumCommits: commitsCount, - Note: form.Content, - IsDraft: len(form.Draft) > 0, - IsPrerelease: form.Prerelease, - } - - if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil { - if err == models.ErrReleaseAlreadyExist { - ctx.RenderWithErr("Release with this tag name has already existed", "release/new", &form) - } else { - ctx.Handle(500, "release.ReleasesNewPost(IsReleaseExist)", err) - } - return - } - log.Trace("%s Release created: %s/%s:%s", ctx.Req.RequestURI, ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName) - - ctx.Redirect(ctx.Repo.RepoLink + "/releases") -} - -func EditRelease(ctx *middleware.Context, params martini.Params) { - if !ctx.Repo.IsOwner { - ctx.Handle(403, "release.ReleasesEdit", nil) - return - } - - tagName := params["tagname"] - rel, err := models.GetRelease(ctx.Repo.Repository.Id, tagName) - if err != nil { - if err == models.ErrReleaseNotExist { - ctx.Handle(404, "release.ReleasesEdit(GetRelease)", err) - } else { - ctx.Handle(500, "release.ReleasesEdit(GetRelease)", err) - } - return - } - ctx.Data["Release"] = rel - - ctx.Data["Title"] = "Edit Release" - ctx.Data["IsRepoToolbarReleases"] = true - ctx.HTML(200, RELEASE_EDIT) -} - -func EditReleasePost(ctx *middleware.Context, params martini.Params, form auth.EditReleaseForm) { - if !ctx.Repo.IsOwner { - ctx.Handle(403, "release.EditReleasePost", nil) - return - } - - tagName := params["tagname"] - rel, err := models.GetRelease(ctx.Repo.Repository.Id, tagName) - if err != nil { - if err == models.ErrReleaseNotExist { - ctx.Handle(404, "release.EditReleasePost(GetRelease)", err) - } else { - ctx.Handle(500, "release.EditReleasePost(GetRelease)", err) - } - return - } - ctx.Data["Release"] = rel - - if ctx.HasError() { - ctx.HTML(200, RELEASE_EDIT) - return - } - - ctx.Data["Title"] = "Edit Release" - ctx.Data["IsRepoToolbarReleases"] = true - - rel.Title = form.Title - rel.Note = form.Content - rel.IsDraft = len(form.Draft) > 0 - rel.IsPrerelease = form.Prerelease - if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil { - ctx.Handle(500, "release.EditReleasePost(UpdateRelease)", err) - return - } - ctx.Redirect(ctx.Repo.RepoLink + "/releases") -} +// func Releases(ctx *middleware.Context) { +// ctx.Data["Title"] = "Releases" +// ctx.Data["IsRepoToolbarReleases"] = true +// ctx.Data["IsRepoReleaseNew"] = false +// rawTags, err := ctx.Repo.GitRepo.GetTags() +// if err != nil { +// ctx.Handle(500, "release.Releases(GetTags)", err) +// return +// } + +// rels, err := models.GetReleasesByRepoId(ctx.Repo.Repository.Id) +// if err != nil { +// ctx.Handle(500, "release.Releases(GetReleasesByRepoId)", err) +// return +// } + +// commitsCount, err := ctx.Repo.Commit.CommitsCount() +// if err != nil { +// ctx.Handle(500, "release.Releases(CommitsCount)", err) +// return +// } + +// // Temproray cache commits count of used branches to speed up. +// countCache := make(map[string]int) + +// tags := make([]*models.Release, len(rawTags)) +// for i, rawTag := range rawTags { +// for _, rel := range rels { +// if rel.IsDraft && !ctx.Repo.IsOwner { +// continue +// } +// if rel.TagName == rawTag { +// rel.Publisher, err = models.GetUserById(rel.PublisherId) +// if err != nil { +// ctx.Handle(500, "release.Releases(GetUserById)", err) +// return +// } +// // Get corresponding target if it's not the current branch. +// if ctx.Repo.BranchName != rel.Target { +// // Get count if not exists. +// if _, ok := countCache[rel.Target]; !ok { +// commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rel.TagName) +// if err != nil { +// ctx.Handle(500, "release.Releases(GetCommitOfTag)", err) +// return +// } +// countCache[rel.Target], err = commit.CommitsCount() +// if err != nil { +// ctx.Handle(500, "release.Releases(CommitsCount2)", err) +// return +// } +// } +// rel.NumCommitsBehind = countCache[rel.Target] - rel.NumCommits +// } else { +// rel.NumCommitsBehind = commitsCount - rel.NumCommits +// } + +// rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink) +// tags[i] = rel +// break +// } +// } + +// if tags[i] == nil { +// commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rawTag) +// if err != nil { +// ctx.Handle(500, "release.Releases(GetCommitOfTag2)", err) +// return +// } + +// tags[i] = &models.Release{ +// Title: rawTag, +// TagName: rawTag, +// Sha1: commit.Id.String(), +// } + +// tags[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.Id.String()) +// if err != nil { +// ctx.Handle(500, "release.Releases(CommitsCount)", err) +// return +// } +// tags[i].NumCommitsBehind = commitsCount - tags[i].NumCommits +// } +// } +// models.SortReleases(tags) +// ctx.Data["Releases"] = tags +// ctx.HTML(200, RELEASES) +// } + +// func NewRelease(ctx *middleware.Context) { +// if !ctx.Repo.IsOwner { +// ctx.Handle(403, "release.ReleasesNew", nil) +// return +// } + +// ctx.Data["Title"] = "New Release" +// ctx.Data["IsRepoToolbarReleases"] = true +// ctx.Data["IsRepoReleaseNew"] = true +// ctx.HTML(200, RELEASE_NEW) +// } + +// func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) { +// if !ctx.Repo.IsOwner { +// ctx.Handle(403, "release.ReleasesNew", nil) +// return +// } + +// ctx.Data["Title"] = "New Release" +// ctx.Data["IsRepoToolbarReleases"] = true +// ctx.Data["IsRepoReleaseNew"] = true + +// if ctx.HasError() { +// ctx.HTML(200, RELEASE_NEW) +// return +// } + +// commitsCount, err := ctx.Repo.Commit.CommitsCount() +// if err != nil { +// ctx.Handle(500, "release.ReleasesNewPost(CommitsCount)", err) +// return +// } + +// if !ctx.Repo.GitRepo.IsBranchExist(form.Target) { +// ctx.RenderWithErr("Target branch does not exist", "release/new", &form) +// return +// } + +// rel := &models.Release{ +// RepoId: ctx.Repo.Repository.Id, +// PublisherId: ctx.User.Id, +// Title: form.Title, +// TagName: form.TagName, +// Target: form.Target, +// Sha1: ctx.Repo.Commit.Id.String(), +// NumCommits: commitsCount, +// Note: form.Content, +// IsDraft: len(form.Draft) > 0, +// IsPrerelease: form.Prerelease, +// } + +// if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil { +// if err == models.ErrReleaseAlreadyExist { +// ctx.RenderWithErr("Release with this tag name has already existed", "release/new", &form) +// } else { +// ctx.Handle(500, "release.ReleasesNewPost(IsReleaseExist)", err) +// } +// return +// } +// log.Trace("%s Release created: %s/%s:%s", ctx.Req.RequestURI, ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName) + +// ctx.Redirect(ctx.Repo.RepoLink + "/releases") +// } + +// func EditRelease(ctx *middleware.Context, params martini.Params) { +// if !ctx.Repo.IsOwner { +// ctx.Handle(403, "release.ReleasesEdit", nil) +// return +// } + +// tagName := params["tagname"] +// rel, err := models.GetRelease(ctx.Repo.Repository.Id, tagName) +// if err != nil { +// if err == models.ErrReleaseNotExist { +// ctx.Handle(404, "release.ReleasesEdit(GetRelease)", err) +// } else { +// ctx.Handle(500, "release.ReleasesEdit(GetRelease)", err) +// } +// return +// } +// ctx.Data["Release"] = rel + +// ctx.Data["Title"] = "Edit Release" +// ctx.Data["IsRepoToolbarReleases"] = true +// ctx.HTML(200, RELEASE_EDIT) +// } + +// func EditReleasePost(ctx *middleware.Context, params martini.Params, form auth.EditReleaseForm) { +// if !ctx.Repo.IsOwner { +// ctx.Handle(403, "release.EditReleasePost", nil) +// return +// } + +// tagName := params["tagname"] +// rel, err := models.GetRelease(ctx.Repo.Repository.Id, tagName) +// if err != nil { +// if err == models.ErrReleaseNotExist { +// ctx.Handle(404, "release.EditReleasePost(GetRelease)", err) +// } else { +// ctx.Handle(500, "release.EditReleasePost(GetRelease)", err) +// } +// return +// } +// ctx.Data["Release"] = rel + +// if ctx.HasError() { +// ctx.HTML(200, RELEASE_EDIT) +// return +// } + +// ctx.Data["Title"] = "Edit Release" +// ctx.Data["IsRepoToolbarReleases"] = true + +// rel.Title = form.Title +// rel.Note = form.Content +// rel.IsDraft = len(form.Draft) > 0 +// rel.IsPrerelease = form.Prerelease +// if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil { +// ctx.Handle(500, "release.EditReleasePost(UpdateRelease)", err) +// return +// } +// ctx.Redirect(ctx.Repo.RepoLink + "/releases") +// } diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 44a2b84f80..d1e196a0bd 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -5,22 +5,15 @@ package repo import ( - "bytes" - "encoding/base64" - "errors" - "fmt" - "io/ioutil" + "os" "path" - "path/filepath" - "strings" - "github.com/go-martini/martini" - - "github.com/gogits/git" + "github.com/Unknwon/com" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/git" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" ) @@ -28,47 +21,63 @@ import ( const ( CREATE base.TplName = "repo/create" MIGRATE base.TplName = "repo/migrate" - SINGLE base.TplName = "repo/single" ) func Create(ctx *middleware.Context) { - ctx.Data["Title"] = "Create repository" - ctx.Data["PageIsNewRepo"] = true - ctx.Data["LanguageIgns"] = models.LanguageIgns + ctx.Data["Title"] = ctx.Tr("new_repo") + ctx.Data["PageIsRepoCreate"] = true + + // Give default value for template to render. + ctx.Data["gitignore"] = "0" + ctx.Data["license"] = "0" + ctx.Data["Gitignores"] = models.Gitignores ctx.Data["Licenses"] = models.Licenses ctxUser := ctx.User - orgId, _ := base.StrTo(ctx.Query("org")).Int64() - if orgId > 0 { - org, err := models.GetUserById(orgId) - if err != nil && err != models.ErrUserNotExist { - ctx.Handle(500, "home.Dashboard(GetUserById)", err) - return - } - ctxUser = org - } + // orgId := com.StrTo(ctx.Query("org")).MustInt64() + // if orgId > 0 { + // org, err := models.GetUserById(orgId) + // if err != nil && err != models.ErrUserNotExist { + // ctx.Handle(500, "home.Dashboard(GetUserById)", err) + // return + // } + // ctxUser = org + // } ctx.Data["ContextUser"] = ctxUser - if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) - return - } - ctx.Data["AllUsers"] = append([]*models.User{ctx.User}, ctx.User.Orgs...) + // if err := ctx.User.GetOrganizations(); err != nil { + // ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) + // return + // } + // ctx.Data["AllUsers"] = append([]*models.User{ctx.User}, ctx.User.Orgs...) ctx.HTML(200, CREATE) } func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { - ctx.Data["Title"] = "Create repository" - ctx.Data["PageIsNewRepo"] = true - ctx.Data["LanguageIgns"] = models.LanguageIgns + ctx.Data["Title"] = ctx.Tr("new_repo") + ctx.Data["PageIsRepoCreate"] = true + + ctx.Data["Gitignores"] = models.Gitignores ctx.Data["Licenses"] = models.Licenses - if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.CreatePost(GetOrganizations)", err) - return - } - ctx.Data["Orgs"] = ctx.User.Orgs + ctxUser := ctx.User + // orgId := com.StrTo(ctx.Query("org")).MustInt64() + // if orgId > 0 { + // org, err := models.GetUserById(orgId) + // if err != nil && err != models.ErrUserNotExist { + // ctx.Handle(500, "home.Dashboard(GetUserById)", err) + // return + // } + // ctxUser = org + // } + ctx.Data["ContextUser"] = ctxUser + + // if err := ctx.User.GetOrganizations(); err != nil { + // ctx.Handle(500, "home.CreatePost(GetOrganizations)", err) + // return + // } + // ctx.Data["Orgs"] = ctx.User.Orgs if ctx.HasError() { ctx.HTML(200, CREATE) @@ -97,337 +106,159 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { } repo, err := models.CreateRepository(u, form.RepoName, form.Description, - form.Language, form.License, form.Private, false, form.InitReadme) + form.Gitignore, form.License, form.Private, false, form.InitReadme) if err == nil { - log.Trace("%s Repository created: %s/%s", ctx.Req.RequestURI, u.LowerName, form.RepoName) + log.Trace("Repository created: %s/%s", u.Name, form.RepoName) ctx.Redirect("/" + u.Name + "/" + form.RepoName) return } else if err == models.ErrRepoAlreadyExist { - ctx.RenderWithErr("Repository name has already been used", CREATE, &form) + ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), CREATE, &form) return } else if err == models.ErrRepoNameIllegal { - ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), CREATE, &form) + ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), CREATE, &form) return } if repo != nil { if errDelete := models.DeleteRepository(u.Id, repo.Id, u.Name); errDelete != nil { - log.Error("repo.CreatePost(DeleteRepository): %v", errDelete) + log.Error(4, "DeleteRepository: %v", errDelete) } } - ctx.Handle(500, "repo.CreatePost(CreateRepository)", err) -} - -func Migrate(ctx *middleware.Context) { - ctx.Data["Title"] = "Migrate repository" - ctx.Data["PageIsNewRepo"] = true - - if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.Migrate(GetOrganizations)", err) - return - } - ctx.Data["Orgs"] = ctx.User.Orgs - - ctx.HTML(200, MIGRATE) + ctx.Handle(500, "CreateRepository", err) } -func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { - ctx.Data["Title"] = "Migrate repository" - ctx.Data["PageIsNewRepo"] = true - - if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.MigratePost(GetOrganizations)", err) +// func Migrate(ctx *middleware.Context) { +// ctx.Data["Title"] = "Migrate repository" +// ctx.Data["PageIsNewRepo"] = true + +// if err := ctx.User.GetOrganizations(); err != nil { +// ctx.Handle(500, "home.Migrate(GetOrganizations)", err) +// return +// } +// ctx.Data["Orgs"] = ctx.User.Orgs + +// ctx.HTML(200, MIGRATE) +// } + +// func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { +// ctx.Data["Title"] = "Migrate repository" +// ctx.Data["PageIsNewRepo"] = true + +// if err := ctx.User.GetOrganizations(); err != nil { +// ctx.Handle(500, "home.MigratePost(GetOrganizations)", err) +// return +// } +// ctx.Data["Orgs"] = ctx.User.Orgs + +// if ctx.HasError() { +// ctx.HTML(200, MIGRATE) +// return +// } + +// u := ctx.User +// // Not equal means current user is an organization. +// if u.Id != form.Uid { +// var err error +// u, err = models.GetUserById(form.Uid) +// if err != nil { +// if err == models.ErrUserNotExist { +// ctx.Handle(404, "home.MigratePost(GetUserById)", err) +// } else { +// ctx.Handle(500, "home.MigratePost(GetUserById)", err) +// } +// return +// } +// } + +// authStr := strings.Replace(fmt.Sprintf("://%s:%s", +// form.AuthUserName, form.AuthPasswd), "@", "%40", -1) +// url := strings.Replace(form.Url, "://", authStr+"@", 1) +// repo, err := models.MigrateRepository(u, form.RepoName, form.Description, form.Private, +// form.Mirror, url) +// if err == nil { +// log.Trace("%s Repository migrated: %s/%s", ctx.Req.RequestURI, u.LowerName, form.RepoName) +// ctx.Redirect("/" + u.Name + "/" + form.RepoName) +// return +// } else if err == models.ErrRepoAlreadyExist { +// ctx.RenderWithErr("Repository name has already been used", MIGRATE, &form) +// return +// } else if err == models.ErrRepoNameIllegal { +// ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), MIGRATE, &form) +// return +// } + +// if repo != nil { +// if errDelete := models.DeleteRepository(u.Id, repo.Id, u.Name); errDelete != nil { +// log.Error("repo.MigratePost(DeleteRepository): %v", errDelete) +// } +// } + +// if strings.Contains(err.Error(), "Authentication failed") { +// ctx.RenderWithErr(err.Error(), MIGRATE, &form) +// return +// } +// ctx.Handle(500, "repo.Migrate(MigrateRepository)", err) +// } + +// func Action(ctx *middleware.Context, params martini.Params) { +// var err error +// switch params["action"] { +// case "watch": +// err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, true) +// case "unwatch": +// err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, false) +// case "desc": +// if !ctx.Repo.IsOwner { +// ctx.Error(404) +// return +// } + +// ctx.Repo.Repository.Description = ctx.Query("desc") +// ctx.Repo.Repository.Website = ctx.Query("site") +// err = models.UpdateRepository(ctx.Repo.Repository) +// } + +// if err != nil { +// log.Error("repo.Action(%s): %v", params["action"], err) +// ctx.JSON(200, map[string]interface{}{ +// "ok": false, +// "err": err.Error(), +// }) +// return +// } +// ctx.JSON(200, map[string]interface{}{ +// "ok": true, +// }) +// } + +func Download(ctx *middleware.Context) { + ext := "." + ctx.Params(":ext") + + var archivePath string + switch ext { + case ".zip": + archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip") + case ".tar.gz": + archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz") + default: + ctx.Error(404) return } - ctx.Data["Orgs"] = ctx.User.Orgs - if ctx.HasError() { - ctx.HTML(200, MIGRATE) - return - } - - u := ctx.User - // Not equal means current user is an organization. - if u.Id != form.Uid { - var err error - u, err = models.GetUserById(form.Uid) - if err != nil { - if err == models.ErrUserNotExist { - ctx.Handle(404, "home.MigratePost(GetUserById)", err) - } else { - ctx.Handle(500, "home.MigratePost(GetUserById)", err) - } + if !com.IsDir(archivePath) { + if err := os.MkdirAll(archivePath, os.ModePerm); err != nil { + ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err) return } } - authStr := strings.Replace(fmt.Sprintf("://%s:%s", - form.AuthUserName, form.AuthPasswd), "@", "%40", -1) - url := strings.Replace(form.Url, "://", authStr+"@", 1) - repo, err := models.MigrateRepository(u, form.RepoName, form.Description, form.Private, - form.Mirror, url) - if err == nil { - log.Trace("%s Repository migrated: %s/%s", ctx.Req.RequestURI, u.LowerName, form.RepoName) - ctx.Redirect("/" + u.Name + "/" + form.RepoName) - return - } else if err == models.ErrRepoAlreadyExist { - ctx.RenderWithErr("Repository name has already been used", MIGRATE, &form) - return - } else if err == models.ErrRepoNameIllegal { - ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), MIGRATE, &form) - return - } - - if repo != nil { - if errDelete := models.DeleteRepository(u.Id, repo.Id, u.Name); errDelete != nil { - log.Error("repo.MigratePost(DeleteRepository): %v", errDelete) - } - } - - if strings.Contains(err.Error(), "Authentication failed") { - ctx.RenderWithErr(err.Error(), MIGRATE, &form) - return - } - ctx.Handle(500, "repo.Migrate(MigrateRepository)", err) -} - -func Single(ctx *middleware.Context, params martini.Params) { - branchName := ctx.Repo.BranchName - userName := ctx.Repo.Owner.Name - repoName := ctx.Repo.Repository.Name - - repoLink := ctx.Repo.RepoLink - branchLink := ctx.Repo.RepoLink + "/src/" + branchName - rawLink := ctx.Repo.RepoLink + "/raw/" + branchName - - // Get tree path - treename := params["_1"] - - if len(treename) > 0 && treename[len(treename)-1] == '/' { - ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1]) - return - } - - ctx.Data["IsRepoToolbarSource"] = true - - isViewBranch := ctx.Repo.IsBranch - ctx.Data["IsViewBranch"] = isViewBranch - - treePath := treename - if len(treePath) != 0 { - treePath = treePath + "/" - } - - entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treename) - if err != nil && err != git.ErrNotExist { - ctx.Handle(404, "repo.Single(GetTreeEntryByPath)", err) - return - } - - if len(treename) != 0 && entry == nil { - ctx.Handle(404, "repo.Single", nil) - return - } - - if entry != nil && !entry.IsDir() { - blob := entry.Blob() - - if dataRc, err := blob.Data(); err != nil { - ctx.Handle(404, "repo.Single(blob.Data)", err) - } else { - ctx.Data["FileSize"] = blob.Size() - ctx.Data["IsFile"] = true - ctx.Data["FileName"] = blob.Name() - ext := path.Ext(blob.Name()) - if len(ext) > 0 { - ext = ext[1:] - } - ctx.Data["FileExt"] = ext - ctx.Data["FileLink"] = rawLink + "/" + treename - - buf := make([]byte, 1024) - n, _ := dataRc.Read(buf) - if n > 0 { - buf = buf[:n] - } - - defer func() { - dataRc.Close() - }() - - _, isTextFile := base.IsTextFile(buf) - _, isImageFile := base.IsImageFile(buf) - ctx.Data["FileIsText"] = isTextFile - - switch { - case isImageFile: - ctx.Data["IsImageFile"] = true - case isTextFile: - d, _ := ioutil.ReadAll(dataRc) - buf = append(buf, d...) - readmeExist := base.IsMarkdownFile(blob.Name()) || base.IsReadmeFile(blob.Name()) - ctx.Data["ReadmeExist"] = readmeExist - if readmeExist { - ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, "")) - } else { - ctx.Data["FileContent"] = string(buf) - } - } - } - - } else { - // Directory and file list. - tree, err := ctx.Repo.Commit.SubTree(treename) - if err != nil { - ctx.Handle(404, "repo.Single(SubTree)", err) - return - } - entries := tree.ListEntries() - entries.Sort() - - files := make([][]interface{}, 0, len(entries)) - - for _, te := range entries { - c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name())) - if err != nil { - ctx.Handle(404, "repo.Single(SubTree)", err) - return - } - - files = append(files, []interface{}{te, c}) - } - - ctx.Data["Files"] = files - - var readmeFile *git.Blob - - for _, f := range entries { - if f.IsDir() || !base.IsReadmeFile(f.Name()) { - continue - } else { - readmeFile = f.Blob() - break - } - } - - if readmeFile != nil { - ctx.Data["ReadmeInSingle"] = true - ctx.Data["ReadmeExist"] = true - if dataRc, err := readmeFile.Data(); err != nil { - ctx.Handle(404, "repo.Single(readmeFile.LookupBlob)", err) - return - } else { - - buf := make([]byte, 1024) - n, _ := dataRc.Read(buf) - if n > 0 { - buf = buf[:n] - } - defer func() { - dataRc.Close() - }() - - ctx.Data["FileSize"] = readmeFile.Size - ctx.Data["FileLink"] = rawLink + "/" + treename - _, isTextFile := base.IsTextFile(buf) - ctx.Data["FileIsText"] = isTextFile - ctx.Data["FileName"] = readmeFile.Name() - if isTextFile { - d, _ := ioutil.ReadAll(dataRc) - buf = append(buf, d...) - switch { - case base.IsMarkdownFile(readmeFile.Name()): - buf = base.RenderMarkdown(buf, branchLink) - default: - buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1) - } - ctx.Data["FileContent"] = string(buf) - } - } - } - } - - ctx.Data["Username"] = userName - ctx.Data["Reponame"] = repoName - - var treenames []string - Paths := make([]string, 0) - - if len(treename) > 0 { - treenames = strings.Split(treename, "/") - for i, _ := range treenames { - Paths = append(Paths, strings.Join(treenames[0:i+1], "/")) - } - - ctx.Data["HasParentPath"] = true - if len(Paths)-2 >= 0 { - ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2] - } - } - - ctx.Data["LastCommit"] = ctx.Repo.Commit - ctx.Data["Paths"] = Paths - ctx.Data["TreeName"] = treename - ctx.Data["Treenames"] = treenames - ctx.Data["TreePath"] = treePath - ctx.Data["BranchLink"] = branchLink - ctx.HTML(200, SINGLE) -} - -func basicEncode(username, password string) string { - auth := username + ":" + password - return base64.StdEncoding.EncodeToString([]byte(auth)) -} - -func basicDecode(encoded string) (user string, name string, err error) { - var s []byte - s, err = base64.StdEncoding.DecodeString(encoded) - if err != nil { - return - } - - a := strings.Split(string(s), ":") - if len(a) == 2 { - user, name = a[0], a[1] - } else { - err = errors.New("decode failed") - } - return -} - -func authRequired(ctx *middleware.Context) { - ctx.ResponseWriter.Header().Set("WWW-Authenticate", "Basic realm=\".\"") - ctx.Data["ErrorMsg"] = "no basic auth and digit auth" - ctx.HTML(401, base.TplName("status/401")) -} - -func Action(ctx *middleware.Context, params martini.Params) { - var err error - switch params["action"] { - case "watch": - err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, true) - case "unwatch": - err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, false) - case "desc": - if !ctx.Repo.IsOwner { - ctx.Error(404) + archivePath = path.Join(archivePath, ctx.Repo.CommitId+ext) + if !com.IsFile(archivePath) { + if err := ctx.Repo.Commit.CreateArchive(archivePath, git.ZIP); err != nil { + ctx.Handle(500, "Download -> CreateArchive "+archivePath, err) return } - - ctx.Repo.Repository.Description = ctx.Query("desc") - ctx.Repo.Repository.Website = ctx.Query("site") - err = models.UpdateRepository(ctx.Repo.Repository) } - if err != nil { - log.Error("repo.Action(%s): %v", params["action"], err) - ctx.JSON(200, map[string]interface{}{ - "ok": false, - "err": err.Error(), - }) - return - } - ctx.JSON(200, map[string]interface{}{ - "ok": true, - }) + ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(ctx.Repo.CommitId)+ext) } diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 5a57f76075..48f2e219ed 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -4,362 +4,362 @@ package repo -import ( - "fmt" - "strings" - "time" - - "github.com/go-martini/martini" - - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/auth" - "github.com/gogits/gogs/modules/base" - "github.com/gogits/gogs/modules/log" - "github.com/gogits/gogs/modules/mailer" - "github.com/gogits/gogs/modules/middleware" - "github.com/gogits/gogs/modules/setting" -) - -const ( - SETTING base.TplName = "repo/setting" - COLLABORATION base.TplName = "repo/collaboration" - - HOOKS base.TplName = "repo/hooks" - HOOK_ADD base.TplName = "repo/hook_add" - HOOK_EDIT base.TplName = "repo/hook_edit" -) - -func Setting(ctx *middleware.Context) { - ctx.Data["IsRepoToolbarSetting"] = true - ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - settings" - ctx.HTML(200, SETTING) -} - -func SettingPost(ctx *middleware.Context, form auth.RepoSettingForm) { - ctx.Data["IsRepoToolbarSetting"] = true - - switch ctx.Query("action") { - case "update": - if ctx.HasError() { - ctx.HTML(200, SETTING) - return - } - - newRepoName := form.RepoName - // Check if repository name has been changed. - if ctx.Repo.Repository.Name != newRepoName { - isExist, err := models.IsRepositoryExist(ctx.Repo.Owner, newRepoName) - if err != nil { - ctx.Handle(500, "setting.SettingPost(update: check existence)", err) - return - } else if isExist { - ctx.RenderWithErr("Repository name has been taken in your repositories.", SETTING, nil) - return - } else if err = models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil { - ctx.Handle(500, "setting.SettingPost(change repository name)", err) - return - } - log.Trace("%s Repository name changed: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newRepoName) - - ctx.Repo.Repository.Name = newRepoName - } - - br := form.Branch - - if ctx.Repo.GitRepo.IsBranchExist(br) { - ctx.Repo.Repository.DefaultBranch = br - } - ctx.Repo.Repository.Description = form.Description - ctx.Repo.Repository.Website = form.Website - ctx.Repo.Repository.IsPrivate = form.Private - ctx.Repo.Repository.IsGoget = form.GoGet - if err := models.UpdateRepository(ctx.Repo.Repository); err != nil { - ctx.Handle(404, "setting.SettingPost(update)", err) - return - } - log.Trace("%s Repository updated: %s/%s", ctx.Req.RequestURI, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) - - if ctx.Repo.Repository.IsMirror { - if form.Interval > 0 { - ctx.Repo.Mirror.Interval = form.Interval - ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour) - if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil { - log.Error("setting.SettingPost(UpdateMirror): %v", err) - } - } - } - - ctx.Flash.Success("Repository options has been successfully updated.") - ctx.Redirect(fmt.Sprintf("/%s/%s/settings", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)) - case "transfer": - if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") { - ctx.RenderWithErr("Please make sure you entered repository name is correct.", SETTING, nil) - return - } else if ctx.Repo.Repository.IsMirror { - ctx.Error(404) - return - } - - newOwner := ctx.Query("owner") - // Check if new owner exists. - isExist, err := models.IsUserExist(newOwner) - if err != nil { - ctx.Handle(500, "setting.SettingPost(transfer: check existence)", err) - return - } else if !isExist { - ctx.RenderWithErr("Please make sure you entered owner name is correct.", SETTING, nil) - return - } else if err = models.TransferOwnership(ctx.Repo.Owner, newOwner, ctx.Repo.Repository); err != nil { - ctx.Handle(500, "setting.SettingPost(transfer repository)", err) - return - } - log.Trace("%s Repository transfered: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newOwner) - - ctx.Redirect("/") - case "delete": - if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") { - ctx.RenderWithErr("Please make sure you entered repository name is correct.", SETTING, nil) - return - } - - if ctx.Repo.Owner.IsOrganization() && - !ctx.Repo.Owner.IsOrgOwner(ctx.User.Id) { - ctx.Error(403) - return - } - - if err := models.DeleteRepository(ctx.Repo.Owner.Id, ctx.Repo.Repository.Id, ctx.Repo.Owner.Name); err != nil { - ctx.Handle(500, "setting.Delete(DeleteRepository)", err) - return - } - log.Trace("%s Repository deleted: %s/%s", ctx.Req.RequestURI, ctx.Repo.Owner.LowerName, ctx.Repo.Repository.LowerName) - - if ctx.Repo.Owner.IsOrganization() { - ctx.Redirect("/org/" + ctx.Repo.Owner.Name + "/dashboard") - } else { - ctx.Redirect("/") - } - } -} - -func Collaboration(ctx *middleware.Context) { - repoLink := strings.TrimPrefix(ctx.Repo.RepoLink, "/") - ctx.Data["IsRepoToolbarCollaboration"] = true - ctx.Data["Title"] = repoLink + " - collaboration" - - // Delete collaborator. - remove := strings.ToLower(ctx.Query("remove")) - if len(remove) > 0 && remove != ctx.Repo.Owner.LowerName { - if err := models.DeleteAccess(&models.Access{UserName: remove, RepoName: repoLink}); err != nil { - ctx.Handle(500, "setting.Collaboration(DeleteAccess)", err) - return - } - ctx.Flash.Success("Collaborator has been removed.") - ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration") - return - } - - names, err := models.GetCollaboratorNames(repoLink) - if err != nil { - ctx.Handle(500, "setting.Collaboration(GetCollaborators)", err) - return - } - - us := make([]*models.User, len(names)) - for i, name := range names { - us[i], err = models.GetUserByName(name) - if err != nil { - ctx.Handle(500, "setting.Collaboration(GetUserByName)", err) - return - } - } - - ctx.Data["Collaborators"] = us - ctx.HTML(200, COLLABORATION) -} - -func CollaborationPost(ctx *middleware.Context) { - repoLink := strings.TrimPrefix(ctx.Repo.RepoLink, "/") - name := strings.ToLower(ctx.Query("collaborator")) - if len(name) == 0 || ctx.Repo.Owner.LowerName == name { - ctx.Redirect(ctx.Req.RequestURI) - return - } - has, err := models.HasAccess(name, repoLink, models.WRITABLE) - if err != nil { - ctx.Handle(500, "setting.CollaborationPost(HasAccess)", err) - return - } else if has { - ctx.Redirect(ctx.Req.RequestURI) - return - } - - u, err := models.GetUserByName(name) - if err != nil { - if err == models.ErrUserNotExist { - ctx.Flash.Error("Given user does not exist.") - ctx.Redirect(ctx.Req.RequestURI) - } else { - ctx.Handle(500, "setting.CollaborationPost(GetUserByName)", err) - } - return - } - - if err = models.AddAccess(&models.Access{UserName: name, RepoName: repoLink, - Mode: models.WRITABLE}); err != nil { - ctx.Handle(500, "setting.CollaborationPost(AddAccess)", err) - return - } - - if setting.Service.EnableNotifyMail { - if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil { - ctx.Handle(500, "setting.CollaborationPost(SendCollaboratorMail)", err) - return - } - } - - ctx.Flash.Success("New collaborator has been added.") - ctx.Redirect(ctx.Req.RequestURI) -} - -func WebHooks(ctx *middleware.Context) { - ctx.Data["IsRepoToolbarWebHooks"] = true - ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhooks" - - // Delete webhook. - remove, _ := base.StrTo(ctx.Query("remove")).Int64() - if remove > 0 { - if err := models.DeleteWebhook(remove); err != nil { - ctx.Handle(500, "setting.WebHooks(DeleteWebhook)", err) - return - } - ctx.Flash.Success("Webhook has been removed.") - ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks") - return - } - - ws, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.Id) - if err != nil { - ctx.Handle(500, "setting.WebHooks(GetWebhooksByRepoId)", err) - return - } - - ctx.Data["Webhooks"] = ws - ctx.HTML(200, HOOKS) -} - -func WebHooksAdd(ctx *middleware.Context) { - ctx.Data["IsRepoToolbarWebHooks"] = true - ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Webhook" - ctx.HTML(200, HOOK_ADD) -} - -func WebHooksAddPost(ctx *middleware.Context, form auth.NewWebhookForm) { - ctx.Data["IsRepoToolbarWebHooks"] = true - ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Webhook" - - if ctx.HasError() { - ctx.HTML(200, HOOK_ADD) - return - } - - ct := models.JSON - if form.ContentType == "2" { - ct = models.FORM - } - - w := &models.Webhook{ - RepoId: ctx.Repo.Repository.Id, - Url: form.Url, - ContentType: ct, - Secret: form.Secret, - HookEvent: &models.HookEvent{ - PushOnly: form.PushOnly, - }, - IsActive: form.Active, - } - if err := w.UpdateEvent(); err != nil { - ctx.Handle(500, "setting.WebHooksAddPost(UpdateEvent)", err) - return - } else if err := models.CreateWebhook(w); err != nil { - ctx.Handle(500, "setting.WebHooksAddPost(CreateWebhook)", err) - return - } - - ctx.Flash.Success("New webhook has been added.") - ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks") -} - -func WebHooksEdit(ctx *middleware.Context, params martini.Params) { - ctx.Data["IsRepoToolbarWebHooks"] = true - ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhook" - - hookId, _ := base.StrTo(params["id"]).Int64() - if hookId == 0 { - ctx.Handle(404, "setting.WebHooksEdit", nil) - return - } - - w, err := models.GetWebhookById(hookId) - if err != nil { - if err == models.ErrWebhookNotExist { - ctx.Handle(404, "setting.WebHooksEdit(GetWebhookById)", nil) - } else { - ctx.Handle(500, "setting.WebHooksEdit(GetWebhookById)", err) - } - return - } - - w.GetEvent() - ctx.Data["Webhook"] = w - ctx.HTML(200, HOOK_EDIT) -} - -func WebHooksEditPost(ctx *middleware.Context, params martini.Params, form auth.NewWebhookForm) { - ctx.Data["IsRepoToolbarWebHooks"] = true - ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhook" - - hookId, _ := base.StrTo(params["id"]).Int64() - if hookId == 0 { - ctx.Handle(404, "setting.WebHooksEditPost", nil) - return - } - - w, err := models.GetWebhookById(hookId) - if err != nil { - if err == models.ErrWebhookNotExist { - ctx.Handle(404, "setting.WebHooksEditPost(GetWebhookById)", nil) - } else { - ctx.Handle(500, "setting.WebHooksEditPost(GetWebhookById)", err) - } - return - } - - if ctx.HasError() { - ctx.HTML(200, HOOK_EDIT) - return - } - - ct := models.JSON - if form.ContentType == "2" { - ct = models.FORM - } - - w.Url = form.Url - w.ContentType = ct - w.Secret = form.Secret - w.HookEvent = &models.HookEvent{ - PushOnly: form.PushOnly, - } - w.IsActive = form.Active - if err := w.UpdateEvent(); err != nil { - ctx.Handle(500, "setting.WebHooksEditPost(UpdateEvent)", err) - return - } else if err := models.UpdateWebhook(w); err != nil { - ctx.Handle(500, "setting.WebHooksEditPost(WebHooksEditPost)", err) - return - } - - ctx.Flash.Success("Webhook has been updated.") - ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", ctx.Repo.RepoLink, hookId)) -} +// import ( +// "fmt" +// "strings" +// "time" + +// "github.com/go-martini/martini" + +// "github.com/gogits/gogs-ng/models" +// "github.com/gogits/gogs/modules/auth" +// "github.com/gogits/gogs/modules/base" +// "github.com/gogits/gogs/modules/log" +// "github.com/gogits/gogs/modules/mailer" +// "github.com/gogits/gogs/modules/middleware" +// "github.com/gogits/gogs/modules/setting" +// ) + +// const ( +// SETTING base.TplName = "repo/setting" +// COLLABORATION base.TplName = "repo/collaboration" + +// HOOKS base.TplName = "repo/hooks" +// HOOK_ADD base.TplName = "repo/hook_add" +// HOOK_EDIT base.TplName = "repo/hook_edit" +// ) + +// func Setting(ctx *middleware.Context) { +// ctx.Data["IsRepoToolbarSetting"] = true +// ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - settings" +// ctx.HTML(200, SETTING) +// } + +// func SettingPost(ctx *middleware.Context, form auth.RepoSettingForm) { +// ctx.Data["IsRepoToolbarSetting"] = true + +// switch ctx.Query("action") { +// case "update": +// if ctx.HasError() { +// ctx.HTML(200, SETTING) +// return +// } + +// newRepoName := form.RepoName +// // Check if repository name has been changed. +// if ctx.Repo.Repository.Name != newRepoName { +// isExist, err := models.IsRepositoryExist(ctx.Repo.Owner, newRepoName) +// if err != nil { +// ctx.Handle(500, "setting.SettingPost(update: check existence)", err) +// return +// } else if isExist { +// ctx.RenderWithErr("Repository name has been taken in your repositories.", SETTING, nil) +// return +// } else if err = models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil { +// ctx.Handle(500, "setting.SettingPost(change repository name)", err) +// return +// } +// log.Trace("%s Repository name changed: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newRepoName) + +// ctx.Repo.Repository.Name = newRepoName +// } + +// br := form.Branch + +// if ctx.Repo.GitRepo.IsBranchExist(br) { +// ctx.Repo.Repository.DefaultBranch = br +// } +// ctx.Repo.Repository.Description = form.Description +// ctx.Repo.Repository.Website = form.Website +// ctx.Repo.Repository.IsPrivate = form.Private +// ctx.Repo.Repository.IsGoget = form.GoGet +// if err := models.UpdateRepository(ctx.Repo.Repository); err != nil { +// ctx.Handle(404, "setting.SettingPost(update)", err) +// return +// } +// log.Trace("%s Repository updated: %s/%s", ctx.Req.RequestURI, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) + +// if ctx.Repo.Repository.IsMirror { +// if form.Interval > 0 { +// ctx.Repo.Mirror.Interval = form.Interval +// ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour) +// if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil { +// log.Error("setting.SettingPost(UpdateMirror): %v", err) +// } +// } +// } + +// ctx.Flash.Success("Repository options has been successfully updated.") +// ctx.Redirect(fmt.Sprintf("/%s/%s/settings", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)) +// case "transfer": +// if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") { +// ctx.RenderWithErr("Please make sure you entered repository name is correct.", SETTING, nil) +// return +// } else if ctx.Repo.Repository.IsMirror { +// ctx.Error(404) +// return +// } + +// newOwner := ctx.Query("owner") +// // Check if new owner exists. +// isExist, err := models.IsUserExist(newOwner) +// if err != nil { +// ctx.Handle(500, "setting.SettingPost(transfer: check existence)", err) +// return +// } else if !isExist { +// ctx.RenderWithErr("Please make sure you entered owner name is correct.", SETTING, nil) +// return +// } else if err = models.TransferOwnership(ctx.Repo.Owner, newOwner, ctx.Repo.Repository); err != nil { +// ctx.Handle(500, "setting.SettingPost(transfer repository)", err) +// return +// } +// log.Trace("%s Repository transfered: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newOwner) + +// ctx.Redirect("/") +// case "delete": +// if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") { +// ctx.RenderWithErr("Please make sure you entered repository name is correct.", SETTING, nil) +// return +// } + +// if ctx.Repo.Owner.IsOrganization() && +// !ctx.Repo.Owner.IsOrgOwner(ctx.User.Id) { +// ctx.Error(403) +// return +// } + +// if err := models.DeleteRepository(ctx.Repo.Owner.Id, ctx.Repo.Repository.Id, ctx.Repo.Owner.Name); err != nil { +// ctx.Handle(500, "setting.Delete(DeleteRepository)", err) +// return +// } +// log.Trace("%s Repository deleted: %s/%s", ctx.Req.RequestURI, ctx.Repo.Owner.LowerName, ctx.Repo.Repository.LowerName) + +// if ctx.Repo.Owner.IsOrganization() { +// ctx.Redirect("/org/" + ctx.Repo.Owner.Name + "/dashboard") +// } else { +// ctx.Redirect("/") +// } +// } +// } + +// func Collaboration(ctx *middleware.Context) { +// repoLink := strings.TrimPrefix(ctx.Repo.RepoLink, "/") +// ctx.Data["IsRepoToolbarCollaboration"] = true +// ctx.Data["Title"] = repoLink + " - collaboration" + +// // Delete collaborator. +// remove := strings.ToLower(ctx.Query("remove")) +// if len(remove) > 0 && remove != ctx.Repo.Owner.LowerName { +// if err := models.DeleteAccess(&models.Access{UserName: remove, RepoName: repoLink}); err != nil { +// ctx.Handle(500, "setting.Collaboration(DeleteAccess)", err) +// return +// } +// ctx.Flash.Success("Collaborator has been removed.") +// ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration") +// return +// } + +// names, err := models.GetCollaboratorNames(repoLink) +// if err != nil { +// ctx.Handle(500, "setting.Collaboration(GetCollaborators)", err) +// return +// } + +// us := make([]*models.User, len(names)) +// for i, name := range names { +// us[i], err = models.GetUserByName(name) +// if err != nil { +// ctx.Handle(500, "setting.Collaboration(GetUserByName)", err) +// return +// } +// } + +// ctx.Data["Collaborators"] = us +// ctx.HTML(200, COLLABORATION) +// } + +// func CollaborationPost(ctx *middleware.Context) { +// repoLink := strings.TrimPrefix(ctx.Repo.RepoLink, "/") +// name := strings.ToLower(ctx.Query("collaborator")) +// if len(name) == 0 || ctx.Repo.Owner.LowerName == name { +// ctx.Redirect(ctx.Req.RequestURI) +// return +// } +// has, err := models.HasAccess(name, repoLink, models.WRITABLE) +// if err != nil { +// ctx.Handle(500, "setting.CollaborationPost(HasAccess)", err) +// return +// } else if has { +// ctx.Redirect(ctx.Req.RequestURI) +// return +// } + +// u, err := models.GetUserByName(name) +// if err != nil { +// if err == models.ErrUserNotExist { +// ctx.Flash.Error("Given user does not exist.") +// ctx.Redirect(ctx.Req.RequestURI) +// } else { +// ctx.Handle(500, "setting.CollaborationPost(GetUserByName)", err) +// } +// return +// } + +// if err = models.AddAccess(&models.Access{UserName: name, RepoName: repoLink, +// Mode: models.WRITABLE}); err != nil { +// ctx.Handle(500, "setting.CollaborationPost(AddAccess)", err) +// return +// } + +// if setting.Service.EnableNotifyMail { +// if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil { +// ctx.Handle(500, "setting.CollaborationPost(SendCollaboratorMail)", err) +// return +// } +// } + +// ctx.Flash.Success("New collaborator has been added.") +// ctx.Redirect(ctx.Req.RequestURI) +// } + +// func WebHooks(ctx *middleware.Context) { +// ctx.Data["IsRepoToolbarWebHooks"] = true +// ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhooks" + +// // Delete webhook. +// remove, _ := base.StrTo(ctx.Query("remove")).Int64() +// if remove > 0 { +// if err := models.DeleteWebhook(remove); err != nil { +// ctx.Handle(500, "setting.WebHooks(DeleteWebhook)", err) +// return +// } +// ctx.Flash.Success("Webhook has been removed.") +// ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks") +// return +// } + +// ws, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.Id) +// if err != nil { +// ctx.Handle(500, "setting.WebHooks(GetWebhooksByRepoId)", err) +// return +// } + +// ctx.Data["Webhooks"] = ws +// ctx.HTML(200, HOOKS) +// } + +// func WebHooksAdd(ctx *middleware.Context) { +// ctx.Data["IsRepoToolbarWebHooks"] = true +// ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Webhook" +// ctx.HTML(200, HOOK_ADD) +// } + +// func WebHooksAddPost(ctx *middleware.Context, form auth.NewWebhookForm) { +// ctx.Data["IsRepoToolbarWebHooks"] = true +// ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Webhook" + +// if ctx.HasError() { +// ctx.HTML(200, HOOK_ADD) +// return +// } + +// ct := models.JSON +// if form.ContentType == "2" { +// ct = models.FORM +// } + +// w := &models.Webhook{ +// RepoId: ctx.Repo.Repository.Id, +// Url: form.Url, +// ContentType: ct, +// Secret: form.Secret, +// HookEvent: &models.HookEvent{ +// PushOnly: form.PushOnly, +// }, +// IsActive: form.Active, +// } +// if err := w.UpdateEvent(); err != nil { +// ctx.Handle(500, "setting.WebHooksAddPost(UpdateEvent)", err) +// return +// } else if err := models.CreateWebhook(w); err != nil { +// ctx.Handle(500, "setting.WebHooksAddPost(CreateWebhook)", err) +// return +// } + +// ctx.Flash.Success("New webhook has been added.") +// ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks") +// } + +// func WebHooksEdit(ctx *middleware.Context, params martini.Params) { +// ctx.Data["IsRepoToolbarWebHooks"] = true +// ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhook" + +// hookId, _ := base.StrTo(params["id"]).Int64() +// if hookId == 0 { +// ctx.Handle(404, "setting.WebHooksEdit", nil) +// return +// } + +// w, err := models.GetWebhookById(hookId) +// if err != nil { +// if err == models.ErrWebhookNotExist { +// ctx.Handle(404, "setting.WebHooksEdit(GetWebhookById)", nil) +// } else { +// ctx.Handle(500, "setting.WebHooksEdit(GetWebhookById)", err) +// } +// return +// } + +// w.GetEvent() +// ctx.Data["Webhook"] = w +// ctx.HTML(200, HOOK_EDIT) +// } + +// func WebHooksEditPost(ctx *middleware.Context, params martini.Params, form auth.NewWebhookForm) { +// ctx.Data["IsRepoToolbarWebHooks"] = true +// ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhook" + +// hookId, _ := base.StrTo(params["id"]).Int64() +// if hookId == 0 { +// ctx.Handle(404, "setting.WebHooksEditPost", nil) +// return +// } + +// w, err := models.GetWebhookById(hookId) +// if err != nil { +// if err == models.ErrWebhookNotExist { +// ctx.Handle(404, "setting.WebHooksEditPost(GetWebhookById)", nil) +// } else { +// ctx.Handle(500, "setting.WebHooksEditPost(GetWebhookById)", err) +// } +// return +// } + +// if ctx.HasError() { +// ctx.HTML(200, HOOK_EDIT) +// return +// } + +// ct := models.JSON +// if form.ContentType == "2" { +// ct = models.FORM +// } + +// w.Url = form.Url +// w.ContentType = ct +// w.Secret = form.Secret +// w.HookEvent = &models.HookEvent{ +// PushOnly: form.PushOnly, +// } +// w.IsActive = form.Active +// if err := w.UpdateEvent(); err != nil { +// ctx.Handle(500, "setting.WebHooksEditPost(UpdateEvent)", err) +// return +// } else if err := models.UpdateWebhook(w); err != nil { +// ctx.Handle(500, "setting.WebHooksEditPost(WebHooksEditPost)", err) +// return +// } + +// ctx.Flash.Success("Webhook has been updated.") +// ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", ctx.Repo.RepoLink, hookId)) +// } diff --git a/routers/repo/view.go b/routers/repo/view.go new file mode 100644 index 0000000000..9562bb785b --- /dev/null +++ b/routers/repo/view.go @@ -0,0 +1,203 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repo + +import ( + "bytes" + "io/ioutil" + "path" + "path/filepath" + "strings" + + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/git" + "github.com/gogits/gogs/modules/middleware" +) + +const ( + HOME base.TplName = "repo/home" +) + +func Home(ctx *middleware.Context) { + ctx.Data["Title"] = ctx.Repo.Repository.Name + + branchName := ctx.Repo.BranchName + userName := ctx.Repo.Owner.Name + repoName := ctx.Repo.Repository.Name + + repoLink := ctx.Repo.RepoLink + branchLink := ctx.Repo.RepoLink + "/src/" + branchName + rawLink := ctx.Repo.RepoLink + "/raw/" + branchName + + // Get tree path + treename := ctx.Params("*") + + if len(treename) > 0 && treename[len(treename)-1] == '/' { + ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1]) + return + } + + ctx.Data["IsRepoToolbarSource"] = true + + isViewBranch := ctx.Repo.IsBranch + ctx.Data["IsViewBranch"] = isViewBranch + + treePath := treename + if len(treePath) != 0 { + treePath = treePath + "/" + } + + entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treename) + if err != nil && err != git.ErrNotExist { + ctx.Handle(404, "GetTreeEntryByPath", err) + return + } + + if len(treename) != 0 && entry == nil { + ctx.Handle(404, "repo.Home", nil) + return + } + + if entry != nil && !entry.IsDir() { + blob := entry.Blob() + + if dataRc, err := blob.Data(); err != nil { + ctx.Handle(404, "blob.Data", err) + return + } else { + ctx.Data["FileSize"] = blob.Size() + ctx.Data["IsFile"] = true + ctx.Data["FileName"] = blob.Name() + ext := path.Ext(blob.Name()) + if len(ext) > 0 { + ext = ext[1:] + } + ctx.Data["FileExt"] = ext + ctx.Data["FileLink"] = rawLink + "/" + treename + + buf := make([]byte, 1024) + n, _ := dataRc.Read(buf) + if n > 0 { + buf = buf[:n] + } + + _, isTextFile := base.IsTextFile(buf) + _, isImageFile := base.IsImageFile(buf) + ctx.Data["IsFileText"] = isTextFile + + switch { + case isImageFile: + ctx.Data["IsImageFile"] = true + case isTextFile: + d, _ := ioutil.ReadAll(dataRc) + buf = append(buf, d...) + readmeExist := base.IsMarkdownFile(blob.Name()) || base.IsReadmeFile(blob.Name()) + ctx.Data["ReadmeExist"] = readmeExist + if readmeExist { + ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, "")) + } else { + ctx.Data["FileContent"] = string(buf) + } + } + } + } else { + // Directory and file list. + tree, err := ctx.Repo.Commit.SubTree(treename) + if err != nil { + ctx.Handle(404, "SubTree", err) + return + } + entries, err := tree.ListEntries(treename) + if err != nil { + ctx.Handle(500, "ListEntries", err) + return + } + entries.Sort() + + files := make([][]interface{}, 0, len(entries)) + + for _, te := range entries { + c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name())) + if err != nil { + ctx.Handle(404, "GetCommitOfRelPath", err) + return + } + + files = append(files, []interface{}{te, c}) + } + + ctx.Data["Files"] = files + + var readmeFile *git.Blob + + for _, f := range entries { + if f.IsDir() || !base.IsReadmeFile(f.Name()) { + continue + } else { + readmeFile = f.Blob() + break + } + } + + if readmeFile != nil { + ctx.Data["ReadmeInHome"] = true + ctx.Data["ReadmeExist"] = true + if dataRc, err := readmeFile.Data(); err != nil { + ctx.Handle(404, "repo.SinglereadmeFile.LookupBlob", err) + return + } else { + + buf := make([]byte, 1024) + n, _ := dataRc.Read(buf) + if n > 0 { + buf = buf[:n] + } + + ctx.Data["FileSize"] = readmeFile.Size() + ctx.Data["FileLink"] = rawLink + "/" + treename + _, isTextFile := base.IsTextFile(buf) + ctx.Data["FileIsText"] = isTextFile + ctx.Data["FileName"] = readmeFile.Name() + if isTextFile { + d, _ := ioutil.ReadAll(dataRc) + buf = append(buf, d...) + switch { + case base.IsMarkdownFile(readmeFile.Name()): + buf = base.RenderMarkdown(buf, branchLink) + default: + buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1) + } + ctx.Data["FileContent"] = string(buf) + } + } + } + } + + ctx.Data["Username"] = userName + ctx.Data["Reponame"] = repoName + + var treenames []string + Paths := make([]string, 0) + + if len(treename) > 0 { + treenames = strings.Split(treename, "/") + for i, _ := range treenames { + Paths = append(Paths, strings.Join(treenames[0:i+1], "/")) + } + + ctx.Data["HasParentPath"] = true + if len(Paths)-2 >= 0 { + ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2] + } + } + + ctx.Data["LastCommit"] = ctx.Repo.Commit + ctx.Data["Paths"] = Paths + ctx.Data["TreeName"] = treename + ctx.Data["Treenames"] = treenames + ctx.Data["TreePath"] = treePath + ctx.Data["BranchLink"] = branchLink + ctx.HTML(200, HOME) +} diff --git a/routers/user/auth.go b/routers/user/auth.go new file mode 100644 index 0000000000..163df34a4a --- /dev/null +++ b/routers/user/auth.go @@ -0,0 +1,442 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package user + +import ( + "net/url" + "strings" + + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/auth" + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/captcha" + "github.com/gogits/gogs/modules/log" + // "github.com/gogits/gogs/modules/mailer" + "github.com/gogits/gogs/modules/middleware" + "github.com/gogits/gogs/modules/setting" +) + +const ( + SIGNIN base.TplName = "user/signin" + SIGNUP base.TplName = "user/signup" + DELETE base.TplName = "user/delete" + ACTIVATE base.TplName = "user/activate" + FORGOT_PASSWORD base.TplName = "user/forgot_passwd" + RESET_PASSWORD base.TplName = "user/reset_passwd" +) + +func SignIn(ctx *middleware.Context) { + ctx.Data["Title"] = ctx.Tr("sign_in") + + // if _, ok := ctx.Session.Get("socialId").(int64); ok { + // ctx.Data["IsSocialLogin"] = true + // ctx.HTML(200, SIGNIN) + // return + // } + + // if setting.OauthService != nil { + // ctx.Data["OauthEnabled"] = true + // ctx.Data["OauthService"] = setting.OauthService + // } + + // Check auto-login. + uname := ctx.GetCookie(setting.CookieUserName) + if len(uname) == 0 { + ctx.HTML(200, SIGNIN) + return + } + + isSucceed := false + defer func() { + if !isSucceed { + log.Trace("auto-login cookie cleared: %s", uname) + ctx.SetCookie(setting.CookieUserName, "", -1) + ctx.SetCookie(setting.CookieRememberName, "", -1) + return + } + }() + + u, err := models.GetUserByName(uname) + if err != nil { + if err != models.ErrUserNotExist { + ctx.Handle(500, "GetUserByName", err) + } + return + } + + if val, _ := ctx.GetSuperSecureCookie( + base.EncodeMd5(u.Rands+u.Passwd), setting.CookieRememberName); val != u.Name { + ctx.HTML(200, SIGNIN) + return + } + + isSucceed = true + + ctx.Session.Set("uid", u.Id) + ctx.Session.Set("uname", u.Name) + if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 { + ctx.SetCookie("redirect_to", "", -1) + ctx.Redirect(redirectTo) + return + } + + ctx.Redirect("/") +} + +func SignInPost(ctx *middleware.Context, form auth.SignInForm) { + ctx.Data["Title"] = ctx.Tr("sign_in") + + // sid, isOauth := ctx.Session.Get("socialId").(int64) + // if isOauth { + // ctx.Data["IsSocialLogin"] = true + // } else if setting.OauthService != nil { + // ctx.Data["OauthEnabled"] = true + // ctx.Data["OauthService"] = setting.OauthService + // } + + if ctx.HasError() { + ctx.HTML(200, SIGNIN) + return + } + + u, err := models.UserSignIn(form.UserName, form.Password) + if err != nil { + if err == models.ErrUserNotExist { + ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), SIGNIN, &form) + return + } + + ctx.Handle(500, "UserSignIn", err) + return + } + + if form.Remember { + days := 86400 * setting.LogInRememberDays + ctx.SetCookie(setting.CookieUserName, u.Name, days) + ctx.SetSuperSecureCookie(base.EncodeMd5(u.Rands+u.Passwd), + setting.CookieRememberName, u.Name, days) + } + + // Bind with social account. + // if isOauth { + // if err = models.BindUserOauth2(user.Id, sid); err != nil { + // if err == models.ErrOauth2RecordNotExist { + // ctx.Handle(404, "user.SignInPost(GetOauth2ById)", err) + // } else { + // ctx.Handle(500, "user.SignInPost(GetOauth2ById)", err) + // } + // return + // } + // ctx.Session.Delete("socialId") + // log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid) + // } + + ctx.Session.Set("uid", u.Id) + ctx.Session.Set("uname", u.Name) + if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 { + ctx.SetCookie("redirect_to", "", -1) + ctx.Redirect(redirectTo) + return + } + + ctx.Redirect("/") +} + +func SignOut(ctx *middleware.Context) { + ctx.Session.Delete("uid") + ctx.Session.Delete("uname") + // ctx.Session.Delete("socialId") + // ctx.Session.Delete("socialName") + // ctx.Session.Delete("socialEmail") + ctx.SetCookie(setting.CookieUserName, "", -1) + ctx.SetCookie(setting.CookieRememberName, "", -1) + ctx.Redirect("/") +} + +func SignUp(ctx *middleware.Context) { + ctx.Data["Title"] = ctx.Tr("sign_up") + + if setting.Service.DisableRegistration { + ctx.Data["DisableRegistration"] = true + ctx.HTML(200, SIGNUP) + return + } + + // if sid, ok := ctx.Session.Get("socialId").(int64); ok { + // oauthSignUp(ctx, sid) + // return + // } + + ctx.HTML(200, SIGNUP) +} + +// func oauthSignUp(ctx *middleware.Context, sid int64) { +// ctx.Data["Title"] = "OAuth Sign Up" +// ctx.Data["PageIsSignUp"] = true + +// if _, err := models.GetOauth2ById(sid); err != nil { +// if err == models.ErrOauth2RecordNotExist { +// ctx.Handle(404, "user.oauthSignUp(GetOauth2ById)", err) +// } else { +// ctx.Handle(500, "user.oauthSignUp(GetOauth2ById)", err) +// } +// return +// } + +// ctx.Data["IsSocialLogin"] = true +// ctx.Data["username"] = strings.Replace(ctx.Session.Get("socialName").(string), " ", "", -1) +// ctx.Data["email"] = ctx.Session.Get("socialEmail") +// log.Trace("user.oauthSignUp(social ID): %v", ctx.Session.Get("socialId")) +// ctx.HTML(200, SIGNUP) +// } + +func SignUpPost(ctx *middleware.Context, cpt *captcha.Captcha, form auth.RegisterForm) { + ctx.Data["Title"] = ctx.Tr("sign_up") + + if setting.Service.DisableRegistration { + ctx.Error(403) + return + } + + isOauth := false + // sid, isOauth := ctx.Session.Get("socialId").(int64) + // if isOauth { + // ctx.Data["IsSocialLogin"] = true + // } + + // May redirect from home page. + if ctx.Query("from") == "home" { + // Clear input error box. + ctx.Data["Err_UserName"] = false + ctx.Data["Err_Email"] = false + + // Make the best guess. + uname := ctx.Query("uname") + i := strings.Index(uname, "@") + if i > -1 { + ctx.Data["email"] = uname + ctx.Data["uname"] = uname[:i] + } else { + ctx.Data["uname"] = uname + } + ctx.Data["password"] = ctx.Query("password") + ctx.HTML(200, SIGNUP) + return + } + + if ctx.HasError() { + ctx.HTML(200, SIGNUP) + return + } + + if !cpt.VerifyReq(ctx.Req) { + ctx.Data["Err_Captcha"] = true + ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), SIGNUP, &form) + return + } else if form.Password != form.Retype { + ctx.Data["Err_Password"] = true + ctx.RenderWithErr(ctx.Tr("form.password_not_match"), SIGNUP, &form) + return + } + + u := &models.User{ + Name: form.UserName, + Email: form.Email, + Passwd: form.Password, + IsActive: !setting.Service.RegisterEmailConfirm || isOauth, + } + + if err := models.CreateUser(u); err != nil { + switch err { + case models.ErrUserAlreadyExist: + ctx.Data["Err_UserName"] = true + ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SIGNUP, &form) + case models.ErrEmailAlreadyUsed: + ctx.Data["Err_Email"] = true + ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SIGNUP, &form) + case models.ErrUserNameIllegal: + ctx.Data["Err_UserName"] = true + ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SIGNUP, &form) + default: + ctx.Handle(500, "CreateUser", err) + } + return + } + log.Trace("Account created: %s", u.Name) + + // Bind social account. + // if isOauth { + // if err = models.BindUserOauth2(u.Id, sid); err != nil { + // ctx.Handle(500, "user.SignUp(BindUserOauth2)", err) + // return + // } + // ctx.Session.Delete("socialId") + // log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid) + // } + + // Send confirmation e-mail, no need for social account. + // if !isOauth && setting.Service.RegisterEmailConfirm && u.Id > 1 { + // mailer.SendRegisterMail(ctx.Render, u) + // ctx.Data["IsSendRegisterMail"] = true + // ctx.Data["Email"] = u.Email + // ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 + // ctx.HTML(200, "user/activate") + + // if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { + // log.Error("Set cache(MailResendLimit) fail: %v", err) + // } + // return + // } + + ctx.Redirect("/user/login") +} + +func Activate(ctx *middleware.Context) { + // code := ctx.Query("code") + // if len(code) == 0 { + // ctx.Data["IsActivatePage"] = true + // if ctx.User.IsActive { + // ctx.Handle(404, "user.Activate", nil) + // return + // } + // // Resend confirmation e-mail. + // if setting.Service.RegisterEmailConfirm { + // if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) { + // ctx.Data["ResendLimited"] = true + // } else { + // ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 + // mailer.SendActiveMail(ctx.Render, ctx.User) + + // if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { + // log.Error("Set cache(MailResendLimit) fail: %v", err) + // } + // } + // } else { + // ctx.Data["ServiceNotEnabled"] = true + // } + // ctx.HTML(200, ACTIVATE) + // return + // } + + // // Verify code. + // if user := models.VerifyUserActiveCode(code); user != nil { + // user.IsActive = true + // user.Rands = models.GetUserSalt() + // if err := models.UpdateUser(user); err != nil { + // ctx.Handle(404, "user.Activate", err) + // return + // } + + // log.Trace("%s User activated: %s", ctx.Req.RequestURI, user.Name) + + // ctx.Session.Set("userId", user.Id) + // ctx.Session.Set("userName", user.Name) + // ctx.Redirect("/") + // return + // } + + // ctx.Data["IsActivateFailed"] = true + // ctx.HTML(200, ACTIVATE) +} + +func ForgotPasswd(ctx *middleware.Context) { + ctx.Data["Title"] = "Forgot Password" + + if setting.MailService == nil { + ctx.Data["IsResetDisable"] = true + ctx.HTML(200, FORGOT_PASSWORD) + return + } + + ctx.Data["IsResetRequest"] = true + ctx.HTML(200, FORGOT_PASSWORD) +} + +func ForgotPasswdPost(ctx *middleware.Context) { + // ctx.Data["Title"] = "Forgot Password" + + // if setting.MailService == nil { + // ctx.Handle(403, "user.ForgotPasswdPost", nil) + // return + // } + // ctx.Data["IsResetRequest"] = true + + // email := ctx.Query("email") + // u, err := models.GetUserByEmail(email) + // if err != nil { + // if err == models.ErrUserNotExist { + // ctx.RenderWithErr("This e-mail address does not associate to any account.", "user/forgot_passwd", nil) + // } else { + // ctx.Handle(500, "user.ResetPasswd(check existence)", err) + // } + // return + // } + + // if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) { + // ctx.Data["ResendLimited"] = true + // ctx.HTML(200, FORGOT_PASSWORD) + // return + // } + + // mailer.SendResetPasswdMail(ctx.Render, u) + // if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { + // log.Error("Set cache(MailResendLimit) fail: %v", err) + // } + + // ctx.Data["Email"] = email + // ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 + // ctx.Data["IsResetSent"] = true + // ctx.HTML(200, FORGOT_PASSWORD) +} + +func ResetPasswd(ctx *middleware.Context) { + ctx.Data["Title"] = "Reset Password" + + code := ctx.Query("code") + if len(code) == 0 { + ctx.Error(404) + return + } + ctx.Data["Code"] = code + ctx.Data["IsResetForm"] = true + ctx.HTML(200, RESET_PASSWORD) +} + +func ResetPasswdPost(ctx *middleware.Context) { + // ctx.Data["Title"] = "Reset Password" + + // code := ctx.Query("code") + // if len(code) == 0 { + // ctx.Error(404) + // return + // } + // ctx.Data["Code"] = code + + // if u := models.VerifyUserActiveCode(code); u != nil { + // // Validate password length. + // passwd := ctx.Query("passwd") + // if len(passwd) < 6 || len(passwd) > 30 { + // ctx.Data["IsResetForm"] = true + // ctx.RenderWithErr("Password length should be in 6 and 30.", "user/reset_passwd", nil) + // return + // } + + // u.Passwd = passwd + // u.Rands = models.GetUserSalt() + // u.Salt = models.GetUserSalt() + // u.EncodePasswd() + // if err := models.UpdateUser(u); err != nil { + // ctx.Handle(500, "user.ResetPasswd(UpdateUser)", err) + // return + // } + + // log.Trace("%s User password reset: %s", ctx.Req.RequestURI, u.Name) + // ctx.Redirect("/user/login") + // return + // } + + // ctx.Data["IsResetFailed"] = true + // ctx.HTML(200, RESET_PASSWORD) +} diff --git a/routers/user/home.go b/routers/user/home.go index 02dc1de154..16e88a9427 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -8,50 +8,49 @@ import ( "fmt" "github.com/Unknwon/com" - "github.com/go-martini/martini" "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" ) const ( - DASHBOARD base.TplName = "user/dashboard" - PROFILE base.TplName = "user/profile" + DASHBOARD base.TplName = "user/dashboard/dashboard" ISSUES base.TplName = "user/issues" PULLS base.TplName = "user/pulls" STARS base.TplName = "user/stars" + PROFILE base.TplName = "user/profile" ) func Dashboard(ctx *middleware.Context) { - ctx.Data["Title"] = "Dashboard" - ctx.Data["PageIsUserDashboard"] = true - - if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) - return - } - ctx.Data["Orgs"] = ctx.User.Orgs + ctx.Data["Title"] = ctx.Tr("dashboard") + ctx.Data["PageIsDashboard"] = true + ctx.Data["PageIsNews"] = true + + // if err := ctx.User.GetOrganizations(); err != nil { + // ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) + // return + // } + // ctx.Data["Orgs"] = ctx.User.Orgs ctx.Data["ContextUser"] = ctx.User - var err error - ctx.Data["MyRepos"], err = models.GetRepositories(ctx.User.Id, true) + repos, err := models.GetRepositories(ctx.User.Id, true) if err != nil { - ctx.Handle(500, "home.Dashboard(GetRepositories)", err) + ctx.Handle(500, "GetRepositories", err) return } + ctx.Data["Repos"] = repos - ctx.Data["CollaborativeRepos"], err = models.GetCollaborativeRepos(ctx.User.Name) - if err != nil { - ctx.Handle(500, "home.Dashboard(GetCollaborativeRepos)", err) - return - } + // ctx.Data["CollaborativeRepos"], err = models.GetCollaborativeRepos(ctx.User.Name) + // if err != nil { + // ctx.Handle(500, "home.Dashboard(GetCollaborativeRepos)", err) + // return + // } - actions, err := models.GetFeeds(ctx.User.Id, 0, false) + actions, err := models.GetFeeds(ctx.User.Id, 0, true) if err != nil { - ctx.Handle(500, "home.Dashboard(GetFeeds)", err) + ctx.Handle(500, "GetFeeds", err) return } @@ -70,11 +69,11 @@ func Dashboard(ctx *middleware.Context) { ctx.HTML(200, DASHBOARD) } -func Profile(ctx *middleware.Context, params martini.Params) { +func Profile(ctx *middleware.Context) { ctx.Data["Title"] = "Profile" ctx.Data["PageIsUserProfile"] = true - u, err := models.GetUserByName(params["username"]) + u, err := models.GetUserByName(ctx.Params(":username")) if err != nil { if err == models.ErrUserNotExist { ctx.Handle(404, "user.Profile(GetUserByName)", err) @@ -133,26 +132,26 @@ const ( <div class="info"><span class="meta">%s</span><br>%s</div>` ) -func Feeds(ctx *middleware.Context, form auth.FeedsForm) { - actions, err := models.GetFeeds(form.UserId, form.Page*20, false) - if err != nil { - ctx.JSON(500, err) - return - } - - feeds := make([]string, 0, len(actions)) - for _, act := range actions { - if act.IsPrivate { - if has, _ := models.HasAccess(ctx.User.Name, act.RepoUserName+"/"+act.RepoName, - models.READABLE); !has { - continue - } - } - feeds = append(feeds, fmt.Sprintf(TPL_FEED, base.ActionIcon(act.OpType), - base.TimeSince(act.Created), base.ActionDesc(act))) - } - ctx.JSON(200, &feeds) -} +// func Feeds(ctx *middleware.Context, form auth.FeedsForm) { +// actions, err := models.GetFeeds(form.UserId, form.Page*20, false) +// if err != nil { +// ctx.JSON(500, err) +// return +// } + +// feeds := make([]string, 0, len(actions)) +// for _, act := range actions { +// if act.IsPrivate { +// if has, _ := models.HasAccess(ctx.User.Name, act.RepoUserName+"/"+act.RepoName, +// models.READABLE); !has { +// continue +// } +// } +// feeds = append(feeds, fmt.Sprintf(TPL_FEED, base.ActionIcon(act.OpType), +// base.TimeSince(act.Created), base.ActionDesc(act))) +// } +// ctx.JSON(200, &feeds) +// } func Issues(ctx *middleware.Context) { ctx.Data["Title"] = "Your Issues" @@ -173,7 +172,7 @@ func Issues(ctx *middleware.Context) { filterMode = models.FM_CREATE } - repoId, _ := base.StrTo(ctx.Query("repoid")).Int64() + repoId, _ := com.StrTo(ctx.Query("repoid")).Int64() issueStats := models.GetUserIssueStats(ctx.User.Id, filterMode) // Get all repositories. @@ -215,7 +214,7 @@ func Issues(ctx *middleware.Context) { repoIds = []int64{repoId} } - page, _ := base.StrTo(ctx.Query("page")).Int() + page, _ := com.StrTo(ctx.Query("page")).Int() // Get all issues. var ius []*models.IssueUser diff --git a/routers/user/setting.go b/routers/user/setting.go index c38b4cb577..e4d6ff9ce7 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -5,76 +5,62 @@ package user import ( - "errors" - "fmt" - "io/ioutil" - "os" - "strconv" - "strings" + "github.com/Unknwon/com" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" - "github.com/gogits/gogs/modules/process" ) const ( - SETTING base.TplName = "user/setting" - SOCIAL base.TplName = "user/social" - PASSWORD base.TplName = "user/password" - PUBLICKEY base.TplName = "user/publickey" - NOTIFICATION base.TplName = "user/notification" - SECURITY base.TplName = "user/security" + SETTINGS_PROFILE base.TplName = "user/settings/profile" + SETTINGS_PASSWORD base.TplName = "user/settings/password" + SETTINGS_SSH_KEYS base.TplName = "user/settings/sshkeys" + SETTINGS_SOCIAL base.TplName = "user/settings/social" + SETTINGS_DELETE base.TplName = "user/settings/delete" + NOTIFICATION base.TplName = "user/notification" + SECURITY base.TplName = "user/security" ) -var ( - MinimumKeySize = map[string]int{ - "(ED25519)": 256, - "(ECDSA)": 256, - "(NTRU)": 1087, - "(MCE)": 1702, - "(McE)": 1702, - "(RSA)": 2048, - } -) - -func Setting(ctx *middleware.Context) { - ctx.Data["Title"] = "Setting" - ctx.Data["PageIsUserSetting"] = true - ctx.Data["IsUserPageSetting"] = true - ctx.Data["Owner"] = ctx.User - ctx.HTML(200, SETTING) +func Settings(ctx *middleware.Context) { + ctx.Data["Title"] = ctx.Tr("settings") + ctx.Data["PageIsUserSettings"] = true + ctx.Data["PageIsSettingsProfile"] = true + ctx.HTML(200, SETTINGS_PROFILE) } -func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) { - ctx.Data["Title"] = "Setting" - ctx.Data["PageIsUserSetting"] = true - ctx.Data["IsUserPageSetting"] = true +func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) { + ctx.Data["Title"] = ctx.Tr("settings") + ctx.Data["PageIsUserSettings"] = true + ctx.Data["PageIsSettingsProfile"] = true if ctx.HasError() { - ctx.HTML(200, SETTING) + ctx.HTML(200, SETTINGS_PROFILE) return } - ctx.Data["Owner"] = ctx.User - // Check if user name has been changed. if ctx.User.Name != form.UserName { isExist, err := models.IsUserExist(form.UserName) if err != nil { - ctx.Handle(500, "user.SettingPost(update: check existence)", err) + ctx.Handle(500, "IsUserExist", err) return } else if isExist { - ctx.RenderWithErr("User name has been taken.", SETTING, &form) + ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_PROFILE, &form) return } else if err = models.ChangeUserName(ctx.User, form.UserName); err != nil { - ctx.Handle(500, "user.SettingPost(change user name)", err) + if err == models.ErrUserNameIllegal { + ctx.Flash.Error(ctx.Tr("form.illegal_username")) + ctx.Redirect("/user/settings") + return + } else { + ctx.Handle(500, "ChangeUserName", err) + } return } - log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, ctx.User.Name, form.UserName) - + log.Trace("User name changed: %s -> %s", ctx.User.Name, form.UserName) ctx.User.Name = form.UserName } @@ -85,213 +71,197 @@ func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) { ctx.User.Avatar = base.EncodeMd5(form.Avatar) ctx.User.AvatarEmail = form.Avatar if err := models.UpdateUser(ctx.User); err != nil { - ctx.Handle(500, "setting.SettingPost(UpdateUser)", err) + ctx.Handle(500, "UpdateUser", err) return } - log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName) - ctx.Flash.Success("Your profile has been successfully updated.") + log.Trace("User setting updated: %s", ctx.User.Name) + ctx.Flash.Success(ctx.Tr("settings.update_profile_success")) ctx.Redirect("/user/settings") } -func SettingSocial(ctx *middleware.Context) { - ctx.Data["Title"] = "Social Account" - ctx.Data["PageIsUserSetting"] = true - ctx.Data["IsUserPageSettingSocial"] = true - - // Unbind social account. - remove, _ := base.StrTo(ctx.Query("remove")).Int64() - if remove > 0 { - if err := models.DeleteOauth2ById(remove); err != nil { - ctx.Handle(500, "user.SettingSocial(DeleteOauth2ById)", err) - return - } - ctx.Flash.Success("OAuth2 has been unbinded.") - ctx.Redirect("/user/settings/social") - return - } - - var err error - ctx.Data["Socials"], err = models.GetOauthByUserId(ctx.User.Id) - if err != nil { - ctx.Handle(500, "user.SettingSocial(GetOauthByUserId)", err) - return - } - ctx.HTML(200, SOCIAL) -} - -func SettingPassword(ctx *middleware.Context) { - ctx.Data["Title"] = "Password" - ctx.Data["PageIsUserSetting"] = true - ctx.Data["IsUserPageSettingPasswd"] = true - ctx.HTML(200, PASSWORD) +func SettingsPassword(ctx *middleware.Context) { + ctx.Data["Title"] = ctx.Tr("settings") + ctx.Data["PageIsUserSettings"] = true + ctx.Data["PageIsSettingsPassword"] = true + ctx.HTML(200, SETTINGS_PASSWORD) } -func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) { - ctx.Data["Title"] = "Password" - ctx.Data["PageIsUserSetting"] = true - ctx.Data["IsUserPageSettingPasswd"] = true +func SettingsPasswordPost(ctx *middleware.Context, form auth.ChangePasswordForm) { + ctx.Data["Title"] = ctx.Tr("settings") + ctx.Data["PageIsUserSettings"] = true + ctx.Data["PageIsSettingsPassword"] = true if ctx.HasError() { - ctx.HTML(200, PASSWORD) + ctx.HTML(200, SETTINGS_PASSWORD) return } tmpUser := &models.User{ - Passwd: form.OldPasswd, + Passwd: form.OldPassword, Salt: ctx.User.Salt, } tmpUser.EncodePasswd() if ctx.User.Passwd != tmpUser.Passwd { - ctx.Flash.Error("Old password is not correct.") - } else if form.NewPasswd != form.RetypePasswd { - ctx.Flash.Error("New password and re-type password are not same.") + ctx.Flash.Error(ctx.Tr("settings.password_incorrect")) + } else if form.Password != form.Retype { + ctx.Flash.Error(ctx.Tr("form.password_not_match")) } else { - ctx.User.Passwd = form.NewPasswd + ctx.User.Passwd = form.Password ctx.User.Salt = models.GetUserSalt() ctx.User.EncodePasswd() if err := models.UpdateUser(ctx.User); err != nil { - ctx.Handle(200, "setting.SettingPassword", err) + ctx.Handle(500, "UpdateUser", err) return } - log.Trace("%s User password updated: %s", ctx.Req.RequestURI, ctx.User.LowerName) - ctx.Flash.Success("Password is changed successfully. You can now sign in via new password.") + log.Trace("User password updated: %s", ctx.User.Name) + ctx.Flash.Success(ctx.Tr("settings.change_password_success")) } + ctx.Redirect("/user/settings/password") } -// Checks if the given public key string is recognized by SSH. -func CheckPublicKeyString(keyContent string) (ok bool, err error) { - if strings.ContainsAny(keyContent, "\n\r") { - return false, errors.New("Only a single line with a single key please") - } - - // write the key to a file… - tmpFile, err := ioutil.TempFile(os.TempDir(), "keytest") - if err != nil { - return false, err - } - tmpPath := tmpFile.Name() - defer os.Remove(tmpPath) - tmpFile.WriteString(keyContent) - tmpFile.Close() +func SettingsSSHKeys(ctx *middleware.Context) { + ctx.Data["Title"] = ctx.Tr("settings") + ctx.Data["PageIsUserSettings"] = true + ctx.Data["PageIsSettingsSSHKeys"] = true - // … see if ssh-keygen recognizes its contents - stdout, stderr, err := process.Exec("CheckPublicKeyString", "ssh-keygen", "-l", "-f", tmpPath) - if err != nil { - return false, errors.New("ssh-keygen -l -f: " + stderr) - } else if len(stdout) < 2 { - return false, errors.New("ssh-keygen returned not enough output to evaluate the key") - } - sshKeygenOutput := strings.Split(stdout, " ") - if len(sshKeygenOutput) < 4 { - return false, errors.New("Not enough fields returned by ssh-keygen -l -f") - } - keySize, err := strconv.Atoi(sshKeygenOutput[0]) + var err error + ctx.Data["Keys"], err = models.ListPublicKey(ctx.User.Id) if err != nil { - return false, errors.New("Cannot get key size of the given key") - } - keyType := strings.TrimSpace(sshKeygenOutput[len(sshKeygenOutput)-1]) - - if minimumKeySize := MinimumKeySize[keyType]; minimumKeySize == 0 { - return false, errors.New("Sorry, unrecognized public key type") - } else { - if keySize < minimumKeySize { - return false, fmt.Errorf("The minimum accepted size of a public key %s is %d", keyType, minimumKeySize) - } + ctx.Handle(500, "ssh.ListPublicKey", err) + return } - return true, nil + ctx.HTML(200, SETTINGS_SSH_KEYS) } -func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) { - ctx.Data["Title"] = "SSH Keys" - ctx.Data["PageIsUserSetting"] = true - ctx.Data["IsUserPageSettingSSH"] = true +func SettingsSSHKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) { + ctx.Data["Title"] = ctx.Tr("settings") + ctx.Data["PageIsUserSettings"] = true + ctx.Data["PageIsSettingsSSHKeys"] = true + + var err error + ctx.Data["Keys"], err = models.ListPublicKey(ctx.User.Id) + if err != nil { + ctx.Handle(500, "ssh.ListPublicKey", err) + return + } // Delete SSH key. - if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" { - id, err := base.StrTo(ctx.Query("id")).Int64() - if err != nil { - log.Error("ssh.DelPublicKey: %v", err) - ctx.JSON(200, map[string]interface{}{ - "ok": false, - "err": err.Error(), - }) + if ctx.Query("_method") == "DELETE" { + id := com.StrTo(ctx.Query("id")).MustInt64() + if id <= 0 { return } if err = models.DeletePublicKey(&models.PublicKey{Id: id}); err != nil { - log.Error("ssh.DelPublicKey: %v", err) - ctx.JSON(200, map[string]interface{}{ - "ok": false, - "err": err.Error(), - }) + ctx.Handle(500, "DeletePublicKey", err) } else { - log.Trace("%s User SSH key deleted: %s", ctx.Req.RequestURI, ctx.User.LowerName) - ctx.JSON(200, map[string]interface{}{ - "ok": true, - }) + log.Trace("SSH key deleted: %s", ctx.User.Name) + ctx.Redirect("/user/settings/ssh") } return } - var err error - // List existed SSH keys. - ctx.Data["Keys"], err = models.ListPublicKey(ctx.User.Id) - if err != nil { - ctx.Handle(500, "ssh.ListPublicKey", err) - return - } - // Add new SSH key. if ctx.Req.Method == "POST" { if ctx.HasError() { - ctx.HTML(200, "user/publickey") + ctx.HTML(200, SETTINGS_SSH_KEYS) return } - if ok, err := CheckPublicKeyString(form.KeyContent); !ok { - ctx.Flash.Error(err.Error()) + if ok, err := models.CheckPublicKeyString(form.Content); !ok { + ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error())) ctx.Redirect("/user/settings/ssh") return } k := &models.PublicKey{ OwnerId: ctx.User.Id, - Name: form.KeyName, - Content: form.KeyContent, + Name: form.SSHTitle, + Content: form.Content, } - if err := models.AddPublicKey(k); err != nil { - if err.Error() == models.ErrKeyAlreadyExist.Error() { - ctx.RenderWithErr("Public key name has been used", "user/publickey", &form) + if err == models.ErrKeyAlreadyExist { + ctx.RenderWithErr(ctx.Tr("form.ssh_key_been_used"), SETTINGS_SSH_KEYS, &form) return } ctx.Handle(500, "ssh.AddPublicKey", err) return } else { - log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName) - ctx.Flash.Success("New SSH Key has been added!") + log.Trace("SSH key added: %s", ctx.User.Name) + ctx.Flash.Success(ctx.Tr("settings.add_key_success")) ctx.Redirect("/user/settings/ssh") return } } - ctx.HTML(200, PUBLICKEY) + ctx.HTML(200, SETTINGS_SSH_KEYS) } -func SettingNotification(ctx *middleware.Context) { - // TODO: user setting notification - ctx.Data["Title"] = "Notification" - ctx.Data["PageIsUserSetting"] = true - ctx.Data["IsUserPageSettingNotify"] = true - ctx.HTML(200, NOTIFICATION) +// func SettingSocial(ctx *middleware.Context) { +// ctx.Data["Title"] = "Social Account" +// ctx.Data["PageIsUserSetting"] = true +// ctx.Data["IsUserPageSettingSocial"] = true + +// // Unbind social account. +// remove, _ := base.StrTo(ctx.Query("remove")).Int64() +// if remove > 0 { +// if err := models.DeleteOauth2ById(remove); err != nil { +// ctx.Handle(500, "user.SettingSocial(DeleteOauth2ById)", err) +// return +// } +// ctx.Flash.Success("OAuth2 has been unbinded.") +// ctx.Redirect("/user/settings/social") +// return +// } + +// var err error +// ctx.Data["Socials"], err = models.GetOauthByUserId(ctx.User.Id) +// if err != nil { +// ctx.Handle(500, "user.SettingSocial(GetOauthByUserId)", err) +// return +// } +// ctx.HTML(200, SOCIAL) +// } + +func SettingsSocial(ctx *middleware.Context) { + ctx.Data["Title"] = ctx.Tr("settings") + ctx.Data["PageIsUserSettings"] = true + ctx.Data["PageIsSettingsSocial"] = true + ctx.HTML(200, SETTINGS_SOCIAL) } -func SettingSecurity(ctx *middleware.Context) { - // TODO: user setting security - ctx.Data["Title"] = "Security" - ctx.Data["PageIsUserSetting"] = true - ctx.Data["IsUserPageSettingSecurity"] = true - ctx.HTML(200, SECURITY) +func SettingsDelete(ctx *middleware.Context) { + ctx.Data["Title"] = ctx.Tr("settings") + ctx.Data["PageIsUserSettings"] = true + ctx.Data["PageIsSettingsDelete"] = true + + if ctx.Req.Method == "POST" { + // tmpUser := models.User{ + // Passwd: ctx.Query("password"), + // Salt: ctx.User.Salt, + // } + // tmpUser.EncodePasswd() + // if tmpUser.Passwd != ctx.User.Passwd { + // ctx.Flash.Error("Password is not correct. Make sure you are owner of this account.") + // } else { + if err := models.DeleteUser(ctx.User); err != nil { + switch err { + case models.ErrUserOwnRepos: + ctx.Flash.Error(ctx.Tr("form.still_own_repo")) + ctx.Redirect("/user/settings/delete") + return + default: + ctx.Handle(500, "DeleteUser", err) + return + } + } else { + log.Trace("Account deleted: %s", ctx.User.Name) + ctx.Redirect("/") + return + } + } + + ctx.HTML(200, SETTINGS_DELETE) } diff --git a/routers/user/social.go b/routers/user/social.go index 9a56415fd4..ef83cd5b42 100644 --- a/routers/user/social.go +++ b/routers/user/social.go @@ -11,8 +11,6 @@ import ( "net/url" "strings" - "github.com/go-martini/martini" - "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" @@ -28,14 +26,14 @@ func extractPath(next string) string { return n.Path } -func SocialSignIn(ctx *middleware.Context, params martini.Params) { +func SocialSignIn(ctx *middleware.Context) { if setting.OauthService == nil { ctx.Handle(404, "social.SocialSignIn(oauth service not enabled)", nil) return } next := extractPath(ctx.Query("next")) - name := params["name"] + name := ctx.Params(":name") connect, ok := social.SocialMap[name] if !ok { ctx.Handle(404, "social.SocialSignIn(social login not enabled)", errors.New(name)) @@ -81,7 +79,7 @@ func SocialSignIn(ctx *middleware.Context, params martini.Params) { } log.Trace("social.SocialSignIn(oa): %v", oa) if err = models.AddOauth2(oa); err != nil { - log.Error("social.SocialSignIn(add oauth2): %v", err) // 501 + log.Error(4, "social.SocialSignIn(add oauth2): %v", err) // 501 return } case models.ErrOauth2NotAssociated: diff --git a/routers/user/user.go b/routers/user/user.go deleted file mode 100644 index 561fe1c111..0000000000 --- a/routers/user/user.go +++ /dev/null @@ -1,457 +0,0 @@ -// Copyright 2014 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package user - -import ( - "net/url" - "strings" - - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/auth" - "github.com/gogits/gogs/modules/base" - "github.com/gogits/gogs/modules/log" - "github.com/gogits/gogs/modules/mailer" - "github.com/gogits/gogs/modules/middleware" - "github.com/gogits/gogs/modules/setting" -) - -const ( - SIGNIN base.TplName = "user/signin" - SIGNUP base.TplName = "user/signup" - DELETE base.TplName = "user/delete" - ACTIVATE base.TplName = "user/activate" - FORGOT_PASSWORD base.TplName = "user/forgot_passwd" - RESET_PASSWORD base.TplName = "user/reset_passwd" -) - -func SignIn(ctx *middleware.Context) { - ctx.Data["Title"] = "Log In" - - if _, ok := ctx.Session.Get("socialId").(int64); ok { - ctx.Data["IsSocialLogin"] = true - ctx.HTML(200, SIGNIN) - return - } - - if setting.OauthService != nil { - ctx.Data["OauthEnabled"] = true - ctx.Data["OauthService"] = setting.OauthService - } - - // Check auto-login. - uname := ctx.GetCookie(setting.CookieUserName) - if len(uname) == 0 { - ctx.HTML(200, SIGNIN) - return - } - - isSucceed := false - defer func() { - if !isSucceed { - log.Trace("user.SignIn(auto-login cookie cleared): %s", uname) - ctx.SetCookie(setting.CookieUserName, "", -1) - ctx.SetCookie(setting.CookieRememberName, "", -1) - return - } - }() - - user, err := models.GetUserByName(uname) - if err != nil { - ctx.Handle(500, "user.SignIn(GetUserByName)", err) - return - } - - secret := base.EncodeMd5(user.Rands + user.Passwd) - value, _ := ctx.GetSecureCookie(secret, setting.CookieRememberName) - if value != user.Name { - ctx.HTML(200, SIGNIN) - return - } - - isSucceed = true - - ctx.Session.Set("userId", user.Id) - ctx.Session.Set("userName", user.Name) - if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 { - ctx.SetCookie("redirect_to", "", -1) - ctx.Redirect(redirectTo) - return - } - - ctx.Redirect("/") -} - -func SignInPost(ctx *middleware.Context, form auth.LogInForm) { - ctx.Data["Title"] = "Log In" - - sid, isOauth := ctx.Session.Get("socialId").(int64) - if isOauth { - ctx.Data["IsSocialLogin"] = true - } else if setting.OauthService != nil { - ctx.Data["OauthEnabled"] = true - ctx.Data["OauthService"] = setting.OauthService - } - - if ctx.HasError() { - ctx.HTML(200, SIGNIN) - return - } - - user, err := models.UserSignIn(form.UserName, form.Password) - if err != nil { - if err == models.ErrUserNotExist { - log.Trace("%s Log in failed: %s", ctx.Req.RequestURI, form.UserName) - ctx.RenderWithErr("Username or password is not correct", SIGNIN, &form) - return - } - - ctx.Handle(500, "user.SignInPost(UserSignIn)", err) - return - } - - if form.Remember { - secret := base.EncodeMd5(user.Rands + user.Passwd) - days := 86400 * setting.LogInRememberDays - ctx.SetCookie(setting.CookieUserName, user.Name, days) - ctx.SetSecureCookie(secret, setting.CookieRememberName, user.Name, days) - } - - // Bind with social account. - if isOauth { - if err = models.BindUserOauth2(user.Id, sid); err != nil { - if err == models.ErrOauth2RecordNotExist { - ctx.Handle(404, "user.SignInPost(GetOauth2ById)", err) - } else { - ctx.Handle(500, "user.SignInPost(GetOauth2ById)", err) - } - return - } - ctx.Session.Delete("socialId") - log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid) - } - - ctx.Session.Set("userId", user.Id) - ctx.Session.Set("userName", user.Name) - if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 { - ctx.SetCookie("redirect_to", "", -1) - ctx.Redirect(redirectTo) - return - } - - ctx.Redirect("/") -} - -func SignOut(ctx *middleware.Context) { - ctx.Session.Delete("userId") - ctx.Session.Delete("userName") - ctx.Session.Delete("socialId") - ctx.Session.Delete("socialName") - ctx.Session.Delete("socialEmail") - ctx.SetCookie(setting.CookieUserName, "", -1) - ctx.SetCookie(setting.CookieRememberName, "", -1) - ctx.Redirect("/") -} - -func SignUp(ctx *middleware.Context) { - ctx.Data["Title"] = "Sign Up" - ctx.Data["PageIsSignUp"] = true - - if setting.Service.DisableRegistration { - ctx.Data["DisableRegistration"] = true - ctx.HTML(200, SIGNUP) - return - } - - if sid, ok := ctx.Session.Get("socialId").(int64); ok { - oauthSignUp(ctx, sid) - return - } - - ctx.HTML(200, SIGNUP) -} - -func oauthSignUp(ctx *middleware.Context, sid int64) { - ctx.Data["Title"] = "OAuth Sign Up" - ctx.Data["PageIsSignUp"] = true - - if _, err := models.GetOauth2ById(sid); err != nil { - if err == models.ErrOauth2RecordNotExist { - ctx.Handle(404, "user.oauthSignUp(GetOauth2ById)", err) - } else { - ctx.Handle(500, "user.oauthSignUp(GetOauth2ById)", err) - } - return - } - - ctx.Data["IsSocialLogin"] = true - ctx.Data["username"] = strings.Replace(ctx.Session.Get("socialName").(string), " ", "", -1) - ctx.Data["email"] = ctx.Session.Get("socialEmail") - log.Trace("user.oauthSignUp(social ID): %v", ctx.Session.Get("socialId")) - ctx.HTML(200, SIGNUP) -} - -func SignUpPost(ctx *middleware.Context, form auth.RegisterForm) { - ctx.Data["Title"] = "Sign Up" - ctx.Data["PageIsSignUp"] = true - - if setting.Service.DisableRegistration { - ctx.Handle(403, "user.SignUpPost", nil) - return - } - - sid, isOauth := ctx.Session.Get("socialId").(int64) - if isOauth { - ctx.Data["IsSocialLogin"] = true - } - - if ctx.HasError() { - ctx.HTML(200, SIGNUP) - return - } - - if form.Password != form.RetypePasswd { - ctx.Data["Err_Password"] = true - ctx.Data["Err_RetypePasswd"] = true - ctx.RenderWithErr("Password and re-type password are not same.", SIGNUP, &form) - return - } - - u := &models.User{ - Name: form.UserName, - Email: form.Email, - Passwd: form.Password, - IsActive: !setting.Service.RegisterEmailConfirm || isOauth, - } - - var err error - if u, err = models.CreateUser(u); err != nil { - switch err { - case models.ErrUserAlreadyExist: - ctx.Data["Err_UserName"] = true - ctx.RenderWithErr("Username has been already taken", SIGNUP, &form) - case models.ErrEmailAlreadyUsed: - ctx.Data["Err_Email"] = true - ctx.RenderWithErr("E-mail address has been already used", SIGNUP, &form) - case models.ErrUserNameIllegal: - ctx.Data["Err_UserName"] = true - ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), SIGNUP, &form) - default: - ctx.Handle(500, "user.SignUpPost(CreateUser)", err) - } - return - } - log.Trace("%s User created: %s", ctx.Req.RequestURI, u.Name) - - // Bind social account. - if isOauth { - if err = models.BindUserOauth2(u.Id, sid); err != nil { - ctx.Handle(500, "user.SignUp(BindUserOauth2)", err) - return - } - ctx.Session.Delete("socialId") - log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid) - } - - // Send confirmation e-mail, no need for social account. - if !isOauth && setting.Service.RegisterEmailConfirm && u.Id > 1 { - mailer.SendRegisterMail(ctx.Render, u) - ctx.Data["IsSendRegisterMail"] = true - ctx.Data["Email"] = u.Email - ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 - ctx.HTML(200, "user/activate") - - if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { - log.Error("Set cache(MailResendLimit) fail: %v", err) - } - return - } - - ctx.Redirect("/user/login") -} - -func Delete(ctx *middleware.Context) { - ctx.Data["Title"] = "Delete Account" - ctx.Data["PageIsUserSetting"] = true - ctx.Data["IsUserPageSettingDelete"] = true - ctx.HTML(200, DELETE) -} - -func DeletePost(ctx *middleware.Context) { - ctx.Data["Title"] = "Delete Account" - ctx.Data["PageIsUserSetting"] = true - ctx.Data["IsUserPageSettingDelete"] = true - - tmpUser := models.User{ - Passwd: ctx.Query("password"), - Salt: ctx.User.Salt, - } - tmpUser.EncodePasswd() - if tmpUser.Passwd != ctx.User.Passwd { - ctx.Flash.Error("Password is not correct. Make sure you are owner of this account.") - } else { - if err := models.DeleteUser(ctx.User); err != nil { - switch err { - case models.ErrUserOwnRepos: - ctx.Flash.Error("Your account still have ownership of repository, you have to delete or transfer them first.") - default: - ctx.Handle(500, "user.DeletePost(DeleteUser)", err) - return - } - } else { - ctx.Redirect("/") - return - } - } - - ctx.Redirect("/user/delete") -} - -func Activate(ctx *middleware.Context) { - code := ctx.Query("code") - if len(code) == 0 { - ctx.Data["IsActivatePage"] = true - if ctx.User.IsActive { - ctx.Handle(404, "user.Activate", nil) - return - } - // Resend confirmation e-mail. - if setting.Service.RegisterEmailConfirm { - if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) { - ctx.Data["ResendLimited"] = true - } else { - ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 - mailer.SendActiveMail(ctx.Render, ctx.User) - - if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { - log.Error("Set cache(MailResendLimit) fail: %v", err) - } - } - } else { - ctx.Data["ServiceNotEnabled"] = true - } - ctx.HTML(200, ACTIVATE) - return - } - - // Verify code. - if user := models.VerifyUserActiveCode(code); user != nil { - user.IsActive = true - user.Rands = models.GetUserSalt() - if err := models.UpdateUser(user); err != nil { - ctx.Handle(404, "user.Activate", err) - return - } - - log.Trace("%s User activated: %s", ctx.Req.RequestURI, user.Name) - - ctx.Session.Set("userId", user.Id) - ctx.Session.Set("userName", user.Name) - ctx.Redirect("/") - return - } - - ctx.Data["IsActivateFailed"] = true - ctx.HTML(200, ACTIVATE) -} - -func ForgotPasswd(ctx *middleware.Context) { - ctx.Data["Title"] = "Forgot Password" - - if setting.MailService == nil { - ctx.Data["IsResetDisable"] = true - ctx.HTML(200, FORGOT_PASSWORD) - return - } - - ctx.Data["IsResetRequest"] = true - ctx.HTML(200, FORGOT_PASSWORD) -} - -func ForgotPasswdPost(ctx *middleware.Context) { - ctx.Data["Title"] = "Forgot Password" - - if setting.MailService == nil { - ctx.Handle(403, "user.ForgotPasswdPost", nil) - return - } - ctx.Data["IsResetRequest"] = true - - email := ctx.Query("email") - u, err := models.GetUserByEmail(email) - if err != nil { - if err == models.ErrUserNotExist { - ctx.RenderWithErr("This e-mail address does not associate to any account.", "user/forgot_passwd", nil) - } else { - ctx.Handle(500, "user.ResetPasswd(check existence)", err) - } - return - } - - if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) { - ctx.Data["ResendLimited"] = true - ctx.HTML(200, FORGOT_PASSWORD) - return - } - - mailer.SendResetPasswdMail(ctx.Render, u) - if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { - log.Error("Set cache(MailResendLimit) fail: %v", err) - } - - ctx.Data["Email"] = email - ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 - ctx.Data["IsResetSent"] = true - ctx.HTML(200, FORGOT_PASSWORD) -} - -func ResetPasswd(ctx *middleware.Context) { - ctx.Data["Title"] = "Reset Password" - - code := ctx.Query("code") - if len(code) == 0 { - ctx.Error(404) - return - } - ctx.Data["Code"] = code - ctx.Data["IsResetForm"] = true - ctx.HTML(200, RESET_PASSWORD) -} - -func ResetPasswdPost(ctx *middleware.Context) { - ctx.Data["Title"] = "Reset Password" - - code := ctx.Query("code") - if len(code) == 0 { - ctx.Error(404) - return - } - ctx.Data["Code"] = code - - if u := models.VerifyUserActiveCode(code); u != nil { - // Validate password length. - passwd := ctx.Query("passwd") - if len(passwd) < 6 || len(passwd) > 30 { - ctx.Data["IsResetForm"] = true - ctx.RenderWithErr("Password length should be in 6 and 30.", "user/reset_passwd", nil) - return - } - - u.Passwd = passwd - u.Rands = models.GetUserSalt() - u.Salt = models.GetUserSalt() - u.EncodePasswd() - if err := models.UpdateUser(u); err != nil { - ctx.Handle(500, "user.ResetPasswd(UpdateUser)", err) - return - } - - log.Trace("%s User password reset: %s", ctx.Req.RequestURI, u.Name) - ctx.Redirect("/user/login") - return - } - - ctx.Data["IsResetFailed"] = true - ctx.HTML(200, RESET_PASSWORD) -} |