Skip to content

Commit 59ffdbf

Browse files
committed
Add create, list, view issue
1 parent b3cfd9f commit 59ffdbf

File tree

11 files changed

+221
-55
lines changed

11 files changed

+221
-55
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Gogs(Go Git Service) is a GitHub-like clone in the Go Programming Language.
55

66
Since we choose to use pure Go implementation of Git manipulation, Gogs certainly supports **ALL platforms** that Go supports, including Linux, Max OS X, and Windows with **ZERO** dependency.
77

8-
##### Current version: 0.1.5 Alpha
8+
##### Current version: 0.1.6 Alpha
99

1010
## Purpose
1111

models/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Action struct {
3030
ActUserName string // Action user name.
3131
RepoId int64
3232
RepoName string
33-
Content string
33+
Content string `xorm:"TEXT"`
3434
Created time.Time `xorm:"created"`
3535
}
3636

models/issue.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
package models
66

77
import (
8+
"errors"
89
"strings"
910
"time"
1011

1112
"github.com/gogits/gogs/modules/base"
1213
)
1314

15+
var (
16+
ErrIssueNotExist = errors.New("Issue does not exist")
17+
)
18+
1419
// Issue represents an issue or pull request of repository.
1520
type Issue struct {
1621
Id int64
@@ -22,22 +27,25 @@ type Issue struct {
2227
AssigneeId int64
2328
IsPull bool // Indicates whether is a pull request or not.
2429
IsClosed bool
25-
Labels string
26-
Mentions string
27-
Content string
30+
Labels string `xorm:"TEXT"`
31+
Mentions string `xorm:"TEXT"`
32+
Content string `xorm:"TEXT"`
2833
NumComments int
2934
Created time.Time `xorm:"created"`
3035
Updated time.Time `xorm:"updated"`
3136
}
3237

3338
// CreateIssue creates new issue for repository.
34-
func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, mentions, content string, isPull bool) error {
39+
func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, content string, isPull bool) (*Issue, error) {
3540
count, err := GetIssueCount(repoId)
3641
if err != nil {
37-
return err
42+
return nil, err
3843
}
3944

40-
_, err = orm.Insert(&Issue{
45+
// TODO: find out mentions
46+
mentions := ""
47+
48+
issue := &Issue{
4149
Index: count + 1,
4250
Name: name,
4351
RepoId: repoId,
@@ -48,18 +56,38 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, me
4856
Labels: labels,
4957
Mentions: mentions,
5058
Content: content,
51-
})
52-
return err
59+
}
60+
_, err = orm.Insert(issue)
61+
return issue, err
5362
}
5463

5564
// GetIssueCount returns count of issues in the repository.
5665
func GetIssueCount(repoId int64) (int64, error) {
5766
return orm.Count(&Issue{RepoId: repoId})
5867
}
5968

