From 3144fac03cb468aa28c0ade7687b1d4df1de6abb Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 27 Mar 2014 12:48:29 -0400 Subject: IP: RC Code Review --- models/issue.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'models/issue.go') diff --git a/models/issue.go b/models/issue.go index 97e51a0c..6b657b7b 100644 --- a/models/issue.go +++ b/models/issue.go @@ -37,7 +37,7 @@ type Issue struct { } // CreateIssue creates new issue for repository. -func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, content string, isPull bool) (*Issue, error) { +func CreateIssue(userId, repoId, milestoneId, assigneeId int64, issueCount int, name, labels, content string, isPull bool) (*Issue, error) { count, err := GetIssueCount(repoId) if err != nil { return nil, err @@ -46,6 +46,10 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, co // TODO: find out mentions mentions := "" + sess := orm.NewSession() + defer sess.Close() + sess.Begin() + issue := &Issue{ Index: count + 1, Name: name, @@ -58,8 +62,23 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, co Mentions: mentions, Content: content, } - _, err = orm.Insert(issue) - return issue, err + if _, err = sess.Insert(issue); err != nil { + sess.Rollback() + return nil, err + } + + rawSql := "UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?" + if _, err = sess.Exec(rawSql, repoId); err != nil { + sess.Rollback() + return nil, err + } + + if err = sess.Commit(); err != nil { + sess.Rollback() + return nil, err + } + + return issue, nil } // GetIssueCount returns count of issues in the repository. -- cgit v1.2.3 From f76eb8a6662dd705f4c59fd59e583a315a1900d2 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 27 Mar 2014 15:24:11 -0400 Subject: IP: RC Code Review --- README.md | 2 +- README_ZH.md | 2 +- conf/app.ini | 1 + gogs.go | 2 +- models/issue.go | 27 +++++---------------------- models/repo.go | 35 +++++++++++++++++++---------------- templates/issue/list.tmpl | 5 +++-- templates/issue/user.tmpl | 39 +++++++++++---------------------------- 8 files changed, 42 insertions(+), 71 deletions(-) (limited to 'models/issue.go') diff --git a/README.md b/README.md index 9d1d7ef5..7d688506 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Gogs(Go Git Service) is a Self Hosted Git Service in the Go Programming Language ![Demo](http://gowalker.org/public/gogs_demo.gif) -##### Current version: 0.1.8 Alpha +##### Current version: 0.1.9 Alpha #### Other language version diff --git a/README_ZH.md b/README_ZH.md index 9698ce4f..8e187c73 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -5,7 +5,7 @@ Gogs(Go Git Service) 是一个由 Go 语言编写的自助 Git 托管服务。 ![Demo](http://gowalker.org/public/gogs_demo.gif) -##### 当前版本:0.1.8 Alpha +##### 当前版本:0.1.9 Alpha ## 开发目的 diff --git a/conf/app.ini b/conf/app.ini index 1a96ebea..d988b4ac 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -32,6 +32,7 @@ PATH = data/gogs.db [admin] [security] +INSTALL_LOCK = false ; Use HTTPS to clone repository, otherwise use HTTP. ENABLE_HTTPS_CLONE = false ; !!CHANGE THIS TO KEEP YOUR USER DATA SAFE!! diff --git a/gogs.go b/gogs.go index 57d9d62c..f2f408cc 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.2 tag above is included in builds. main.go refers to this definition. const go12tag = true -const APP_VER = "0.1.8.0327 Alpha" +const APP_VER = "0.1.9.0327 Alpha" func init() { base.AppVer = APP_VER diff --git a/models/issue.go b/models/issue.go index 6b657b7b..b05667c3 100644 --- a/models/issue.go +++ b/models/issue.go @@ -37,12 +37,7 @@ type Issue struct { } // CreateIssue creates new issue for repository. -func CreateIssue(userId, repoId, milestoneId, assigneeId int64, issueCount int, name, labels, content string, isPull bool) (*Issue, error) { - count, err := GetIssueCount(repoId) - if err != nil { - return nil, err - } - +func CreateIssue(userId, repoId, milestoneId, assigneeId int64, issueCount int, name, labels, content string, isPull bool) (issue *Issue, err error) { // TODO: find out mentions mentions := "" @@ -50,8 +45,8 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, issueCount int, defer sess.Close() sess.Begin() - issue := &Issue{ - Index: count + 1, + issue = &Issue{ + Index: int64(issueCount) + 1, Name: name, RepoId: repoId, PosterId: userId, @@ -81,11 +76,6 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, issueCount int, return issue, nil } -// GetIssueCount returns count of issues in the repository. -func GetIssueCount(repoId int64) (int64, error) { - return orm.Count(&Issue{RepoId: repoId}) -} - // GetIssueById returns issue object by given id. func GetIssueByIndex(repoId, index int64) (*Issue, error) { issue := &Issue{RepoId: repoId, Index: index} @@ -148,16 +138,10 @@ func GetIssues(userId, repoId, posterId, milestoneId int64, page int, isClosed, // UpdateIssue updates information of issue. func UpdateIssue(issue *Issue) error { - _, err := orm.Update(issue, &Issue{RepoId: issue.RepoId, Index: issue.Index}) + _, err := orm.Id(issue.Id).AllCols().Update(issue) return err } -func CloseIssue() { -} - -func ReopenIssue() { -} - // Label represents a list of labels of repository for issues. type Label struct { Id int64 @@ -197,8 +181,7 @@ func CreateComment(userId, issueId, commitId, line int64, content string) error sess.Begin() if _, err := orm.Insert(&Comment{PosterId: userId, IssueId: issueId, - CommitId: commitId, Line: line, Content: content, - }); err != nil { + CommitId: commitId, Line: line, Content: content}); err != nil { sess.Rollback() return err } diff --git a/models/repo.go b/models/repo.go index c8ffc851..e6d4639b 100644 --- a/models/repo.go +++ b/models/repo.go @@ -96,12 +96,11 @@ func IsRepositoryExist(user *User, repoName string) (bool, error) { has, err := orm.Where("lower_name = ?", strings.ToLower(repoName)).Get(&repo) if err != nil { return has, err + } else if !has { + return false, nil } - s, err := os.Stat(RepoPath(user.Name, repoName)) - if err != nil { - return false, nil // Error simply means does not exist, but we don't want to show up. - } - return s.IsDir(), nil + + return com.IsDir(RepoPath(user.Name, repoName)), nil } var ( @@ -224,16 +223,24 @@ func initRepoCommit(tmpPath string, sig *git.Signature) (err error) { if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "add", "--all"); err != nil { return err } - log.Trace("stderr(1): %s", stderr) + if len(stderr) > 0 { + log.Trace("stderr(1): %s", stderr) + } + if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", "Init commit"); err != nil { return err } - log.Trace("stderr(2): %s", stderr) + if len(stderr) > 0 { + log.Trace("stderr(2): %s", stderr) + } + if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "push", "origin", "master"); err != nil { return err } - log.Trace("stderr(3): %s", stderr) + if len(stderr) > 0 { + log.Trace("stderr(3): %s", stderr) + } return nil } @@ -243,10 +250,9 @@ func createHookUpdate(hookPath, content string) error { return err } defer pu.Close() - if _, err = pu.WriteString(content); err != nil { - return err - } - return nil + + _, err = pu.WriteString(content) + return err } // InitRepository initializes README and .gitignore if needed. @@ -322,10 +328,7 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep } // Apply changes and commit. - if err := initRepoCommit(tmpDir, user.NewGitSig()); err != nil { - return err - } - return nil + return initRepoCommit(tmpDir, user.NewGitSig()) } // UserRepo reporesents a repository with user name. diff --git a/templates/issue/list.tmpl b/templates/issue/list.tmpl index 401a53a3..b8947d9f 100644 --- a/templates/issue/list.tmpl +++ b/templates/issue/list.tmpl @@ -7,8 +7,9 @@
diff --git a/templates/issue/user.tmpl b/templates/issue/user.tmpl index 2cf95713..cf5e365d 100644 --- a/templates/issue/user.tmpl +++ b/templates/issue/user.tmpl @@ -17,8 +17,9 @@
-
- #123 -
Bug: When running tests after generating a beego app, templates do not load.
+ {{range .Issues}} +
+ #{{.Index}} +
{{.Name}}

