Finish close and reopen issue, install page, ready going to test stage of v0.2.0

This commit is contained in:
Unknown 2014-03-29 17:50:51 -04:00
parent 1f671e5d29
commit 107a1eadac
15 changed files with 315 additions and 161 deletions

View file

@ -6,13 +6,46 @@ package routers
import (
"errors"
"os"
"strings"
"github.com/Unknwon/goconfig"
"github.com/codegangsta/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"
)
// Check run mode(Default of martini is Dev).
func checkRunMode() {
switch base.Cfg.MustValue("", "RUN_MODE") {
case "prod":
martini.Env = martini.Prod
case "test":
martini.Env = martini.Test
}
log.Info("Run Mode: %s", strings.Title(martini.Env))
}
// GlobalInit is for global configuration reload-able.
func GlobalInit() {
base.NewConfigContext()
mailer.NewMailerContext()
models.LoadModelsConfig()
models.LoadRepoConfig()
models.NewRepoContext()
if err := models.NewEngine(); err != nil && base.InstallLock {
log.Error("%v", err)
os.Exit(2)
}
base.NewServices()
checkRunMode()
}
func Install(ctx *middleware.Context, form auth.InstallForm) {
if base.InstallLock {
ctx.Handle(404, "install.Install", errors.New("Installation is prohibited"))
@ -20,14 +53,114 @@ func Install(ctx *middleware.Context, form auth.InstallForm) {
}
ctx.Data["Title"] = "Install"
ctx.Data["DbCfg"] = models.DbCfg
ctx.Data["RepoRootPath"] = base.RepoRootPath
ctx.Data["RunUser"] = base.RunUser
ctx.Data["AppUrl"] = base.AppUrl
ctx.Data["PageIsInstall"] = true
// Get and assign value to install form.
if len(form.Host) == 0 {
form.Host = models.DbCfg.Host
}
if len(form.User) == 0 {
form.User = models.DbCfg.User
}
if len(form.Passwd) == 0 {
form.Passwd = models.DbCfg.Pwd
}
if len(form.DatabaseName) == 0 {
form.DatabaseName = models.DbCfg.Name
}
if len(form.DatabasePath) == 0 {
form.DatabasePath = models.DbCfg.Path
}
if len(form.RepoRootPath) == 0 {
form.RepoRootPath = base.RepoRootPath
}
if len(form.RunUser) == 0 {
form.RunUser = base.RunUser
}
if len(form.Domain) == 0 {
form.Domain = base.Domain
}
if len(form.AppUrl) == 0 {
form.AppUrl = base.AppUrl
}
auth.AssignForm(form, ctx.Data)
if ctx.Req.Method == "GET" {
ctx.HTML(200, "install")
return
}
if ctx.HasError() {
ctx.HTML(200, "install")
return
}
// Pass basic check, now test configuration.
// Test database setting.
dbTypes := map[string]string{"mysql": "mysql", "pgsql": "postgres", "sqlite": "sqlite3"}
models.DbCfg.Type = dbTypes[form.Database]
models.DbCfg.Host = form.Host
models.DbCfg.User = form.User
models.DbCfg.Pwd = form.Passwd
models.DbCfg.Name = form.DatabaseName
models.DbCfg.SslMode = form.SslMode
models.DbCfg.Path = form.DatabasePath
if err := models.NewEngine(); err != nil {
ctx.RenderWithErr("Database setting is not correct: "+err.Error(), "install", &form)
return
}
// Test repository root path.
if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
ctx.RenderWithErr("Repository root path is invalid: "+err.Error(), "install", &form)
return
}
// Create admin account.
if _, err := models.RegisterUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
IsAdmin: true, IsActive: true}); err != nil {
if err != models.ErrUserAlreadyExist {
ctx.RenderWithErr("Admin account setting is invalid: "+err.Error(), "install", &form)
return
}
}
// Save settings.
base.Cfg.SetValue("database", "DB_TYPE", models.DbCfg.Type)
base.Cfg.SetValue("database", "HOST", models.DbCfg.Host)
base.Cfg.SetValue("database", "NAME", models.DbCfg.Name)
base.Cfg.SetValue("database", "USER", models.DbCfg.User)
base.Cfg.SetValue("database", "PASSWD", models.DbCfg.Pwd)
base.Cfg.SetValue("database", "SSL_MODE", models.DbCfg.SslMode)
base.Cfg.SetValue("database", "PATH", models.DbCfg.Path)
base.Cfg.SetValue("repository", "ROOT", form.RepoRootPath)
base.Cfg.SetValue("", "RUN_USER", form.RunUser)
base.Cfg.SetValue("server", "DOMAIN", form.Domain)
base.Cfg.SetValue("server", "ROOT_URL", form.AppUrl)
if len(form.Host) > 0 {
base.Cfg.SetValue("mailer", "ENABLED", "true")
base.Cfg.SetValue("mailer", "HOST", form.SmtpHost)
base.Cfg.SetValue("mailer", "USER", form.SmtpEmail)
base.Cfg.SetValue("mailer", "PASSWD", form.SmtpPasswd)
base.Cfg.SetValue("service", "REGISTER_EMAIL_CONFIRM", base.ToStr(form.RegisterConfirm == "on"))
base.Cfg.SetValue("service", "ENABLE_NOTIFY_MAIL", base.ToStr(form.MailNotify == "on"))
}
base.Cfg.SetValue("security", "INSTALL_LOCK", "true")
if err := goconfig.SaveConfigFile(base.Cfg, "custom/conf/app.ini"); err != nil {
ctx.RenderWithErr("Fail to save configuration: "+err.Error(), "install", &form)
return
}
GlobalInit()
log.Info("First-time run install finished!")
ctx.Redirect("/user/login")
}

