You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

action.go 5.8KB

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