From 9924e65ca11770f247723b10fcbdb81dc9534b32 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 22 Jun 2014 13:14:03 -0400 Subject: In progress of name template name constant --- routers/repo/repo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'routers/repo') diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 6db45300..6154eede 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -318,7 +318,7 @@ func basicDecode(encoded string) (user string, name string, err error) { 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, fmt.Sprintf("status/401")) + ctx.HTML(401, base.TplName("status/401")) } func Action(ctx *middleware.Context, params martini.Params) { -- cgit v1.2.3 From 314193029a246c2498c6d54c085d57f7589c8dbe Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 22 Jun 2014 23:11:12 -0400 Subject: Use constants to name template file --- gogs.go | 2 +- routers/repo/branch.go | 11 +++-- routers/repo/commit.go | 92 ++++++++++++++++++++++-------------------- routers/repo/issue.go | 34 ++++++++++++---- routers/repo/pull.go | 7 +++- routers/repo/release.go | 19 +++++++-- routers/repo/repo.go | 30 ++++++++------ routers/repo/setting.go | 67 ++++++++++++++++++------------ routers/user/home.go | 18 ++++++--- routers/user/setting.go | 25 ++++++++---- routers/user/user.go | 53 ++++++++++++++---------- templates/VERSION | 2 +- templates/repo/branch.tmpl | 42 +++++++++++++++++++ templates/repo/branches.tmpl | 42 ------------------- templates/repo/hook_add.tmpl | 62 ++++++++++++++++++++++++++++ templates/repo/hook_edit.tmpl | 72 +++++++++++++++++++++++++++++++++ templates/repo/hooks_add.tmpl | 62 ---------------------------- templates/repo/hooks_edit.tmpl | 72 --------------------------------- templates/user/issue.tmpl | 52 ------------------------ templates/user/issues.tmpl | 52 ++++++++++++++++++++++++ 20 files changed, 454 insertions(+), 362 deletions(-) create mode 100644 templates/repo/branch.tmpl delete mode 100644 templates/repo/branches.tmpl create mode 100644 templates/repo/hook_add.tmpl create mode 100644 templates/repo/hook_edit.tmpl delete mode 100644 templates/repo/hooks_add.tmpl delete mode 100644 templates/repo/hooks_edit.tmpl delete mode 100644 templates/user/issue.tmpl create mode 100644 templates/user/issues.tmpl (limited to 'routers/repo') diff --git a/gogs.go b/gogs.go index 7719ee0a..03c28601 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.4.5.0621 Alpha" +const APP_VER = "0.4.5.0622 Alpha" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/routers/repo/branch.go b/routers/repo/branch.go index 2e2ae692..9bad7289 100644 --- a/routers/repo/branch.go +++ b/routers/repo/branch.go @@ -7,22 +7,27 @@ package repo import ( "github.com/go-martini/martini" + "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" ) +const ( + BRANCH base.TplName = "repo/branch" +) + func Branches(ctx *middleware.Context, params martini.Params) { ctx.Data["Title"] = "Branches" ctx.Data["IsRepoToolbarBranches"] = true brs, err := ctx.Repo.GitRepo.GetBranches() if err != nil { - ctx.Handle(500, "repo.Branches", err) + ctx.Handle(500, "repo.Branches(GetBranches)", err) return } else if len(brs) == 0 { - ctx.Handle(404, "repo.Branches", nil) + ctx.Handle(404, "repo.Branches(GetBranches)", nil) return } ctx.Data["Branches"] = brs - ctx.HTML(200, "repo/branches") + ctx.HTML(200, BRANCH) } diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 09dcaf5e..aa5c22e4 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -14,6 +14,11 @@ import ( "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 @@ -22,10 +27,10 @@ func Commits(ctx *middleware.Context, params martini.Params) { brs, err := ctx.Repo.GitRepo.GetBranches() if err != nil { - ctx.Handle(500, "repo.Commits", err) + ctx.Handle(500, "repo.Commits(GetBranches)", err) return } else if len(brs) == 0 { - ctx.Handle(404, "repo.Commits", nil) + ctx.Handle(404, "repo.Commits(GetBranches)", nil) return } @@ -61,7 +66,43 @@ func Commits(ctx *middleware.Context, params martini.Params) { ctx.Data["CommitCount"] = commitsCount ctx.Data["LastPageNum"] = lastPage ctx.Data["NextPageNum"] = nextPage - ctx.HTML(200, "repo/commits") + 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) { @@ -75,7 +116,7 @@ func Diff(ctx *middleware.Context, params martini.Params) { diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId) if err != nil { - ctx.Handle(404, "repo.Diff", err) + ctx.Handle(404, "repo.Diff(GetDiff)", err) return } @@ -119,43 +160,7 @@ func Diff(ctx *middleware.Context, params martini.Params) { 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, "repo/diff") -} - -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, "repo/commits") + ctx.HTML(200, DIFF) } func FileHistory(ctx *middleware.Context, params martini.Params) { @@ -184,8 +189,7 @@ func FileHistory(ctx *middleware.Context, params martini.Params) { if err != nil { ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err) return - } - if commitsCount == 0 { + } else if commitsCount == 0 { ctx.Handle(404, "repo.FileHistory", nil) return } @@ -217,5 +221,5 @@ func FileHistory(ctx *middleware.Context, params martini.Params) { ctx.Data["CommitCount"] = commitsCount ctx.Data["LastPageNum"] = lastPage ctx.Data["NextPageNum"] = nextPage - ctx.HTML(200, "repo/commits") + ctx.HTML(200, COMMITS) } diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 11c573f5..cf454bc8 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -22,6 +22,16 @@ import ( "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" +) + func Issues(ctx *middleware.Context) { ctx.Data["Title"] = "Issues" ctx.Data["IsRepoToolbarIssues"] = true @@ -134,7 +144,7 @@ func Issues(ctx *middleware.Context) { } else { ctx.Data["ShowCount"] = issueStats.OpenCount } - ctx.HTML(200, "issue/list") + ctx.HTML(200, ISSUES) } func CreateIssue(ctx *middleware.Context, params martini.Params) { @@ -161,7 +171,7 @@ func CreateIssue(ctx *middleware.Context, params martini.Params) { return } ctx.Data["Collaborators"] = us - ctx.HTML(200, "issue/create") + ctx.HTML(200, ISSUE_CREATE) } func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { @@ -190,7 +200,7 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C ctx.Data["Collaborators"] = us if ctx.HasError() { - ctx.HTML(200, "issue/create") + ctx.HTML(200, ISSUE_CREATE) return } @@ -392,7 +402,7 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) { 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") + ctx.HTML(200, ISSUE_VIEW) } func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) { @@ -794,14 +804,14 @@ func Milestones(ctx *middleware.Context) { } else { ctx.Data["State"] = "open" } - ctx.HTML(200, "issue/milestone") + 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, "issue/milestone_new") + ctx.HTML(200, MILESTONE_NEW) } func NewMilestonePost(ctx *middleware.Context, form auth.CreateMilestoneForm) { @@ -809,6 +819,11 @@ func NewMilestonePost(ctx *middleware.Context, form auth.CreateMilestoneForm) { 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 { @@ -890,7 +905,7 @@ func UpdateMilestone(ctx *middleware.Context, params martini.Params) { } ctx.Data["Milestone"] = mile - ctx.HTML(200, "issue/milestone_edit") + ctx.HTML(200, MILESTONE_EDIT) } func UpdateMilestonePost(ctx *middleware.Context, params martini.Params, form auth.CreateMilestoneForm) { @@ -914,6 +929,11 @@ func UpdateMilestonePost(ctx *middleware.Context, params martini.Params, form au return } + if ctx.HasError() { + ctx.HTML(200, MILESTONE_EDIT) + return + } + var deadline time.Time if len(form.Deadline) == 0 { form.Deadline = "12/31/9999" diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 430c6a81..db208f9f 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -7,10 +7,15 @@ package repo import ( "github.com/go-martini/martini" + "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" ) +const ( + PULLS base.TplName = "repo/pulls" +) + func Pulls(ctx *middleware.Context, params martini.Params) { ctx.Data["IsRepoToolbarPulls"] = true - ctx.HTML(200, "repo/pulls") + ctx.HTML(200, PULLS) } diff --git a/routers/repo/release.go b/routers/repo/release.go index b6149b63..a901436f 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -14,6 +14,12 @@ import ( "github.com/gogits/gogs/modules/middleware" ) +const ( + RELEASES base.TplName = "repo/release/list" + RELEASE_NEW base.TplName = "repo/release/new" + RELEASE_EDIT base.TplName = "repo/release/edit" +) + func Releases(ctx *middleware.Context) { ctx.Data["Title"] = "Releases" ctx.Data["IsRepoToolbarReleases"] = true @@ -100,7 +106,7 @@ func Releases(ctx *middleware.Context) { } models.SortReleases(tags) ctx.Data["Releases"] = tags - ctx.HTML(200, "release/list") + ctx.HTML(200, RELEASES) } func NewRelease(ctx *middleware.Context) { @@ -112,7 +118,7 @@ func NewRelease(ctx *middleware.Context) { ctx.Data["Title"] = "New Release" ctx.Data["IsRepoToolbarReleases"] = true ctx.Data["IsRepoReleaseNew"] = true - ctx.HTML(200, "release/new") + ctx.HTML(200, RELEASE_NEW) } func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) { @@ -126,7 +132,7 @@ func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) { ctx.Data["IsRepoReleaseNew"] = true if ctx.HasError() { - ctx.HTML(200, "release/new") + ctx.HTML(200, RELEASE_NEW) return } @@ -187,7 +193,7 @@ func EditRelease(ctx *middleware.Context, params martini.Params) { ctx.Data["Title"] = "Edit Release" ctx.Data["IsRepoToolbarReleases"] = true - ctx.HTML(200, "release/edit") + ctx.HTML(200, RELEASE_EDIT) } func EditReleasePost(ctx *middleware.Context, params martini.Params, form auth.EditReleaseForm) { @@ -208,6 +214,11 @@ func EditReleasePost(ctx *middleware.Context, params martini.Params, form auth.E } ctx.Data["Release"] = rel + if ctx.HasError() { + ctx.HTML(200, RELEASE_EDIT) + return + } + ctx.Data["Title"] = "Edit Release" ctx.Data["IsRepoToolbarReleases"] = true diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 6154eede..2ead24df 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -25,12 +25,18 @@ import ( "github.com/gogits/gogs/modules/middleware" ) +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["Licenses"] = models.Licenses - ctx.HTML(200, "repo/create") + ctx.HTML(200, CREATE) } func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { @@ -40,7 +46,7 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Data["Licenses"] = models.Licenses if ctx.HasError() { - ctx.HTML(200, "repo/create") + ctx.HTML(200, CREATE) return } @@ -51,25 +57,25 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Redirect("/" + ctx.User.Name + "/" + form.RepoName) return } else if err == models.ErrRepoAlreadyExist { - ctx.RenderWithErr("Repository name has already been used", "repo/create", &form) + ctx.RenderWithErr("Repository name has already been used", CREATE, &form) return } else if err == models.ErrRepoNameIllegal { - ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "repo/create", &form) + ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), CREATE, &form) return } if repo != nil { if errDelete := models.DeleteRepository(ctx.User.Id, repo.Id, ctx.User.Name); errDelete != nil { - log.Error("repo.MigratePost(CreatePost): %v", errDelete) + log.Error("repo.CreatePost(DeleteRepository): %v", errDelete) } } - ctx.Handle(500, "repo.Create", err) + ctx.Handle(500, "repo.CreatePost", err) } func Migrate(ctx *middleware.Context) { ctx.Data["Title"] = "Migrate repository" ctx.Data["PageIsNewRepo"] = true - ctx.HTML(200, "repo/migrate") + ctx.HTML(200, MIGRATE) } func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { @@ -77,7 +83,7 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { ctx.Data["PageIsNewRepo"] = true if ctx.HasError() { - ctx.HTML(200, "repo/migrate") + ctx.HTML(200, MIGRATE) return } @@ -91,10 +97,10 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { ctx.Redirect("/" + ctx.User.Name + "/" + form.RepoName) return } else if err == models.ErrRepoAlreadyExist { - ctx.RenderWithErr("Repository name has already been used", "repo/migrate", &form) + ctx.RenderWithErr("Repository name has already been used", MIGRATE, &form) return } else if err == models.ErrRepoNameIllegal { - ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "repo/migrate", &form) + ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), MIGRATE, &form) return } @@ -105,7 +111,7 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { } if strings.Contains(err.Error(), "Authentication failed") { - ctx.RenderWithErr(err.Error(), "repo/migrate", &form) + ctx.RenderWithErr(err.Error(), MIGRATE, &form) return } ctx.Handle(500, "repo.Migrate", err) @@ -291,7 +297,7 @@ func Single(ctx *middleware.Context, params martini.Params) { ctx.Data["Treenames"] = treenames ctx.Data["TreePath"] = treePath ctx.Data["BranchLink"] = branchLink - ctx.HTML(200, "repo/single") + ctx.HTML(200, SINGLE) } func basicEncode(username, password string) string { diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 6313971c..6479cb30 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -20,10 +20,19 @@ import ( "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, "repo/setting") + ctx.HTML(200, SETTING) } func SettingPost(ctx *middleware.Context, form auth.RepoSettingForm) { @@ -32,7 +41,7 @@ func SettingPost(ctx *middleware.Context, form auth.RepoSettingForm) { switch ctx.Query("action") { case "update": if ctx.HasError() { - ctx.HTML(200, "repo/setting") + ctx.HTML(200, SETTING) return } @@ -44,7 +53,7 @@ func SettingPost(ctx *middleware.Context, form auth.RepoSettingForm) { ctx.Handle(500, "setting.SettingPost(update: check existence)", err) return } else if isExist { - ctx.RenderWithErr("Repository name has been taken in your repositories.", "repo/setting", nil) + 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) @@ -84,7 +93,7 @@ func SettingPost(ctx *middleware.Context, form auth.RepoSettingForm) { 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.", "repo/setting", nil) + ctx.RenderWithErr("Please make sure you entered repository name is correct.", SETTING, nil) return } else if ctx.Repo.Repository.IsMirror { ctx.Error(404) @@ -98,7 +107,7 @@ func SettingPost(ctx *middleware.Context, form auth.RepoSettingForm) { ctx.Handle(500, "setting.SettingPost(transfer: check existence)", err) return } else if !isExist { - ctx.RenderWithErr("Please make sure you entered owner name is correct.", "repo/setting", nil) + ctx.RenderWithErr("Please make sure you entered owner name is correct.", SETTING, nil) return } else if err = models.TransferOwnership(ctx.User, newOwner, ctx.Repo.Repository); err != nil { ctx.Handle(500, "setting.SettingPost(transfer repository)", err) @@ -109,7 +118,7 @@ func SettingPost(ctx *middleware.Context, form auth.RepoSettingForm) { 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.", "repo/setting", nil) + ctx.RenderWithErr("Please make sure you entered repository name is correct.", SETTING, nil) return } @@ -156,7 +165,7 @@ func Collaboration(ctx *middleware.Context) { } ctx.Data["Collaborators"] = us - ctx.HTML(200, "repo/collaboration") + ctx.HTML(200, COLLABORATION) } func CollaborationPost(ctx *middleware.Context) { @@ -226,13 +235,13 @@ func WebHooks(ctx *middleware.Context) { } ctx.Data["Webhooks"] = ws - ctx.HTML(200, "repo/hooks") + 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, "repo/hooks_add") + ctx.HTML(200, HOOK_ADD) } func WebHooksAddPost(ctx *middleware.Context, form auth.NewWebhookForm) { @@ -240,7 +249,7 @@ func WebHooksAddPost(ctx *middleware.Context, form auth.NewWebhookForm) { ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Webhook" if ctx.HasError() { - ctx.HTML(200, "repo/hooks_add") + ctx.HTML(200, HOOK_ADD) return } @@ -293,40 +302,46 @@ func WebHooksEdit(ctx *middleware.Context, params martini.Params) { w.GetEvent() ctx.Data["Webhook"] = w - ctx.HTML(200, "repo/hooks_edit") + 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" - if ctx.HasError() { - ctx.HTML(200, "repo/hooks_add") - return - } - 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 := &models.Webhook{ - Id: hookId, - RepoId: ctx.Repo.Repository.Id, - Url: form.Url, - ContentType: ct, - Secret: form.Secret, - HookEvent: &models.HookEvent{ - PushOnly: form.PushOnly, - }, - IsActive: form.Active, + 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 diff --git a/routers/user/home.go b/routers/user/home.go index a9674ac2..7d0333cb 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -17,6 +17,14 @@ import ( "github.com/gogits/gogs/modules/middleware" ) +const ( + DASHBOARD base.TplName = "user/dashboard" + PROFILE base.TplName = "user/profile" + ISSUES base.TplName = "user/issue" + PULLS base.TplName = "user/pulls" + STARS base.TplName = "user/stars" +) + func Dashboard(ctx *middleware.Context) { ctx.Data["Title"] = "Dashboard" ctx.Data["PageIsUserDashboard"] = true @@ -52,7 +60,7 @@ func Dashboard(ctx *middleware.Context) { feeds = append(feeds, act) } ctx.Data["Feeds"] = feeds - ctx.HTML(200, "user/dashboard") + ctx.HTML(200, DASHBOARD) } func Profile(ctx *middleware.Context, params martini.Params) { @@ -87,7 +95,7 @@ func Profile(ctx *middleware.Context, params martini.Params) { } } - ctx.HTML(200, "user/profile") + ctx.HTML(200, PROFILE) } func Email2User(ctx *middleware.Context) { @@ -254,13 +262,13 @@ func Issues(ctx *middleware.Context) { } else { ctx.Data["ShowCount"] = issueStats.OpenCount } - ctx.HTML(200, "user/issue") + ctx.HTML(200, ISSUES) } func Pulls(ctx *middleware.Context) { - ctx.HTML(200, "user/pulls") + ctx.HTML(200, PULLS) } func Stars(ctx *middleware.Context) { - ctx.HTML(200, "user/stars") + ctx.HTML(200, STARS) } diff --git a/routers/user/setting.go b/routers/user/setting.go index 1fae516a..9075ee0b 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -14,12 +14,21 @@ import ( "github.com/gogits/gogs/modules/middleware" ) +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" +) + 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, "user/setting") + ctx.HTML(200, SETTING) } func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) { @@ -28,7 +37,7 @@ func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) { ctx.Data["IsUserPageSetting"] = true if ctx.HasError() { - ctx.HTML(200, "user/setting") + ctx.HTML(200, SETTING) return } @@ -90,14 +99,14 @@ func SettingSocial(ctx *middleware.Context) { ctx.Handle(500, "user.SettingSocial(GetOauthByUserId)", err) return } - ctx.HTML(200, "user/social") + ctx.HTML(200, SOCIAL) } func SettingPassword(ctx *middleware.Context) { ctx.Data["Title"] = "Password" ctx.Data["PageIsUserSetting"] = true ctx.Data["IsUserPageSettingPasswd"] = true - ctx.HTML(200, "user/password") + ctx.HTML(200, PASSWORD) } func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) { @@ -106,7 +115,7 @@ func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) { ctx.Data["IsUserPageSettingPasswd"] = true if ctx.HasError() { - ctx.HTML(200, "user/password") + ctx.HTML(200, PASSWORD) return } @@ -207,7 +216,7 @@ func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) { } } - ctx.HTML(200, "user/publickey") + ctx.HTML(200, PUBLICKEY) } func SettingNotification(ctx *middleware.Context) { @@ -215,7 +224,7 @@ func SettingNotification(ctx *middleware.Context) { ctx.Data["Title"] = "Notification" ctx.Data["PageIsUserSetting"] = true ctx.Data["IsUserPageSettingNotify"] = true - ctx.HTML(200, "user/notification") + ctx.HTML(200, NOTIFICATION) } func SettingSecurity(ctx *middleware.Context) { @@ -223,5 +232,5 @@ func SettingSecurity(ctx *middleware.Context) { ctx.Data["Title"] = "Security" ctx.Data["PageIsUserSetting"] = true ctx.Data["IsUserPageSettingSecurity"] = true - ctx.HTML(200, "user/security") + ctx.HTML(200, SECURITY) } diff --git a/routers/user/user.go b/routers/user/user.go index 8fcbeb6a..8144730e 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -17,12 +17,21 @@ import ( "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, "user/signin") + ctx.HTML(200, SIGNIN) return } @@ -34,7 +43,7 @@ func SignIn(ctx *middleware.Context) { // Check auto-login. uname := ctx.GetCookie(setting.CookieUserName) if len(uname) == 0 { - ctx.HTML(200, "user/signin") + ctx.HTML(200, SIGNIN) return } @@ -57,7 +66,7 @@ func SignIn(ctx *middleware.Context) { secret := base.EncodeMd5(user.Rands + user.Passwd) value, _ := ctx.GetSecureCookie(secret, setting.CookieRememberName) if value != user.Name { - ctx.HTML(200, "user/signin") + ctx.HTML(200, SIGNIN) return } @@ -86,7 +95,7 @@ func SignInPost(ctx *middleware.Context, form auth.LogInForm) { } if ctx.HasError() { - ctx.HTML(200, "user/signin") + ctx.HTML(200, SIGNIN) return } @@ -94,7 +103,7 @@ func SignInPost(ctx *middleware.Context, form auth.LogInForm) { 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", "user/signin", &form) + ctx.RenderWithErr("Username or password is not correct", SIGNIN, &form) return } @@ -151,7 +160,7 @@ func SignUp(ctx *middleware.Context) { if setting.Service.DisableRegistration { ctx.Data["DisableRegistration"] = true - ctx.HTML(200, "user/signup") + ctx.HTML(200, SIGNUP) return } @@ -160,7 +169,7 @@ func SignUp(ctx *middleware.Context) { return } - ctx.HTML(200, "user/signup") + ctx.HTML(200, SIGNUP) } func oauthSignUp(ctx *middleware.Context, sid int64) { @@ -180,7 +189,7 @@ func oauthSignUp(ctx *middleware.Context, sid int64) { 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, "user/signup") + ctx.HTML(200, SIGNUP) } func SignUpPost(ctx *middleware.Context, form auth.RegisterForm) { @@ -198,14 +207,14 @@ func SignUpPost(ctx *middleware.Context, form auth.RegisterForm) { } if ctx.HasError() { - ctx.HTML(200, "user/signup") + 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.", "user/signup", &form) + ctx.RenderWithErr("Password and re-type password are not same.", SIGNUP, &form) return } @@ -221,12 +230,12 @@ func SignUpPost(ctx *middleware.Context, form auth.RegisterForm) { switch err { case models.ErrUserAlreadyExist: ctx.Data["Err_UserName"] = true - ctx.RenderWithErr("Username has been already taken", "user/signup", &form) + 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", "user/signup", &form) + ctx.RenderWithErr("E-mail address has been already used", SIGNUP, &form) case models.ErrUserNameIllegal: - ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "user/signup", &form) + ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), SIGNUP, &form) default: ctx.Handle(500, "user.SignUpPost(RegisterUser)", err) } @@ -265,7 +274,7 @@ func Delete(ctx *middleware.Context) { ctx.Data["Title"] = "Delete Account" ctx.Data["PageIsUserSetting"] = true ctx.Data["IsUserPageSettingDelete"] = true - ctx.HTML(200, "user/delete") + ctx.HTML(200, DELETE) } func DeletePost(ctx *middleware.Context) { @@ -321,7 +330,7 @@ func Activate(ctx *middleware.Context) { } else { ctx.Data["ServiceNotEnabled"] = true } - ctx.HTML(200, "user/activate") + ctx.HTML(200, ACTIVATE) return } @@ -343,7 +352,7 @@ func Activate(ctx *middleware.Context) { } ctx.Data["IsActivateFailed"] = true - ctx.HTML(200, "user/activate") + ctx.HTML(200, ACTIVATE) } func ForgotPasswd(ctx *middleware.Context) { @@ -351,12 +360,12 @@ func ForgotPasswd(ctx *middleware.Context) { if setting.MailService == nil { ctx.Data["IsResetDisable"] = true - ctx.HTML(200, "user/forgot_passwd") + ctx.HTML(200, FORGOT_PASSWORD) return } ctx.Data["IsResetRequest"] = true - ctx.HTML(200, "user/forgot_passwd") + ctx.HTML(200, FORGOT_PASSWORD) } func ForgotPasswdPost(ctx *middleware.Context) { @@ -381,7 +390,7 @@ func ForgotPasswdPost(ctx *middleware.Context) { if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) { ctx.Data["ResendLimited"] = true - ctx.HTML(200, "user/forgot_passwd") + ctx.HTML(200, FORGOT_PASSWORD) return } @@ -393,7 +402,7 @@ func ForgotPasswdPost(ctx *middleware.Context) { ctx.Data["Email"] = email ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 ctx.Data["IsResetSent"] = true - ctx.HTML(200, "user/forgot_passwd") + ctx.HTML(200, FORGOT_PASSWORD) } func ResetPasswd(ctx *middleware.Context) { @@ -406,7 +415,7 @@ func ResetPasswd(ctx *middleware.Context) { } ctx.Data["Code"] = code ctx.Data["IsResetForm"] = true - ctx.HTML(200, "user/reset_passwd") + ctx.HTML(200, RESET_PASSWORD) } func ResetPasswdPost(ctx *middleware.Context) { @@ -443,5 +452,5 @@ func ResetPasswdPost(ctx *middleware.Context) { } ctx.Data["IsResetFailed"] = true - ctx.HTML(200, "user/reset_passwd") + ctx.HTML(200, RESET_PASSWORD) } diff --git a/templates/VERSION b/templates/VERSION index 7d143dcc..414dc2ef 100644 --- a/templates/VERSION +++ b/templates/VERSION @@ -1 +1 @@ -0.4.5.0621 Alpha \ No newline at end of file +0.4.5.0622 Alpha \ No newline at end of file diff --git a/templates/repo/branch.tmpl b/templates/repo/branch.tmpl new file mode 100644 index 00000000..8d2d59d7 --- /dev/null +++ b/templates/repo/branch.tmpl @@ -0,0 +1,42 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +{{template "repo/nav" .}} +{{template "repo/toolbar" .}} +
+
+
+
+

Branches

+
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+{{template "base/footer" .}} \ No newline at end of file diff --git a/templates/repo/branches.tmpl b/templates/repo/branches.tmpl deleted file mode 100644 index 8d2d59d7..00000000 --- a/templates/repo/branches.tmpl +++ /dev/null @@ -1,42 +0,0 @@ -{{template "base/head" .}} -{{template "base/navbar" .}} -{{template "repo/nav" .}} -{{template "repo/toolbar" .}} -
-
-
-
-

Branches

-
- - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
-{{template "base/footer" .}} \ No newline at end of file diff --git a/templates/repo/hook_add.tmpl b/templates/repo/hook_add.tmpl new file mode 100644 index 00000000..df3ff3bd --- /dev/null +++ b/templates/repo/hook_add.tmpl @@ -0,0 +1,62 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +{{template "repo/nav" .}} +{{template "repo/toolbar" .}} +
+ {{template "repo/setting_nav" .}} +
+ {{template "base/alert" .}} +
+ {{.CsrfTokenHtml}} +
+
+ Add Webhook +
+ +
+
+

We’ll send a POST request to the URL below with details of any subscribed events.

+
+
+ + +
+ +
+ + +
+ +
+ + +
+
+
+ +
+ +
+
+
+
+ +

We will deliver event details when this hook is triggered.

+
+
+
+ + +
+
+
+
+{{template "base/footer" .}} \ No newline at end of file diff --git a/templates/repo/hook_edit.tmpl b/templates/repo/hook_edit.tmpl new file mode 100644 index 00000000..c3fe217e --- /dev/null +++ b/templates/repo/hook_edit.tmpl @@ -0,0 +1,72 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +{{template "repo/nav" .}} +{{template "repo/toolbar" .}} +
+ {{template "repo/setting_nav" .}} +
+ {{template "base/alert" .}} +
+ {{.CsrfTokenHtml}} +
+
+ Manage Webhook +
+ +
+
+

We’ll send a POST request to the URL below with details of any subscribed events.

+
+
+ + +
+ +
+ + +
+ +
+ + +
+
+
+ +
+ +
+
+
+
+ +

We will deliver event details when this hook is triggered.

+
+
+
+ + +
+
+
+
+

Recent Deliveries

+
+ +
+ Coming soon +
+
+
+
+{{template "base/footer" .}} \ No newline at end of file diff --git a/templates/repo/hooks_add.tmpl b/templates/repo/hooks_add.tmpl deleted file mode 100644 index df3ff3bd..00000000 --- a/templates/repo/hooks_add.tmpl +++ /dev/null @@ -1,62 +0,0 @@ -{{template "base/head" .}} -{{template "base/navbar" .}} -{{template "repo/nav" .}} -{{template "repo/toolbar" .}} -
- {{template "repo/setting_nav" .}} -
- {{template "base/alert" .}} -
- {{.CsrfTokenHtml}} -
-
- Add Webhook -
- -
-
-

We’ll send a POST request to the URL below with details of any subscribed events.

-
-
- - -
- -
- - -
- -
- - -
-
-
- -
- -
-
-
-
- -

We will deliver event details when this hook is triggered.

-
-
-
- - -
-
-
-
-{{template "base/footer" .}} \ No newline at end of file diff --git a/templates/repo/hooks_edit.tmpl b/templates/repo/hooks_edit.tmpl deleted file mode 100644 index c3fe217e..00000000 --- a/templates/repo/hooks_edit.tmpl +++ /dev/null @@ -1,72 +0,0 @@ -{{template "base/head" .}} -{{template "base/navbar" .}} -{{template "repo/nav" .}} -{{template "repo/toolbar" .}} -
- {{template "repo/setting_nav" .}} -
- {{template "base/alert" .}} -
- {{.CsrfTokenHtml}} -
-
- Manage Webhook -
- -
-
-

We’ll send a POST request to the URL below with details of any subscribed events.

-
-
- - -
- -
- - -
- -
- - -
-
-
- -
- -
-
-
-
- -

We will deliver event details when this hook is triggered.

-
-
-
- - -
-
-
-
-

Recent Deliveries

-
- -
- Coming soon -
-
-
-
-{{template "base/footer" .}} \ No newline at end of file diff --git a/templates/user/issue.tmpl b/templates/user/issue.tmpl deleted file mode 100644 index d1c2bd99..00000000 --- a/templates/user/issue.tmpl +++ /dev/null @@ -1,52 +0,0 @@ -{{template "base/head" .}} -{{template "base/navbar" .}} -
-
- -

Your Issues

-
-
-
- {{if .HasInfo}}
{{.InfoMsg}}
{{end}} -
- -
-
-
- Open - Closed -
-
-
- {{range .Issues}}{{if .}} -
- #{{.Index}} -
{{.Name}}
-

- - {{.Poster.Name}} - {{TimeSince .Created}} - {{.NumComments}} -

-
- {{end}}{{end}} -
-
-
-
-{{template "base/footer" .}} \ No newline at end of file diff --git a/templates/user/issues.tmpl b/templates/user/issues.tmpl new file mode 100644 index 00000000..d1c2bd99 --- /dev/null +++ b/templates/user/issues.tmpl @@ -0,0 +1,52 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +
+
+ +

Your Issues

+
+
+
+ {{if .HasInfo}}
{{.InfoMsg}}
{{end}} +
+ +
+
+
+ Open + Closed +
+
+
+ {{range .Issues}}{{if .}} +
+ #{{.Index}} +
{{.Name}}
+

+ + {{.Poster.Name}} + {{TimeSince .Created}} + {{.NumComments}} +

+
+ {{end}}{{end}} +
+
+
+
+{{template "base/footer" .}} \ No newline at end of file -- cgit v1.2.3 From 47d29a1ee027734dcbbd1a6ed4d8ff3dde1a789b Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 23 Jun 2014 16:22:34 -0400 Subject: Mirror fix on #248 --- cmd/update.go | 4 +++- gogs.go | 2 +- models/update.go | 26 +++++++++++++------------- routers/repo/http.go | 5 ++++- templates/VERSION | 2 +- 5 files changed, 22 insertions(+), 17 deletions(-) (limited to 'routers/repo') diff --git a/cmd/update.go b/cmd/update.go index d0e0acde..c030b6cf 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -42,5 +42,7 @@ func runUpdate(c *cli.Context) { repoUserName := os.Getenv("repoUserName") repoName := os.Getenv("repoName") - models.Update(args[0], args[1], args[2], userName, repoUserName, repoName, userId) + if err := models.Update(args[0], args[1], args[2], userName, repoUserName, repoName, userId); err != nil { + log.GitLogger.Fatal(err.Error()) + } } diff --git a/gogs.go b/gogs.go index 03c28601..a39d2d43 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.4.5.0622 Alpha" +const APP_VER = "0.4.5.0623 Alpha" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/update.go b/models/update.go index 5675a94c..3328f221 100644 --- a/models/update.go +++ b/models/update.go @@ -6,6 +6,7 @@ package models import ( "container/list" + "fmt" "os/exec" "strings" @@ -15,11 +16,11 @@ import ( "github.com/gogits/gogs/modules/log" ) -func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName string, userId int64) { +func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName string, userId int64) error { isNew := strings.HasPrefix(oldCommitId, "0000000") if isNew && strings.HasPrefix(newCommitId, "0000000") { - log.GitLogger.Fatal("old rev and new rev both 000000") + return fmt.Errorf("old rev and new rev both 000000") } f := RepoPath(repoUserName, repoName) @@ -31,18 +32,17 @@ func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName isDel := strings.HasPrefix(newCommitId, "0000000") if isDel { log.GitLogger.Info("del rev", refName, "from", userName+"/"+repoName+".git", "by", userId) - return + return nil } repo, err := git.OpenRepository(f) if err != nil { - log.GitLogger.Fatal("runUpdate.Open repoId: %v", err) + return fmt.Errorf("runUpdate.Open repoId: %v", err) } newCommit, err := repo.GetCommit(newCommitId) if err != nil { - log.GitLogger.Fatal("runUpdate GetCommit of newCommitId: %v", err) - return + return fmt.Errorf("runUpdate GetCommit of newCommitId: %v", err) } var l *list.List @@ -50,28 +50,27 @@ func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName if isNew { l, err = newCommit.CommitsBefore() if err != nil { - log.GitLogger.Fatal("Find CommitsBefore erro: %v", err) + return fmt.Errorf("Find CommitsBefore erro: %v", err) } } else { l, err = newCommit.CommitsBeforeUntil(oldCommitId) if err != nil { - log.GitLogger.Fatal("Find CommitsBeforeUntil erro: %v", err) - return + return fmt.Errorf("Find CommitsBeforeUntil erro: %v", err) } } if err != nil { - log.GitLogger.Fatal("runUpdate.Commit repoId: %v", err) + return fmt.Errorf("runUpdate.Commit repoId: %v", err) } ru, err := GetUserByName(repoUserName) if err != nil { - log.GitLogger.Fatal("runUpdate.GetUserByName: %v", err) + return fmt.Errorf("runUpdate.GetUserByName: %v", err) } repos, err := GetRepositoryByName(ru.Id, repoName) if err != nil { - log.GitLogger.Fatal("runUpdate.GetRepositoryByName userId: %v", err) + return fmt.Errorf("runUpdate.GetRepositoryByName userId: %v", err) } commits := make([]*base.PushCommit, 0) @@ -95,6 +94,7 @@ func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName //commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()}) if err = CommitRepoAction(userId, ru.Id, userName, actEmail, repos.Id, repoUserName, repoName, refName, &base.PushCommits{l.Len(), commits}); err != nil { - log.GitLogger.Fatal("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err) + return fmt.Errorf("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err) } + return nil } diff --git a/routers/repo/http.go b/routers/repo/http.go index 5915e876..d2bff299 100644 --- a/routers/repo/http.go +++ b/routers/repo/http.go @@ -141,7 +141,10 @@ func Http(ctx *middleware.Context, params martini.Params) { newCommitId := fields[1] refName := fields[2] - models.Update(refName, oldCommitId, newCommitId, authUsername, username, reponame, authUser.Id) + if err = models.Update(refName, oldCommitId, newCommitId, authUsername, username, reponame, authUser.Id); err != nil { + log.GitLogger.Error(err.Error()) + return + } } } } diff --git a/templates/VERSION b/templates/VERSION index 414dc2ef..ca3b3f1b 100644 --- a/templates/VERSION +++ b/templates/VERSION @@ -1 +1 @@ -0.4.5.0622 Alpha \ No newline at end of file +0.4.5.0623 Alpha \ No newline at end of file -- cgit v1.2.3 From e0f9c628c5ff7399167944b3d0730698487af498 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 25 Jun 2014 00:44:48 -0400 Subject: Add create organization --- cmd/serve.go | 20 ++--- cmd/web.go | 5 +- gogs.go | 2 +- models/access.go | 17 ++-- models/issue.go | 4 +- models/login.go | 4 +- models/models.go | 2 +- models/org.go | 69 ++++++++++++++++ models/repo.go | 10 ++- models/user.go | 186 +++++++++++++++++++++++++++++++++--------- modules/auth/org.go | 33 ++++++++ modules/middleware/repo.go | 4 +- routers/admin/user.go | 4 +- routers/install.go | 2 +- routers/org/org.go | 96 ++++++++++++++++++++-- routers/repo/http.go | 8 +- routers/repo/setting.go | 4 +- routers/user/home.go | 13 ++- routers/user/user.go | 7 +- templates/VERSION | 2 +- templates/org/dashboard.tmpl | 73 ----------------- templates/org/new.tmpl | 24 +----- templates/user/dashboard.tmpl | 39 ++++++--- templates/user/issues.tmpl | 3 +- 24 files changed, 436 insertions(+), 195 deletions(-) create mode 100644 models/org.go create mode 100644 modules/auth/org.go delete mode 100644 templates/org/dashboard.tmpl (limited to 'routers/repo') diff --git a/cmd/serve.go b/cmd/serve.go index 62e290d8..2a76da79 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -56,19 +56,19 @@ func parseCmd(cmd string) (string, string) { } var ( - COMMANDS_READONLY = map[string]int{ - "git-upload-pack": models.AU_WRITABLE, - "git upload-pack": models.AU_WRITABLE, - "git-upload-archive": models.AU_WRITABLE, + COMMANDS_READONLY = map[string]models.AccessType{ + "git-upload-pack": models.WRITABLE, + "git upload-pack": models.WRITABLE, + "git-upload-archive": models.WRITABLE, } - COMMANDS_WRITE = map[string]int{ - "git-receive-pack": models.AU_READABLE, - "git receive-pack": models.AU_READABLE, + COMMANDS_WRITE = map[string]models.AccessType{ + "git-receive-pack": models.READABLE, + "git receive-pack": models.READABLE, } ) -func In(b string, sl map[string]int) bool { +func In(b string, sl map[string]models.AccessType) bool { _, e := sl[b] return e } @@ -129,7 +129,7 @@ func runServ(k *cli.Context) { // Access check. switch { case isWrite: - has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_WRITABLE) + has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.WRITABLE) if err != nil { println("Gogs: internal error:", err) log.GitLogger.Fatal("Fail to check write access:", err) @@ -152,7 +152,7 @@ func runServ(k *cli.Context) { break } - has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_READABLE) + has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.READABLE) if err != nil { println("Gogs: internal error:", err) log.GitLogger.Fatal("Fail to check read access:", err) diff --git a/cmd/web.go b/cmd/web.go index d29183a9..878fdeac 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -188,14 +188,15 @@ func runWeb(*cli.Context) { reqOwner := middleware.RequireOwner() - m.Group("/o", func(r martini.Router) { + m.Group("/org", func(r martini.Router) { r.Get("/create", org.New) + r.Post("/create", bindIgnErr(auth.CreateOrganizationForm{}), org.NewPost) r.Get("/:org", org.Organization) r.Get("/:org/dashboard", org.Dashboard) r.Get("/:org/members", org.Members) r.Get("/:org/teams", org.Teams) r.Get("/:org/setting", org.Setting) - }) + }, reqSignIn) m.Group("/:username/:reponame", func(r martini.Router) { r.Get("/settings", repo.Setting) diff --git a/gogs.go b/gogs.go index 75d3eb4b..5c2c6ed9 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.4.5.0624 Alpha" +const APP_VER = "0.4.5.0625 Alpha" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/access.go b/models/access.go index cf31fc13..5238daba 100644 --- a/models/access.go +++ b/models/access.go @@ -11,19 +11,20 @@ import ( "github.com/go-xorm/xorm" ) -// Access types. +type AccessType int + const ( - AU_READABLE = iota + 1 - AU_WRITABLE + READABLE AccessType = iota + 1 + WRITABLE ) // Access represents the accessibility of user to repository. type Access struct { Id int64 - UserName string `xorm:"unique(s)"` - RepoName string `xorm:"unique(s)"` // / - Mode int `xorm:"unique(s)"` - Created time.Time `xorm:"created"` + UserName string `xorm:"unique(s)"` + RepoName string `xorm:"unique(s)"` // / + Mode AccessType `xorm:"unique(s)"` + Created time.Time `xorm:"created"` } // AddAccess adds new access record. @@ -59,7 +60,7 @@ func UpdateAccessWithSession(sess *xorm.Session, access *Access) error { // HasAccess returns true if someone can read or write to given repository. // The repoName should be in format /. -func HasAccess(uname, repoName string, mode int) (bool, error) { +func HasAccess(uname, repoName string, mode AccessType) (bool, error) { if len(repoName) == 0 { return false, nil } diff --git a/models/issue.go b/models/issue.go index 11f6dd4e..6d67a72b 100644 --- a/models/issue.go +++ b/models/issue.go @@ -213,9 +213,9 @@ func GetIssueCountByPoster(uid, rid int64, isClosed bool) int64 { // IssueUser represents an issue-user relation. type IssueUser struct { Id int64 - Uid int64 // User ID. + Uid int64 `xorm:"INDEX"` // User ID. IssueId int64 - RepoId int64 + RepoId int64 `xorm:"INDEX"` MilestoneId int64 IsRead bool IsAssigned bool diff --git a/models/login.go b/models/login.go index 98c5c64e..e99b61e7 100644 --- a/models/login.go +++ b/models/login.go @@ -255,7 +255,7 @@ func LoginUserLdapSource(user *User, name, passwd string, sourceId int64, cfg *L Email: mail, } - return RegisterUser(user) + return CreateUser(user) } type loginAuth struct { @@ -359,5 +359,5 @@ func LoginUserSMTPSource(user *User, name, passwd string, sourceId int64, cfg *S Passwd: passwd, Email: name, } - return RegisterUser(user) + return CreateUser(user) } diff --git a/models/models.go b/models/models.go index d6273d7f..4e65c00b 100644 --- a/models/models.go +++ b/models/models.go @@ -35,7 +35,7 @@ func init() { tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch), new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow), new(Mirror), new(Release), new(LoginSource), new(Webhook), new(IssueUser), - new(Milestone), new(Label), new(HookTask)) + new(Milestone), new(Label), new(HookTask), new(Team), new(OrgUser), new(TeamUser)) } func LoadModelsConfig() { diff --git a/models/org.go b/models/org.go new file mode 100644 index 00000000..1cfe1798 --- /dev/null +++ b/models/org.go @@ -0,0 +1,69 @@ +// 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 models + +type AuthorizeType int + +const ( + ORG_READABLE AuthorizeType = iota + 1 + ORG_WRITABLE + ORG_ADMIN +) + +// Team represents a organization team. +type Team struct { + Id int64 + OrgId int64 `xorm:"INDEX"` + Name string + Description string + Authorize AuthorizeType + NumMembers int + NumRepos int +} + +// NewTeam creates a record of new team. +func NewTeam(t *Team) error { + _, err := x.Insert(t) + return err +} + +// ________ ____ ___ +// \_____ \_______ ____ | | \______ ___________ +// / | \_ __ \/ ___\| | / ___// __ \_ __ \ +// / | \ | \/ /_/ > | /\___ \\ ___/| | \/ +// \_______ /__| \___ /|______//____ >\___ >__| +// \/ /_____/ \/ \/ + +// OrgUser represents an organization-user relation. +type OrgUser struct { + Id int64 + Uid int64 `xorm:"INDEX"` + OrgId int64 `xorm:"INDEX"` + IsPublic bool + IsOwner bool + NumTeam int +} + +// GetOrgUsersByUserId returns all organization-user relations by user ID. +func GetOrgUsersByUserId(uid int64) ([]*OrgUser, error) { + ous := make([]*OrgUser, 0, 10) + err := x.Where("uid=?", uid).Find(&ous) + return ous, err +} + +// ___________ ____ ___ +// \__ ___/___ _____ _____ | | \______ ___________ +// | |_/ __ \\__ \ / \| | / ___// __ \_ __ \ +// | |\ ___/ / __ \| Y Y \ | /\___ \\ ___/| | \/ +// |____| \___ >____ /__|_| /______//____ >\___ >__| +// \/ \/ \/ \/ \/ + +// TeamUser represents an team-user relation. +type TeamUser struct { + Id int64 + Uid int64 + OrgId int64 `xorm:"INDEX"` + TeamId int64 +} diff --git a/models/repo.go b/models/repo.go index 4ccaccbf..f0e46c71 100644 --- a/models/repo.go +++ b/models/repo.go @@ -158,7 +158,7 @@ func IsRepositoryExist(u *User, repoName string) (bool, error) { } var ( - illegalEquals = []string{"raw", "install", "api", "avatar", "user", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin"} + illegalEquals = []string{"raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin"} illegalSuffixs = []string{".git"} ) @@ -483,7 +483,9 @@ func CreateRepository(user *User, name, desc, lang, license string, private, mir sess := x.NewSession() defer sess.Close() - sess.Begin() + if err = sess.Begin(); err != nil { + return nil, err + } if _, err = sess.Insert(repo); err != nil { if err2 := os.RemoveAll(repoPath); err2 != nil { @@ -495,9 +497,9 @@ func CreateRepository(user *User, name, desc, lang, license string, private, mir return nil, err } - mode := AU_WRITABLE + mode := WRITABLE if mirror { - mode = AU_READABLE + mode = READABLE } access := Access{ UserName: user.LowerName, diff --git a/models/user.go b/models/user.go index 50d81c94..2388be9a 100644 --- a/models/user.go +++ b/models/user.go @@ -21,10 +21,11 @@ import ( "github.com/gogits/gogs/modules/setting" ) -// User types. +type UserType int + const ( - UT_INDIVIDUAL = iota + 1 - UT_ORGANIZATION + INDIVIDUAL UserType = iota // Historic reason to make it starts at 0. + ORGANIZATION ) var ( @@ -50,7 +51,8 @@ type User struct { LoginType LoginType LoginSource int64 `xorm:"not null default 0"` LoginName string - Type int + Type UserType + Orgs []*User `xorm:"-"` NumFollowers int NumFollowings int NumStars int @@ -65,36 +67,60 @@ type User struct { Salt string `xorm:"VARCHAR(10)"` Created time.Time `xorm:"created"` Updated time.Time `xorm:"updated"` + + // For organization. + NumTeams int + NumMembers int } // HomeLink returns the user home page link. -func (user *User) HomeLink() string { - return "/user/" + user.Name +func (u *User) HomeLink() string { + return "/user/" + u.Name } // AvatarLink returns user gravatar link. -func (user *User) AvatarLink() string { +func (u *User) AvatarLink() string { if setting.DisableGravatar { return "/img/avatar_default.jpg" } else if setting.Service.EnableCacheAvatar { - return "/avatar/" + user.Avatar + return "/avatar/" + u.Avatar } - return "//1.gravatar.com/avatar/" + user.Avatar + return "//1.gravatar.com/avatar/" + u.Avatar } // NewGitSig generates and returns the signature of given user. -func (user *User) NewGitSig() *git.Signature { +func (u *User) NewGitSig() *git.Signature { return &git.Signature{ - Name: user.Name, - Email: user.Email, + Name: u.Name, + Email: u.Email, When: time.Now(), } } // EncodePasswd encodes password to safe format. -func (user *User) EncodePasswd() { - newPasswd := base.PBKDF2([]byte(user.Passwd), []byte(user.Salt), 10000, 50, sha256.New) - user.Passwd = fmt.Sprintf("%x", newPasswd) +func (u *User) EncodePasswd() { + newPasswd := base.PBKDF2([]byte(u.Passwd), []byte(u.Salt), 10000, 50, sha256.New) + u.Passwd = fmt.Sprintf("%x", newPasswd) +} + +func (u *User) IsOrganization() bool { + return u.Type == ORGANIZATION +} + +func (u *User) GetOrganizations() error { + ous, err := GetOrgUsersByUserId(u.Id) + if err != nil { + return err + } + + u.Orgs = make([]*User, len(ous)) + for i, ou := range ous { + u.Orgs[i], err = GetUserById(ou.OrgId) + if err != nil { + return err + } + } + return nil } // Member represents user is member of organization. @@ -126,49 +152,135 @@ func GetUserSalt() string { return base.GetRandomString(10) } -// RegisterUser creates record of a new user. -func RegisterUser(user *User) (*User, error) { +// CreateUser creates record of a new user. +func CreateUser(u *User) (*User, error) { + if !IsLegalName(u.Name) { + return nil, ErrUserNameIllegal + } + + isExist, err := IsUserExist(u.Name) + if err != nil { + return nil, err + } else if isExist { + return nil, ErrUserAlreadyExist + } + + isExist, err = IsEmailUsed(u.Email) + if err != nil { + return nil, err + } else if isExist { + return nil, ErrEmailAlreadyUsed + } + + u.LowerName = strings.ToLower(u.Name) + u.Avatar = base.EncodeMd5(u.Email) + u.AvatarEmail = u.Email + u.Rands = GetUserSalt() + u.Salt = GetUserSalt() + u.EncodePasswd() + + sess := x.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return nil, err + } - if !IsLegalName(user.Name) { + if _, err = sess.Insert(u); err != nil { + sess.Rollback() + return nil, err + } + + if err = os.MkdirAll(UserPath(u.Name), os.ModePerm); err != nil { + sess.Rollback() + return nil, err + } + + if err = sess.Commit(); err != nil { + return nil, err + } + + // Auto-set admin for user whose ID is 1. + if u.Id == 1 { + u.IsAdmin = true + u.IsActive = true + _, err = x.Id(u.Id).UseBool().Update(u) + } + return u, err +} + +// CreateOrganization creates record of a new organization. +func CreateOrganization(org, owner *User) (*User, error) { + if !IsLegalName(org.Name) { return nil, ErrUserNameIllegal } - isExist, err := IsUserExist(user.Name) + isExist, err := IsUserExist(org.Name) if err != nil { return nil, err } else if isExist { return nil, ErrUserAlreadyExist } - isExist, err = IsEmailUsed(user.Email) + isExist, err = IsEmailUsed(org.Email) if err != nil { return nil, err } else if isExist { return nil, ErrEmailAlreadyUsed } - user.LowerName = strings.ToLower(user.Name) - user.Avatar = base.EncodeMd5(user.Email) - user.AvatarEmail = user.Email - user.Rands = GetUserSalt() - user.Salt = GetUserSalt() - user.EncodePasswd() - if _, err = x.Insert(user); err != nil { + org.LowerName = strings.ToLower(org.Name) + org.Avatar = base.EncodeMd5(org.Email) + org.AvatarEmail = org.Email + // No password for organization. + org.NumTeams = 1 + org.NumMembers = 1 + + sess := x.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return nil, err + } + + if _, err = sess.Insert(org); err != nil { + sess.Rollback() return nil, err - } else if err = os.MkdirAll(UserPath(user.Name), os.ModePerm); err != nil { - if _, err := x.Id(user.Id).Delete(&User{}); err != nil { - return nil, errors.New(fmt.Sprintf( - "both create userpath %s and delete table record faild: %v", user.Name, err)) - } + } + + // Create default owner team. + t := &Team{ + OrgId: org.Id, + Name: "Owner", + Authorize: ORG_ADMIN, + NumMembers: 1, + } + if _, err = sess.Insert(t); err != nil { + sess.Rollback() return nil, err } - if user.Id == 1 { - user.IsAdmin = true - user.IsActive = true - _, err = x.Id(user.Id).UseBool().Update(user) + // Add initial creator to organization and owner team. + ou := &OrgUser{ + Uid: owner.Id, + OrgId: org.Id, + IsOwner: true, + NumTeam: 1, } - return user, err + if _, err = sess.Insert(ou); err != nil { + sess.Rollback() + return nil, err + } + + tu := &TeamUser{ + Uid: owner.Id, + OrgId: org.Id, + TeamId: t.Id, + } + if _, err = sess.Insert(tu); err != nil { + sess.Rollback() + return nil, err + } + + return org, sess.Commit() } // GetUsers returns given number of user objects with offset. diff --git a/modules/auth/org.go b/modules/auth/org.go new file mode 100644 index 00000000..a60fbb85 --- /dev/null +++ b/modules/auth/org.go @@ -0,0 +1,33 @@ +// 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 auth + +import ( + "net/http" + "reflect" + + "github.com/go-martini/martini" + + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/middleware/binding" +) + +type CreateOrganizationForm struct { + OrgName string `form:"orgname" binding:"Required;AlphaDashDot;MaxSize(30)"` + Email string `form:"email" binding:"Required;Email;MaxSize(50)"` +} + +func (f *CreateOrganizationForm) Name(field string) string { + names := map[string]string{ + "OrgName": "Organization name", + "Email": "E-mail address", + } + return names[field] +} + +func (f *CreateOrganizationForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) { + data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + validate(errs, data, f) +} diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index 6c77ed2a..43ba1e8c 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -46,7 +46,7 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { // Collaborators who have write access can be seen as owners. if ctx.IsSigned { - ctx.Repo.IsOwner, err = models.HasAccess(ctx.User.Name, userName+"/"+repoName, models.AU_WRITABLE) + ctx.Repo.IsOwner, err = models.HasAccess(ctx.User.Name, userName+"/"+repoName, models.WRITABLE) if err != nil { ctx.Handle(500, "RepoAssignment(HasAccess)", err) return @@ -107,7 +107,7 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { return } - hasAccess, err := models.HasAccess(ctx.User.Name, ctx.Repo.Owner.Name+"/"+repo.Name, models.AU_READABLE) + hasAccess, err := models.HasAccess(ctx.User.Name, ctx.Repo.Owner.Name+"/"+repo.Name, models.READABLE) if err != nil { ctx.Handle(500, "RepoAssignment(HasAccess)", err) return diff --git a/routers/admin/user.go b/routers/admin/user.go index d1bbb480..cf99db2b 100644 --- a/routers/admin/user.go +++ b/routers/admin/user.go @@ -67,7 +67,7 @@ func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) { } var err error - if u, err = models.RegisterUser(u); err != nil { + if u, err = models.CreateUser(u); err != nil { switch err { case models.ErrUserAlreadyExist: ctx.RenderWithErr("Username has been already taken", USER_NEW, &form) @@ -76,7 +76,7 @@ func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) { case models.ErrUserNameIllegal: ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), USER_NEW, &form) default: - ctx.Handle(500, "admin.user.NewUser(RegisterUser)", err) + ctx.Handle(500, "admin.user.NewUser(CreateUser)", err) } return } diff --git a/routers/install.go b/routers/install.go index 6ce7c980..bb3c16ea 100644 --- a/routers/install.go +++ b/routers/install.go @@ -227,7 +227,7 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) { GlobalInit() // Create admin account. - if _, err := models.RegisterUser(&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 ff97402e..0595a81b 100644 --- a/routers/org/org.go +++ b/routers/org/org.go @@ -1,33 +1,117 @@ +// 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 org import ( "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" + "github.com/gogits/gogs/routers/user" +) + +const ( + NEW base.TplName = "org/new" ) func Organization(ctx *middleware.Context, params martini.Params) { - ctx.Data["Title"] = "Organization "+params["org"] + ctx.Data["Title"] = "Organization " + params["org"] ctx.HTML(200, "org/org") } func Members(ctx *middleware.Context, params martini.Params) { - ctx.Data["Title"] = "Organization "+params["org"]+" Members" + ctx.Data["Title"] = "Organization " + params["org"] + " Members" ctx.HTML(200, "org/members") } func Teams(ctx *middleware.Context, params martini.Params) { - ctx.Data["Title"] = "Organization "+params["org"]+" Teams" + ctx.Data["Title"] = "Organization " + params["org"] + " Teams" ctx.HTML(200, "org/teams") } func New(ctx *middleware.Context) { - ctx.Data["Title"] = "Create an Organization" - ctx.HTML(200, "org/new") + ctx.Data["Title"] = "Create An Organization" + ctx.HTML(200, NEW) +} + +func NewPost(ctx *middleware.Context, form auth.CreateOrganizationForm) { + ctx.Data["Title"] = "Create An Organization" + + if ctx.HasError() { + ctx.HTML(200, NEW) + return + } + + org := &models.User{ + Name: form.OrgName, + Email: form.Email, + IsActive: true, // NOTE: may need to set false when require e-mail confirmation. + Type: models.ORGANIZATION, + } + + var err error + if org, err = models.CreateOrganization(org, ctx.User); err != nil { + switch err { + case models.ErrUserAlreadyExist: + ctx.Data["Err_OrgName"] = true + ctx.RenderWithErr("Organization name has been already taken", NEW, &form) + case models.ErrEmailAlreadyUsed: + ctx.Data["Err_Email"] = true + ctx.RenderWithErr("E-mail address has been already used", NEW, &form) + case models.ErrUserNameIllegal: + ctx.Data["Err_OrgName"] = true + ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), NEW, &form) + default: + ctx.Handle(500, "user.NewPost(CreateUser)", err) + } + return + } + log.Trace("%s Organization created: %s", ctx.Req.RequestURI, org.Name) + + ctx.Redirect("/org/" + form.OrgName + "/dashboard") } func Dashboard(ctx *middleware.Context, params martini.Params) { ctx.Data["Title"] = "Dashboard" - ctx.HTML(200, "org/dashboard") + ctx.Data["PageIsUserDashboard"] = true + ctx.Data["PageIsOrgDashboard"] = true + + org, err := models.GetUserByName(params["org"]) + if err != nil { + if err == models.ErrUserNotExist { + ctx.Handle(404, "org.Dashboard(GetUserByName)", err) + } else { + ctx.Handle(500, "org.Dashboard(GetUserByName)", err) + } + return + } + + 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) + if err != nil { + ctx.Handle(500, "org.Dashboard(GetRepositories)", err) + return + } + + actions, err := models.GetFeeds(org.Id, 0, false) + if err != nil { + ctx.Handle(500, "org.Dashboard(GetFeeds)", err) + return + } + ctx.Data["Feeds"] = actions + + ctx.HTML(200, user.DASHBOARD) } func Setting(ctx *middleware.Context, param martini.Params) { diff --git a/routers/repo/http.go b/routers/repo/http.go index d2bff299..981266d5 100644 --- a/routers/repo/http.go +++ b/routers/repo/http.go @@ -107,9 +107,9 @@ func Http(ctx *middleware.Context, params martini.Params) { } if !isPublicPull { - var tp = models.AU_WRITABLE + var tp = models.WRITABLE if isPull { - tp = models.AU_READABLE + tp = models.READABLE } has, err := models.HasAccess(authUsername, username+"/"+reponame, tp) @@ -117,8 +117,8 @@ func Http(ctx *middleware.Context, params martini.Params) { ctx.Handle(401, "no basic auth and digit auth", nil) return } else if !has { - if tp == models.AU_READABLE { - has, err = models.HasAccess(authUsername, username+"/"+reponame, models.AU_WRITABLE) + if tp == models.READABLE { + has, err = models.HasAccess(authUsername, username+"/"+reponame, models.WRITABLE) if err != nil || !has { ctx.Handle(401, "no basic auth and digit auth", nil) return diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 6479cb30..3d48e79c 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -175,7 +175,7 @@ func CollaborationPost(ctx *middleware.Context) { ctx.Redirect(ctx.Req.RequestURI) return } - has, err := models.HasAccess(name, repoLink, models.AU_WRITABLE) + has, err := models.HasAccess(name, repoLink, models.WRITABLE) if err != nil { ctx.Handle(500, "setting.CollaborationPost(HasAccess)", err) return @@ -196,7 +196,7 @@ func CollaborationPost(ctx *middleware.Context) { } if err = models.AddAccess(&models.Access{UserName: name, RepoName: repoLink, - Mode: models.AU_WRITABLE}); err != nil { + Mode: models.WRITABLE}); err != nil { ctx.Handle(500, "setting.CollaborationPost(AddAccess)", err) return } diff --git a/routers/user/home.go b/routers/user/home.go index 0f2cee25..86907b5a 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -20,7 +20,7 @@ import ( const ( DASHBOARD base.TplName = "user/dashboard" PROFILE base.TplName = "user/profile" - ISSUES base.TplName = "user/issue" + ISSUES base.TplName = "user/issues" PULLS base.TplName = "user/pulls" STARS base.TplName = "user/stars" ) @@ -29,6 +29,13 @@ 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["ContextUser"] = ctx.User + var err error ctx.Data["MyRepos"], err = models.GetRepositories(ctx.User.Id, true) if err != nil { @@ -53,7 +60,7 @@ func Dashboard(ctx *middleware.Context) { for _, act := range actions { if act.IsPrivate { if has, _ := models.HasAccess(ctx.User.Name, act.RepoUserName+"/"+act.RepoName, - models.AU_READABLE); !has { + models.READABLE); !has { continue } } @@ -131,7 +138,7 @@ func Feeds(ctx *middleware.Context, form auth.FeedsForm) { for _, act := range actions { if act.IsPrivate { if has, _ := models.HasAccess(ctx.User.Name, act.RepoUserName+"/"+act.RepoName, - models.AU_READABLE); !has { + models.READABLE); !has { continue } } diff --git a/routers/user/user.go b/routers/user/user.go index 8144730e..a50f126c 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -226,7 +226,7 @@ func SignUpPost(ctx *middleware.Context, form auth.RegisterForm) { } var err error - if u, err = models.RegisterUser(u); err != nil { + if u, err = models.CreateUser(u); err != nil { switch err { case models.ErrUserAlreadyExist: ctx.Data["Err_UserName"] = true @@ -235,13 +235,14 @@ func SignUpPost(ctx *middleware.Context, form auth.RegisterForm) { 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(RegisterUser)", err) + ctx.Handle(500, "user.SignUpPost(CreateUser)", err) } return } - log.Trace("%s User created: %s", ctx.Req.RequestURI, form.UserName) + log.Trace("%s User created: %s", ctx.Req.RequestURI, u.Name) // Bind social account. if isOauth { diff --git a/templates/VERSION b/templates/VERSION index 8c2be60b..b5cda695 100644 --- a/templates/VERSION +++ b/templates/VERSION @@ -1 +1 @@ -0.4.5.0624 Alpha \ No newline at end of file +0.4.5.0625 Alpha \ No newline at end of file diff --git a/templates/org/dashboard.tmpl b/templates/org/dashboard.tmpl deleted file mode 100644 index f86de1f4..00000000 --- a/templates/org/dashboard.tmpl +++ /dev/null @@ -1,73 +0,0 @@ -{{template "base/head" .}} -{{template "base/navbar" .}} -
-
-
- - - -
- -

News Feed

-
-
-
- {{if .HasInfo}}
{{.InfoMsg}}
{{end}} -
-
    - {{range .Feeds}} -
  • - -
    {{TimeSince .Created}}
    {{ActionDesc . | str2html}}
    - -
  • - {{else}} -
  • Oh. Looks like there isn't any activity here yet. Get Busy!
  • - {{end}} -
-
-
-
-
Repositories -
- - -
-
- -
- -
-
-
-
-{{template "base/footer" .}} diff --git a/templates/org/new.tmpl b/templates/org/new.tmpl index baa9c9df..bb46db4a 100644 --- a/templates/org/new.tmpl +++ b/templates/org/new.tmpl @@ -1,22 +1,14 @@ {{template "base/head" .}} {{template "base/navbar" .}}
-
+ {{.CsrfTokenHtml}}

Create New Organization

{{template "base/alert" .}} -
- -
-

{{.SignedUserName}}

- -
-
- -
+
- + Great organization names are short and memorable.
@@ -24,18 +16,10 @@
- + Organization's Email receives all notifications and confirmations.
- -
diff --git a/templates/user/dashboard.tmpl b/templates/user/dashboard.tmpl index 12018ad8..e1b43e15 100644 --- a/templates/user/dashboard.tmpl +++ b/templates/user/dashboard.tmpl @@ -4,29 +4,46 @@
-

News Feed

+
{{if .HasInfo}}
{{.InfoMsg}}
{{end}}
@@ -44,7 +61,7 @@
-
Your Repositories +
{{if not .PageIsOrgDashboard}}Your {{end}}Repositories
- + + {{if not .PageIsOrgDashboard}}
Collaborative Repositories
@@ -78,6 +96,7 @@
+ {{end}}
{{template "base/footer" .}} diff --git a/templates/user/issues.tmpl b/templates/user/issues.tmpl index d1c2bd99..c4ad64a4 100644 --- a/templates/user/issues.tmpl +++ b/templates/user/issues.tmpl @@ -3,7 +3,7 @@
+
{{if .HasInfo}}
{{.InfoMsg}}
{{end}}
-- cgit v1.2.3 From 4cf7a1fca5ed22c4d46317477e6feab324fb337e Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 25 Jun 2014 03:55:59 -0400 Subject: Mirror update on create repo page --- routers/repo/repo.go | 13 +++++++++++++ templates/repo/create.tmpl | 11 ++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) (limited to 'routers/repo') diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 2ead24df..6d241699 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -36,6 +36,13 @@ func Create(ctx *middleware.Context) { ctx.Data["PageIsNewRepo"] = true ctx.Data["LanguageIgns"] = models.LanguageIgns ctx.Data["Licenses"] = models.Licenses + + if err := ctx.User.GetOrganizations(); err != nil { + ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) + return + } + ctx.Data["Orgs"] = ctx.User.Orgs + ctx.HTML(200, CREATE) } @@ -45,6 +52,12 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Data["LanguageIgns"] = models.LanguageIgns ctx.Data["Licenses"] = models.Licenses + if err := ctx.User.GetOrganizations(); err != nil { + ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) + return + } + ctx.Data["Orgs"] = ctx.User.Orgs + if ctx.HasError() { ctx.HTML(200, CREATE) return diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index c3ba89ae..3288b736 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -19,12 +19,21 @@
-- cgit v1.2.3 From 43b33440b53c79a22de08880851b5b55e9b6a4b3 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 25 Jun 2014 05:14:36 -0400 Subject: Work on create organization repo and #257 --- models/action.go | 8 ++-- models/org.go | 37 +++++++++++++++ models/repo.go | 114 ++++++++++++++++++++++++++++----------------- models/user.go | 15 +++--- modules/auth/repo.go | 5 +- routers/repo/repo.go | 23 +++++++-- templates/repo/create.tmpl | 7 +-- 7 files changed, 144 insertions(+), 65 deletions(-) (limited to 'routers/repo') diff --git a/models/action.go b/models/action.go index 8ecdf1de..bbbe2134 100644 --- a/models/action.go +++ b/models/action.go @@ -182,14 +182,14 @@ func CommitRepoAction(userId, repoUserId int64, userName, actEmail string, } // NewRepoAction adds new action for creating repository. -func NewRepoAction(user *User, repo *Repository) (err error) { - if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email, +func NewRepoAction(u *User, repo *Repository) (err error) { + if err = NotifyWatchers(&Action{ActUserId: u.Id, ActUserName: u.Name, ActEmail: u.Email, OpType: OP_CREATE_REPO, RepoId: repo.Id, RepoName: repo.Name, IsPrivate: repo.IsPrivate}); err != nil { - log.Error("action.NewRepoAction(notify watchers): %d/%s", user.Id, repo.Name) + log.Error("action.NewRepoAction(notify watchers): %d/%s", u.Id, repo.Name) return err } - log.Trace("action.NewRepoAction: %s/%s", user.LowerName, repo.LowerName) + log.Trace("action.NewRepoAction: %s/%s", u.LowerName, repo.LowerName) return err } diff --git a/models/org.go b/models/org.go index 1cfe1798..227151ab 100644 --- a/models/org.go +++ b/models/org.go @@ -12,6 +12,8 @@ const ( ORG_ADMIN ) +const OWNER_TEAM = "Owner" + // Team represents a organization team. type Team struct { Id int64 @@ -19,6 +21,7 @@ type Team struct { Name string Description string Authorize AuthorizeType + RepoIds string `xorm:"TEXT"` NumMembers int NumRepos int } @@ -29,6 +32,15 @@ func NewTeam(t *Team) error { return err } +func UpdateTeam(t *Team) error { + if len(t.Description) > 255 { + t.Description = t.Description[:255] + } + + _, err := x.Id(t.Id).AllCols().Update(t) + return err +} + // ________ ____ ___ // \_____ \_______ ____ | | \______ ___________ // / | \_ __ \/ ___\| | / ___// __ \_ __ \ @@ -53,6 +65,13 @@ func GetOrgUsersByUserId(uid int64) ([]*OrgUser, error) { return ous, err } +// GetOrgUsersByOrgId returns all organization-user relations by organization ID. +func GetOrgUsersByOrgId(orgId int64) ([]*OrgUser, error) { + ous := make([]*OrgUser, 0, 10) + err := x.Where("org_id=?", orgId).Find(&ous) + return ous, err +} + // ___________ ____ ___ // \__ ___/___ _____ _____ | | \______ ___________ // | |_/ __ \\__ \ / \| | / ___// __ \_ __ \ @@ -67,3 +86,21 @@ type TeamUser struct { OrgId int64 `xorm:"INDEX"` TeamId int64 } + +// GetTeamMembers returns all members in given team of organization. +func GetTeamMembers(orgId, teamId int64) ([]*User, error) { + tus := make([]*TeamUser, 0, 10) + err := x.Where("org_id=?", orgId).And("team_id=?", teamId).Find(&tus) + if err != nil { + return nil, err + } + + us := make([]*User, len(tus)) + for i, tu := range tus { + us[i], err = GetUserById(tu.Uid) + if err != nil { + return nil, err + } + } + return us, nil +} diff --git a/models/repo.go b/models/repo.go index f0e46c71..9cf90a94 100644 --- a/models/repo.go +++ b/models/repo.go @@ -454,21 +454,27 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep return initRepoCommit(tmpDir, user.NewGitSig()) } -// CreateRepository creates a repository for given user or orgnaziation. -func CreateRepository(user *User, name, desc, lang, license string, private, mirror, initReadme bool) (*Repository, error) { +// CreateRepository creates a repository for given user or organization. +func CreateRepository(u *User, name, desc, lang, license string, private, mirror, initReadme bool) (*Repository, error) { if !IsLegalName(name) { return nil, ErrRepoNameIllegal } - isExist, err := IsRepositoryExist(user, name) + isExist, err := IsRepositoryExist(u, name) if err != nil { return nil, err } else if isExist { return nil, ErrRepoAlreadyExist } + sess := x.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return nil, err + } + repo := &Repository{ - OwnerId: user.Id, + OwnerId: u.Id, Name: name, LowerName: strings.ToLower(name), Description: desc, @@ -479,69 +485,85 @@ func CreateRepository(user *User, name, desc, lang, license string, private, mir repo.DefaultBranch = "master" } - repoPath := RepoPath(user.Name, repo.Name) - - sess := x.NewSession() - defer sess.Close() - if err = sess.Begin(); err != nil { - return nil, err - } - if _, err = sess.Insert(repo); err != nil { - if err2 := os.RemoveAll(repoPath); err2 != nil { - log.Error("repo.CreateRepository(repo): %v", err) - return nil, errors.New(fmt.Sprintf( - "delete repo directory %s/%s failed(1): %v", user.Name, repo.Name, err2)) - } sess.Rollback() return nil, err } + var t *Team // Owner team. + mode := WRITABLE if mirror { mode = READABLE } - access := Access{ - UserName: user.LowerName, - RepoName: strings.ToLower(path.Join(user.Name, repo.Name)), + access := &Access{ + UserName: u.LowerName, + RepoName: strings.ToLower(path.Join(u.Name, repo.Name)), Mode: mode, } - if _, err = sess.Insert(&access); err != nil { - sess.Rollback() - if err2 := os.RemoveAll(repoPath); err2 != nil { - log.Error("repo.CreateRepository(access): %v", err) - return nil, errors.New(fmt.Sprintf( - "delete repo directory %s/%s failed(2): %v", user.Name, repo.Name, err2)) + // Give access to all members in owner team. + if u.IsOrganization() { + t, err = u.GetOwnerTeam() + if err != nil { + sess.Rollback() + return nil, err + } + us, err := GetTeamMembers(u.Id, t.Id) + if err != nil { + sess.Rollback() + return nil, err + } + for _, u := range us { + access.UserName = u.LowerName + if _, err = sess.Insert(access); err != nil { + sess.Rollback() + return nil, err + } + } + } else { + if _, err = sess.Insert(access); err != nil { + sess.Rollback() + return nil, err } - return nil, err } rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?" - if _, err = sess.Exec(rawSql, user.Id); err != nil { + if _, err = sess.Exec(rawSql, u.Id); err != nil { sess.Rollback() - if err2 := os.RemoveAll(repoPath); err2 != nil { - log.Error("repo.CreateRepository(repo count): %v", err) - return nil, errors.New(fmt.Sprintf( - "delete repo directory %s/%s failed(3): %v", user.Name, repo.Name, err2)) - } return nil, err } - if err = sess.Commit(); err != nil { - sess.Rollback() - if err2 := os.RemoveAll(repoPath); err2 != nil { - log.Error("repo.CreateRepository(commit): %v", err) - return nil, errors.New(fmt.Sprintf( - "delete repo directory %s/%s failed(3): %v", user.Name, repo.Name, err2)) + // Update owner team info and count. + if u.IsOrganization() { + t.RepoIds += "$" + base.ToStr(repo.Id) + "|" + t.NumRepos++ + if _, err = sess.Id(t.Id).AllCols().Update(t); err != nil { + sess.Rollback() + return nil, err } + } + + if err = sess.Commit(); err != nil { return nil, err } - if err = WatchRepo(user.Id, repo.Id, true); err != nil { - log.Error("repo.CreateRepository(WatchRepo): %v", err) + if u.IsOrganization() { + ous, err := GetOrgUsersByOrgId(u.Id) + if err != nil { + log.Error("repo.CreateRepository(GetOrgUsersByOrgId): %v", err) + } else { + for _, ou := range ous { + if err = WatchRepo(ou.Uid, repo.Id, true); err != nil { + log.Error("repo.CreateRepository(WatchRepo): %v", err) + } + } + } + } + if err = WatchRepo(u.Id, repo.Id, true); err != nil { + log.Error("repo.CreateRepository(WatchRepo2): %v", err) } - if err = NewRepoAction(user, repo); err != nil { + if err = NewRepoAction(u, repo); err != nil { log.Error("repo.CreateRepository(NewRepoAction): %v", err) } @@ -550,7 +572,13 @@ func CreateRepository(user *User, name, desc, lang, license string, private, mir return repo, nil } - if err = initRepository(repoPath, user, repo, initReadme, lang, license); err != nil { + repoPath := RepoPath(u.Name, repo.Name) + if err = initRepository(repoPath, u, repo, initReadme, lang, license); err != nil { + if err2 := os.RemoveAll(repoPath); err2 != nil { + log.Error("repo.CreateRepository(initRepository): %v", err) + return nil, errors.New(fmt.Sprintf( + "delete repo directory %s/%s failed(2): %v", u.Name, repo.Name, err2)) + } return nil, err } diff --git a/models/user.go b/models/user.go index 2388be9a..f67911ca 100644 --- a/models/user.go +++ b/models/user.go @@ -123,11 +123,14 @@ func (u *User) GetOrganizations() error { return nil } -// Member represents user is member of organization. -type Member struct { - Id int64 - OrgId int64 `xorm:"unique(member) index"` - UserId int64 `xorm:"unique(member)"` +// GetOwnerTeam returns owner team of organization. +func (org *User) GetOwnerTeam() (*Team, error) { + t := &Team{ + OrgId: org.Id, + Name: OWNER_TEAM, + } + _, err := x.Get(t) + return t, err } // IsUserExist checks if given user name exist, @@ -249,7 +252,7 @@ func CreateOrganization(org, owner *User) (*User, error) { // Create default owner team. t := &Team{ OrgId: org.Id, - Name: "Owner", + Name: OWNER_TEAM, Authorize: ORG_ADMIN, NumMembers: 1, } diff --git a/modules/auth/repo.go b/modules/auth/repo.go index 92ba64a2..999f33fe 100644 --- a/modules/auth/repo.go +++ b/modules/auth/repo.go @@ -22,9 +22,10 @@ import ( // \/ \/|__| \/ \/ type CreateRepoForm struct { + Uid int64 `form:"uid" binding:"Required"` RepoName string `form:"repo" binding:"Required;AlphaDash;MaxSize(100)"` Private bool `form:"private"` - Description string `form:"desc" binding:"MaxSize(100)"` + Description string `form:"desc" binding:"MaxSize(255)"` Language string `form:"language"` License string `form:"license"` InitReadme bool `form:"initReadme"` @@ -50,7 +51,7 @@ type MigrateRepoForm struct { RepoName string `form:"repo" binding:"Required;AlphaDash;MaxSize(100)"` Mirror bool `form:"mirror"` Private bool `form:"private"` - Description string `form:"desc" binding:"MaxSize(100)"` + Description string `form:"desc" binding:"MaxSize(255)"` } func (f *MigrateRepoForm) Name(field string) string { diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 6d241699..d9645642 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -63,11 +63,26 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { return } - repo, err := models.CreateRepository(ctx.User, form.RepoName, form.Description, + 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.Dashboard(GetUserById)", err) + } else { + ctx.Handle(500, "home.Dashboard(GetUserById)", err) + } + return + } + } + + repo, err := models.CreateRepository(u, form.RepoName, form.Description, form.Language, form.License, form.Private, false, form.InitReadme) if err == nil { - log.Trace("%s Repository created: %s/%s", ctx.Req.RequestURI, ctx.User.LowerName, form.RepoName) - ctx.Redirect("/" + ctx.User.Name + "/" + form.RepoName) + log.Trace("%s Repository created: %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", CREATE, &form) @@ -78,7 +93,7 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { } if repo != nil { - if errDelete := models.DeleteRepository(ctx.User.Id, repo.Id, ctx.User.Name); errDelete != nil { + if errDelete := models.DeleteRepository(u.Id, repo.Id, u.Name); errDelete != nil { log.Error("repo.CreatePost(DeleteRepository): %v", errDelete) } } diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index 0eee1e57..38b32a3b 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -38,12 +38,7 @@
- - - +
-- cgit v1.2.3 From 32b09681b2da96afe9a44790e7944c4ac51e696c Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 25 Jun 2014 05:35:23 -0400 Subject: Migrate repository by organization --- models/repo.go | 8 ++++---- modules/auth/repo.go | 1 + routers/repo/repo.go | 46 ++++++++++++++++++++++++++++++++++++--------- templates/repo/migrate.tmpl | 32 +++++++++++++++++++++++++++++-- 4 files changed, 72 insertions(+), 15 deletions(-) (limited to 'routers/repo') diff --git a/models/repo.go b/models/repo.go index 728f14a7..840529b9 100644 --- a/models/repo.go +++ b/models/repo.go @@ -251,8 +251,8 @@ func MirrorUpdate() { } // MigrateRepository migrates a existing repository from other project hosting. -func MigrateRepository(user *User, name, desc string, private, mirror bool, url string) (*Repository, error) { - repo, err := CreateRepository(user, name, desc, "", "", private, mirror, false) +func MigrateRepository(u *User, name, desc string, private, mirror bool, url string) (*Repository, error) { + repo, err := CreateRepository(u, name, desc, "", "", private, mirror, false) if err != nil { return nil, err } @@ -261,11 +261,11 @@ func MigrateRepository(user *User, name, desc string, private, mirror bool, url tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond())) os.MkdirAll(tmpDir, os.ModePerm) - repoPath := RepoPath(user.Name, name) + repoPath := RepoPath(u.Name, name) repo.IsBare = false if mirror { - if err = MirrorRepository(repo.Id, user.Name, repo.Name, repoPath, url); err != nil { + if err = MirrorRepository(repo.Id, u.Name, repo.Name, repoPath, url); err != nil { return repo, err } repo.IsMirror = true diff --git a/modules/auth/repo.go b/modules/auth/repo.go index 999f33fe..db13743d 100644 --- a/modules/auth/repo.go +++ b/modules/auth/repo.go @@ -48,6 +48,7 @@ type MigrateRepoForm struct { Url string `form:"url" binding:"Url"` AuthUserName string `form:"auth_username"` AuthPasswd string `form:"auth_password"` + Uid int64 `form:"uid" binding:"Required"` RepoName string `form:"repo" binding:"Required;AlphaDash;MaxSize(100)"` Mirror bool `form:"mirror"` Private bool `form:"private"` diff --git a/routers/repo/repo.go b/routers/repo/repo.go index d9645642..6cb6c066 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -53,7 +53,7 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Data["Licenses"] = models.Licenses if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) + ctx.Handle(500, "home.CreatePost(GetOrganizations)", err) return } ctx.Data["Orgs"] = ctx.User.Orgs @@ -70,9 +70,9 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { u, err = models.GetUserById(form.Uid) if err != nil { if err == models.ErrUserNotExist { - ctx.Handle(404, "home.Dashboard(GetUserById)", err) + ctx.Handle(404, "home.CreatePost(GetUserById)", err) } else { - ctx.Handle(500, "home.Dashboard(GetUserById)", err) + ctx.Handle(500, "home.CreatePost(GetUserById)", err) } return } @@ -97,12 +97,19 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { log.Error("repo.CreatePost(DeleteRepository): %v", errDelete) } } - ctx.Handle(500, "repo.CreatePost", err) + 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) } @@ -110,19 +117,40 @@ 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(ctx.User, form.RepoName, form.Description, form.Private, + 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, ctx.User.LowerName, form.RepoName) - ctx.Redirect("/" + ctx.User.Name + "/" + form.RepoName) + 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) @@ -133,7 +161,7 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { } if repo != nil { - if errDelete := models.DeleteRepository(ctx.User.Id, repo.Id, ctx.User.Name); errDelete != nil { + if errDelete := models.DeleteRepository(u.Id, repo.Id, u.Name); errDelete != nil { log.Error("repo.MigratePost(DeleteRepository): %v", errDelete) } } @@ -142,7 +170,7 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { ctx.RenderWithErr(err.Error(), MIGRATE, &form) return } - ctx.Handle(500, "repo.Migrate", err) + ctx.Handle(500, "repo.Migrate(MigrateRepository)", err) } func Single(ctx *middleware.Context, params martini.Params) { diff --git a/templates/repo/migrate.tmpl b/templates/repo/migrate.tmpl index 34a4077e..fff25e6d 100644 --- a/templates/repo/migrate.tmpl +++ b/templates/repo/migrate.tmpl @@ -44,9 +44,37 @@
-

{{.SignedUserName}}

- +
+ + + +
+
-- cgit v1.2.3