action.go 5.4 KB

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