- - Obama - 3 days ago - 3 -

-
- -
- #123 -
Bug: When running tests after generating a beego app, templates do not load.
-

- - Obama - 3 days ago - 3 + + {{.Poster.Name}} + {{TimeSince .Created}} + {{.NumComments}}

+ {{end}}
-- cgit v1.2.3 From 34f4af9ebf179dbb24a7da6091b4259d66a3c426 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 27 Mar 2014 16:31:32 -0400 Subject: Working on issue and install page --- models/issue.go | 6 +++++ models/repo.go | 1 + routers/repo/issue.go | 12 ++++++++-- routers/user/user.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++ templates/install.tmpl | 54 +++++++++++++++++++++++++----------------- templates/issue/list.tmpl | 4 ++-- templates/issue/user.tmpl | 15 ++++++------ 7 files changed, 118 insertions(+), 34 deletions(-) (limited to 'models/issue.go') diff --git a/models/issue.go b/models/issue.go index b05667c3..b30676e9 100644 --- a/models/issue.go +++ b/models/issue.go @@ -136,6 +136,12 @@ func GetIssues(userId, repoId, posterId, milestoneId int64, page int, isClosed, return issues, err } +// GetUserIssueCount returns the number of issues that were created by given user in repository. +func GetUserIssueCount(userId, repoId int64) int64 { + count, _ := orm.Where("poster_id=?", userId).And("repo_id=?", repoId).Count(new(Issue)) + return count +} + // UpdateIssue updates information of issue. func UpdateIssue(issue *Issue) error { _, err := orm.Id(issue.Id).AllCols().Update(issue) diff --git a/models/repo.go b/models/repo.go index e6d4639b..43d4aa73 100644 --- a/models/repo.go +++ b/models/repo.go @@ -84,6 +84,7 @@ type Repository struct { NumForks int NumIssues int NumClosedIssues int + NumOpenIssues int `xorm:"-"` IsPrivate bool IsBare bool Created time.Time `xorm:"created"` diff --git a/routers/repo/issue.go b/routers/repo/issue.go index effb1fab..23571e75 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -21,13 +21,21 @@ func Issues(ctx *middleware.Context) { ctx.Data["Title"] = "Issues" ctx.Data["IsRepoToolbarIssues"] = true ctx.Data["IsRepoToolbarIssuesList"] = true + ctx.Data["ViewType"] = "all" milestoneId, _ := base.StrTo(ctx.Query("milestone")).Int() page, _ := base.StrTo(ctx.Query("page")).Int() + var posterId int64 = 0 + if ctx.Query("type") == "created_by" { + posterId = ctx.User.Id + ctx.Data["ViewType"] = "created_by" + } + ctx.Data["IssueCreatedCount"] = models.GetUserIssueCount(ctx.User.Id, ctx.Repo.Repository.Id) + // Get issues. - issues, err := models.GetIssues(0, ctx.Repo.Repository.Id, 0, - int64(milestoneId), page, ctx.Query("state") == "closed", false, ctx.Query("labels"), ctx.Query("sortType")) + issues, err := models.GetIssues(0, ctx.Repo.Repository.Id, posterId, int64(milestoneId), page, + ctx.Query("state") == "closed", false, ctx.Query("labels"), ctx.Query("sortType")) if err != nil { ctx.Handle(200, "issue.Issues: %v", err) return diff --git a/routers/user/user.go b/routers/user/user.go index d3ef9621..052a2774 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -286,6 +286,66 @@ func Feeds(ctx *middleware.Context, form auth.FeedsForm) { func Issues(ctx *middleware.Context) { ctx.Data["Title"] = "Your Issues" + ctx.Data["ViewType"] = "all" + + page, _ := base.StrTo(ctx.Query("page")).Int() + + var posterId int64 = 0 + if ctx.Query("type") == "created_by" { + posterId = ctx.User.Id + ctx.Data["ViewType"] = "created_by" + } + + // Get all repositories. + repos, err := models.GetRepositories(ctx.User) + if err != nil { + ctx.Handle(200, "user.Issues(get repository)", err) + return + } + + var closedIssueCount, createdByCount 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, "", "") + if err != nil { + ctx.Handle(200, "user.Issues(get issues)", err) + return + } + + closedIssueCount += repo.NumClosedIssues + repos[i].NumOpenIssues = repo.NumIssues - repo.NumClosedIssues + allIssues = append(allIssues, issues...) + } + + showIssues := make([]models.Issue, 0, len(allIssues)) + isShowClosed := ctx.Query("state") == "closed" + ctx.Data["IsShowClosed"] = isShowClosed + + // Get posters and filter issues. + for i := range allIssues { + u, err := models.GetUserById(allIssues[i].PosterId) + if err != nil { + ctx.Handle(200, "user.Issues(get poster): %v", err) + return + } + allIssues[i].Poster = u + if u.Id == ctx.User.Id { + createdByCount++ + } + + if isShowClosed == allIssues[i].IsClosed { + showIssues = append(showIssues, allIssues[i]) + } + } + + ctx.Data["Repos"] = repos + ctx.Data["Issues"] = showIssues + ctx.Data["AllIssueCount"] = len(allIssues) + ctx.Data["ClosedIssueCount"] = closedIssueCount + ctx.Data["OpenIssueCount"] = len(allIssues) - closedIssueCount + ctx.Data["CreatedByCount"] = createdByCount ctx.HTML(200, "issue/user") } diff --git a/templates/install.tmpl b/templates/install.tmpl index b2ae3bac..4fbef3cb 100644 --- a/templates/install.tmpl +++ b/templates/install.tmpl @@ -2,13 +2,12 @@
{{.CsrfTokenHtml}} -

