diff options
author | Justin Nuß <justin.nuss@hmmh.de> | 2014-07-24 12:29:37 +0200 |
---|---|---|
committer | Justin Nuß <justin.nuss@hmmh.de> | 2014-07-24 12:29:37 +0200 |
commit | 967856f36ebd2d6216ceb2299b5399f7d25e86e9 (patch) | |
tree | bfde3326df1efcda39927bea2b44e9dcc0d436e5 /models | |
parent | 3c025b395077292a721419942f997311ef575fd9 (diff) | |
parent | a76a948a029f46697a0e2327ea6ca86872a760d7 (diff) | |
download | gitea-967856f36ebd2d6216ceb2299b5399f7d25e86e9.tar.gz gitea-967856f36ebd2d6216ceb2299b5399f7d25e86e9.zip |
Merge branch 'dev' into feature/attachments
Conflicts:
models/issue.go
routers/repo/issue.go
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 97 | ||||
-rw-r--r-- | models/issue.go | 97 | ||||
-rw-r--r-- | models/repo.go | 25 |
3 files changed, 208 insertions, 11 deletions
diff --git a/models/action.go b/models/action.go index 55557da2ff..f6684baa0a 100644 --- a/models/action.go +++ b/models/action.go @@ -8,8 +8,10 @@ import ( "encoding/json" "errors" "fmt" + "regexp" "strings" "time" + "unicode" "github.com/gogits/git" @@ -32,6 +34,20 @@ const ( OP_COMMENT_ISSUE ) +var ( + ErrNotImplemented = errors.New("Not implemented yet") +) + +var ( + // Same as Github. See https://help.github.com/articles/closing-issues-via-commit-messages + IssueKeywords = []string{"close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved"} + IssueKeywordsPat *regexp.Regexp +) + +func init() { + IssueKeywordsPat = regexp.MustCompile(fmt.Sprintf(`(?i)(?:%s) \S+`, strings.Join(IssueKeywords, "|"))) +} + // Action represents user operation type and other information to repository., // it implemented interface base.Actioner so that can be used in template render. type Action struct { @@ -78,6 +94,81 @@ func (a Action) GetContent() string { return a.Content } +func updateIssuesCommit(userId, repoId int64, repoUserName, repoName string, commits []*base.PushCommit) error { + for _, c := range commits { + refs := IssueKeywordsPat.FindAllString(c.Message, -1) + + for _, ref := range refs { + ref := ref[strings.IndexByte(ref, byte(' '))+1:] + ref = strings.TrimRightFunc(ref, func(c rune) bool { + return !unicode.IsDigit(c) + }) + + if len(ref) == 0 { + continue + } + + // Add repo name if missing + if ref[0] == '#' { + ref = fmt.Sprintf("%s/%s%s", repoUserName, repoName, ref) + } else if strings.Contains(ref, "/") == false { + // We don't support User#ID syntax yet + // return ErrNotImplemented + + continue + } + + issue, err := GetIssueByRef(ref) + + if err != nil { + return err + } + + url := fmt.Sprintf("/%s/%s/commit/%s", 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, COMMIT, message, nil); err != nil { + return err + } + + if issue.RepoId == repoId { + if issue.IsClosed { + continue + } + + issue.IsClosed = true + + if err = UpdateIssue(issue); err != nil { + return err + } + + issue.Repo, err = GetRepositoryById(issue.RepoId) + + if err != nil { + return err + } + + issue.Repo.NumClosedIssues++ + + if err = UpdateRepository(issue.Repo); err != nil { + return err + } + + if err = ChangeMilestoneIssueStats(issue); err != nil { + return err + } + + // If commit happened in the referenced repository, it means the issue can be closed. + if _, err = CreateComment(userId, repoId, issue.Id, 0, 0, CLOSE, "", nil); err != nil { + return err + } + } + } + } + + return nil +} + // 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) error { @@ -107,6 +198,12 @@ func CommitRepoAction(userId, repoUserId int64, userName, actEmail string, return errors.New("action.CommitRepoAction(UpdateRepository): " + err.Error()) } + err = updateIssuesCommit(userId, repoId, repoUserName, repoName, commit.Commits) + + if err != nil { + log.Debug("action.CommitRepoAction(updateIssuesCommit): ", err) + } + if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, ActEmail: actEmail, OpType: opType, Content: string(bs), RepoId: repoId, RepoUserName: repoUserName, RepoName: repoName, RefName: refName, diff --git a/models/issue.go b/models/issue.go index 43575a07c9..f16c384b6f 100644 --- a/models/issue.go +++ b/models/issue.go @@ -7,6 +7,7 @@ package models import ( "bytes" "errors" + "html/template" "os" "strconv" "strings" @@ -25,6 +26,7 @@ var ( ErrWrongIssueCounter = errors.New("Invalid number of issues for this milestone") ErrAttachmentNotExist = errors.New("Attachment does not exist") ErrAttachmentNotLinked = errors.New("Attachment does not belong to this issue") + ErrMissingIssueNumber = errors.New("No issue number specified") ) // Issue represents an issue or pull request of repository. @@ -140,6 +142,29 @@ func NewIssue(issue *Issue) (err error) { return } +// GetIssueByRef returns an Issue specified by a GFM reference. +// See https://help.github.com/articles/writing-on-github#references for more information on the syntax. +func GetIssueByRef(ref string) (issue *Issue, err error) { + var issueNumber int64 + var repo *Repository + + n := strings.IndexByte(ref, byte('#')) + + if n == -1 { + return nil, ErrMissingIssueNumber + } + + if issueNumber, err = strconv.ParseInt(ref[n+1:], 10, 64); err != nil { + return + } + + if repo, err = GetRepositoryByRef(ref[:n]); err != nil { + return + } + + return GetIssueByIndex(repo.Id, issueNumber) +} + // GetIssueByIndex returns issue by given index in repository. func GetIssueByIndex(rid, index int64) (*Issue, error) { issue := &Issue{RepoId: rid, Index: index} @@ -418,6 +443,11 @@ 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) + + if err != nil { + return err + } + return err } @@ -688,6 +718,32 @@ 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 { + if issue.MilestoneId == 0 { + return nil + } + + m, err := GetMilestoneById(issue.MilestoneId) + + if err != nil { + return err + } + + if issue.IsClosed { + m.NumOpenIssues-- + m.NumClosedIssues++ + } else { + m.NumOpenIssues++ + m.NumClosedIssues-- + } + + m.Completeness = m.NumClosedIssues * 100 / m.NumIssues + + return UpdateMilestone(m) +} + // ChangeMilestoneAssign changes assignment of milestone for issue. func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) { sess := x.NewSession() @@ -711,6 +767,7 @@ func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) { } else { m.Completeness = 0 } + if _, err = sess.Id(m.Id).Update(m); err != nil { sess.Rollback() return err @@ -728,6 +785,7 @@ func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) { if err != nil { return err } + m.NumIssues++ if issue.IsClosed { m.NumClosedIssues++ @@ -749,6 +807,7 @@ func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) { return err } } + return sess.Commit() } @@ -792,17 +851,33 @@ func DeleteMilestone(m *Milestone) (err error) { // \______ /\____/|__|_| /__|_| /\___ >___| /__| // \/ \/ \/ \/ \/ -// Issue types. +// CommentType defines whether a comment is just a simple comment, an action (like close) or a reference. +type CommentType int + const ( - IT_PLAIN = iota // Pure comment. - IT_REOPEN // Issue reopen status change prompt. - IT_CLOSE // Issue close status change prompt. + // Plain comment, can be associated with a commit (CommitId > 0) and a line (Line > 0) + COMMENT CommentType = iota + + // Reopen action + REOPEN + + // Close action + CLOSE + + // Reference from another issue + ISSUE + + // Reference from some commit (not part of a pull request) + COMMIT + + // Reference from some pull request + PULL ) // Comment represents a comment in commit and issue page. type Comment struct { Id int64 - Type int + Type CommentType PosterId int64 Poster *User `xorm:"-"` IssueId int64 @@ -813,7 +888,7 @@ type Comment struct { } // CreateComment creates comment of issue or commit. -func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, content string, attachments []int64) (*Comment, error) { +func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType CommentType, content string, attachments []int64) (*Comment, error) { sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil { @@ -830,7 +905,7 @@ func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, c // Check comment type. switch cmtType { - case IT_PLAIN: + case COMMENT: rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?" if _, err := sess.Exec(rawSql, issueId); err != nil { sess.Rollback() @@ -851,13 +926,13 @@ func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, c return nil, err } } - case IT_REOPEN: + case REOPEN: rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?" if _, err := sess.Exec(rawSql, repoId); err != nil { sess.Rollback() return nil, err } - case IT_CLOSE: + case CLOSE: rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?" if _, err := sess.Exec(rawSql, repoId); err != nil { sess.Rollback() @@ -876,6 +951,10 @@ func GetCommentById(commentId int64) (*Comment, error) { return c, err } +func (c *Comment) ContentHtml() template.HTML { + return template.HTML(c.Content) +} + // GetIssueComments returns list of comment by given issue id. func GetIssueComments(issueId int64) ([]Comment, error) { comments := make([]Comment, 0, 10) diff --git a/models/repo.go b/models/repo.go index 845c1b75a9..8dda6f0d03 100644 --- a/models/repo.go +++ b/models/repo.go @@ -7,9 +7,9 @@ package models import ( "errors" "fmt" - "io/ioutil" "html" "html/template" + "io/ioutil" "os" "path" "path/filepath" @@ -43,6 +43,7 @@ var ( ErrRepoNameIllegal = errors.New("Repository name contains illegal characters") ErrRepoFileNotLoaded = errors.New("Repository file not loaded") ErrMirrorNotExist = errors.New("Mirror does not exist") + ErrInvalidReference = errors.New("Invalid reference specified") ) var ( @@ -837,6 +838,26 @@ func DeleteRepository(userId, repoId int64, userName string) error { return sess.Commit() } +// GetRepositoryByRef returns a Repository specified by a GFM reference. +// See https://help.github.com/articles/writing-on-github#references for more information on the syntax. +func GetRepositoryByRef(ref string) (*Repository, error) { + n := strings.IndexByte(ref, byte('/')) + + if n < 2 { + return nil, ErrInvalidReference + } + + userName, repoName := ref[:n], ref[n+1:] + + user, err := GetUserByName(userName) + + if err != nil { + return nil, err + } + + return GetRepositoryByName(user.Id, repoName) +} + // GetRepositoryByName returns the repository by given name under user if exists. func GetRepositoryByName(userId int64, repoName string) (*Repository, error) { repo := &Repository{ @@ -1017,4 +1038,4 @@ func IsWatching(uid, rid int64) bool { func ForkRepository(repoName string, uid int64) { -}
\ No newline at end of file +} |