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.

216 lines
6.0 KiB

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