action.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. "strings"
  10. "time"
  11. "github.com/gogits/git"
  12. qlog "github.com/qiniu/log"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/hooks"
  15. "github.com/gogits/gogs/modules/log"
  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. // Action represents user operation type and other information to repository.,
  31. // it implemented interface base.Actioner so that can be used in template render.
  32. type Action struct {
  33. Id int64
  34. UserId int64 // Receiver user id.
  35. OpType int
  36. ActUserId int64 // Action user id.
  37. ActUserName string // Action user name.
  38. ActEmail string
  39. RepoId int64
  40. RepoUserName string
  41. RepoName string
  42. RefName string
  43. IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
  44. Content string `xorm:"TEXT"`
  45. Created time.Time `xorm:"created"`
  46. }
  47. func (a Action) GetOpType() int {
  48. return a.OpType
  49. }
  50. func (a Action) GetActUserName() string {
  51. return a.ActUserName
  52. }
  53. func (a Action) GetActEmail() string {
  54. return a.ActEmail
  55. }
  56. func (a Action) GetRepoUserName() string {
  57. return a.RepoUserName
  58. }
  59. func (a Action) GetRepoName() string {
  60. return a.RepoName
  61. }
  62. func (a Action) GetBranch() string {
  63. return a.RefName
  64. }
  65. func (a Action) GetContent() string {
  66. return a.Content
  67. }
  68. // CommitRepoAction adds new action for committing repository.
  69. func CommitRepoAction(userId, repoUserId int64, userName, actEmail string,
  70. repoId int64, repoUserName, repoName string, refFullName string, commit *base.PushCommits) error {
  71. // log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName)
  72. opType := OP_COMMIT_REPO
  73. // Check it's tag push or branch.
  74. if strings.HasPrefix(refFullName, "refs/tags/") {
  75. opType = OP_PUSH_TAG
  76. commit = &base.PushCommits{}
  77. }
  78. refName := git.RefEndName(refFullName)
  79. bs, err := json.Marshal(commit)
  80. if err != nil {
  81. return errors.New("action.CommitRepoAction(json): " + err.Error())
  82. }
  83. // Change repository bare status and update last updated time.
  84. repo, err := GetRepositoryByName(repoUserId, repoName)
  85. if err != nil {
  86. return errors.New("action.CommitRepoAction(GetRepositoryByName): " + err.Error())
  87. }
  88. repo.IsBare = false
  89. if err = UpdateRepository(repo); err != nil {
  90. return errors.New("action.CommitRepoAction(UpdateRepository): " + err.Error())
  91. }
  92. if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, ActEmail: actEmail,
  93. OpType: opType, Content: string(bs), RepoId: repoId, RepoUserName: repoUserName,
  94. RepoName: repoName, RefName: refName,
  95. IsPrivate: repo.IsPrivate}); err != nil {
  96. return errors.New("action.CommitRepoAction(NotifyWatchers): " + err.Error())
  97. }
  98. qlog.Info("action.CommitRepoAction(end): %d/%s", repoUserId, repoName)
  99. // New push event hook.
  100. if err := repo.GetOwner(); err != nil {
  101. return errors.New("action.CommitRepoAction(GetOwner): " + err.Error())
  102. }
  103. ws, err := GetActiveWebhooksByRepoId(repoId)
  104. if err != nil {
  105. return errors.New("action.CommitRepoAction(GetWebhooksByRepoId): " + err.Error())
  106. } else if len(ws) == 0 {
  107. return nil
  108. }
  109. repoLink := fmt.Sprintf("%s%s/%s", base.AppUrl, repoUserName, repoName)
  110. commits := make([]*hooks.PayloadCommit, len(commit.Commits))
  111. for i, cmt := range commit.Commits {
  112. commits[i] = &hooks.PayloadCommit{
  113. Id: cmt.Sha1,
  114. Message: cmt.Message,
  115. Url: fmt.Sprintf("%s/commit/%s", repoLink, cmt.Sha1),
  116. Author: &hooks.PayloadAuthor{
  117. Name: cmt.AuthorName,
  118. Email: cmt.AuthorEmail,
  119. },
  120. }
  121. }
  122. p := &hooks.Payload{
  123. Ref: refFullName,
  124. Commits: commits,
  125. Repo: &hooks.PayloadRepo{
  126. Id: repo.Id,
  127. Name: repo.LowerName,
  128. Url: repoLink,
  129. Description: repo.Description,
  130. Website: repo.Website,
  131. Watchers: repo.NumWatches,
  132. Owner: &hooks.PayloadAuthor{
  133. Name: repoUserName,
  134. Email: actEmail,
  135. },
  136. Private: repo.IsPrivate,
  137. },
  138. Pusher: &hooks.PayloadAuthor{
  139. Name: repo.Owner.LowerName,
  140. Email: repo.Owner.Email,
  141. },
  142. }
  143. for _, w := range ws {
  144. w.GetEvent()
  145. if !w.HasPushEvent() {
  146. continue
  147. }
  148. p.Secret = w.Secret
  149. hooks.AddHookTask(&hooks.HookTask{hooks.HTT_WEBHOOK, w.Url, p, w.ContentType, w.IsSsl})
  150. }
  151. return nil
  152. }
  153. // NewRepoAction adds new action for creating repository.
  154. func NewRepoAction(user *User, repo *Repository) (err error) {
  155. if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
  156. OpType: OP_CREATE_REPO, RepoId: repo.Id, RepoName: repo.Name, IsPrivate: repo.IsPrivate}); err != nil {
  157. log.Error("action.NewRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
  158. return err
  159. }
  160. log.Trace("action.NewRepoAction: %s/%s", user.LowerName, repo.LowerName)
  161. return err
  162. }
  163. // TransferRepoAction adds new action for transfering repository.
  164. func TransferRepoAction(user, newUser *User, repo *Repository) (err error) {
  165. if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
  166. OpType: OP_TRANSFER_REPO, RepoId: repo.Id, RepoName: repo.Name, Content: newUser.Name,
  167. IsPrivate: repo.IsPrivate}); err != nil {
  168. log.Error("action.TransferRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
  169. return err
  170. }
  171. log.Trace("action.TransferRepoAction: %s/%s", user.LowerName, repo.LowerName)
  172. return err
  173. }
  174. // GetFeeds returns action list of given user in given context.
  175. func GetFeeds(userid, offset int64, isProfile bool) ([]*Action, error) {
  176. actions := make([]*Action, 0, 20)
  177. sess := orm.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
  178. if isProfile {
  179. sess.Where("is_private=?", false).And("act_user_id=?", userid)
  180. } else {
  181. sess.And("act_user_id!=?", userid)
  182. }
  183. err := sess.Find(&actions)
  184. return actions, err
  185. }