summaryrefslogtreecommitdiffstats
path: root/models/action.go
diff options
context:
space:
mode:
authorJustin Nuß <justin.nuss@hmmh.de>2014-07-23 13:48:06 +0200
committerJustin Nuß <justin.nuss@hmmh.de>2014-07-23 13:48:06 +0200
commit469cbc881340dd3c31d4eeb3eb07ee805308a246 (patch)
tree65d3b8ee6542752a226ce616a703520e9c2d69fb /models/action.go
parent6e9f1c52b18f112eecd5c72e295cfea1809f07fa (diff)
downloadgitea-469cbc881340dd3c31d4eeb3eb07ee805308a246.tar.gz
gitea-469cbc881340dd3c31d4eeb3eb07ee805308a246.zip
Update progress of milestones when closing/reopening issues and allow closing of issues in commit messages
Diffstat (limited to 'models/action.go')
-rw-r--r--models/action.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/models/action.go b/models/action.go
index 55557da2ff..2fbbdc3133 100644
--- a/models/action.go
+++ b/models/action.go
@@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "regexp"
"strings"
"time"
@@ -32,6 +33,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 +93,52 @@ func (a Action) GetContent() string {
return a.Content
}
+func updateIssuesCommit(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:]
+
+ 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
+ }
+
+ if issue.IsClosed {
+ continue
+ }
+
+ issue.IsClosed = true
+
+ if err = UpdateIssue(issue); err != nil {
+ return err
+ }
+
+ if err = ChangeMilestoneIssueStats(issue); 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 +168,12 @@ func CommitRepoAction(userId, repoUserId int64, userName, actEmail string,
return errors.New("action.CommitRepoAction(UpdateRepository): " + err.Error())
}
+ err = updateIssuesCommit(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,