Install Steps For First Running

+

Install Steps For First-time Run

{{.ErrorMsg}}
-

GoGits need MySQL or PostgreSQL server

+

Gogs requires MySQL or PostgreSQL based on your choice

-
+
- +
-

Recommend use INNODB engine with utf8_general_ci charset.

+
- +
- -

Use SSL protocol to connect PostgreSQL.

+
@@ -74,15 +77,15 @@ -
+
-

General settings for GoGits

+

General Settings of Gogs

@@ -98,8 +101,7 @@
- -

The user has access to visit and run GoGits.

+

The user has access to visit and run Gogs.

@@ -125,7 +127,7 @@
- + @@ -163,20 +165,28 @@

Notification Settings

- - -
- +
+
+ +
-
- -
- +
+
+
+ +
+ diff --git a/templates/issue/list.tmpl b/templates/issue/list.tmpl index b8947d9f..39a4af56 100644 --- a/templates/issue/list.tmpl +++ b/templates/issue/list.tmpl @@ -6,9 +6,9 @@
diff --git a/templates/issue/user.tmpl b/templates/issue/user.tmpl index cf5e365d..edffde4f 100644 --- a/templates/issue/user.tmpl +++ b/templates/issue/user.tmpl @@ -16,21 +16,20 @@
-- cgit v1.2.3 From eb6021f73f270b6fad937432964ab15f42e8f0d9 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 27 Mar 2014 19:42:10 -0400 Subject: Fix issue pages URL params --- models/issue.go | 3 ++- models/repo.go | 3 ++- modules/avatar/avatar.go | 19 +++++++++---------- modules/base/tool.go | 17 ++++++++--------- routers/user/user.go | 25 ++++++++++++++++++++++--- templates/issue/list.tmpl | 4 ++-- templates/issue/user.tmpl | 8 ++++---- 7 files changed, 49 insertions(+), 30 deletions(-) (limited to 'models/issue.go') diff --git a/models/issue.go b/models/issue.go index b30676e9..39558ae2 100644 --- a/models/issue.go +++ b/models/issue.go @@ -21,7 +21,8 @@ type Issue struct { Id int64 Index int64 // Index in one repository. Name string - RepoId int64 `xorm:"index"` + RepoId int64 `xorm:"index"` + Repo *Repository `xorm:"-"` PosterId int64 Poster *User `xorm:"-"` MilestoneId int64 diff --git a/models/repo.go b/models/repo.go index 43d4aa73..726d435d 100644 --- a/models/repo.go +++ b/models/repo.go @@ -436,7 +436,8 @@ func GetRepositoryByName(userId int64, repoName string) (*Repository, error) { } // GetRepositoryById returns the repository by given id if exists. -func GetRepositoryById(id int64) (repo *Repository, err error) { +func GetRepositoryById(id int64) (*Repository, error) { + repo := &Repository{} has, err := orm.Id(id).Get(repo) if err != nil { return nil, err diff --git a/modules/avatar/avatar.go b/modules/avatar/avatar.go index 06e2c138..edeb256f 100644 --- a/modules/avatar/avatar.go +++ b/modules/avatar/avatar.go @@ -47,6 +47,7 @@ func HashEmail(email string) string { return hex.EncodeToString(h.Sum(nil)) } +// Avatar represents the avatar object. type Avatar struct { Hash string AlterImage string // image path @@ -96,8 +97,8 @@ func (this *Avatar) Encode(wr io.Writer, size int) (err error) { return } defer fd.Close() - img, err = jpeg.Decode(fd) - if err != nil { + + if img, err = jpeg.Decode(fd); err != nil { fd.Seek(0, os.SEEK_SET) img, err = png.Decode(fd) } @@ -110,8 +111,8 @@ func (this *Avatar) Encode(wr io.Writer, size int) (err error) { } imgPath = this.AlterImage } - img, err = decodeImageFile(imgPath) - if err != nil { + + if img, err = decodeImageFile(imgPath); err != nil { return } m := resize.Resize(uint(size), 0, img, resize.Lanczos3) @@ -124,8 +125,7 @@ func (this *Avatar) Update() { this.imagePath) } -func (this *Avatar) UpdateTimeout(timeout time.Duration) error { - var err error +func (this *Avatar) UpdateTimeout(timeout time.Duration) (err error) { select { case <-time.After(timeout): err = fmt.Errorf("get gravatar image %s timeout", this.Hash) @@ -140,8 +140,7 @@ type service struct { altImage string } -func (this *service) mustInt(r *http.Request, defaultValue int, keys ...string) int { - var v int +func (this *service) mustInt(r *http.Request, defaultValue int, keys ...string) (v int) { for _, k := range keys { if _, err := fmt.Sscanf(r.FormValue(k), "%d", &v); err == nil { defaultValue = v @@ -176,8 +175,8 @@ func (this *service) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set("ETag", etag) } w.Header().Set("Content-Type", "image/jpeg") - err := avatar.Encode(w, size) - if err != nil { + + if err := avatar.Encode(w, size); err != nil { log.Warn("avatar encode error: %v", err) w.WriteHeader(500) } diff --git a/modules/base/tool.go b/modules/base/tool.go index 6f4fbe83..9ddb90f7 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -412,6 +412,11 @@ func (f StrTo) Int() (int, error) { return int(v), err } +func (f StrTo) Int64() (int64, error) { + v, err := strconv.ParseInt(f.String(), 10, 64) + return int64(v), err +} + func (f StrTo) String() string { if f.Exist() { return string(f) @@ -541,16 +546,10 @@ func ActionDesc(act Actioner, avatarLink string) string { } func DiffTypeToStr(diffType int) string { - switch diffType { - case 1: - return "add" - case 2: - return "modify" - case 3: - return "del" - default: - return "unknown" + diffTypes := map[int]string{ + 1: "add", 2: "modify", 3: "del", } + return diffTypes[diffType] } func DiffLineTypeToStr(diffType int) string { diff --git a/routers/user/user.go b/routers/user/user.go index 052a2774..b0fc5839 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -289,6 +289,9 @@ func Issues(ctx *middleware.Context) { ctx.Data["ViewType"] = "all" page, _ := base.StrTo(ctx.Query("page")).Int() + repoId, _ := base.StrTo(ctx.Query("repoid")).Int64() + + ctx.Data["RepoId"] = repoId var posterId int64 = 0 if ctx.Query("type") == "created_by" { @@ -299,10 +302,12 @@ func Issues(ctx *middleware.Context) { // Get all repositories. repos, err := models.GetRepositories(ctx.User) if err != nil { - ctx.Handle(200, "user.Issues(get repository)", err) + ctx.Handle(200, "user.Issues(get repositories)", err) return } + showRepos := make([]models.Repository, 0, len(repos)) + var closedIssueCount, createdByCount int // Get all issues. @@ -315,8 +320,18 @@ func Issues(ctx *middleware.Context) { } closedIssueCount += repo.NumClosedIssues - repos[i].NumOpenIssues = repo.NumIssues - repo.NumClosedIssues + + // Set repository information to issues. + for j := range issues { + issues[j].Repo = &repos[i] + } allIssues = append(allIssues, issues...) + + repos[i].NumOpenIssues = repo.NumIssues - repo.NumClosedIssues + if repos[i].NumOpenIssues > 0 { + showRepos = append(showRepos, repos[i]) + + } } showIssues := make([]models.Issue, 0, len(allIssues)) @@ -335,12 +350,16 @@ func Issues(ctx *middleware.Context) { createdByCount++ } + if repoId > 0 && repoId != allIssues[i].Repo.Id { + continue + } + if isShowClosed == allIssues[i].IsClosed { showIssues = append(showIssues, allIssues[i]) } } - ctx.Data["Repos"] = repos + ctx.Data["Repos"] = showRepos ctx.Data["Issues"] = showIssues ctx.Data["AllIssueCount"] = len(allIssues) ctx.Data["ClosedIssueCount"] = closedIssueCount diff --git a/templates/issue/list.tmpl b/templates/issue/list.tmpl index 39a4af56..7622a4c1 100644 --- a/templates/issue/list.tmpl +++ b/templates/issue/list.tmpl @@ -15,8 +15,8 @@
diff --git a/templates/issue/user.tmpl b/templates/issue/user.tmpl index edffde4f..1d49395c 100644 --- a/templates/issue/user.tmpl +++ b/templates/issue/user.tmpl @@ -21,22 +21,22 @@
  • Created by you {{.CreatedByCount}}

  • {{range .Repos}} -
  • {{.OwnerId}}/{{.Name}} {{.NumOpenIssues}}
  • +
  • {{$.SignedUser.Name}}/{{.Name}} {{.NumOpenIssues}}
  • {{end}}