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

action.go 5.5 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. ws, err := GetActiveWebhooksByRepoId(repoId)
  98. if err != nil {
  99. return errors.New("action.CommitRepoAction(GetWebhooksByRepoId): " + err.Error())
  100. } else if len(ws) == 0 {
  101. return nil
  102. }
  103. commits := make([]*hooks.PayloadCommit, len(commit.Commits))
  104. for i, cmt := range commit.Commits {
  105. commits[i] = &hooks.PayloadCommit{
  106. Id: cmt.Sha1,
  107. Message: cmt.Message,
  108. Url: fmt.Sprintf("%s%s/%s/commit/%s", base.AppUrl, repoUserName, repoName, cmt.Sha1),
  109. Author: &hooks.PayloadAuthor{
  110. Name: cmt.AuthorName,
  111. Email: cmt.AuthorEmail,
  112. },
  113. }
  114. }
  115. p := &hooks.Payload{
  116. Ref: refFullName,
  117. Commits: commits,
  118. Pusher: &hooks.PayloadAuthor{
  119. Name: userName,
  120. Email: actEmail,
  121. },
  122. }
  123. for _, w := range ws {
  124. w.GetEvent()
  125. if !w.HasPushEvent() {
  126. continue
  127. }
  128. p.Secret = w.Secret
  129. hooks.AddHookTask(&hooks.HookTask{hooks.HTT_WEBHOOK, w.Url, p, w.ContentType, w.IsSsl})
  130. }
  131. return nil
  132. }
  133. // NewRepoAction adds new action for creating repository.
  134. func NewRepoAction(user *User, repo *Repository) (err error) {
  135. if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
  136. OpType: OP_CREATE_REPO, RepoId: repo.Id, RepoName: repo.Name, IsPrivate: repo.IsPrivate}); err != nil {
  137. log.Error("action.NewRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
  138. return err
  139. }
  140. log.Trace("action.NewRepoAction: %s/%s", user.LowerName, repo.LowerName)
  141. return err
  142. }
  143. // TransferRepoAction adds new action for transfering repository.
  144. func TransferRepoAction(user, newUser *User, repo *Repository) (err error) {
  145. if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
  146. OpType: OP_TRANSFER_REPO, RepoId: repo.Id, RepoName: repo.Name, Content: newUser.Name,
  147. IsPrivate: repo.IsPrivate}); err != nil {
  148. log.Error("action.TransferRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
  149. return err
  150. }
  151. log.Trace("action.TransferRepoAction: %s/%s", user.LowerName, repo.LowerName)
  152. return err
  153. }
  154. // GetFeeds returns action list of given user in given context.
  155. func GetFeeds(userid, offset int64, isProfile bool) ([]Action, error) {
  156. actions := make([]Action, 0, 20)
  157. sess := orm.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
  158. if isProfile {
  159. sess.Where("is_private=?", false).And("act_user_id=?", userid)
  160. } else {
  161. sess.And("act_user_id!=?", userid)
  162. }
  163. err := sess.Find(&actions)
  164. return actions, err
  165. }