action.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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/modules/base"
  20. "github.com/gogits/gogs/modules/setting"
  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 // Action user id.
  67. ActUserName string // Action user name.
  68. ActAvatar string `xorm:"-"`
  69. RepoID int64
  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 base.EllipsisString(a.ActUserName, 20)
  95. }
  96. func (a *Action) GetRepoUserName() string {
  97. return a.RepoUserName
  98. }
  99. func (a *Action) ShortRepoUserName() string {
  100. return base.EllipsisString(a.RepoUserName, 20)
  101. }
  102. func (a *Action) GetRepoName() string {
  103. return a.RepoName
  104. }
  105. func (a *Action) ShortRepoName() string {
  106. return base.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(repoLink string) []*api.PayloadCommit {
  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. }
  220. committerUsername := ""
  221. committer, err := GetUserByEmail(commit.CommitterEmail)
  222. if err == nil {
  223. // TODO: check errors other than email not found.
  224. committerUsername = committer.Name
  225. }
  226. commits[i] = &api.PayloadCommit{
  227. ID: commit.Sha1,
  228. Message: commit.Message,
  229. URL: fmt.Sprintf("%s/commit/%s", repoLink, commit.Sha1),
  230. Author: &api.PayloadUser{
  231. Name: commit.AuthorName,
  232. Email: commit.AuthorEmail,
  233. UserName: authorUsername,
  234. },
  235. Committer: &api.PayloadUser{
  236. Name: commit.CommitterName,
  237. Email: commit.CommitterEmail,
  238. UserName: committerUsername,
  239. },
  240. Timestamp: commit.Timestamp,
  241. }
  242. }
  243. return commits
  244. }
  245. // AvatarLink tries to match user in database with e-mail
  246. // in order to show custom avatar, and falls back to general avatar link.
  247. func (push *PushCommits) AvatarLink(email string) string {
  248. _, ok := push.avatars[email]
  249. if !ok {
  250. u, err := GetUserByEmail(email)
  251. if err != nil {
  252. push.avatars[email] = base.AvatarLink(email)
  253. if !errors.IsUserNotExist(err) {
  254. log.Error(4, "GetUserByEmail: %v", err)
  255. }
  256. } else {
  257. push.avatars[email] = u.RelAvatarLink()
  258. }
  259. }
  260. return push.avatars[email]
  261. }
  262. // UpdateIssuesCommit checks if issues are manipulated by commit message.
  263. func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit) error {
  264. // Commits are appended in the reverse order.
  265. for i := len(commits) - 1; i >= 0; i-- {
  266. c := commits[i]
  267. refMarked := make(map[int64]bool)
  268. for _, ref := range IssueReferenceKeywordsPat.FindAllString(c.Message, -1) {
  269. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  270. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  271. if len(ref) == 0 {
  272. continue
  273. }
  274. // Add repo name if missing
  275. if ref[0] == '#' {
  276. ref = fmt.Sprintf("%s%s", repo.FullName(), ref)
  277. } else if !strings.Contains(ref, "/") {
  278. // FIXME: We don't support User#ID syntax yet
  279. // return ErrNotImplemented
  280. continue
  281. }
  282. issue, err := GetIssueByRef(ref)
  283. if err != nil {
  284. if IsErrIssueNotExist(err) {
  285. continue
  286. }
  287. return err
  288. }
  289. if refMarked[issue.ID] {
  290. continue
  291. }
  292. refMarked[issue.ID] = true
  293. msgLines := strings.Split(c.Message, "\n")
  294. shortMsg := msgLines[0]
  295. if len(msgLines) > 2 {
  296. shortMsg += "..."
  297. }
  298. message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, repo.Link(), c.Sha1, shortMsg)
  299. if err = CreateRefComment(doer, repo, issue, message, c.Sha1); err != nil {
  300. return err
  301. }
  302. }
  303. refMarked = make(map[int64]bool)
  304. // FIXME: can merge this one and next one to a common function.
  305. for _, ref := range IssueCloseKeywordsPat.FindAllString(c.Message, -1) {
  306. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  307. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  308. if len(ref) == 0 {
  309. continue
  310. }
  311. // Add repo name if missing
  312. if ref[0] == '#' {
  313. ref = fmt.Sprintf("%s%s", repo.FullName(), ref)
  314. } else if !strings.Contains(ref, "/") {
  315. // We don't support User#ID syntax yet
  316. // return ErrNotImplemented
  317. continue
  318. }
  319. issue, err := GetIssueByRef(ref)
  320. if err != nil {
  321. if IsErrIssueNotExist(err) {
  322. continue
  323. }
  324. return err
  325. }
  326. if refMarked[issue.ID] {
  327. continue
  328. }
  329. refMarked[issue.ID] = true
  330. if issue.RepoID != repo.ID || issue.IsClosed {
  331. continue
  332. }
  333. if err = issue.ChangeStatus(doer, repo, true); err != nil {
  334. return err
  335. }
  336. }
  337. // It is conflict to have close and reopen at same time, so refsMarkd doesn't need to reinit here.
  338. for _, ref := range IssueReopenKeywordsPat.FindAllString(c.Message, -1) {
  339. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  340. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  341. if len(ref) == 0 {
  342. continue
  343. }
  344. // Add repo name if missing
  345. if ref[0] == '#' {
  346. ref = fmt.Sprintf("%s%s", repo.FullName(), ref)
  347. } else if !strings.Contains(ref, "/") {
  348. // We don't support User#ID syntax yet
  349. // return ErrNotImplemented
  350. continue
  351. }
  352. issue, err := GetIssueByRef(ref)
  353. if err != nil {
  354. if IsErrIssueNotExist(err) {
  355. continue
  356. }
  357. return err
  358. }
  359. if refMarked[issue.ID] {
  360. continue
  361. }
  362. refMarked[issue.ID] = true
  363. if issue.RepoID != repo.ID || !issue.IsClosed {
  364. continue
  365. }
  366. if err = issue.ChangeStatus(doer, repo, false); err != nil {
  367. return err
  368. }
  369. }
  370. }
  371. return nil
  372. }
  373. type CommitRepoActionOptions struct {
  374. PusherName string
  375. RepoOwnerID int64
  376. RepoName string
  377. RefFullName string
  378. OldCommitID string
  379. NewCommitID string
  380. Commits *PushCommits
  381. }
  382. // CommitRepoAction adds new commit actio to the repository, and prepare corresponding webhooks.
  383. func CommitRepoAction(opts CommitRepoActionOptions) error {
  384. pusher, err := GetUserByName(opts.PusherName)
  385. if err != nil {
  386. return fmt.Errorf("GetUserByName [%s]: %v", opts.PusherName, err)
  387. }
  388. repo, err := GetRepositoryByName(opts.RepoOwnerID, opts.RepoName)
  389. if err != nil {
  390. return fmt.Errorf("GetRepositoryByName [owner_id: %d, name: %s]: %v", opts.RepoOwnerID, opts.RepoName, err)
  391. }
  392. // Change repository bare status and update last updated time.
  393. repo.IsBare = false
  394. if err = UpdateRepository(repo, false); err != nil {
  395. return fmt.Errorf("UpdateRepository: %v", err)
  396. }
  397. isNewRef := opts.OldCommitID == git.EMPTY_SHA
  398. isDelRef := opts.NewCommitID == git.EMPTY_SHA
  399. opType := ACTION_COMMIT_REPO
  400. // Check if it's tag push or branch.
  401. if strings.HasPrefix(opts.RefFullName, git.TAG_PREFIX) {
  402. opType = ACTION_PUSH_TAG
  403. } else {
  404. // if not the first commit, set the compare URL.
  405. if !isNewRef && !isDelRef {
  406. opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
  407. }
  408. if err = UpdateIssuesCommit(pusher, repo, opts.Commits.Commits); err != nil {
  409. log.Error(2, "UpdateIssuesCommit: %v", err)
  410. }
  411. }
  412. if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
  413. opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
  414. }
  415. data, err := json.Marshal(opts.Commits)
  416. if err != nil {
  417. return fmt.Errorf("Marshal: %v", err)
  418. }
  419. refName := git.RefEndName(opts.RefFullName)
  420. action := &Action{
  421. ActUserID: pusher.ID,
  422. ActUserName: pusher.Name,
  423. Content: string(data),
  424. RepoID: repo.ID,
  425. RepoUserName: repo.MustOwner().Name,
  426. RepoName: repo.Name,
  427. RefName: refName,
  428. IsPrivate: repo.IsPrivate,
  429. }
  430. apiRepo := repo.APIFormat(nil)
  431. apiPusher := pusher.APIFormat()
  432. switch opType {
  433. case ACTION_COMMIT_REPO: // Push
  434. if isDelRef {
  435. if err = PrepareWebhooks(repo, HOOK_EVENT_DELETE, &api.DeletePayload{
  436. Ref: refName,
  437. RefType: "branch",
  438. PusherType: api.PUSHER_TYPE_USER,
  439. Repo: apiRepo,
  440. Sender: apiPusher,
  441. }); err != nil {
  442. return fmt.Errorf("PrepareWebhooks.(delete branch): %v", err)
  443. }
  444. action.OpType = ACTION_DELETE_BRANCH
  445. if err = NotifyWatchers(action); err != nil {
  446. return fmt.Errorf("NotifyWatchers.(delete branch): %v", err)
  447. }
  448. // Delete branch doesn't have anything to push or compare
  449. return nil
  450. }
  451. compareURL := setting.AppUrl + opts.Commits.CompareURL
  452. if isNewRef {
  453. compareURL = ""
  454. if err = PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{
  455. Ref: refName,
  456. RefType: "branch",
  457. DefaultBranch: repo.DefaultBranch,
  458. Repo: apiRepo,
  459. Sender: apiPusher,
  460. }); err != nil {
  461. return fmt.Errorf("PrepareWebhooks.(new branch): %v", err)
  462. }
  463. action.OpType = ACTION_CREATE_BRANCH
  464. if err = NotifyWatchers(action); err != nil {
  465. return fmt.Errorf("NotifyWatchers.(new branch): %v", err)
  466. }
  467. }
  468. if err = PrepareWebhooks(repo, HOOK_EVENT_PUSH, &api.PushPayload{
  469. Ref: opts.RefFullName,
  470. Before: opts.OldCommitID,
  471. After: opts.NewCommitID,
  472. CompareURL: compareURL,
  473. Commits: opts.Commits.ToApiPayloadCommits(repo.HTMLURL()),
  474. Repo: apiRepo,
  475. Pusher: apiPusher,
  476. Sender: apiPusher,
  477. }); err != nil {
  478. return fmt.Errorf("PrepareWebhooks.(new commit): %v", err)
  479. }
  480. action.OpType = ACTION_COMMIT_REPO
  481. if err = NotifyWatchers(action); err != nil {
  482. return fmt.Errorf("NotifyWatchers.(new commit): %v", err)
  483. }
  484. case ACTION_PUSH_TAG: // Tag
  485. if isDelRef {
  486. if err = PrepareWebhooks(repo, HOOK_EVENT_DELETE, &api.DeletePayload{
  487. Ref: refName,
  488. RefType: "tag",
  489. PusherType: api.PUSHER_TYPE_USER,
  490. Repo: apiRepo,
  491. Sender: apiPusher,
  492. }); err != nil {
  493. return fmt.Errorf("PrepareWebhooks.(delete tag): %v", err)
  494. }
  495. action.OpType = ACTION_DELETE_TAG
  496. if err = NotifyWatchers(action); err != nil {
  497. return fmt.Errorf("NotifyWatchers.(delete tag): %v", err)
  498. }
  499. return nil
  500. }
  501. if err = PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{
  502. Ref: refName,
  503. RefType: "tag",
  504. DefaultBranch: repo.DefaultBranch,
  505. Repo: apiRepo,
  506. Sender: apiPusher,
  507. }); err != nil {
  508. return fmt.Errorf("PrepareWebhooks.(new tag): %v", err)
  509. }
  510. action.OpType = ACTION_PUSH_TAG
  511. if err = NotifyWatchers(action); err != nil {
  512. return fmt.Errorf("NotifyWatchers.(new tag): %v", err)
  513. }
  514. }
  515. return nil
  516. }
  517. func transferRepoAction(e Engine, doer, oldOwner *User, repo *Repository) (err error) {
  518. if err = notifyWatchers(e, &Action{
  519. ActUserID: doer.ID,
  520. ActUserName: doer.Name,
  521. OpType: ACTION_TRANSFER_REPO,
  522. RepoID: repo.ID,
  523. RepoUserName: repo.Owner.Name,
  524. RepoName: repo.Name,
  525. IsPrivate: repo.IsPrivate,
  526. Content: path.Join(oldOwner.Name, repo.Name),
  527. }); err != nil {
  528. return fmt.Errorf("notifyWatchers: %v", err)
  529. }
  530. // Remove watch for organization.
  531. if oldOwner.IsOrganization() {
  532. if err = watchRepo(e, oldOwner.ID, repo.ID, false); err != nil {
  533. return fmt.Errorf("watchRepo [false]: %v", err)
  534. }
  535. }
  536. return nil
  537. }
  538. // TransferRepoAction adds new action for transferring repository,
  539. // the Owner field of repository is assumed to be new owner.
  540. func TransferRepoAction(doer, oldOwner *User, repo *Repository) error {
  541. return transferRepoAction(x, doer, oldOwner, repo)
  542. }
  543. func mergePullRequestAction(e Engine, doer *User, repo *Repository, issue *Issue) error {
  544. return notifyWatchers(e, &Action{
  545. ActUserID: doer.ID,
  546. ActUserName: doer.Name,
  547. OpType: ACTION_MERGE_PULL_REQUEST,
  548. Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
  549. RepoID: repo.ID,
  550. RepoUserName: repo.Owner.Name,
  551. RepoName: repo.Name,
  552. IsPrivate: repo.IsPrivate,
  553. })
  554. }
  555. // MergePullRequestAction adds new action for merging pull request.
  556. func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error {
  557. return mergePullRequestAction(x, actUser, repo, pull)
  558. }
  559. // GetFeeds returns action list of given user in given context.
  560. // actorID is the user who's requesting, ctxUserID is the user/org that is requested.
  561. // actorID can be -1 when isProfile is true or to skip the permission check.
  562. func GetFeeds(ctxUser *User, actorID, offset int64, isProfile bool) ([]*Action, error) {
  563. actions := make([]*Action, 0, 20)
  564. sess := x.Limit(20, int(offset)).Desc("id").Where("user_id = ?", ctxUser.ID)
  565. if isProfile {
  566. sess.And("is_private = ?", false).And("act_user_id = ?", ctxUser.ID)
  567. } else if actorID != -1 && ctxUser.IsOrganization() {
  568. // FIXME: only need to get IDs here, not all fields of repository.
  569. repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos)
  570. if err != nil {
  571. return nil, fmt.Errorf("GetUserRepositories: %v", err)
  572. }
  573. var repoIDs []int64
  574. for _, repo := range repos {
  575. repoIDs = append(repoIDs, repo.ID)
  576. }
  577. if len(repoIDs) > 0 {
  578. sess.In("repo_id", repoIDs)
  579. }
  580. }
  581. err := sess.Find(&actions)
  582. return actions, err
  583. }