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

659 lines
18 KiB

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