summaryrefslogtreecommitdiffstats
path: root/models/action.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/action.go')
-rw-r--r--models/action.go97
1 files changed, 97 insertions, 0 deletions
diff --git a/models/action.go b/models/action.go
index 55557da2ff..362b238f26 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); 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, ""); 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,