Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

action.go 5.9KB

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