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. // We don't support User#ID syntax yet
  326. // return ErrNotImplemented
  327. continue
  328. }
  329. issue, err := GetIssueByRef(ref)
  330. if err != nil {
  331. if errors.IsIssueNotExist(err) {
  332. continue
  333. }
  334. return err
  335. }
  336. if refMarked[issue.ID] {
  337. continue
  338. }
  339. refMarked[issue.ID] = true
  340. if issue.RepoID != repo.ID || issue.IsClosed {
  341. continue
  342. }
  343. if err = issue.ChangeStatus(doer, repo, true); err != nil {
  344. return err
  345. }
  346. }
  347. // It is conflict to have close and reopen at same time, so refsMarkd doesn't need to reinit here.
  348. for _, ref := range IssueReopenKeywordsPat.FindAllString(c.Message, -1) {
  349. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  350. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  351. if len(ref) == 0 {
  352. continue
  353. }
  354. // Add repo name if missing
  355. if ref[0] == '#' {
  356. ref = fmt.Sprintf("%s%s", repo.FullName(), ref)
  357. } else if !strings.Contains(ref, "/") {
  358. // We don't support User#ID syntax yet
  359. // return ErrNotImplemented
  360. continue
  361. }
  362. issue, err := GetIssueByRef(ref)
  363. if err != nil {
  364. if errors.IsIssueNotExist(err) {
  365. continue
  366. }
  367. return err
  368. }
  369. if refMarked[issue.ID] {
  370. continue
  371. }
  372. refMarked[issue.ID] = true
  373. if issue.RepoID != repo.ID || !issue.IsClosed {
  374. continue
  375. }
  376. if err = issue.ChangeStatus(doer, repo, false); err != nil {
  377. return err
  378. }
  379. }
  380. }
  381. return nil
  382. }
  383. type CommitRepoActionOptions struct {
  384. PusherName string
  385. RepoOwnerID int64
  386. RepoName string
  387. RefFullName string
  388. OldCommitID string
  389. NewCommitID string
  390. Commits *PushCommits
  391. }
  392. // CommitRepoAction adds new commit actio to the repository, and prepare corresponding webhooks.
  393. func CommitRepoAction(opts CommitRepoActionOptions) error {
  394. pusher, err := GetUserByName(opts.PusherName)
  395. if err != nil {
  396. return fmt.Errorf("GetUserByName [%s]: %v", opts.PusherName, err)
  397. }
  398. repo, err := GetRepositoryByName(opts.RepoOwnerID, opts.RepoName)
  399. if err != nil {
  400. return fmt.Errorf("GetRepositoryByName [owner_id: %d, name: %s]: %v", opts.RepoOwnerID, opts.RepoName, err)
  401. }
  402. // Change repository bare status and update last updated time.
  403. repo.IsBare = false
  404. if err = UpdateRepository(repo, false); err != nil {
  405. return fmt.Errorf("UpdateRepository: %v", err)
  406. }
  407. isNewRef := opts.OldCommitID == git.EMPTY_SHA
  408. isDelRef := opts.NewCommitID == git.EMPTY_SHA
  409. opType := ACTION_COMMIT_REPO
  410. // Check if it's tag push or branch.
  411. if strings.HasPrefix(opts.RefFullName, git.TAG_PREFIX) {
  412. opType = ACTION_PUSH_TAG
  413. } else {
  414. // if not the first commit, set the compare URL.
  415. if !isNewRef && !isDelRef {
  416. opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
  417. }
  418. if err = UpdateIssuesCommit(pusher, repo, opts.Commits.Commits); err != nil {
  419. log.Error(2, "UpdateIssuesCommit: %v", err)
  420. }
  421. }
  422. if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
  423. opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
  424. }
  425. data, err := json.Marshal(opts.Commits)
  426. if err != nil {
  427. return fmt.Errorf("Marshal: %v", err)
  428. }
  429. refName := git.RefEndName(opts.RefFullName)
  430. action := &Action{
  431. ActUserID: pusher.ID,
  432. ActUserName: pusher.Name,
  433. Content: string(data),
  434. RepoID: repo.ID,
  435. RepoUserName: repo.MustOwner().Name,
  436. RepoName: repo.Name,
  437. RefName: refName,
  438. IsPrivate: repo.IsPrivate,
  439. }
  440. apiRepo := repo.APIFormat(nil)
  441. apiPusher := pusher.APIFormat()
  442. switch opType {
  443. case ACTION_COMMIT_REPO: // Push
  444. if isDelRef {
  445. if err = PrepareWebhooks(repo, HOOK_EVENT_DELETE, &api.DeletePayload{
  446. Ref: refName,
  447. RefType: "branch",
  448. PusherType: api.PUSHER_TYPE_USER,
  449. Repo: apiRepo,
  450. Sender: apiPusher,
  451. }); err != nil {
  452. return fmt.Errorf("PrepareWebhooks.(delete branch): %v", err)
  453. }
  454. action.OpType = ACTION_DELETE_BRANCH
  455. if err = NotifyWatchers(action); err != nil {
  456. return fmt.Errorf("NotifyWatchers.(delete branch): %v", err)
  457. }
  458. // Delete branch doesn't have anything to push or compare
  459. return nil
  460. }
  461. compareURL := setting.AppURL + opts.Commits.CompareURL
  462. if isNewRef {
  463. compareURL = ""
  464. if err = PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{
  465. Ref: refName,
  466. RefType: "branch",
  467. DefaultBranch: repo.DefaultBranch,
  468. Repo: apiRepo,
  469. Sender: apiPusher,
  470. }); err != nil {
  471. return fmt.Errorf("PrepareWebhooks.(new branch): %v", err)
  472. }
  473. action.OpType = ACTION_CREATE_BRANCH
  474. if err = NotifyWatchers(action); err != nil {
  475. return fmt.Errorf("NotifyWatchers.(new branch): %v", err)
  476. }
  477. }
  478. commits, err := opts.Commits.ToApiPayloadCommits(repo.RepoPath(), repo.HTMLURL())
  479. if err != nil {
  480. return fmt.Errorf("ToApiPayloadCommits: %v", err)
  481. }
  482. if err = PrepareWebhooks(repo, HOOK_EVENT_PUSH, &api.PushPayload{
  483. Ref: opts.RefFullName,
  484. Before: opts.OldCommitID,
  485. After: opts.NewCommitID,
  486. CompareURL: compareURL,
  487. Commits: commits,
  488. Repo: apiRepo,
  489. Pusher: apiPusher,
  490. Sender: apiPusher,
  491. }); err != nil {
  492. return fmt.Errorf("PrepareWebhooks.(new commit): %v", err)
  493. }
  494. action.OpType = ACTION_COMMIT_REPO
  495. if err = NotifyWatchers(action); err != nil {
  496. return fmt.Errorf("NotifyWatchers.(new commit): %v", err)
  497. }
  498. case ACTION_PUSH_TAG: // Tag
  499. if isDelRef {
  500. if err = PrepareWebhooks(repo, HOOK_EVENT_DELETE, &api.DeletePayload{
  501. Ref: refName,
  502. RefType: "tag",
  503. PusherType: api.PUSHER_TYPE_USER,
  504. Repo: apiRepo,
  505. Sender: apiPusher,
  506. }); err != nil {
  507. return fmt.Errorf("PrepareWebhooks.(delete tag): %v", err)
  508. }
  509. action.OpType = ACTION_DELETE_TAG
  510. if err = NotifyWatchers(action); err != nil {
  511. return fmt.Errorf("NotifyWatchers.(delete tag): %v", err)
  512. }
  513. return nil
  514. }
  515. if err = PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{
  516. Ref: refName,
  517. RefType: "tag",
  518. DefaultBranch: repo.DefaultBranch,
  519. Repo: apiRepo,
  520. Sender: apiPusher,
  521. }); err != nil {
  522. return fmt.Errorf("PrepareWebhooks.(new tag): %v", err)
  523. }
  524. action.OpType = ACTION_PUSH_TAG
  525. if err = NotifyWatchers(action); err != nil {
  526. return fmt.Errorf("NotifyWatchers.(new tag): %v", err)
  527. }
  528. }
  529. return nil
  530. }
  531. func transferRepoAction(e Engine, doer, oldOwner *User, repo *Repository) (err error) {
  532. if err = notifyWatchers(e, &Action{
  533. ActUserID: doer.ID,
  534. ActUserName: doer.Name,
  535. OpType: ACTION_TRANSFER_REPO,
  536. RepoID: repo.ID,
  537. RepoUserName: repo.Owner.Name,
  538. RepoName: repo.Name,
  539. IsPrivate: repo.IsPrivate,
  540. Content: path.Join(oldOwner.Name, repo.Name),
  541. }); err != nil {
  542. return fmt.Errorf("notifyWatchers: %v", err)
  543. }
  544. // Remove watch for organization.
  545. if oldOwner.IsOrganization() {
  546. if err = watchRepo(e, oldOwner.ID, repo.ID, false); err != nil {
  547. return fmt.Errorf("watchRepo [false]: %v", err)
  548. }
  549. }
  550. return nil
  551. }
  552. // TransferRepoAction adds new action for transferring repository,
  553. // the Owner field of repository is assumed to be new owner.
  554. func TransferRepoAction(doer, oldOwner *User, repo *Repository) error {
  555. return transferRepoAction(x, doer, oldOwner, repo)
  556. }
  557. func mergePullRequestAction(e Engine, doer *User, repo *Repository, issue *Issue) error {
  558. return notifyWatchers(e, &Action{
  559. ActUserID: doer.ID,
  560. ActUserName: doer.Name,
  561. OpType: ACTION_MERGE_PULL_REQUEST,
  562. Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
  563. RepoID: repo.ID,
  564. RepoUserName: repo.Owner.Name,
  565. RepoName: repo.Name,
  566. IsPrivate: repo.IsPrivate,
  567. })
  568. }
  569. // MergePullRequestAction adds new action for merging pull request.
  570. func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error {
  571. return mergePullRequestAction(x, actUser, repo, pull)
  572. }
  573. // GetFeeds returns action list of given user in given context.
  574. // actorID is the user who's requesting, ctxUserID is the user/org that is requested.
  575. // actorID can be -1 when isProfile is true or to skip the permission check.
  576. func GetFeeds(ctxUser *User, actorID, afterID int64, isProfile bool) ([]*Action, error) {
  577. actions := make([]*Action, 0, setting.UI.User.NewsFeedPagingNum)
  578. sess := x.Limit(setting.UI.User.NewsFeedPagingNum).Where("user_id = ?", ctxUser.ID).Desc("id")
  579. if afterID > 0 {
  580. sess.And("id < ?", afterID)
  581. }
  582. if isProfile {
  583. sess.And("is_private = ?", false).And("act_user_id = ?", ctxUser.ID)
  584. } else if actorID != -1 && ctxUser.IsOrganization() {
  585. // FIXME: only need to get IDs here, not all fields of repository.
  586. repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos)
  587. if err != nil {
  588. return nil, fmt.Errorf("GetUserRepositories: %v", err)
  589. }
  590. var repoIDs []int64
  591. for _, repo := range repos {
  592. repoIDs = append(repoIDs, repo.ID)
  593. }
  594. if len(repoIDs) > 0 {
  595. sess.In("repo_id", repoIDs)
  596. }
  597. }
  598. err := sess.Find(&actions)
  599. return actions, err
  600. }