diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 48 | ||||
-rw-r--r-- | models/error.go | 22 | ||||
-rw-r--r-- | models/issue.go | 392 | ||||
-rw-r--r-- | models/repo.go | 28 | ||||
-rw-r--r-- | models/user.go | 19 |
5 files changed, 374 insertions, 135 deletions
diff --git a/models/action.go b/models/action.go index 4069cbca..de7f93a9 100644 --- a/models/action.go +++ b/models/action.go @@ -124,7 +124,7 @@ func (a Action) GetIssueInfos() []string { return strings.SplitN(a.Content, "|", 2) } -func updateIssuesCommit(userId, repoId int64, repoUserName, repoName string, commits []*base.PushCommit) error { +func updateIssuesCommit(u *User, repo *Repository, repoUserName, repoName string, commits []*base.PushCommit) error { for _, c := range commits { for _, ref := range IssueReferenceKeywordsPat.FindAllString(c.Message, -1) { ref := ref[strings.IndexByte(ref, byte(' '))+1:] @@ -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(u, repo, issue, 0, 0, COMMENT_TYPE_COMMIT_REF, message, nil); err != nil { return err } } @@ -183,7 +183,7 @@ func updateIssuesCommit(userId, repoId int64, repoUserName, repoName string, com return err } - if issue.RepoID == repoId { + if issue.RepoID == repo.ID { if issue.IsClosed { continue } @@ -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 = UpdateIssueUsersByStatus(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(u, repo, issue, 0, 0, COMMENT_TYPE_CLOSE, "", nil); err != nil { return err } } @@ -242,7 +242,7 @@ func updateIssuesCommit(userId, repoId int64, repoUserName, repoName string, com return err } - if issue.RepoID == repoId { + if issue.RepoID == repo.ID { if !issue.IsClosed { continue } @@ -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 = UpdateIssueUsersByStatus(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(u, repo, issue, 0, 0, COMMENT_TYPE_REOPEN, "", nil); err != nil { return err } } @@ -280,8 +280,8 @@ func updateIssuesCommit(userId, repoId int64, repoUserName, repoName string, com } // CommitRepoAction adds new action for committing repository. -func CommitRepoAction(userId, repoUserId int64, userName, actEmail string, - repoId int64, repoUserName, repoName string, refFullName string, commit *base.PushCommits, oldCommitId string, newCommitId string) error { +func CommitRepoAction(userID, repoUserID int64, userName, actEmail string, + repoID int64, repoUserName, repoName string, refFullName string, commit *base.PushCommits, oldCommitID string, newCommitID string) error { opType := COMMIT_REPO // Check it's tag push or branch. @@ -292,40 +292,44 @@ func CommitRepoAction(userId, repoUserId int64, userName, actEmail string, repoLink := fmt.Sprintf("%s%s/%s", setting.AppUrl, repoUserName, repoName) // if not the first commit, set the compareUrl - if !strings.HasPrefix(oldCommitId, "0000000") { - commit.CompareUrl = fmt.Sprintf("%s/%s/compare/%s...%s", repoUserName, repoName, oldCommitId, newCommitId) + if !strings.HasPrefix(oldCommitID, "0000000") { + commit.CompareUrl = fmt.Sprintf("%s/%s/compare/%s...%s", repoUserName, repoName, oldCommitID, newCommitID) } bs, err := json.Marshal(commit) if err != nil { - return errors.New("json: " + err.Error()) + return fmt.Errorf("Marshal: %v", err) } refName := git.RefEndName(refFullName) // Change repository bare status and update last updated time. - repo, err := GetRepositoryByName(repoUserId, repoName) + repo, err := GetRepositoryByName(repoUserID, repoName) if err != nil { - return errors.New("GetRepositoryByName: " + err.Error()) + return fmt.Errorf("GetRepositoryByName: %v", err) } repo.IsBare = false if err = UpdateRepository(repo, false); err != nil { - return errors.New("UpdateRepository: " + err.Error()) + return fmt.Errorf("UpdateRepository: %v", err) } - err = updateIssuesCommit(userId, repoId, repoUserName, repoName, commit.Commits) + u, err := GetUserByID(userID) + if err != nil { + return fmt.Errorf("GetUserByID: %v", err) + } + err = updateIssuesCommit(u, repo, repoUserName, repoName, commit.Commits) if err != nil { log.Debug("updateIssuesCommit: ", err) } if err = NotifyWatchers(&Action{ - ActUserID: userId, + ActUserID: u.Id, ActUserName: userName, ActEmail: actEmail, OpType: opType, Content: string(bs), - RepoID: repoId, + RepoID: repo.ID, RepoUserName: repoUserName, RepoName: repoName, RefName: refName, @@ -340,7 +344,7 @@ func CommitRepoAction(userId, repoUserId int64, userName, actEmail string, return errors.New("GetOwner: " + err.Error()) } - ws, err := GetActiveWebhooksByRepoId(repoId) + ws, err := GetActiveWebhooksByRepoId(repo.ID) if err != nil { return errors.New("GetActiveWebhooksByRepoId: " + err.Error()) } @@ -406,8 +410,8 @@ func CommitRepoAction(userId, repoUserId int64, userName, actEmail string, Email: pusher_email, UserName: userName, }, - Before: oldCommitId, - After: newCommitId, + Before: oldCommitID, + After: newCommitID, CompareUrl: setting.AppUrl + commit.CompareUrl, } diff --git a/models/error.go b/models/error.go index 8fc0ba77..c3e48063 100644 --- a/models/error.go +++ b/models/error.go @@ -239,6 +239,28 @@ func (err ErrRepoAlreadyExist) Error() string { return fmt.Sprintf("repository already exists [uname: %d, name: %s]", err.Uname, err.Name) } +// .___ +// | | ______ ________ __ ____ +// | |/ ___// ___/ | \_/ __ \ +// | |\___ \ \___ \| | /\ ___/ +// |___/____ >____ >____/ \___ > +// \/ \/ \/ + +type ErrIssueNotExist struct { + ID int64 + RepoID int64 + Index int64 +} + +func IsErrIssueNotExist(err error) bool { + _, ok := err.(ErrIssueNotExist) + return ok +} + +func (err ErrIssueNotExist) Error() string { + return fmt.Sprintf("issue does not exist [id: %d, repo_id: %d, index: %4]", err.ID, err.RepoID, err.Index) +} + // .____ ___. .__ // | | _____ \_ |__ ____ | | // | | \__ \ | __ \_/ __ \| | diff --git a/models/issue.go b/models/issue.go index 37fc125c..7f16f936 100644 --- a/models/issue.go +++ b/models/issue.go @@ -8,7 +8,6 @@ import ( "bytes" "errors" "fmt" - "html/template" "io" "mime/multipart" "os" @@ -27,7 +26,6 @@ import ( ) var ( - ErrIssueNotExist = errors.New("Issue does not exist") ErrWrongIssueCounter = errors.New("Invalid number of issues for this milestone") ErrAttachmentNotLinked = errors.New("Attachment does not belong to this issue") ErrMissingIssueNumber = errors.New("No issue number specified") @@ -57,11 +55,30 @@ type Issue struct { Deadline time.Time Created time.Time `xorm:"CREATED"` Updated time.Time `xorm:"UPDATED"` + + Attachments []*Attachment `xorm:"-"` + Comments []*Comment `xorm:"-"` +} + +// HashTag returns unique hash tag for issue. +func (i *Issue) HashTag() string { + return "issue-" + com.ToStr(i.ID) } func (i *Issue) AfterSet(colName string, _ xorm.Cell) { var err error switch colName { + case "id": + i.Attachments, err = GetAttachmentsByIssueID(i.ID) + if err != nil { + log.Error(3, "GetAttachmentsByIssueID[%d]: %v", i.ID, err) + } + + i.Comments, err = GetCommentsByIssueID(i.ID) + if err != nil { + log.Error(3, "GetCommentsByIssueID[%d]: %v", i.ID, err) + } + case "milestone_id": if i.MilestoneID == 0 { return @@ -69,7 +86,7 @@ func (i *Issue) AfterSet(colName string, _ xorm.Cell) { i.Milestone, err = GetMilestoneByID(i.MilestoneID) if err != nil { - log.Error(3, "GetMilestoneById: %v", err) + log.Error(3, "GetMilestoneById[%d]: %v", i.ID, err) } case "assignee_id": if i.AssigneeID == 0 { @@ -78,15 +95,20 @@ func (i *Issue) AfterSet(colName string, _ xorm.Cell) { i.Assignee, err = GetUserByID(i.AssigneeID) if err != nil { - log.Error(3, "GetUserByID: %v", err) + log.Error(3, "GetUserByID[%d]: %v", i.ID, err) } } } +// IsPoster returns true if given user by ID is the poster. +func (i *Issue) IsPoster(uid int64) bool { + return i.PosterID == uid +} + func (i *Issue) GetPoster() (err error) { i.Poster, err = GetUserByID(i.PosterID) if IsErrUserNotExist(err) { - i.Poster = &User{Name: "FakeUser"} + i.Poster = &User{Name: "Someone"} return nil } return err @@ -148,17 +170,64 @@ func (i *Issue) GetAssignee() (err error) { return err } -func (i *Issue) Attachments() []*Attachment { - a, _ := GetAttachmentsForIssue(i.ID) - return a +// ReadBy sets issue to be read by given user. +func (i *Issue) ReadBy(uid int64) error { + return UpdateIssueUserByRead(uid, i.ID) } -func (i *Issue) AfterDelete() { - _, err := DeleteAttachmentsByIssue(i.ID, true) +func (i *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (err error) { + if i.IsClosed == isClosed { + return nil + } + i.IsClosed = isClosed - if err != nil { - log.Info("Could not delete files for issue #%d: %s", i.ID, err) + if err = updateIssue(e, i); err != nil { + return err + } else if err = updateIssueUsersByStatus(e, i.ID, isClosed); err != nil { + return err } + + // Update labels. + if err = i.getLabels(e); err != nil { + return err + } + for idx := range i.Labels { + if i.IsClosed { + i.Labels[idx].NumClosedIssues++ + } else { + i.Labels[idx].NumClosedIssues-- + } + if err = updateLabel(e, i.Labels[idx]); err != nil { + return err + } + } + + // Update milestone. + if err = changeMilestoneIssueStats(e, i); err != nil { + return err + } + + // New action comment. + if _, err = createStatusComment(e, doer, i.Repo, i); err != nil { + return err + } + + return nil +} + +// ChangeStatus changes issue status to open/closed. +func (i *Issue) ChangeStatus(doer *User, isClosed bool) (err error) { + sess := x.NewSession() + defer sessionRelease(sess) + if err = sess.Begin(); err != nil { + return err + } + + if err = i.changeStatus(sess, doer, isClosed); err != nil { + return err + } + + return sess.Commit() } // CreateIssue creates new issue with labels for repository. @@ -255,25 +324,28 @@ func GetIssueByRef(ref string) (issue *Issue, err error) { } // GetIssueByIndex returns issue by given index in repository. -func GetIssueByIndex(rid, index int64) (*Issue, error) { - issue := &Issue{RepoID: rid, Index: index} +func GetIssueByIndex(repoID, index int64) (*Issue, error) { + issue := &Issue{ + RepoID: repoID, + Index: index, + } has, err := x.Get(issue) if err != nil { return nil, err } else if !has { - return nil, ErrIssueNotExist + return nil, ErrIssueNotExist{0, repoID, index} } return issue, nil } -// GetIssueById returns an issue by ID. -func GetIssueById(id int64) (*Issue, error) { - issue := &Issue{ID: id} - has, err := x.Get(issue) +// GetIssueByID returns an issue by given ID. +func GetIssueByID(id int64) (*Issue, error) { + issue := new(Issue) + has, err := x.Id(id).Get(issue) if err != nil { return nil, err } else if !has { - return nil, ErrIssueNotExist + return nil, ErrIssueNotExist{id, 0, 0} } return issue, nil } @@ -580,13 +652,16 @@ func UpdateIssue(issue *Issue) error { return updateIssue(x, issue) } -// UpdateIssueUserByStatus updates issue-user pairs by issue status. -func UpdateIssueUserPairsByStatus(iid int64, isClosed bool) error { - rawSql := "UPDATE `issue_user` SET is_closed = ? WHERE issue_id = ?" - _, err := x.Exec(rawSql, isClosed, iid) +func updateIssueUsersByStatus(e Engine, issueID int64, isClosed bool) error { + _, err := e.Exec("UPDATE `issue_user` SET is_closed=? WHERE issue_id=?", isClosed, issueID) return err } +// UpdateIssueUsersByStatus updates issue-user relations by issue status. +func UpdateIssueUsersByStatus(issueID int64, isClosed bool) error { + return updateIssueUsersByStatus(x, issueID, isClosed) +} + func updateIssueUserByAssignee(e *xorm.Session, issueID, assigneeID int64) (err error) { if _, err = e.Exec("UPDATE `issue_user` SET is_assigned=? WHERE issue_id=?", false, issueID); err != nil { return err @@ -615,10 +690,9 @@ func UpdateIssueUserByAssignee(issueID, assigneeID int64) (err error) { return sess.Commit() } -// UpdateIssueUserPairByRead updates issue-user pair for reading. -func UpdateIssueUserPairByRead(uid, iid int64) error { - rawSql := "UPDATE `issue_user` SET is_read = ? WHERE uid = ? AND issue_id = ?" - _, err := x.Exec(rawSql, true, uid, iid) +// UpdateIssueUserByRead updates issue-user relation for reading. +func UpdateIssueUserByRead(uid, issueID int64) error { + _, err := x.Exec("UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?", true, uid, issueID) return err } @@ -723,10 +797,14 @@ func GetLabelsByIssueID(issueID int64) ([]*Label, error) { return getLabelsByIssueID(x, issueID) } +func updateLabel(e Engine, l *Label) error { + _, err := e.Id(l.ID).AllCols().Update(l) + return err +} + // UpdateLabel updates label information. func UpdateLabel(l *Label) error { - _, err := x.Id(l.ID).AllCols().Update(l) - return err + return updateLabel(x, l) } // DeleteLabel delete a label of given repository. @@ -992,14 +1070,12 @@ func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) { return sess.Commit() } -// ChangeMilestoneIssueStats updates the open/closed issues counter and progress -// for the milestone associated witht the given issue. -func ChangeMilestoneIssueStats(issue *Issue) error { +func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error { if issue.MilestoneID == 0 { return nil } - m, err := GetMilestoneByID(issue.MilestoneID) + m, err := getMilestoneByID(e, issue.MilestoneID) if err != nil { return err } @@ -1012,7 +1088,23 @@ func ChangeMilestoneIssueStats(issue *Issue) error { m.NumClosedIssues-- } - return UpdateMilestone(m) + return updateMilestone(e, m) +} + +// ChangeMilestoneIssueStats updates the open/closed issues counter and progress +// for the milestone associated witht the given issue. +func ChangeMilestoneIssueStats(issue *Issue) (err error) { + sess := x.NewSession() + defer sessionRelease(sess) + if err = sess.Begin(); err != nil { + return err + } + + if err = changeMilestoneIssueStats(sess, issue); err != nil { + return err + } + + return sess.Commit() } func changeMilestoneAssign(e *xorm.Session, oldMid int64, issue *Issue) error { @@ -1130,107 +1222,193 @@ const ( COMMENT_TYPE_CLOSE // References. - COMMENT_TYPE_ISSUE - // Reference from some commit (not part of a pull request) - COMMENT_TYPE_COMMIT - // Reference from some pull request - COMMENT_TYPE_PULL + COMMENT_TYPE_ISSUE_REF + // Reference from a commit (not part of a pull request) + COMMENT_TYPE_COMMIT_REF + // Reference from a comment + COMMENT_TYPE_COMMENT_REF + // Reference from a pull request + COMMENT_TYPE_PULL_REF +) + +type CommentTag int + +const ( + COMMENT_TAG_NONE CommentTag = iota + COMMENT_TAG_POSTER + COMMENT_TAG_ADMIN + COMMENT_TAG_OWNER ) // Comment represents a comment in commit and issue page. type Comment struct { - Id int64 - Type CommentType - PosterId int64 - Poster *User `xorm:"-"` - IssueId int64 - CommitId int64 - Line int64 - Content string `xorm:"TEXT"` - Created time.Time `xorm:"CREATED"` + ID int64 `xorm:"pk autoincr"` + Type CommentType + PosterID int64 + Poster *User `xorm:"-"` + IssueID int64 `xorm:"INDEX"` + CommitID int64 + Line int64 + Content string `xorm:"TEXT"` + RenderedContent string `xorm:"-"` + Created time.Time `xorm:"CREATED"` + + Attachments []*Attachment `xorm:"-"` + + // For view issue page. + ShowTag CommentTag `xorm:"-"` } -// CreateComment creates comment of issue or commit. -func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType CommentType, content string, attachments []int64) (*Comment, error) { - sess := x.NewSession() - defer sessionRelease(sess) - if err := sess.Begin(); err != nil { - return nil, err - } +// HashTag returns unique hash tag for comment. +func (c *Comment) HashTag() string { + return "issuecomment-" + com.ToStr(c.ID) +} + +// EventTag returns unique event hash tag for comment. +func (c *Comment) EventTag() string { + return "event-" + com.ToStr(c.ID) +} + +func (c *Comment) AfterSet(colName string, _ xorm.Cell) { + var err error + switch colName { + case "id": + c.Attachments, err = GetAttachmentsByCommentID(c.ID) + if err != nil { + log.Error(3, "GetAttachmentsByCommentID[%d]: %v", c.ID, err) + } - comment := &Comment{PosterId: userId, Type: cmtType, IssueId: issueId, - CommitId: commitId, Line: line, Content: content} + case "poster_id": + c.Poster, err = GetUserByID(c.PosterID) + if err != nil { + if IsErrUserNotExist(err) { + c.PosterID = -1 + c.Poster = &User{Name: "someone"} + } else { + log.Error(3, "GetUserByID[%d]: %v", c.ID, err) + } + } + } +} - if _, err := sess.Insert(comment); err != nil { +func createComment(e *xorm.Session, u *User, repo *Repository, issue *Issue, commitID, line int64, cmtType CommentType, content string, uuids []string) (_ *Comment, err error) { + comment := &Comment{ + PosterID: u.Id, + Type: cmtType, + IssueID: issue.ID, + CommitID: commitID, + Line: line, + Content: content, + } + if _, err = e.Insert(comment); err != nil { return nil, err } // Check comment type. switch cmtType { case COMMENT_TYPE_COMMENT: - rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?" - if _, err := sess.Exec(rawSql, issueId); err != nil { + if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", issue.ID); err != nil { return nil, err } - if len(attachments) > 0 { - rawSql = "UPDATE `attachment` SET comment_id = ? WHERE id IN (?)" - - astrs := make([]string, 0, len(attachments)) - - for _, a := range attachments { - astrs = append(astrs, strconv.FormatInt(a, 10)) + // Check attachments. + attachments := make([]*Attachment, 0, len(uuids)) + for _, uuid := range uuids { + attach, err := getAttachmentByUUID(e, uuid) + if err != nil { + if IsErrAttachmentNotExist(err) { + continue + } + return nil, fmt.Errorf("getAttachmentByUUID[%s]: %v", uuid, err) } + attachments = append(attachments, attach) + } - if _, err := sess.Exec(rawSql, comment.Id, strings.Join(astrs, ",")); err != nil { - return nil, err + for i := range attachments { + attachments[i].IssueID = issue.ID + attachments[i].CommentID = comment.ID + // No assign value could be 0, so ignore AllCols(). + if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil { + return nil, fmt.Errorf("update attachment[%d]: %v", attachments[i].ID, err) } } + + // Notify watchers. + act := &Action{ + ActUserID: u.Id, + ActUserName: u.LowerName, + ActEmail: u.Email, + OpType: COMMENT_ISSUE, + Content: fmt.Sprintf("%d|%s", issue.Index, strings.Split(content, "\n")[0]), + RepoID: repo.ID, + RepoUserName: repo.Owner.LowerName, + RepoName: repo.LowerName, + IsPrivate: repo.IsPrivate, + } + if err = notifyWatchers(e, act); err != nil { + return nil, err + } + case COMMENT_TYPE_REOPEN: - rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?" - if _, err := sess.Exec(rawSql, repoId); err != nil { + if _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", repo.ID); err != nil { return nil, err } case COMMENT_TYPE_CLOSE: - rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?" - if _, err := sess.Exec(rawSql, repoId); err != nil { + if _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", repo.ID); err != nil { return nil, err } } - return comment, sess.Commit() + return comment, nil } -// GetCommentById returns the comment with the given id -func GetCommentById(commentId int64) (*Comment, error) { - c := &Comment{Id: commentId} - _, err := x.Get(c) +func createStatusComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue) (*Comment, error) { + cmtType := COMMENT_TYPE_CLOSE + if !issue.IsClosed { + cmtType = COMMENT_TYPE_REOPEN + } + return createComment(e, doer, repo, issue, 0, 0, cmtType, "", nil) +} - return c, err +// CreateComment creates comment of issue or commit. +func CreateComment(doer *User, repo *Repository, issue *Issue, commitID, line int64, cmtType CommentType, content string, attachments []string) (comment *Comment, err error) { + sess := x.NewSession() + defer sessionRelease(sess) + if err = sess.Begin(); err != nil { + return nil, err + } + + comment, err = createComment(sess, doer, repo, issue, commitID, line, cmtType, content, attachments) + if err != nil { + return nil, err + } + + return comment, sess.Commit() } -func (c *Comment) ContentHtml() template.HTML { - return template.HTML(c.Content) +// CreateIssueComment creates a plain issue comment. +func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) { + return CreateComment(doer, repo, issue, 0, 0, COMMENT_TYPE_COMMENT, content, attachments) } -// GetIssueComments returns list of comment by given issue id. -func GetIssueComments(issueId int64) ([]Comment, error) { - comments := make([]Comment, 0, 10) - err := x.Asc("created").Find(&comments, &Comment{IssueId: issueId}) - return comments, err +// GetCommentById returns the comment with the given id +func GetCommentById(id int64) (*Comment, error) { + c := new(Comment) + _, err := x.Id(id).Get(c) + return c, err } -// Attachments returns the attachments for this comment. -func (c *Comment) Attachments() []*Attachment { - a, _ := GetAttachmentsByComment(c.Id) - return a +// GetCommentsByIssueID returns all comments of issue by given ID. +func GetCommentsByIssueID(issueID int64) ([]*Comment, error) { + comments := make([]*Comment, 0, 10) + return comments, x.Where("issue_id=?", issueID).Asc("created").Find(&comments) } func (c *Comment) AfterDelete() { - _, err := DeleteAttachmentsByComment(c.Id, true) + _, err := DeleteAttachmentsByComment(c.ID, true) if err != nil { - log.Info("Could not delete files for comment %d on issue #%d: %s", c.Id, c.IssueId, err) + log.Info("Could not delete files for comment %d on issue #%d: %s", c.ID, c.IssueID, err) } } @@ -1291,8 +1469,7 @@ func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, return attach, sess.Commit() } -// GetAttachmentByUUID returns attachment by given UUID. -func GetAttachmentByUUID(uuid string) (*Attachment, error) { +func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) { attach := &Attachment{UUID: uuid} has, err := x.Get(attach) if err != nil { @@ -1303,24 +1480,21 @@ func GetAttachmentByUUID(uuid string) (*Attachment, error) { return attach, nil } -func GetAttachmentsForIssue(issueId int64) ([]*Attachment, error) { - attachments := make([]*Attachment, 0, 10) - err := x.Where("issue_id = ?", issueId).And("comment_id = 0").Find(&attachments) - return attachments, err +// GetAttachmentByUUID returns attachment by given UUID. +func GetAttachmentByUUID(uuid string) (*Attachment, error) { + return getAttachmentByUUID(x, uuid) } -// GetAttachmentsByIssue returns a list of attachments for the given issue -func GetAttachmentsByIssue(issueId int64) ([]*Attachment, error) { +// GetAttachmentsByIssueID returns all attachments for given issue by ID. +func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) { attachments := make([]*Attachment, 0, 10) - err := x.Where("issue_id = ?", issueId).And("comment_id > 0").Find(&attachments) - return attachments, err + return attachments, x.Where("issue_id=? AND comment_id=0", issueID).Find(&attachments) } -// GetAttachmentsByComment returns a list of attachments for the given comment -func GetAttachmentsByComment(commentId int64) ([]*Attachment, error) { +// GetAttachmentsByCommentID returns all attachments if comment by given ID. +func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) { attachments := make([]*Attachment, 0, 10) - err := x.Where("comment_id = ?", commentId).Find(&attachments) - return attachments, err + return attachments, x.Where("comment_id=?", commentID).Find(&attachments) } // DeleteAttachment deletes the given attachment and optionally the associated file. @@ -1348,7 +1522,7 @@ func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) { // DeleteAttachmentsByIssue deletes all attachments associated with the given issue. func DeleteAttachmentsByIssue(issueId int64, remove bool) (int, error) { - attachments, err := GetAttachmentsByIssue(issueId) + attachments, err := GetAttachmentsByIssueID(issueId) if err != nil { return 0, err @@ -1359,7 +1533,7 @@ func DeleteAttachmentsByIssue(issueId int64, remove bool) (int, error) { // DeleteAttachmentsByComment deletes all attachments associated with the given comment. func DeleteAttachmentsByComment(commentId int64, remove bool) (int, error) { - attachments, err := GetAttachmentsByComment(commentId) + attachments, err := GetAttachmentsByCommentID(commentId) if err != nil { return 0, err diff --git a/models/repo.go b/models/repo.go index 1c4f09c4..bad6f386 100644 --- a/models/repo.go +++ b/models/repo.go @@ -247,8 +247,8 @@ func (repo *Repository) HasAccess(u *User) bool { return has } -func (repo *Repository) IsOwnedBy(u *User) bool { - return repo.OwnerID == u.Id +func (repo *Repository) IsOwnedBy(userID int64) bool { + return repo.OwnerID == userID } // DescriptionHtml does special handles to description and return HTML string. @@ -923,13 +923,26 @@ func DeleteRepository(uid, repoID int64, userName string) error { return err } - // Delete comments. + // Delete comments and attachments. issues := make([]*Issue, 0, 25) + attachmentPaths := make([]string, 0, len(issues)) if err = sess.Where("repo_id=?", repoID).Find(&issues); err != nil { 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 + } + + attachments := make([]*Attachment, 0, 5) + if err = sess.Where("issue_id=?", issues[i].ID).Find(&attachments); err != nil { + return err + } + for j := range attachments { + attachmentPaths = append(attachmentPaths, attachments[j].LocalPath()) + } + + if _, err = sess.Delete(&Attachment{IssueID: issues[i].ID}); err != nil { return err } } @@ -957,6 +970,13 @@ func DeleteRepository(uid, repoID int64, userName string) error { } } + // Remove attachment files. + for i := range attachmentPaths { + if err = os.Remove(attachmentPaths[i]); err != nil { + log.Warn("delete attachment: %v", err) + } + } + return sess.Commit() } diff --git a/models/user.go b/models/user.go index a58bb634..f2151a70 100644 --- a/models/user.go +++ b/models/user.go @@ -222,6 +222,25 @@ func (u *User) UploadAvatar(data []byte) error { return sess.Commit() } +// IsAdminOfRepo returns true if user has admin or higher access of repository. +func (u *User) IsAdminOfRepo(repo *Repository) bool { + if err := repo.GetOwner(); err != nil { + log.Error(3, "GetOwner: %v", err) + return false + } + + if repo.Owner.IsOrganization() { + has, err := HasAccess(u, repo, ACCESS_MODE_ADMIN) + if err != nil { + log.Error(3, "HasAccess: %v", err) + return false + } + return has + } + + return repo.IsOwnedBy(u.Id) +} + // IsOrganization returns true if user is actually a organization. func (u *User) IsOrganization() bool { return u.Type == ORGANIZATION |