action.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. issue.Repo.NumClosedIssues++
  107. if err = UpdateRepository(issue.Repo); err != nil {
  108. return err
  109. }
  110. if err = ChangeMilestoneIssueStats(issue); err != nil {
  111. return err
  112. }
  113. }
  114. }
  115. return nil
  116. }
  117. // CommitRepoAction adds new action for committing repository.
  118. func CommitRepoAction(userId, repoUserId int64, userName, actEmail string,
  119. repoId int64, repoUserName, repoName string, refFullName string, commit *base.PushCommits) error {
  120. // log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName)
  121. opType := OP_COMMIT_REPO
  122. // Check it's tag push or branch.
  123. if strings.HasPrefix(refFullName, "refs/tags/") {
  124. opType = OP_PUSH_TAG
  125. commit = &base.PushCommits{}
  126. }
  127. refName := git.RefEndName(refFullName)
  128. bs, err := json.Marshal(commit)
  129. if err != nil {
  130. return errors.New("action.CommitRepoAction(json): " + err.Error())
  131. }
  132. // Change repository bare status and update last updated time.
  133. repo, err := GetRepositoryByName(repoUserId, repoName)
  134. if err != nil {
  135. return errors.New("action.CommitRepoAction(GetRepositoryByName): " + err.Error())
  136. }
  137. repo.IsBare = false
  138. if err = UpdateRepository(repo); err != nil {
  139. return errors.New("action.CommitRepoAction(UpdateRepository): " + err.Error())
  140. }
  141. err = updateIssuesCommit(repoUserName, repoName, commit.Commits)
  142. if err != nil {
  143. log.Debug("action.CommitRepoAction(updateIssuesCommit): ", err)
  144. }
  145. if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, ActEmail: actEmail,
  146. OpType: opType, Content: string(bs), RepoId: repoId, RepoUserName: repoUserName,
  147. RepoName: repoName, RefName: refName,
  148. IsPrivate: repo.IsPrivate}); err != nil {
  149. return errors.New("action.CommitRepoAction(NotifyWatchers): " + err.Error())
  150. }
  151. //qlog.Info("action.CommitRepoAction(end): %d/%s", repoUserId, repoName)
  152. // New push event hook.
  153. if err := repo.GetOwner(); err != nil {
  154. return errors.New("action.CommitRepoAction(GetOwner): " + err.Error())
  155. }
  156. ws, err := GetActiveWebhooksByRepoId(repoId)
  157. if err != nil {
  158. return errors.New("action.CommitRepoAction(GetWebhooksByRepoId): " + err.Error())
  159. } else if len(ws) == 0 {
  160. return nil
  161. }
  162. repoLink := fmt.Sprintf("%s%s/%s", setting.AppUrl, repoUserName, repoName)
  163. commits := make([]*PayloadCommit, len(commit.Commits))
  164. for i, cmt := range commit.Commits {
  165. commits[i] = &PayloadCommit{
  166. Id: cmt.Sha1,
  167. Message: cmt.Message,
  168. Url: fmt.Sprintf("%s/commit/%s", repoLink, cmt.Sha1),
  169. Author: &PayloadAuthor{
  170. Name: cmt.AuthorName,
  171. Email: cmt.AuthorEmail,
  172. },
  173. }
  174. }
  175. p := &Payload{
  176. Ref: refFullName,
  177. Commits: commits,
  178. Repo: &PayloadRepo{
  179. Id: repo.Id,
  180. Name: repo.LowerName,
  181. Url: repoLink,
  182. Description: repo.Description,
  183. Website: repo.Website,
  184. Watchers: repo.NumWatches,
  185. Owner: &PayloadAuthor{
  186. Name: repoUserName,
  187. Email: actEmail,
  188. },
  189. Private: repo.IsPrivate,
  190. },
  191. Pusher: &PayloadAuthor{
  192. Name: repo.Owner.LowerName,
  193. Email: repo.Owner.Email,
  194. },
  195. }
  196. for _, w := range ws {
  197. w.GetEvent()
  198. if !w.HasPushEvent() {
  199. continue
  200. }
  201. p.Secret = w.Secret
  202. CreateHookTask(&HookTask{
  203. Type: WEBHOOK,
  204. Url: w.Url,
  205. Payload: p,
  206. ContentType: w.ContentType,
  207. IsSsl: w.IsSsl,
  208. })
  209. }
  210. return nil
  211. }
  212. // NewRepoAction adds new action for creating repository.
  213. func NewRepoAction(u *User, repo *Repository) (err error) {
  214. if err = NotifyWatchers(&Action{ActUserId: u.Id, ActUserName: u.Name, ActEmail: u.Email,
  215. OpType: OP_CREATE_REPO, RepoId: repo.Id, RepoUserName: repo.Owner.Name, RepoName: repo.Name,
  216. IsPrivate: repo.IsPrivate}); err != nil {
  217. log.Error("action.NewRepoAction(notify watchers): %d/%s", u.Id, repo.Name)
  218. return err
  219. }
  220. log.Trace("action.NewRepoAction: %s/%s", u.LowerName, repo.LowerName)
  221. return err
  222. }
  223. // TransferRepoAction adds new action for transfering repository.
  224. func TransferRepoAction(user, newUser *User, repo *Repository) (err error) {
  225. if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
  226. OpType: OP_TRANSFER_REPO, RepoId: repo.Id, RepoName: repo.Name, Content: newUser.Name,
  227. IsPrivate: repo.IsPrivate}); err != nil {
  228. log.Error("action.TransferRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
  229. return err
  230. }
  231. log.Trace("action.TransferRepoAction: %s/%s", user.LowerName, repo.LowerName)
  232. return err
  233. }
  234. // GetFeeds returns action list of given user in given context.
  235. func GetFeeds(userid, offset int64, isProfile bool) ([]*Action, error) {
  236. actions := make([]*Action, 0, 20)
  237. sess := x.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
  238. if isProfile {
  239. sess.Where("is_private=?", false).And("act_user_id=?", userid)
  240. } else {
  241. sess.And("act_user_id!=?", userid)
  242. }
  243. err := sess.Find(&actions)
  244. return actions, err
  245. }