action.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "regexp"
  10. "strings"
  11. "time"
  12. "github.com/gogits/git"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. // Operation types of user action.
  18. const (
  19. OP_CREATE_REPO = iota + 1
  20. OP_DELETE_REPO
  21. OP_STAR_REPO
  22. OP_FOLLOW_REPO
  23. OP_COMMIT_REPO
  24. OP_CREATE_ISSUE
  25. OP_PULL_REQUEST
  26. OP_TRANSFER_REPO
  27. OP_PUSH_TAG
  28. OP_COMMENT_ISSUE
  29. )
  30. var (
  31. ErrNotImplemented = errors.New("Not implemented yet")
  32. )
  33. var (
  34. // Same as Github. See https://help.github.com/articles/closing-issues-via-commit-messages
  35. IssueKeywords = []string{"close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved"}
  36. IssueKeywordsPat *regexp.Regexp
  37. )
  38. func init() {
  39. IssueKeywordsPat = regexp.MustCompile(fmt.Sprintf(`(?i)(?:%s) \S+`, strings.Join(IssueKeywords, "|")))
  40. }
  41. // Action represents user operation type and other information to repository.,
  42. // it implemented interface base.Actioner so that can be used in template render.
  43. type Action struct {
  44. Id int64
  45. UserId int64 // Receiver user id.
  46. OpType int
  47. ActUserId int64 // Action user id.
  48. ActUserName string // Action user name.
  49. ActEmail string
  50. RepoId int64
  51. RepoUserName string
  52. RepoName string
  53. RefName string
  54. IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
  55. Content string `xorm:"TEXT"`
  56. Created time.Time `xorm:"created"`
  57. }
  58. func (a Action) GetOpType() int {
  59. return a.OpType
  60. }
  61. func (a Action) GetActUserName() string {
  62. return a.ActUserName
  63. }
  64. func (a Action) GetActEmail() string {
  65. return a.ActEmail
  66. }
  67. func (a Action) GetRepoUserName() string {
  68. return a.RepoUserName
  69. }
  70. func (a Action) GetRepoName() string {
  71. return a.RepoName
  72. }
  73. func (a Action) GetBranch() string {
  74. return a.RefName
  75. }
  76. func (a Action) GetContent() string {
  77. return a.Content
  78. }
  79. func updateIssuesCommit(repoUserName, repoName string, commits []*base.PushCommit) error {
  80. for _, c := range commits {
  81. refs := IssueKeywordsPat.FindAllString(c.Message, -1)
  82. for _, ref := range refs {
  83. ref := ref[strings.IndexByte(ref, byte(' '))+1:]
  84. if len(ref) == 0 {
  85. continue
  86. }
  87. // Add repo name if missing
  88. if ref[0] == '#' {
  89. ref = fmt.Sprintf("%s/%s%s", repoUserName, repoName, ref)
  90. } else if strings.Contains(ref, "/") == false {
  91. // We don't support User#ID syntax yet
  92. // return ErrNotImplemented
  93. continue
  94. }
  95. issue, err := GetIssueByRef(ref)
  96. if err != nil {
  97. return err
  98. }
  99. if issue.IsClosed {
  100. continue
  101. }
  102. issue.IsClosed = true
  103. if err = UpdateIssue(issue); err != nil {
  104. return err
  105. }
  106. if err = ChangeMilestoneIssueStats(issue); err != nil {
  107. return err
  108. }
  109. }
  110. }
  111. return nil
  112. }
  113. // CommitRepoAction adds new action for committing repository.
  114. func CommitRepoAction(userId, repoUserId int64, userName, actEmail string,
  115. repoId int64, repoUserName, repoName string, refFullName string, commit *base.PushCommits) error {
  116. // log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName)
  117. opType := OP_COMMIT_REPO
  118. // Check it's tag push or branch.
  119. if strings.HasPrefix(refFullName, "refs/tags/") {
  120. opType = OP_PUSH_TAG
  121. commit = &base.PushCommits{}
  122. }
  123. refName := git.RefEndName(refFullName)
  124. bs, err := json.Marshal(commit)
  125. if err != nil {
  126. return errors.New("action.CommitRepoAction(json): " + err.Error())
  127. }
  128. // Change repository bare status and update last updated time.
  129. repo, err := GetRepositoryByName(repoUserId, repoName)
  130. if err != nil {
  131. return errors.New("action.CommitRepoAction(GetRepositoryByName): " + err.Error())
  132. }
  133. repo.IsBare = false
  134. if err = UpdateRepository(repo); err != nil {
  135. return errors.New("action.CommitRepoAction(UpdateRepository): " + err.Error())
  136. }
  137. err = updateIssuesCommit(repoUserName, repoName, commit.Commits)
  138. if err != nil {
  139. log.Debug("action.CommitRepoAction(updateIssuesCommit): ", err)
  140. }
  141. if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, ActEmail: actEmail,
  142. OpType: opType, Content: string(bs), RepoId: repoId, RepoUserName: repoUserName,
  143. RepoName: repoName, RefName: refName,
  144. IsPrivate: repo.IsPrivate}); err != nil {
  145. return errors.New("action.CommitRepoAction(NotifyWatchers): " + err.Error())
  146. }
  147. //qlog.Info("action.CommitRepoAction(end): %d/%s", repoUserId, repoName)
  148. // New push event hook.
  149. if err := repo.GetOwner(); err != nil {
  150. return errors.New("action.CommitRepoAction(GetOwner): " + err.Error())
  151. }
  152. ws, err := GetActiveWebhooksByRepoId(repoId)
  153. if err != nil {
  154. return errors.New("action.CommitRepoAction(GetWebhooksByRepoId): " + err.Error())
  155. } else if len(ws) == 0 {
  156. return nil
  157. }
  158. repoLink := fmt.Sprintf("%s%s/%s", setting.AppUrl, repoUserName, repoName)
  159. commits := make([]*PayloadCommit, len(commit.Commits))
  160. for i, cmt := range commit.Commits {
  161. commits[i] = &PayloadCommit{
  162. Id: cmt.Sha1,
  163. Message: cmt.Message,
  164. Url: fmt.Sprintf("%s/commit/%s", repoLink, cmt.Sha1),
  165. Author: &PayloadAuthor{
  166. Name: cmt.AuthorName,
  167. Email: cmt.AuthorEmail,
  168. },
  169. }
  170. }
  171. p := &Payload{
  172. Ref: refFullName,
  173. Commits: commits,
  174. Repo: &PayloadRepo{
  175. Id: repo.Id,
  176. Name: repo.LowerName,
  177. Url: repoLink,
  178. Description: repo.Description,
  179. Website: repo.Website,
  180. Watchers: repo.NumWatches,
  181. Owner: &PayloadAuthor{
  182. Name: repoUserName,
  183. Email: actEmail,
  184. },
  185. Private: repo.IsPrivate,
  186. },
  187. Pusher: &PayloadAuthor{
  188. Name: repo.Owner.LowerName,
  189. Email: repo.Owner.Email,
  190. },
  191. }
  192. for _, w := range ws {
  193. w.GetEvent()
  194. if !w.HasPushEvent() {
  195. continue
  196. }
  197. p.Secret = w.Secret
  198. CreateHookTask(&HookTask{
  199. Type: WEBHOOK,
  200. Url: w.Url,
  201. Payload: p,
  202. ContentType: w.ContentType,
  203. IsSsl: w.IsSsl,
  204. })
  205. }
  206. return nil
  207. }
  208. // NewRepoAction adds new action for creating repository.
  209. func NewRepoAction(u *User, repo *Repository) (err error) {
  210. if err = NotifyWatchers(&Action{ActUserId: u.Id, ActUserName: u.Name, ActEmail: u.Email,
  211. OpType: OP_CREATE_REPO, RepoId: repo.Id, RepoUserName: repo.Owner.Name, RepoName: repo.Name,
  212. IsPrivate: repo.IsPrivate}); err != nil {
  213. log.Error("action.NewRepoAction(notify watchers): %d/%s", u.Id, repo.Name)
  214. return err
  215. }
  216. log.Trace("action.NewRepoAction: %s/%s", u.LowerName, repo.LowerName)
  217. return err
  218. }
  219. // TransferRepoAction adds new action for transfering repository.
  220. func TransferRepoAction(user, newUser *User, repo *Repository) (err error) {
  221. if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
  222. OpType: OP_TRANSFER_REPO, RepoId: repo.Id, RepoName: repo.Name, Content: newUser.Name,
  223. IsPrivate: repo.IsPrivate}); err != nil {
  224. log.Error("action.TransferRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
  225. return err
  226. }
  227. log.Trace("action.TransferRepoAction: %s/%s", user.LowerName, repo.LowerName)
  228. return err
  229. }
  230. // GetFeeds returns action list of given user in given context.
  231. func GetFeeds(userid, offset int64, isProfile bool) ([]*Action, error) {
  232. actions := make([]*Action, 0, 20)
  233. sess := x.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
  234. if isProfile {
  235. sess.Where("is_private=?", false).And("act_user_id=?", userid)
  236. } else {
  237. sess.And("act_user_id!=?", userid)
  238. }
  239. err := sess.Find(&actions)
  240. return actions, err
  241. }