action.go 17 KB

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