action.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. "fmt"
  8. "path"
  9. "regexp"
  10. "strings"
  11. "time"
  12. "unicode"
  13. "github.com/Unknwon/com"
  14. "github.com/go-xorm/xorm"
  15. log "gopkg.in/clog.v1"
  16. "github.com/gogits/git-module"
  17. api "github.com/gogits/go-gogs-client"
  18. "github.com/gogits/gogs/models/errors"
  19. "github.com/gogits/gogs/pkg/setting"
  20. "github.com/gogits/gogs/pkg/tool"
  21. )
  22. type ActionType int
  23. // To maintain backward compatibility only append to the end of list
  24. const (
  25. ACTION_CREATE_REPO ActionType = iota + 1 // 1
  26. ACTION_RENAME_REPO // 2
  27. ACTION_STAR_REPO // 3
  28. ACTION_WATCH_REPO // 4
  29. ACTION_COMMIT_REPO // 5
  30. ACTION_CREATE_ISSUE // 6
  31. ACTION_CREATE_PULL_REQUEST // 7
  32. ACTION_TRANSFER_REPO // 8
  33. ACTION_PUSH_TAG // 9
  34. ACTION_COMMENT_ISSUE // 10
  35. ACTION_MERGE_PULL_REQUEST // 11
  36. ACTION_CLOSE_ISSUE // 12
  37. ACTION_REOPEN_ISSUE // 13
  38. ACTION_CLOSE_PULL_REQUEST // 14
  39. ACTION_REOPEN_PULL_REQUEST // 15
  40. ACTION_CREATE_BRANCH // 16
  41. ACTION_DELETE_BRANCH // 17
  42. ACTION_DELETE_TAG // 18
  43. ACTION_FORK_REPO // 19
  44. )
  45. var (
  46. // Same as Github. See https://help.github.com/articles/closing-issues-via-commit-messages
  47. IssueCloseKeywords = []string{"close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved"}
  48. IssueReopenKeywords = []string{"reopen", "reopens", "reopened"}
  49. IssueCloseKeywordsPat, IssueReopenKeywordsPat *regexp.Regexp
  50. IssueReferenceKeywordsPat *regexp.Regexp
  51. )
  52. func assembleKeywordsPattern(words []string) string {
  53. return fmt.Sprintf(`(?i)(?:%s) \S+`, strings.Join(words, "|"))
  54. }
  55. func init() {
  56. IssueCloseKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(IssueCloseKeywords))
  57. IssueReopenKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(IssueReopenKeywords))
  58. IssueReferenceKeywordsPat = regexp.MustCompile(`(?i)(?:)(^| )\S+`)
  59. }
  60. // Action represents user operation type and other information to repository,
  61. // it implemented interface base.Actioner so that can be used in template render.
  62. type Action struct {
  63. ID int64
  64. UserID int64 // Receiver user ID
  65. OpType ActionType
  66. ActUserID int64 // Doer user ID
  67. ActUserName string // Doer user name
  68. ActAvatar string `xorm:"-"`
  69. RepoID int64 `xorm:"INDEX"`
  70. RepoUserName string
  71. RepoName string
  72. RefName string
  73. IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
  74. Content string `xorm:"TEXT"`
  75. Created time.Time `xorm:"-"`
  76. CreatedUnix int64
  77. }
  78. func (a *Action) BeforeInsert() {
  79. a.CreatedUnix = time.Now().Unix()
  80. }
  81. func (a *Action) AfterSet(colName string, _ xorm.Cell) {
  82. switch colName {
  83. case "created_unix":
  84. a.Created = time.Unix(a.CreatedUnix, 0).Local()
  85. }
  86. }
  87. func (a *Action) GetOpType() int {
  88. return int(a.OpType)
  89. }
  90. func (a *Action) GetActUserName() string {
  91. return a.ActUserName
  92. }
  93. func (a *Action) ShortActUserName() string {
  94. return tool.EllipsisString(a.ActUserName, 20)
  95. }
  96. func (a *Action) GetRepoUserName() string {
  97. return a.RepoUserName
  98. }
  99. func (a *Action) ShortRepoUserName() string {
  100. return tool.EllipsisString(a.RepoUserName, 20)
  101. }
  102. func (a *Action) GetRepoName() string {
  103. return a.RepoName
  104. }
  105. func (a *Action) ShortRepoName() string {
  106. return tool.EllipsisString(a.RepoName, 33)
  107. }
  108. func (a *Action) GetRepoPath() string {
  109. return path.Join(a.RepoUserName, a.RepoName)
  110. }
  111. func (a *Action) ShortRepoPath() string {
  112. return path.Join(a.ShortRepoUserName(), a.ShortRepoName())
  113. }
  114. func (a *Action) GetRepoLink() string {
  115. if len(setting.AppSubURL) > 0 {
  116. return path.Join(setting.AppSubURL, a.GetRepoPath())
  117. }
  118. return "/" + a.GetRepoPath()
  119. }
  120. func (a *Action) GetBranch() string {
  121. return a.RefName
  122. }
  123. func (a *Action) GetContent() string {
  124. return a.Content
  125. }
  126. func (a *Action) GetCreate() time.Time {
  127. return a.Created
  128. }
  129. func (a *Action) GetIssueInfos() []string {
  130. return strings.SplitN(a.Content, "|", 2)
  131. }
  132. func (a *Action) GetIssueTitle() string {
  133. index := com.StrTo(a.GetIssueInfos()[0]).MustInt64()
  134. issue, err := GetIssueByIndex(a.RepoID, index)
  135. if err != nil {
  136. log.Error(4, "GetIssueByIndex: %v", err)
  137. return "500 when get issue"
  138. }
  139. return issue.Title
  140. }
  141. func (a *Action) GetIssueContent() string {
  142. index := com.StrTo(a.GetIssueInfos()[0]).MustInt64()
  143. issue, err := GetIssueByIndex(a.RepoID, index)
  144. if err != nil {
  145. log.Error(4, "GetIssueByIndex: %v", err)
  146. return "500 when get issue"
  147. }
  148. return issue.Content
  149. }
  150. func newRepoAction(e Engine, doer, owner *User, repo *Repository) (err error) {
  151. opType := ACTION_CREATE_REPO
  152. if repo.IsFork {
  153. opType = ACTION_FORK_REPO
  154. }
  155. return notifyWatchers(e, &Action{
  156. ActUserID: doer.ID,
  157. ActUserName: doer.Name,
  158. OpType: opType,
  159. RepoID: repo.ID,
  160. RepoUserName: repo.Owner.Name,
  161. RepoName: repo.Name,
  162. IsPrivate: repo.IsPrivate,
  163. })
  164. }
  165. // NewRepoAction adds new action for creating repository.
  166. func NewRepoAction(doer, owner *User, repo *Repository) (err error) {
  167. return newRepoAction(x, doer, owner, repo)
  168. }
  169. func renameRepoAction(e Engine, actUser *User, oldRepoName string, repo *Repository) (err error) {
  170. if err = notifyWatchers(e, &Action{
  171. ActUserID: actUser.ID,
  172. ActUserName: actUser.Name,
  173. OpType: ACTION_RENAME_REPO,
  174. RepoID: repo.ID,
  175. RepoUserName: repo.Owner.Name,
  176. RepoName: repo.Name,
  177. IsPrivate: repo.IsPrivate,
  178. Content: oldRepoName,
  179. }); err != nil {
  180. return fmt.Errorf("notify watchers: %v", err)
  181. }
  182. log.Trace("action.renameRepoAction: %s/%s", actUser.Name, repo.Name)
  183. return nil
  184. }
  185. // RenameRepoAction adds new action for renaming a repository.
  186. func RenameRepoAction(actUser *User, oldRepoName string, repo *Repository) error {
  187. return renameRepoAction(x, actUser, oldRepoName, repo)
  188. }
  189. func issueIndexTrimRight(c rune) bool {
  190. return !unicode.IsDigit(c)
  191. }
  192. type PushCommit struct {
  193. Sha1 string
  194. Message string
  195. AuthorEmail string
  196. AuthorName string
  197. CommitterEmail string
  198. CommitterName string
  199. Timestamp time.Time
  200. }
  201. type PushCommits struct {
  202. Len int
  203. Commits []*PushCommit
  204. CompareURL string
  205. avatars map[string]string
  206. }
  207. func NewPushCommits() *PushCommits {
  208. return &PushCommits{
  209. avatars: make(map[string]string),
  210. }
  211. }
  212. func (pc *PushCommits) ToApiPayloadCommits(repoPath, repoLink string) ([]*api.PayloadCommit, error) {
  213. commits := make([]*api.PayloadCommit, len(pc.Commits))
  214. for i, commit := range pc.Commits {
  215. authorUsername := ""
  216. author, err := GetUserByEmail(commit.AuthorEmail)
  217. if err == nil {
  218. authorUsername = author.Name
  219. } else if !errors.IsUserNotExist(err) {
  220. return nil, fmt.Errorf("GetUserByEmail: %v", err)
  221. }
  222. committerUsername := ""
  223. committer, err := GetUserByEmail(commit.CommitterEmail)
  224. if err == nil {
  225. committerUsername = committer.Name
  226. } else if !errors.IsUserNotExist(err) {
  227. return nil, fmt.Errorf("GetUserByEmail: %v", err)
  228. }
  229. fileStatus, err := git.GetCommitFileStatus(repoPath, commit.Sha1)
  230. if err != nil {
  231. return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %v", commit.Sha1, err)
  232. }
  233. commits[i] = &api.PayloadCommit{
  234. ID: commit.Sha1,
  235. Message: commit.Message,
  236. URL: fmt.Sprintf("%s/commit/%s", repoLink, commit.Sha1),
  237. Author: &api.PayloadUser{
  238. Name: commit.AuthorName,
  239. Email: commit.AuthorEmail,
  240. UserName: authorUsername,
  241. },
  242. Committer: &api.PayloadUser{
  243. Name: commit.CommitterName,
  244. Email: commit.CommitterEmail,
  245. UserName: committerUsername,
  246. },
  247. Added: fileStatus.Added,
  248. Removed: fileStatus.Removed,
  249. Modified: fileStatus.Modified,
  250. Timestamp: commit.Timestamp,
  251. }
  252. }
  253. return commits, nil
  254. }
  255. // AvatarLink tries to match user in database with e-mail
  256. // in order to show custom avatar, and falls back to general avatar link.
  257. func (push *PushCommits) AvatarLink(email string) string {
  258. _, ok := push.avatars[email]
  259. if !ok {
  260. u, err := GetUserByEmail(email)
  261. if err != nil {
  262. push.avatars[email] = tool.AvatarLink(email)
  263. if !errors.IsUserNotExist(err) {
  264. log.Error(4, "GetUserByEmail: %v", err)
  265. }
  266. } else {
  267. push.avatars[email] = u.RelAvatarLink()
  268. }
  269. }
  270. return push.avatars[email]
  271. }
  272. // UpdateIssuesCommit checks if issues are manipulated by commit message.
  273. func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit) error {
  274. // Commits are appended in the reverse order.
  275. for i := len(commits) - 1; i >= 0; i-- {
  276. c := commits[i]
  277. refMarked := make(map[int64]bool)
  278. for _, ref := range IssueReferenceKeywordsPat.FindAllString(c.Message, -1) {
  279. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  280. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  281. if len(ref) == 0 {
  282. continue
  283. }
  284. // Add repo name if missing
  285. if ref[0] == '#' {
  286. ref = fmt.Sprintf("%s%s", repo.FullName(), ref)
  287. } else if !strings.Contains(ref, "/") {
  288. // FIXME: We don't support User#ID syntax yet
  289. // return ErrNotImplemented
  290. continue
  291. }
  292. issue, err := GetIssueByRef(ref)
  293. if err != nil {
  294. if errors.IsIssueNotExist(err) {
  295. continue
  296. }
  297. return err
  298. }
  299. if refMarked[issue.ID] {
  300. continue
  301. }
  302. refMarked[issue.ID] = true
  303. msgLines := strings.Split(c.Message, "\n")
  304. shortMsg := msgLines[0]
  305. if len(msgLines) > 2 {
  306. shortMsg += "..."
  307. }
  308. message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, repo.Link(), c.Sha1, shortMsg)
  309. if err = CreateRefComment(doer, repo, issue, message, c.Sha1); err != nil {
  310. return err
  311. }
  312. }
  313. refMarked = make(map[int64]bool)
  314. // FIXME: can merge this one and next one to a common function.
  315. for _, ref := range IssueCloseKeywordsPat.FindAllString(c.Message, -1) {
  316. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  317. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  318. if len(ref) == 0 {
  319. continue
  320. }
  321. // Add repo name if missing
  322. if ref[0] == '#' {
  323. ref = fmt.Sprintf("%s%s", repo.FullName(), ref)
  324. } else if !strings.Contains(ref, "/") {
  325. // FIXME: We don't support User#ID syntax yet
  326. continue
  327. }
  328. issue, err := GetIssueByRef(ref)
  329. if err != nil {
  330. if errors.IsIssueNotExist(err) {
  331. continue
  332. }
  333. return err
  334. }
  335. if refMarked[issue.ID] {
  336. continue
  337. }
  338. refMarked[issue.ID] = true
  339. if issue.RepoID != repo.ID || issue.IsClosed {
  340. continue
  341. }
  342. if err = issue.ChangeStatus(doer, repo, true); err != nil {
  343. return err
  344. }
  345. }
  346. // It is conflict to have close and reopen at same time, so refsMarkd doesn't need to reinit here.
  347. for _, ref := range IssueReopenKeywordsPat.FindAllString(c.Message, -1) {
  348. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  349. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  350. if len(ref) == 0 {
  351. continue
  352. }
  353. // Add repo name if missing
  354. if ref[0] == '#' {
  355. ref = fmt.Sprintf("%s%s", repo.FullName(), ref)
  356. } else if !strings.Contains(ref, "/") {
  357. // We don't support User#ID syntax yet
  358. // return ErrNotImplemented
  359. continue
  360. }
  361. issue, err := GetIssueByRef(ref)
  362. if err != nil {
  363. if errors.IsIssueNotExist(err) {
  364. continue
  365. }
  366. return err
  367. }
  368. if refMarked[issue.ID] {
  369. continue
  370. }
  371. refMarked[issue.ID] = true
  372. if issue.RepoID != repo.ID || !issue.IsClosed {
  373. continue
  374. }
  375. if err = issue.ChangeStatus(doer, repo, false); err != nil {
  376. return err
  377. }
  378. }
  379. }
  380. return nil
  381. }
  382. type CommitRepoActionOptions struct {
  383. PusherName string
  384. RepoOwnerID int64
  385. RepoName string
  386. RefFullName string
  387. OldCommitID string
  388. NewCommitID string
  389. Commits *PushCommits
  390. }
  391. // CommitRepoAction adds new commit actio to the repository, and prepare corresponding webhooks.
  392. func CommitRepoAction(opts CommitRepoActionOptions) error {
  393. pusher, err := GetUserByName(opts.PusherName)
  394. if err != nil {
  395. return fmt.Errorf("GetUserByName [%s]: %v", opts.PusherName, err)
  396. }
  397. repo, err := GetRepositoryByName(opts.RepoOwnerID, opts.RepoName)
  398. if err != nil {
  399. return fmt.Errorf("GetRepositoryByName [owner_id: %d, name: %s]: %v", opts.RepoOwnerID, opts.RepoName, err)
  400. }
  401. // Change repository bare status and update last updated time.
  402. repo.IsBare = false
  403. if err = UpdateRepository(repo, false); err != nil {
  404. return fmt.Errorf("UpdateRepository: %v", err)
  405. }
  406. isNewRef := opts.OldCommitID == git.EMPTY_SHA
  407. isDelRef := opts.NewCommitID == git.EMPTY_SHA
  408. opType := ACTION_COMMIT_REPO
  409. // Check if it's tag push or branch.
  410. if strings.HasPrefix(opts.RefFullName, git.TAG_PREFIX) {
  411. opType = ACTION_PUSH_TAG
  412. } else {
  413. // if not the first commit, set the compare URL.
  414. if !isNewRef && !isDelRef {
  415. opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
  416. }
  417. if err = UpdateIssuesCommit(pusher, repo, opts.Commits.Commits); err != nil {
  418. log.Error(2, "UpdateIssuesCommit: %v", err)
  419. }
  420. }
  421. if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
  422. opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
  423. }
  424. data, err := json.Marshal(opts.Commits)
  425. if err != nil {
  426. return fmt.Errorf("Marshal: %v", err)
  427. }
  428. refName := git.RefEndName(opts.RefFullName)
  429. action := &Action{
  430. ActUserID: pusher.ID,
  431. ActUserName: pusher.Name,
  432. Content: string(data),
  433. RepoID: repo.ID,
  434. RepoUserName: repo.MustOwner().Name,
  435. RepoName: repo.Name,
  436. RefName: refName,
  437. IsPrivate: repo.IsPrivate,
  438. }
  439. apiRepo := repo.APIFormat(nil)
  440. apiPusher := pusher.APIFormat()
  441. switch opType {
  442. case ACTION_COMMIT_REPO: // Push
  443. if isDelRef {
  444. if err = PrepareWebhooks(repo, HOOK_EVENT_DELETE, &api.DeletePayload{
  445. Ref: refName,
  446. RefType: "branch",
  447. PusherType: api.PUSHER_TYPE_USER,
  448. Repo: apiRepo,
  449. Sender: apiPusher,
  450. }); err != nil {
  451. return fmt.Errorf("PrepareWebhooks.(delete branch): %v", err)
  452. }
  453. action.OpType = ACTION_DELETE_BRANCH
  454. if err = NotifyWatchers(action); err != nil {
  455. return fmt.Errorf("NotifyWatchers.(delete branch): %v", err)
  456. }
  457. // Delete branch doesn't have anything to push or compare
  458. return nil
  459. }
  460. compareURL := setting.AppURL + opts.Commits.CompareURL
  461. if isNewRef {
  462. compareURL = ""
  463. if err = PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{
  464. Ref: refName,
  465. RefType: "branch",
  466. DefaultBranch: repo.DefaultBranch,
  467. Repo: apiRepo,
  468. Sender: apiPusher,
  469. }); err != nil {
  470. return fmt.Errorf("PrepareWebhooks.(new branch): %v", err)
  471. }
  472. action.OpType = ACTION_CREATE_BRANCH
  473. if err = NotifyWatchers(action); err != nil {
  474. return fmt.Errorf("NotifyWatchers.(new branch): %v", err)
  475. }
  476. }
  477. commits, err := opts.Commits.ToApiPayloadCommits(repo.RepoPath(), repo.HTMLURL())
  478. if err != nil {
  479. return fmt.Errorf("ToApiPayloadCommits: %v", err)
  480. }
  481. if err = PrepareWebhooks(repo, HOOK_EVENT_PUSH, &api.PushPayload{
  482. Ref: opts.RefFullName,
  483. Before: opts.OldCommitID,
  484. After: opts.NewCommitID,
  485. CompareURL: compareURL,
  486. Commits: commits,
  487. Repo: apiRepo,
  488. Pusher: apiPusher,
  489. Sender: apiPusher,
  490. }); err != nil {
  491. return fmt.Errorf("PrepareWebhooks.(new commit): %v", err)
  492. }
  493. action.OpType = ACTION_COMMIT_REPO
  494. if err = NotifyWatchers(action); err != nil {
  495. return fmt.Errorf("NotifyWatchers.(new commit): %v", err)
  496. }
  497. case ACTION_PUSH_TAG: // Tag
  498. if isDelRef {
  499. if err = PrepareWebhooks(repo, HOOK_EVENT_DELETE, &api.DeletePayload{
  500. Ref: refName,
  501. RefType: "tag",
  502. PusherType: api.PUSHER_TYPE_USER,
  503. Repo: apiRepo,
  504. Sender: apiPusher,
  505. }); err != nil {
  506. return fmt.Errorf("PrepareWebhooks.(delete tag): %v", err)
  507. }
  508. action.OpType = ACTION_DELETE_TAG
  509. if err = NotifyWatchers(action); err != nil {
  510. return fmt.Errorf("NotifyWatchers.(delete tag): %v", err)
  511. }
  512. return nil
  513. }
  514. if err = PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{
  515. Ref: refName,
  516. RefType: "tag",
  517. DefaultBranch: repo.DefaultBranch,
  518. Repo: apiRepo,
  519. Sender: apiPusher,
  520. }); err != nil {
  521. return fmt.Errorf("PrepareWebhooks.(new tag): %v", err)
  522. }
  523. action.OpType = ACTION_PUSH_TAG
  524. if err = NotifyWatchers(action); err != nil {
  525. return fmt.Errorf("NotifyWatchers.(new tag): %v", err)
  526. }
  527. }
  528. return nil
  529. }
  530. func transferRepoAction(e Engine, doer, oldOwner *User, repo *Repository) (err error) {
  531. if err = notifyWatchers(e, &Action{
  532. ActUserID: doer.ID,
  533. ActUserName: doer.Name,
  534. OpType: ACTION_TRANSFER_REPO,
  535. RepoID: repo.ID,
  536. RepoUserName: repo.Owner.Name,
  537. RepoName: repo.Name,
  538. IsPrivate: repo.IsPrivate,
  539. Content: path.Join(oldOwner.Name, repo.Name),
  540. }); err != nil {
  541. return fmt.Errorf("notifyWatchers: %v", err)
  542. }
  543. // Remove watch for organization.
  544. if oldOwner.IsOrganization() {
  545. if err = watchRepo(e, oldOwner.ID, repo.ID, false); err != nil {
  546. return fmt.Errorf("watchRepo [false]: %v", err)
  547. }
  548. }
  549. return nil
  550. }
  551. // TransferRepoAction adds new action for transferring repository,
  552. // the Owner field of repository is assumed to be new owner.
  553. func TransferRepoAction(doer, oldOwner *User, repo *Repository) error {
  554. return transferRepoAction(x, doer, oldOwner, repo)
  555. }
  556. func mergePullRequestAction(e Engine, doer *User, repo *Repository, issue *Issue) error {
  557. return notifyWatchers(e, &Action{
  558. ActUserID: doer.ID,
  559. ActUserName: doer.Name,
  560. OpType: ACTION_MERGE_PULL_REQUEST,
  561. Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
  562. RepoID: repo.ID,
  563. RepoUserName: repo.Owner.Name,
  564. RepoName: repo.Name,
  565. IsPrivate: repo.IsPrivate,
  566. })
  567. }
  568. // MergePullRequestAction adds new action for merging pull request.
  569. func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error {
  570. return mergePullRequestAction(x, actUser, repo, pull)
  571. }
  572. // GetFeeds returns action list of given user in given context.
  573. // actorID is the user who's requesting, ctxUserID is the user/org that is requested.
  574. // actorID can be -1 when isProfile is true or to skip the permission check.
  575. func GetFeeds(ctxUser *User, actorID, afterID int64, isProfile bool) ([]*Action, error) {
  576. actions := make([]*Action, 0, setting.UI.User.NewsFeedPagingNum)
  577. sess := x.Limit(setting.UI.User.NewsFeedPagingNum).Where("user_id = ?", ctxUser.ID).Desc("id")
  578. if afterID > 0 {
  579. sess.And("id < ?", afterID)
  580. }
  581. if isProfile {
  582. sess.And("is_private = ?", false).And("act_user_id = ?", ctxUser.ID)
  583. } else if actorID != -1 && ctxUser.IsOrganization() {
  584. // FIXME: only need to get IDs here, not all fields of repository.
  585. repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos)
  586. if err != nil {
  587. return nil, fmt.Errorf("GetUserRepositories: %v", err)
  588. }
  589. var repoIDs []int64
  590. for _, repo := range repos {
  591. repoIDs = append(repoIDs, repo.ID)
  592. }
  593. if len(repoIDs) > 0 {
  594. sess.In("repo_id", repoIDs)
  595. }
  596. }
  597. err := sess.Find(&actions)
  598. return actions, err
  599. }