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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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().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.Title
  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. CommitterEmail string
  202. CommitterName string
  203. Timestamp time.Time
  204. }
  205. type PushCommits struct {
  206. Len int
  207. Commits []*PushCommit
  208. CompareUrl string
  209. avatars map[string]string
  210. }
  211. func NewPushCommits() *PushCommits {
  212. return &PushCommits{
  213. avatars: make(map[string]string),
  214. }
  215. }
  216. func (pc *PushCommits) ToApiPayloadCommits(repoLink string) []*api.PayloadCommit {
  217. commits := make([]*api.PayloadCommit, len(pc.Commits))
  218. for i, commit := range pc.Commits {
  219. authorUsername := ""
  220. author, err := GetUserByEmail(commit.AuthorEmail)
  221. if err == nil {
  222. authorUsername = author.Name
  223. }
  224. committerUsername := ""
  225. committer, err := GetUserByEmail(commit.CommitterEmail)
  226. if err == nil {
  227. // TODO: check errors other than email not found.
  228. committerUsername = committer.Name
  229. }
  230. commits[i] = &api.PayloadCommit{
  231. ID: commit.Sha1,
  232. Message: commit.Message,
  233. URL: fmt.Sprintf("%s/commit/%s", repoLink, commit.Sha1),
  234. Author: &api.PayloadAuthor{
  235. Name: commit.AuthorName,
  236. Email: commit.AuthorEmail,
  237. UserName: authorUsername,
  238. },
  239. Committer: &api.PayloadCommitter{
  240. Name: commit.CommitterName,
  241. Email: commit.CommitterEmail,
  242. UserName: committerUsername,
  243. },
  244. Timestamp: commit.Timestamp,
  245. }
  246. }
  247. return commits
  248. }
  249. // AvatarLink tries to match user in database with e-mail
  250. // in order to show custom avatar, and falls back to general avatar link.
  251. func (push *PushCommits) AvatarLink(email string) string {
  252. _, ok := push.avatars[email]
  253. if !ok {
  254. u, err := GetUserByEmail(email)
  255. if err != nil {
  256. push.avatars[email] = base.AvatarLink(email)
  257. if !IsErrUserNotExist(err) {
  258. log.Error(4, "GetUserByEmail: %v", err)
  259. }
  260. } else {
  261. push.avatars[email] = u.RelAvatarLink()
  262. }
  263. }
  264. return push.avatars[email]
  265. }
  266. // updateIssuesCommit checks if issues are manipulated by commit message.
  267. func updateIssuesCommit(u *User, repo *Repository, repoUserName, repoName string, commits []*PushCommit) error {
  268. // Commits are appended in the reverse order.
  269. for i := len(commits) - 1; i >= 0; i-- {
  270. c := commits[i]
  271. refMarked := make(map[int64]bool)
  272. for _, ref := range IssueReferenceKeywordsPat.FindAllString(c.Message, -1) {
  273. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  274. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  275. if len(ref) == 0 {
  276. continue
  277. }
  278. // Add repo name if missing
  279. if ref[0] == '#' {
  280. ref = fmt.Sprintf("%s/%s%s", repoUserName, repoName, ref)
  281. } else if !strings.Contains(ref, "/") {
  282. // FIXME: We don't support User#ID syntax yet
  283. // return ErrNotImplemented
  284. continue
  285. }
  286. issue, err := GetIssueByRef(ref)
  287. if err != nil {
  288. if IsErrIssueNotExist(err) {
  289. continue
  290. }
  291. return err
  292. }
  293. if refMarked[issue.ID] {
  294. continue
  295. }
  296. refMarked[issue.ID] = true
  297. url := fmt.Sprintf("%s/%s/%s/commit/%s", setting.AppSubUrl, repoUserName, repoName, c.Sha1)
  298. message := fmt.Sprintf(`<a href="%s">%s</a>`, url, c.Message)
  299. if err = CreateRefComment(u, 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%s", repoUserName, repoName, 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(u, 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%s", repoUserName, repoName, 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(u, repo, false); err != nil {
  367. return err
  368. }
  369. }
  370. }
  371. return nil
  372. }
  373. // CommitRepoAction adds new action for committing repository.
  374. func CommitRepoAction(
  375. userID, repoUserID int64,
  376. userName, actEmail string,
  377. repoID int64,
  378. repoUserName, repoName string,
  379. refFullName string,
  380. commit *PushCommits,
  381. oldCommitID string, newCommitID string) error {
  382. u, err := GetUserByID(userID)
  383. if err != nil {
  384. return fmt.Errorf("GetUserByID: %v", err)
  385. }
  386. repo, err := GetRepositoryByName(repoUserID, repoName)
  387. if err != nil {
  388. return fmt.Errorf("GetRepositoryByName: %v", err)
  389. } else if err = repo.GetOwner(); err != nil {
  390. return fmt.Errorf("GetOwner: %v", 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. isNewBranch := false
  398. opType := ACTION_COMMIT_REPO
  399. // Check it's tag push or branch.
  400. if strings.HasPrefix(refFullName, "refs/tags/") {
  401. opType = ACTION_PUSH_TAG
  402. commit = &PushCommits{}
  403. } else {
  404. // if not the first commit, set the compareUrl
  405. if !strings.HasPrefix(oldCommitID, "0000000") {
  406. commit.CompareUrl = repo.ComposeCompareURL(oldCommitID, newCommitID)
  407. } else {
  408. isNewBranch = true
  409. }
  410. if err = updateIssuesCommit(u, repo, repoUserName, repoName, commit.Commits); err != nil {
  411. log.Error(4, "updateIssuesCommit: %v", err)
  412. }
  413. }
  414. if len(commit.Commits) > setting.UI.FeedMaxCommitNum {
  415. commit.Commits = commit.Commits[:setting.UI.FeedMaxCommitNum]
  416. }
  417. bs, err := json.Marshal(commit)
  418. if err != nil {
  419. return fmt.Errorf("Marshal: %v", err)
  420. }
  421. refName := git.RefEndName(refFullName)
  422. if err = NotifyWatchers(&Action{
  423. ActUserID: u.ID,
  424. ActUserName: userName,
  425. ActEmail: actEmail,
  426. OpType: opType,
  427. Content: string(bs),
  428. RepoID: repo.ID,
  429. RepoUserName: repoUserName,
  430. RepoName: repo.Name,
  431. RefName: refName,
  432. IsPrivate: repo.IsPrivate,
  433. }); err != nil {
  434. return fmt.Errorf("NotifyWatchers: %v", err)
  435. }
  436. payloadRepo := repo.ComposePayload()
  437. var pusherEmail, pusherName string
  438. pusher, err := GetUserByName(userName)
  439. if err == nil {
  440. pusherEmail = pusher.Email
  441. pusherName = pusher.DisplayName()
  442. }
  443. payloadSender := &api.PayloadUser{
  444. UserName: pusher.Name,
  445. ID: pusher.ID,
  446. AvatarUrl: pusher.AvatarLink(),
  447. }
  448. switch opType {
  449. case ACTION_COMMIT_REPO: // Push
  450. if err = PrepareWebhooks(repo, HOOK_EVENT_PUSH, &api.PushPayload{
  451. Ref: refFullName,
  452. Before: oldCommitID,
  453. After: newCommitID,
  454. CompareUrl: setting.AppUrl + commit.CompareUrl,
  455. Commits: commit.ToApiPayloadCommits(repo.FullLink()),
  456. Repo: payloadRepo,
  457. Pusher: &api.PayloadAuthor{
  458. Name: pusherName,
  459. Email: pusherEmail,
  460. UserName: userName,
  461. },
  462. Sender: payloadSender,
  463. }); err != nil {
  464. return fmt.Errorf("PrepareWebhooks: %v", err)
  465. }
  466. if isNewBranch {
  467. return PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{
  468. Ref: refName,
  469. RefType: "branch",
  470. Repo: payloadRepo,
  471. Sender: payloadSender,
  472. })
  473. }
  474. case ACTION_PUSH_TAG: // Create
  475. return PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{
  476. Ref: refName,
  477. RefType: "tag",
  478. Repo: payloadRepo,
  479. Sender: payloadSender,
  480. })
  481. }
  482. return nil
  483. }
  484. func transferRepoAction(e Engine, actUser, oldOwner, newOwner *User, repo *Repository) (err error) {
  485. if err = notifyWatchers(e, &Action{
  486. ActUserID: actUser.ID,
  487. ActUserName: actUser.Name,
  488. ActEmail: actUser.Email,
  489. OpType: ACTION_TRANSFER_REPO,
  490. RepoID: repo.ID,
  491. RepoUserName: newOwner.Name,
  492. RepoName: repo.Name,
  493. IsPrivate: repo.IsPrivate,
  494. Content: path.Join(oldOwner.Name, repo.Name),
  495. }); err != nil {
  496. return fmt.Errorf("notify watchers '%d/%d': %v", actUser.ID, repo.ID, err)
  497. }
  498. // Remove watch for organization.
  499. if repo.Owner.IsOrganization() {
  500. if err = watchRepo(e, repo.Owner.ID, repo.ID, false); err != nil {
  501. return fmt.Errorf("watch repository: %v", err)
  502. }
  503. }
  504. log.Trace("action.transferRepoAction: %s/%s", actUser.Name, repo.Name)
  505. return nil
  506. }
  507. // TransferRepoAction adds new action for transferring repository.
  508. func TransferRepoAction(actUser, oldOwner, newOwner *User, repo *Repository) error {
  509. return transferRepoAction(x, actUser, oldOwner, newOwner, repo)
  510. }
  511. func mergePullRequestAction(e Engine, actUser *User, repo *Repository, pull *Issue) error {
  512. return notifyWatchers(e, &Action{
  513. ActUserID: actUser.ID,
  514. ActUserName: actUser.Name,
  515. ActEmail: actUser.Email,
  516. OpType: ACTION_MERGE_PULL_REQUEST,
  517. Content: fmt.Sprintf("%d|%s", pull.Index, pull.Title),
  518. RepoID: repo.ID,
  519. RepoUserName: repo.Owner.Name,
  520. RepoName: repo.Name,
  521. IsPrivate: repo.IsPrivate,
  522. })
  523. }
  524. // MergePullRequestAction adds new action for merging pull request.
  525. func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error {
  526. return mergePullRequestAction(x, actUser, repo, pull)
  527. }
  528. // GetFeeds returns action list of given user in given context.
  529. // actorID is the user who's requesting, ctxUserID is the user/org that is requested.
  530. // actorID can be -1 when isProfile is true or to skip the permission check.
  531. func GetFeeds(ctxUser *User, actorID, offset int64, isProfile bool) ([]*Action, error) {
  532. actions := make([]*Action, 0, 20)
  533. sess := x.Limit(20, int(offset)).Desc("id").Where("user_id = ?", ctxUser.ID)
  534. if isProfile {
  535. sess.And("is_private = ?", false).And("act_user_id = ?", ctxUser.ID)
  536. } else if actorID != -1 && ctxUser.IsOrganization() {
  537. // FIXME: only need to get IDs here, not all fields of repository.
  538. repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos)
  539. if err != nil {
  540. return nil, fmt.Errorf("GetUserRepositories: %v", err)
  541. }
  542. var repoIDs []int64
  543. for _, repo := range repos {
  544. repoIDs = append(repoIDs, repo.ID)
  545. }
  546. if len(repoIDs) > 0 {
  547. sess.In("repo_id", repoIDs)
  548. }
  549. }
  550. err := sess.Find(&actions)
  551. return actions, err
  552. }