aboutsummaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/action.go10
-rw-r--r--models/issue.go76
-rw-r--r--models/repo.go2
3 files changed, 45 insertions, 43 deletions
diff --git a/models/action.go b/models/action.go
index d3393728..86520b57 100644
--- a/models/action.go
+++ b/models/action.go
@@ -153,7 +153,7 @@ func updateIssuesCommit(userId, repoId int64, repoUserName, repoName string, com
url := fmt.Sprintf("%s/%s/%s/commit/%s", setting.AppSubUrl, repoUserName, repoName, c.Sha1)
message := fmt.Sprintf(`<a href="%s">%s</a>`, url, c.Message)
- if _, err = CreateComment(userId, issue.RepoId, issue.Id, 0, 0, COMMENT_TYPE_COMMIT, message, nil); err != nil {
+ if _, err = CreateComment(userId, issue.RepoId, issue.ID, 0, 0, COMMENT_TYPE_COMMIT, message, nil); err != nil {
return err
}
}
@@ -202,7 +202,7 @@ func updateIssuesCommit(userId, repoId int64, repoUserName, repoName string, com
if err = UpdateIssue(issue); err != nil {
return err
- } else if err = UpdateIssueUserPairsByStatus(issue.Id, issue.IsClosed); err != nil {
+ } else if err = UpdateIssueUserPairsByStatus(issue.ID, issue.IsClosed); err != nil {
return err
}
@@ -211,7 +211,7 @@ func updateIssuesCommit(userId, repoId int64, repoUserName, repoName string, com
}
// If commit happened in the referenced repository, it means the issue can be closed.
- if _, err = CreateComment(userId, repoId, issue.Id, 0, 0, COMMENT_TYPE_CLOSE, "", nil); err != nil {
+ if _, err = CreateComment(userId, repoId, issue.ID, 0, 0, COMMENT_TYPE_CLOSE, "", nil); err != nil {
return err
}
}
@@ -261,7 +261,7 @@ func updateIssuesCommit(userId, repoId int64, repoUserName, repoName string, com
if err = UpdateIssue(issue); err != nil {
return err
- } else if err = UpdateIssueUserPairsByStatus(issue.Id, issue.IsClosed); err != nil {
+ } else if err = UpdateIssueUserPairsByStatus(issue.ID, issue.IsClosed); err != nil {
return err
}
@@ -270,7 +270,7 @@ func updateIssuesCommit(userId, repoId int64, repoUserName, repoName string, com
}
// If commit happened in the referenced repository, it means the issue can be closed.
- if _, err = CreateComment(userId, repoId, issue.Id, 0, 0, COMMENT_TYPE_REOPEN, "", nil); err != nil {
+ if _, err = CreateComment(userId, repoId, issue.ID, 0, 0, COMMENT_TYPE_REOPEN, "", nil); err != nil {
return err
}
}
diff --git a/models/issue.go b/models/issue.go
index 60030447..f62bc1e0 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -17,6 +17,7 @@ import (
"github.com/go-xorm/xorm"
"github.com/gogits/gogs/modules/log"
+ "github.com/gogits/gogs/modules/setting"
)
var (
@@ -31,7 +32,7 @@ var (
// Issue represents an issue or pull request of repository.
type Issue struct {
- Id int64
+ ID int64 `xorm:"pk autoincr"`
RepoId int64 `xorm:"INDEX"`
Index int64 // Index in one repository.
Name string
@@ -99,34 +100,29 @@ func (i *Issue) GetAssignee() (err error) {
}
func (i *Issue) Attachments() []*Attachment {
- a, _ := GetAttachmentsForIssue(i.Id)
+ a, _ := GetAttachmentsForIssue(i.ID)
return a
}
func (i *Issue) AfterDelete() {
- _, err := DeleteAttachmentsByIssue(i.Id, true)
+ _, err := DeleteAttachmentsByIssue(i.ID, true)
if err != nil {
- log.Info("Could not delete files for issue #%d: %s", i.Id, err)
+ log.Info("Could not delete files for issue #%d: %s", i.ID, err)
}
}
// CreateIssue creates new issue for repository.
func NewIssue(issue *Issue) (err error) {
sess := x.NewSession()
- defer sess.Close()
+ defer sessionRelease(sess)
if err = sess.Begin(); err != nil {
return err
}
if _, err = sess.Insert(issue); err != nil {
- sess.Rollback()
return err
- }
-
- rawSql := "UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?"
- if _, err = sess.Exec(rawSql, issue.RepoId); err != nil {
- sess.Rollback()
+ } else if _, err = sess.Exec("UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?", issue.RepoId); err != nil {
return err
}
@@ -179,7 +175,7 @@ func GetIssueByIndex(rid, index int64) (*Issue, error) {
// GetIssueById returns an issue by ID.
func GetIssueById(id int64) (*Issue, error) {
- issue := &Issue{Id: id}
+ issue := &Issue{ID: id}
has, err := x.Get(issue)
if err != nil {
return nil, err
@@ -191,7 +187,7 @@ func GetIssueById(id int64) (*Issue, error) {
// GetIssues returns a list of issues by given conditions.
func GetIssues(uid, rid, pid, mid int64, page int, isClosed bool, labelIds, sortType string) ([]Issue, error) {
- sess := x.Limit(20, (page-1)*20)
+ sess := x.Limit(setting.IssuePagingNum, (page-1)*setting.IssuePagingNum)
if rid > 0 {
sess.Where("repo_id=?", rid).And("is_closed=?", isClosed)
@@ -211,9 +207,8 @@ func GetIssues(uid, rid, pid, mid int64, page int, isClosed bool, labelIds, sort
if len(labelIds) > 0 {
for _, label := range strings.Split(labelIds, ",") {
- // Prevent SQL inject.
if com.StrTo(label).MustInt() > 0 {
- sess.And("label_ids like '%$" + label + "|%'")
+ sess.And("label_ids like ?", "'%$"+label+"|%'")
}
}
}
@@ -236,8 +231,7 @@ func GetIssues(uid, rid, pid, mid int64, page int, isClosed bool, labelIds, sort
}
var issues []Issue
- err := sess.Find(&issues)
- return issues, err
+ return issues, sess.Find(&issues)
}
type IssueStatus int
@@ -248,10 +242,9 @@ const (
)
// GetIssuesByLabel returns a list of issues by given label and repository.
-func GetIssuesByLabel(repoId int64, label string) ([]*Issue, error) {
+func GetIssuesByLabel(repoID, labelID int64) ([]*Issue, error) {
issues := make([]*Issue, 0, 10)
- err := x.Where("repo_id=?", repoId).And("label_ids like '%$" + label + "|%'").Find(&issues)
- return issues, err
+ return issues, x.Where("repo_id=?", repoID).And("label_ids like '%$" + com.ToStr(labelID) + "|%'").Find(&issues)
}
// GetIssueCountByPoster returns number of issues of repository by poster.
@@ -281,6 +274,7 @@ type IssueUser struct {
IsClosed bool
}
+// FIXME: organization
// NewIssueUserPairs adds new issue-user pairs for new issue of repository.
func NewIssueUserPairs(repo *Repository, issueID, orgID, posterID, assigneeID int64) error {
users, err := repo.GetCollaborators()
@@ -316,13 +310,24 @@ func NewIssueUserPairs(repo *Repository, issueID, orgID, posterID, assigneeID in
}
}
+ // Add owner's as well.
+ if repo.OwnerId != posterID {
+ iu.Id = 0
+ iu.Uid = repo.OwnerId
+ iu.IsAssigned = iu.Uid == assigneeID
+ if _, err = x.Insert(iu); err != nil {
+ return err
+ }
+ }
+
return nil
}
// PairsContains returns true when pairs list contains given issue.
-func PairsContains(ius []*IssueUser, issueId int64) int {
+func PairsContains(ius []*IssueUser, issueId, uid int64) int {
for i := range ius {
- if ius[i].IssueId == issueId {
+ if ius[i].IssueId == issueId &&
+ ius[i].Uid == uid {
return i
}
}
@@ -450,7 +455,7 @@ func GetUserIssueStats(uid int64, filterMode int) *IssueStats {
// UpdateIssue updates information of issue.
func UpdateIssue(issue *Issue) error {
- _, err := x.Id(issue.Id).AllCols().Update(issue)
+ _, err := x.Id(issue.ID).AllCols().Update(issue)
if err != nil {
return err
@@ -520,7 +525,7 @@ func UpdateIssueUserPairsByMentions(uids []int64, iid int64) error {
// Label represents a label of repository for issues.
type Label struct {
- Id int64
+ ID int64 `xorm:"pk autoincr"`
RepoId int64 `xorm:"INDEX"`
Name string
Color string `xorm:"VARCHAR(7)"`
@@ -547,7 +552,7 @@ func GetLabelById(id int64) (*Label, error) {
return nil, ErrLabelNotExist
}
- l := &Label{Id: id}
+ l := &Label{ID: id}
has, err := x.Get(l)
if err != nil {
return nil, err
@@ -566,14 +571,13 @@ func GetLabels(repoId int64) ([]*Label, error) {
// UpdateLabel updates label information.
func UpdateLabel(l *Label) error {
- _, err := x.Id(l.Id).AllCols().Update(l)
+ _, err := x.Id(l.ID).AllCols().Update(l)
return err
}
// DeleteLabel delete a label of given repository.
-func DeleteLabel(repoId int64, strId string) error {
- id, _ := com.StrTo(strId).Int64()
- l, err := GetLabelById(id)
+func DeleteLabel(repoID, labelID int64) error {
+ l, err := GetLabelById(labelID)
if err != nil {
if err == ErrLabelNotExist {
return nil
@@ -581,27 +585,25 @@ func DeleteLabel(repoId int64, strId string) error {
return err
}
- issues, err := GetIssuesByLabel(repoId, strId)
+ issues, err := GetIssuesByLabel(repoID, labelID)
if err != nil {
return err
}
sess := x.NewSession()
- defer sess.Close()
+ defer sessionRelease(sess)
if err = sess.Begin(); err != nil {
return err
}
for _, issue := range issues {
- issue.LabelIds = strings.Replace(issue.LabelIds, "$"+strId+"|", "", -1)
- if _, err = sess.Id(issue.Id).AllCols().Update(issue); err != nil {
- sess.Rollback()
+ issue.LabelIds = strings.Replace(issue.LabelIds, "$"+com.ToStr(labelID)+"|", "", -1)
+ if _, err = sess.Id(issue.ID).AllCols().Update(issue); err != nil {
return err
}
}
if _, err = sess.Delete(l); err != nil {
- sess.Rollback()
return err
}
return sess.Commit()
@@ -782,7 +784,7 @@ func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) {
}
rawSql := "UPDATE `issue_user` SET milestone_id = 0 WHERE issue_id = ?"
- if _, err = sess.Exec(rawSql, issue.Id); err != nil {
+ if _, err = sess.Exec(rawSql, issue.ID); err != nil {
sess.Rollback()
return err
}
@@ -810,7 +812,7 @@ func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) {
}
rawSql := "UPDATE `issue_user` SET milestone_id = ? WHERE issue_id = ?"
- if _, err = sess.Exec(rawSql, m.Id, issue.Id); err != nil {
+ if _, err = sess.Exec(rawSql, m.Id, issue.ID); err != nil {
sess.Rollback()
return err
}
diff --git a/models/repo.go b/models/repo.go
index 4ee5c382..5610776a 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -876,7 +876,7 @@ func DeleteRepository(uid, repoID int64, userName string) error {
return err
}
for i := range issues {
- if _, err = sess.Delete(&Comment{IssueId: issues[i].Id}); err != nil {
+ if _, err = sess.Delete(&Comment{IssueId: issues[i].ID}); err != nil {
return err
}
}