action.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, repo.Link(), c.Sha1, c.Message)
  288. if err = CreateRefComment(doer, repo, issue, message, c.Sha1); err != nil {
  289. return err
  290. }
  291. }
  292. refMarked = make(map[int64]bool)
  293. // FIXME: can merge this one and next one to a common function.
  294. for _, ref := range IssueCloseKeywordsPat.FindAllString(c.Message, -1) {
  295. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  296. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  297. if len(ref) == 0 {
  298. continue
  299. }
  300. // Add repo name if missing
  301. if ref[0] == '#' {
  302. ref = fmt.Sprintf("%s%s", repo.FullName(), ref)
  303. } else if !strings.Contains(ref, "/") {
  304. // We don't support User#ID syntax yet
  305. // return ErrNotImplemented
  306. continue
  307. }
  308. issue, err := GetIssueByRef(ref)
  309. if err != nil {
  310. if IsErrIssueNotExist(err) {
  311. continue
  312. }
  313. return err
  314. }
  315. if refMarked[issue.ID] {
  316. continue
  317. }
  318. refMarked[issue.ID] = true
  319. if issue.RepoID != repo.ID || issue.IsClosed {
  320. continue
  321. }
  322. if err = issue.ChangeStatus(doer, repo, true); err != nil {
  323. return err
  324. }
  325. }
  326. // It is conflict to have close and reopen at same time, so refsMarkd doesn't need to reinit here.
  327. for _, ref := range IssueReopenKeywordsPat.FindAllString(c.Message, -1) {
  328. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  329. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  330. if len(ref) == 0 {
  331. continue
  332. }
  333. // Add repo name if missing
  334. if ref[0] == '#' {
  335. ref = fmt.Sprintf("%s%s", repo.FullName(), ref)
  336. } else if !strings.Contains(ref, "/") {
  337. // We don't support User#ID syntax yet
  338. // return ErrNotImplemented
  339. continue
  340. }
  341. issue, err := GetIssueByRef(ref)
  342. if err != nil {
  343. if IsErrIssueNotExist(err) {
  344. continue
  345. }
  346. return err
  347. }
  348. if refMarked[issue.ID] {
  349. continue
  350. }
  351. refMarked[issue.ID] = true
  352. if issue.RepoID != repo.ID || !issue.IsClosed {
  353. continue
  354. }
  355. if err = issue.ChangeStatus(doer, repo, false); err != nil {
  356. return err
  357. }
  358. }
  359. }
  360. return nil
  361. }
  362. type CommitRepoActionOptions struct {
  363. PusherName string
  364. RepoOwnerID int64
  365. RepoName string
  366. RefFullName string
  367. OldCommitID string
  368. NewCommitID string
  369. Commits *PushCommits
  370. }
  371. // CommitRepoAction adds new commit actio to the repository, and prepare corresponding webhooks.
  372. func CommitRepoAction(opts CommitRepoActionOptions) error {
  373. pusher, err := GetUserByName(opts.PusherName)
  374. if err != nil {
  375. return fmt.Errorf("GetUserByName [%s]: %v", opts.PusherName, err)
  376. }
  377. repo, err := GetRepositoryByName(opts.RepoOwnerID, opts.RepoName)
  378. if err != nil {
  379. return fmt.Errorf("GetRepositoryByName [owner_id: %d, name: %s]: %v", opts.RepoOwnerID, opts.RepoName, err)
  380. }
  381. // Change repository bare status and update last updated time.
  382. repo.IsBare = false
  383. if err = UpdateRepository(repo, false); err != nil {
  384. return fmt.Errorf("UpdateRepository: %v", err)
  385. }
  386. isNewBranch := false
  387. opType := ACTION_COMMIT_REPO
  388. // Check it's tag push or branch.
  389. if strings.HasPrefix(opts.RefFullName, git.TAG_PREFIX) {
  390. opType = ACTION_PUSH_TAG
  391. opts.Commits = &PushCommits{}
  392. } else {
  393. // TODO: detect branch deletion
  394. // if not the first commit, set the compare URL.
  395. if opts.OldCommitID == git.EMPTY_SHA {
  396. isNewBranch = true
  397. } else {
  398. opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
  399. }
  400. if err = UpdateIssuesCommit(pusher, repo, opts.Commits.Commits); err != nil {
  401. log.Error(2, "UpdateIssuesCommit: %v", err)
  402. }
  403. }
  404. if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
  405. opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
  406. }
  407. data, err := json.Marshal(opts.Commits)
  408. if err != nil {
  409. return fmt.Errorf("Marshal: %v", err)
  410. }
  411. refName := git.RefEndName(opts.RefFullName)
  412. if err = NotifyWatchers(&Action{
  413. ActUserID: pusher.ID,
  414. ActUserName: pusher.Name,
  415. OpType: opType,
  416. Content: string(data),
  417. RepoID: repo.ID,
  418. RepoUserName: repo.MustOwner().Name,
  419. RepoName: repo.Name,
  420. RefName: refName,
  421. IsPrivate: repo.IsPrivate,
  422. }); err != nil {
  423. return fmt.Errorf("NotifyWatchers: %v", err)
  424. }
  425. defer func() {
  426. go HookQueue.Add(repo.ID)
  427. }()
  428. apiPusher := pusher.APIFormat()
  429. apiRepo := repo.APIFormat(nil)
  430. switch opType {
  431. case ACTION_COMMIT_REPO: // Push
  432. compareURL := setting.AppUrl + opts.Commits.CompareURL
  433. if isNewBranch {
  434. compareURL = ""
  435. if err = PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{
  436. Ref: refName,
  437. RefType: "branch",
  438. Repo: apiRepo,
  439. Sender: apiPusher,
  440. }); err != nil {
  441. return fmt.Errorf("PrepareWebhooks (new branch): %v", err)
  442. }
  443. }
  444. if err = PrepareWebhooks(repo, HOOK_EVENT_PUSH, &api.PushPayload{
  445. Ref: opts.RefFullName,
  446. Before: opts.OldCommitID,
  447. After: opts.NewCommitID,
  448. CompareURL: compareURL,
  449. Commits: opts.Commits.ToApiPayloadCommits(repo.HTMLURL()),
  450. Repo: apiRepo,
  451. Pusher: apiPusher,
  452. Sender: apiPusher,
  453. }); err != nil {
  454. return fmt.Errorf("PrepareWebhooks (new commit): %v", err)
  455. }
  456. case ACTION_PUSH_TAG: // Create
  457. return PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{
  458. Ref: refName,
  459. RefType: "tag",
  460. Repo: apiRepo,
  461. Sender: apiPusher,
  462. })
  463. }
  464. return nil
  465. }
  466. func transferRepoAction(e Engine, doer, oldOwner *User, repo *Repository) (err error) {
  467. if err = notifyWatchers(e, &Action{
  468. ActUserID: doer.ID,
  469. ActUserName: doer.Name,
  470. OpType: ACTION_TRANSFER_REPO,
  471. RepoID: repo.ID,
  472. RepoUserName: repo.Owner.Name,
  473. RepoName: repo.Name,
  474. IsPrivate: repo.IsPrivate,
  475. Content: path.Join(oldOwner.Name, repo.Name),
  476. }); err != nil {
  477. return fmt.Errorf("notifyWatchers: %v", err)
  478. }
  479. // Remove watch for organization.
  480. if oldOwner.IsOrganization() {
  481. if err = watchRepo(e, oldOwner.ID, repo.ID, false); err != nil {
  482. return fmt.Errorf("watchRepo [false]: %v", err)
  483. }
  484. }
  485. return nil
  486. }
  487. // TransferRepoAction adds new action for transferring repository,
  488. // the Owner field of repository is assumed to be new owner.
  489. func TransferRepoAction(doer, oldOwner *User, repo *Repository) error {
  490. return transferRepoAction(x, doer, oldOwner, repo)
  491. }
  492. func mergePullRequestAction(e Engine, doer *User, repo *Repository, issue *Issue) error {
  493. return notifyWatchers(e, &Action{
  494. ActUserID: doer.ID,
  495. ActUserName: doer.Name,
  496. OpType: ACTION_MERGE_PULL_REQUEST,
  497. Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
  498. RepoID: repo.ID,
  499. RepoUserName: repo.Owner.Name,
  500. RepoName: repo.Name,
  501. IsPrivate: repo.IsPrivate,
  502. })
  503. }
  504. // MergePullRequestAction adds new action for merging pull request.
  505. func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error {
  506. return mergePullRequestAction(x, actUser, repo, pull)
  507. }
  508. // GetFeeds returns action list of given user in given context.
  509. // actorID is the user who's requesting, ctxUserID is the user/org that is requested.
  510. // actorID can be -1 when isProfile is true or to skip the permission check.
  511. func GetFeeds(ctxUser *User, actorID, offset int64, isProfile bool) ([]*Action, error) {
  512. actions := make([]*Action, 0, 20)
  513. sess := x.Limit(20, int(offset)).Desc("id").Where("user_id = ?", ctxUser.ID)
  514. if isProfile {
  515. sess.And("is_private = ?", false).And("act_user_id = ?", ctxUser.ID)
  516. } else if actorID != -1 && ctxUser.IsOrganization() {
  517. // FIXME: only need to get IDs here, not all fields of repository.
  518. repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos)
  519. if err != nil {
  520. return nil, fmt.Errorf("GetUserRepositories: %v", err)
  521. }
  522. var repoIDs []int64
  523. for _, repo := range repos {
  524. repoIDs = append(repoIDs, repo.ID)
  525. }
  526. if len(repoIDs) > 0 {
  527. sess.In("repo_id", repoIDs)
  528. }
  529. }
  530. err := sess.Find(&actions)
  531. return actions, err
  532. }