69+
// GetIssueById returns issue object by given id.
70+
func GetIssueById(id int64) (*Issue, error) {
71+
issue := new(Issue)
72+
has, err := orm.Id(id).Get(issue)
73+
if err != nil {
74+
return nil, err
75+
} else if !has {
76+
return nil, ErrIssueNotExist
77+
}
78+
return issue, nil
79+
}
80+
6081
// GetIssues returns a list of issues by given conditions.
6182
func GetIssues(userId, repoId, posterId, milestoneId int64, page int, isClosed, isMention bool, labels, sortType string) ([]Issue, error) {
62-
sess := orm.Limit(20, (page-1)*20).Where("repo_id=?", repoId).And("is_closed=?", isClosed)
83+
sess := orm.Limit(20, (page-1)*20)
84+
85+
if repoId > 0 {
86+
sess = sess.Where("repo_id=?", repoId).And("is_closed=?", isClosed)
87+
} else {
88+
sess = sess.Where("is_closed=?", isClosed)
89+
}
90+
6391
if userId > 0 {
6492
sess = sess.And("assignee_id=?", userId)
6593
} else if posterId > 0 {

models/publickey.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type PublicKey struct {
8080
OwnerId int64 `xorm:"index"`
8181
Name string `xorm:"unique not null"`
8282
Fingerprint string
83-
Content string `xorm:"text not null"`
83+
Content string `xorm:"TEXT not null"`
8484
Created time.Time `xorm:"created"`
8585
Updated time.Time `xorm:"updated"`
8686
}

models/repo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,13 @@ func RepoPath(userName, repoName string) string {
372372
}
373373

374374
func UpdateRepository(repo *Repository) error {
375+
if len(repo.Description) > 255 {
376+
repo.Description = repo.Description[:255]
377+
}
378+
if len(repo.Website) > 255 {
379+
repo.Website = repo.Website[:255]
380+
}
381+
375382
_, err := orm.Id(repo.Id).UseBool().Cols("description", "website").Update(repo)
376383
return err
377384
}

models/user.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ func VerifyUserActiveCode(code string) (user *User) {
201201

202202
// UpdateUser updates user's information.
203203
func UpdateUser(user *User) (err error) {
204+
if len(user.Location) > 255 {
205+
user.Location = user.Location[:255]
206+
}
207+
if len(user.Website) > 255 {
208+
user.Website = user.Website[:255]
209+
}
210+
204211
_, err = orm.Id(user.Id).UseBool().Cols("website", "location").Update(user)
205212
return err
206213
}

modules/auth/issue.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package auth
6+
7+
import (
8+
"net/http"
9+
"reflect"
10+
11+
"github.com/codegangsta/martini"
12+
13+
"github.com/gogits/binding"
14+
15+
"github.com/gogits/gogs/modules/base"
16+
"github.com/gogits/gogs/modules/log"
17+
)
18+
19+
type CreateIssueForm struct {
20+
IssueName string `form:"name" binding:"Required;MaxSize(50)"`
21+
RepoId int64 `form:"repoid" binding:"Required"`
22+
MilestoneId int64 `form:"milestoneid" binding:"Required"`
23+
AssigneeId int64 `form:"assigneeid"`
24+
Labels string `form:"labels"`
25+
Content string `form:"content"`
26+
}
27+
28+
func (f *CreateIssueForm) Name(field string) string {
29+
names := map[string]string{
30+
"IssueName": "Issue name",
31+
"RepoId": "Repository ID",
32+
"MilestoneId": "Milestone ID",
33+
}
34+
return names[field]
35+
}
36+
37+
func (f *CreateIssueForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
38+
if req.Method == "GET" || errors.Count() == 0 {
39+
return
40+
}
41+
42+
data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
43+
data["HasError"] = true
44+
AssignForm(f, data)
45+
46+
if len(errors.Overall) > 0 {
47+
for _, err := range errors.Overall {
48+
log.Error("CreateIssueForm.Validate: %v", err)
49+
}
50+
return
51+
}
52+
53+
validate(errors, data, f)
54+
}

routers/repo/issue.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
package repo
66

77
import (
8+
"fmt"
9+
810
"github.com/codegangsta/martini"
911

1012
"github.com/gogits/gogs/models"
13+
"github.com/gogits/gogs/modules/auth"
1114
"github.com/gogits/gogs/modules/base"
15+
"github.com/gogits/gogs/modules/log"
1216
"github.com/gogits/gogs/modules/middleware"
1317
)
1418

1519
func Issues(ctx *middleware.Context, params martini.Params) {
20+
ctx.Data["Title"] = "Issues"
1621
ctx.Data["IsRepoToolbarIssues"] = true
1722

1823
milestoneId, _ := base.StrTo(params["milestone"]).Int()
@@ -29,12 +34,52 @@ func Issues(ctx *middleware.Context, params martini.Params) {
2934
ctx.HTML(200, "repo/issues")
3035
}
3136

32-
func CreateIssue(ctx *middleware.Context, params martini.Params) {
37+
func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
3338
if !ctx.Repo.IsOwner {
3439
ctx.Error(404)
3540
return
3641
}
37-
// else if err = models.CreateIssue(userId, repoId, milestoneId, assigneeId, name, labels, mentions, content, isPull); err != nil {
3842

39-
// }
43+
ctx.Data["Title"] = "Create issue"
44+
45+
if ctx.Req.Method == "GET" {
46+
ctx.HTML(200, "issue/create")
47+
return
48+
}
49+
50+
if ctx.HasError() {
51+
ctx.HTML(200, "issue/create")
52+
return
53+
}
54+
55+
issue, err := models.CreateIssue(ctx.User.Id, form.RepoId, form.MilestoneId, form.AssigneeId,
56+
form.IssueName, form.Labels, form.Content, false)
57+
if err == nil {
58+
log.Trace("%s Issue created: %d", form.RepoId, issue.Id)
59+
ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index), 302)
60+
return
61+
}
62+
ctx.Handle(200, "issue.CreateIssue", err)
63+
}
64+
65+
func ViewIssue(ctx *middleware.Context, params martini.Params) {
66+
issueid, err := base.StrTo(params["issueid"]).Int()
67+
if err != nil {
68+
ctx.Error(404)
69+
return
70+
}
71+
72+
issue, err := models.GetIssueById(int64(issueid))
73+
if err != nil {
74+
if err == models.ErrIssueNotExist {
75+
ctx.Error(404)
76+
} else {
77+
ctx.Handle(200, "issue.ViewIssue", err)
78+
}
79+
return
80+
}
81+
82+
ctx.Data["Title"] = issue.Name
83+
ctx.Data["Issue"] = issue
84+
ctx.HTML(200, "issue/view")
4085
}

routers/repo/repo.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) {
3131
return
3232
}
3333

34+
if ctx.HasError() {
35+
ctx.HTML(200, "repo/create")
36+
return
37+
}
38+
3439
_, err := models.CreateRepository(ctx.User, form.RepoName, form.Description,
3540
form.Language, form.License, form.Visibility == "private", form.InitReadme == "on")
3641
if err == nil {

templates/admin/repos.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<td>{{.Id}}</td>
2828
<th>{{.UserName}}</th>
2929
<td><a href="/{{.UserName}}/{{.Name}}">{{.Name}}</a></td>
30-
<td><i class="fa fa{{if .Private}}-check{{end}}-square-o"></i></td>
30+
<td><i class="fa fa{{if .IsPrivate}}-check{{end}}-square-o"></i></td>
3131
<td>{{.NumWatches}}</td>
3232
<td>{{.NumForks}}</td>
3333
<td>{{DateFormat .Created "M d, Y"}}</td>

0 commit comments

Comments
 (0)