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.

630 lines
16 KiB

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