aboutsummaryrefslogtreecommitdiff
path: root/models/action.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/action.go')
-rw-r--r--models/action.go157
1 files changed, 126 insertions, 31 deletions
diff --git a/models/action.go b/models/action.go
index 55557da2..b5f692c4 100644
--- a/models/action.go
+++ b/models/action.go
@@ -8,36 +8,53 @@ import (
"encoding/json"
"errors"
"fmt"
+ "path"
+ "regexp"
"strings"
"time"
-
- "github.com/gogits/git"
+ "unicode"
"github.com/gogits/gogs/modules/base"
+ "github.com/gogits/gogs/modules/git"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/setting"
)
-// Operation types of user action.
+type ActionType int
+
const (
- OP_CREATE_REPO = iota + 1
- OP_DELETE_REPO
- OP_STAR_REPO
- OP_FOLLOW_REPO
- OP_COMMIT_REPO
- OP_CREATE_ISSUE
- OP_PULL_REQUEST
- OP_TRANSFER_REPO
- OP_PUSH_TAG
- OP_COMMENT_ISSUE
+ CREATE_REPO ActionType = iota + 1 // 1
+ DELETE_REPO // 2
+ STAR_REPO // 3
+ FOLLOW_REPO // 4
+ COMMIT_REPO // 5
+ CREATE_ISSUE // 6
+ PULL_REQUEST // 7
+ TRANSFER_REPO // 8
+ PUSH_TAG // 9
+ COMMENT_ISSUE // 10
+)
+
+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 {
Id int64
UserId int64 // Receiver user id.
- OpType int
+ OpType ActionType
ActUserId int64 // Action user id.
ActUserName string // Action user name.
ActEmail string
@@ -51,7 +68,7 @@ type Action struct {
}
func (a Action) GetOpType() int {
- return a.OpType
+ return int(a.OpType)
}
func (a Action) GetActUserName() string {
@@ -70,6 +87,10 @@ func (a Action) GetRepoName() string {
return a.RepoName
}
+func (a Action) GetRepoLink() string {
+ return path.Join(a.RepoUserName, a.RepoName)
+}
+
func (a Action) GetBranch() string {
return a.RefName
}
@@ -78,15 +99,85 @@ func (a Action) GetContent() string {
return a.Content
}
+func (a Action) GetCreate() time.Time {
+ return a.Created
+}
+
+func (a Action) GetIssueInfos() []string {
+ return strings.SplitN(a.Content, "|", 2)
+}
+
+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
+ }
+
+ 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 {
- // log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName)
- opType := OP_COMMIT_REPO
+ opType := COMMIT_REPO
// Check it's tag push or branch.
if strings.HasPrefix(refFullName, "refs/tags/") {
- opType = OP_PUSH_TAG
+ opType = PUSH_TAG
commit = &base.PushCommits{}
}
@@ -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,
@@ -184,37 +281,35 @@ func CommitRepoAction(userId, repoUserId int64, userName, actEmail string,
// NewRepoAction adds new action for creating repository.
func NewRepoAction(u *User, repo *Repository) (err error) {
if err = NotifyWatchers(&Action{ActUserId: u.Id, ActUserName: u.Name, ActEmail: u.Email,
- OpType: OP_CREATE_REPO, RepoId: repo.Id, RepoUserName: repo.Owner.Name, RepoName: repo.Name,
+ OpType: CREATE_REPO, RepoId: repo.Id, RepoUserName: repo.Owner.Name, RepoName: repo.Name,
IsPrivate: repo.IsPrivate}); err != nil {
- log.Error("action.NewRepoAction(notify watchers): %d/%s", u.Id, repo.Name)
+ log.Error(4, "NotifyWatchers: %d/%s", u.Id, repo.Name)
return err
}
- log.Trace("action.NewRepoAction: %s/%s", u.LowerName, repo.LowerName)
+ log.Trace("action.NewRepoAction: %s/%s", u.Name, repo.Name)
return err
}
// TransferRepoAction adds new action for transfering repository.
-func TransferRepoAction(user, newUser *User, repo *Repository) (err error) {
- if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
- OpType: OP_TRANSFER_REPO, RepoId: repo.Id, RepoName: repo.Name, Content: newUser.Name,
+func TransferRepoAction(u, newUser *User, repo *Repository) (err error) {
+ if err = NotifyWatchers(&Action{ActUserId: u.Id, ActUserName: u.Name, ActEmail: u.Email,
+ OpType: TRANSFER_REPO, RepoId: repo.Id, RepoName: repo.Name, Content: newUser.Name,
IsPrivate: repo.IsPrivate}); err != nil {
- log.Error("action.TransferRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
+ log.Error(4, "NotifyWatchers: %d/%s", u.Id, repo.Name)
return err
}
- log.Trace("action.TransferRepoAction: %s/%s", user.LowerName, repo.LowerName)
+ log.Trace("action.TransferRepoAction: %s/%s", u.Name, repo.Name)
return err
}
// GetFeeds returns action list of given user in given context.
-func GetFeeds(userid, offset int64, isProfile bool) ([]*Action, error) {
+func GetFeeds(uid, offset int64, isProfile bool) ([]*Action, error) {
actions := make([]*Action, 0, 20)
- sess := x.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
+ sess := x.Limit(20, int(offset)).Desc("id").Where("user_id=?", uid)
if isProfile {
- sess.Where("is_private=?", false).And("act_user_id=?", userid)
- } else {
- sess.And("act_user_id!=?", userid)
+ sess.Where("is_private=?", false).And("act_user_id=?", uid)
}
err := sess.Find(&actions)
return actions, err