View file

@ -7,6 +7,7 @@ package repo
import (
"fmt"
"net/url"
"strings"
"github.com/codegangsta/martini"
@ -175,7 +176,7 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) {
ctx.Data["Title"] = issue.Name
ctx.Data["Issue"] = issue
ctx.Data["Comments"] = comments
ctx.Data["IsIssueOwner"] = issue.PosterId == ctx.User.Id
ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner || issue.PosterId == ctx.User.Id
ctx.Data["IsRepoToolbarIssues"] = true
ctx.Data["IsRepoToolbarIssuesList"] = false
ctx.HTML(200, "issue/view")
@ -225,24 +226,17 @@ func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat
}
func Comment(ctx *middleware.Context, params martini.Params) {
fmt.Println(ctx.Query("change_status"))
if !ctx.Repo.IsValid {
ctx.Handle(404, "issue.Comment(invalid repo):", nil)
}
index, err := base.StrTo(ctx.Query("issueIndex")).Int()
index, err := base.StrTo(ctx.Query("issueIndex")).Int64()
if err != nil {
ctx.Handle(404, "issue.Comment", err)
ctx.Handle(404, "issue.Comment(get index)", err)
return
}
content := ctx.Query("content")
if len(content) == 0 {
ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", ctx.User.Name, ctx.Repo.Repository.Name, index))
return
}
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, index)
if err != nil {
if err == models.ErrIssueNotExist {
ctx.Handle(404, "issue.Comment", err)
@ -252,16 +246,46 @@ func Comment(ctx *middleware.Context, params martini.Params) {
return
}
switch params["action"] {
case "new":
if err = models.CreateComment(ctx.User.Id, issue.Id, 0, 0, content); err != nil {
ctx.Handle(500, "issue.Comment(create comment)", err)
// 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 {
ctx.Handle(200, "issue.Comment(update issue status)", err)
return
}
cmtType := models.IT_CLOSE
if !issue.IsClosed {
cmtType = models.IT_REOPEN
}
if err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, cmtType, ""); err != nil {
ctx.Handle(200, "issue.Comment(create status change comment)", err)
return
}
log.Trace("%s Issue(%d) status changed: %v", ctx.Req.RequestURI, issue.Id, !issue.IsClosed)
}
}
content := ctx.Query("content")
if len(content) > 0 {
switch params["action"] {
case "new":
if err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.IT_PLAIN, content); err != nil {
ctx.Handle(500, "issue.Comment(create comment)", err)
return
}
log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id)
default:
ctx.Handle(404, "issue.Comment", err)
return
}
log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id)
default:
ctx.Handle(404, "issue.Comment", err)
return
}
ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", ctx.User.Name, ctx.Repo.Repository.Name, index))

View file

@ -120,7 +120,7 @@ func SignIn(ctx *middleware.Context, form auth.LogInForm) {
return
}
if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
if ctx.HasError() {
ctx.HTML(200, "user/signin")
return
}
@ -308,17 +308,19 @@ func Issues(ctx *middleware.Context) {
showRepos := make([]models.Repository, 0, len(repos))
var closedIssueCount, createdByCount int
isShowClosed := ctx.Query("state") == "closed"
var closedIssueCount, createdByCount, allIssueCount int
// Get all issues.
allIssues := make([]models.Issue, 0, 5*len(repos))
for i, repo := range repos {
issues, err := models.GetIssues(0, repo.Id, posterId, 0, page, false, false, "", "")
issues, err := models.GetIssues(0, repo.Id, posterId, 0, page, isShowClosed, false, "", "")
if err != nil {
ctx.Handle(200, "user.Issues(get issues)", err)
return
}
allIssueCount += repo.NumIssues
closedIssueCount += repo.NumClosedIssues
// Set repository information to issues.
@ -330,12 +332,10 @@ func Issues(ctx *middleware.Context) {
repos[i].NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
if repos[i].NumOpenIssues > 0 {
showRepos = append(showRepos, repos[i])
}
}
showIssues := make([]models.Issue, 0, len(allIssues))
isShowClosed := ctx.Query("state") == "closed"
ctx.Data["IsShowClosed"] = isShowClosed
// Get posters and filter issues.
@ -361,9 +361,9 @@ func Issues(ctx *middleware.Context) {
ctx.Data["Repos"] = showRepos
ctx.Data["Issues"] = showIssues
ctx.Data["AllIssueCount"] = len(allIssues)
ctx.Data["AllIssueCount"] = allIssueCount
ctx.Data["ClosedIssueCount"] = closedIssueCount
ctx.Data["OpenIssueCount"] = len(allIssues) - closedIssueCount
ctx.Data["OpenIssueCount"] = allIssueCount - closedIssueCount
ctx.Data["CreatedByCount"] = createdByCount
ctx.HTML(200, "issue/user")
}