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.

824 lines
21 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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. "bytes"
  7. "errors"
  8. "strings"
  9. "time"
  10. "github.com/go-xorm/xorm"
  11. "github.com/gogits/gogs/modules/base"
  12. )
  13. var (
  14. ErrIssueNotExist = errors.New("Issue does not exist")
  15. ErrLabelNotExist = errors.New("Label does not exist")
  16. ErrMilestoneNotExist = errors.New("Milestone does not exist")
  17. )
  18. // Issue represents an issue or pull request of repository.
  19. type Issue struct {
  20. Id int64
  21. RepoId int64 `xorm:"INDEX"`
  22. Index int64 // Index in one repository.
  23. Name string
  24. Repo *Repository `xorm:"-"`
  25. PosterId int64
  26. Poster *User `xorm:"-"`
  27. LabelIds string `xorm:"TEXT"`
  28. Labels []*Label `xorm:"-"`
  29. MilestoneId int64
  30. AssigneeId int64
  31. Assignee *User `xorm:"-"`
  32. IsRead bool `xorm:"-"`
  33. IsPull bool // Indicates whether is a pull request or not.
  34. IsClosed bool
  35. Content string `xorm:"TEXT"`
  36. RenderedContent string `xorm:"-"`
  37. Priority int
  38. NumComments int
  39. Deadline time.Time
  40. Created time.Time `xorm:"CREATED"`
  41. Updated time.Time `xorm:"UPDATED"`
  42. }
  43. func (i *Issue) GetPoster() (err error) {
  44. i.Poster, err = GetUserById(i.PosterId)
  45. if err == ErrUserNotExist {
  46. i.Poster = &User{Name: "FakeUser"}
  47. return nil
  48. }
  49. return err
  50. }
  51. func (i *Issue) GetLabels() error {
  52. if len(i.LabelIds) < 3 {
  53. return nil
  54. }
  55. strIds := strings.Split(strings.TrimSuffix(i.LabelIds[1:], "|"), "|$")
  56. i.Labels = make([]*Label, 0, len(strIds))
  57. for _, strId := range strIds {
  58. id, _ := base.StrTo(strId).Int64()
  59. if id > 0 {
  60. l, err := GetLabelById(id)
  61. if err != nil {
  62. if err == ErrLabelNotExist {
  63. continue
  64. }
  65. return err
  66. }
  67. i.Labels = append(i.Labels, l)
  68. }
  69. }
  70. return nil
  71. }
  72. func (i *Issue) GetAssignee() (err error) {
  73. if i.AssigneeId == 0 {
  74. return nil
  75. }
  76. i.Assignee, err = GetUserById(i.AssigneeId)
  77. if err == ErrUserNotExist {
  78. return nil
  79. }
  80. return err
  81. }
  82. // CreateIssue creates new issue for repository.
  83. func NewIssue(issue *Issue) (err error) {
  84. sess := x.NewSession()
  85. defer sess.Close()
  86. if err = sess.Begin(); err != nil {
  87. return err
  88. }
  89. if _, err = sess.Insert(issue); err != nil {
  90. sess.Rollback()
  91. return err
  92. }
  93. rawSql := "UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?"
  94. if _, err = sess.Exec(rawSql, issue.RepoId); err != nil {
  95. sess.Rollback()
  96. return err
  97. }
  98. return sess.Commit()
  99. }
  100. // GetIssueByIndex returns issue by given index in repository.
  101. func GetIssueByIndex(rid, index int64) (*Issue, error) {
  102. issue := &Issue{RepoId: rid, Index: index}
  103. has, err := x.Get(issue)
  104. if err != nil {
  105. return nil, err
  106. } else if !has {
  107. return nil, ErrIssueNotExist
  108. }
  109. return issue, nil
  110. }
  111. // GetIssueById returns an issue by ID.
  112. func GetIssueById(id int64) (*Issue, error) {
  113. issue := &Issue{Id: id}
  114. has, err := x.Get(issue)
  115. if err != nil {
  116. return nil, err
  117. } else if !has {
  118. return nil, ErrIssueNotExist
  119. }
  120. return issue, nil
  121. }
  122. // GetIssues returns a list of issues by given conditions.
  123. func GetIssues(uid, rid, pid, mid int64, page int, isClosed bool, labelIds, sortType string) ([]Issue, error) {
  124. sess := x.Limit(20, (page-1)*20)
  125. if rid > 0 {
  126. sess.Where("repo_id=?", rid).And("is_closed=?", isClosed)
  127. } else {
  128. sess.Where("is_closed=?", isClosed)
  129. }
  130. if uid > 0 {
  131. sess.And("assignee_id=?", uid)
  132. } else if pid > 0 {
  133. sess.And("poster_id=?", pid)
  134. }
  135. if mid > 0 {
  136. sess.And("milestone_id=?", mid)
  137. }
  138. if len(labelIds) > 0 {
  139. for _, label := range strings.Split(labelIds, ",") {
  140. sess.And("label_ids like '%$" + label + "|%'")
  141. }
  142. }
  143. switch sortType {
  144. case "oldest":
  145. sess.Asc("created")
  146. case "recentupdate":
  147. sess.Desc("updated")
  148. case "leastupdate":
  149. sess.Asc("updated")
  150. case "mostcomment":
  151. sess.Desc("num_comments")
  152. case "leastcomment":
  153. sess.Asc("num_comments")
  154. case "priority":
  155. sess.Desc("priority")
  156. default:
  157. sess.Desc("created")
  158. }
  159. var issues []Issue
  160. err := sess.Find(&issues)
  161. return issues, err
  162. }
  163. type IssueStatus int
  164. const (
  165. IS_OPEN = iota + 1
  166. IS_CLOSE
  167. )
  168. // GetIssuesByLabel returns a list of issues by given label and repository.
  169. func GetIssuesByLabel(repoId int64, label string) ([]*Issue, error) {
  170. issues := make([]*Issue, 0, 10)
  171. err := x.Where("repo_id=?", repoId).And("label_ids like '%$" + label + "|%'").Find(&issues)
  172. return issues, err
  173. }
  174. // GetIssueCountByPoster returns number of issues of repository by poster.
  175. func GetIssueCountByPoster(uid, rid int64, isClosed bool) int64 {
  176. count, _ := x.Where("repo_id=?", rid).And("poster_id=?", uid).And("is_closed=?", isClosed).Count(new(Issue))
  177. return count
  178. }
  179. // .___ ____ ___
  180. // | | ______ ________ __ ____ | | \______ ___________
  181. // | |/ ___// ___/ | \_/ __ \| | / ___// __ \_ __ \
  182. // | |\___ \ \___ \| | /\ ___/| | /\___ \\ ___/| | \/
  183. // |___/____ >____ >____/ \___ >______//____ >\___ >__|
  184. // \/ \/ \/ \/ \/
  185. // IssueUser represents an issue-user relation.
  186. type IssueUser struct {
  187. Id int64
  188. Uid int64 `xorm:"INDEX"` // User ID.
  189. IssueId int64
  190. RepoId int64 `xorm:"INDEX"`
  191. MilestoneId int64
  192. IsRead bool
  193. IsAssigned bool
  194. IsMentioned bool
  195. IsPoster bool
  196. IsClosed bool
  197. }
  198. // NewIssueUserPairs adds new issue-user pairs for new issue of repository.
  199. func NewIssueUserPairs(rid, iid, oid, pid, aid int64, repoName string) (err error) {
  200. iu := &IssueUser{IssueId: iid, RepoId: rid}
  201. us, err := GetCollaborators(repoName)
  202. if err != nil {
  203. return err
  204. }
  205. isNeedAddPoster := true
  206. for _, u := range us {
  207. iu.Uid = u.Id
  208. iu.IsPoster = iu.Uid == pid
  209. if isNeedAddPoster && iu.IsPoster {
  210. isNeedAddPoster = false
  211. }
  212. iu.IsAssigned = iu.Uid == aid
  213. if _, err = x.Insert(iu); err != nil {
  214. return err
  215. }
  216. }
  217. if isNeedAddPoster {
  218. iu.Uid = pid
  219. iu.IsPoster = true
  220. iu.IsAssigned = iu.Uid == aid
  221. if _, err = x.Insert(iu); err != nil {
  222. return err
  223. }
  224. }
  225. return nil
  226. }
  227. // PairsContains returns true when pairs list contains given issue.
  228. func PairsContains(ius []*IssueUser, issueId int64) int {
  229. for i := range ius {
  230. if ius[i].IssueId == issueId {
  231. return i
  232. }
  233. }
  234. return -1
  235. }
  236. // GetIssueUserPairs returns issue-user pairs by given repository and user.
  237. func GetIssueUserPairs(rid, uid int64, isClosed bool) ([]*IssueUser, error) {
  238. ius := make([]*IssueUser, 0, 10)
  239. err := x.Where("is_closed=?", isClosed).Find(&ius, &IssueUser{RepoId: rid, Uid: uid})
  240. return ius, err
  241. }
  242. // GetIssueUserPairsByRepoIds returns issue-user pairs by given repository IDs.
  243. func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*IssueUser, error) {
  244. if len(rids) == 0 {
  245. return []*IssueUser{}, nil
  246. }
  247. buf := bytes.NewBufferString("")
  248. for _, rid := range rids {
  249. buf.WriteString("repo_id=")
  250. buf.WriteString(base.ToStr(rid))
  251. buf.WriteString(" OR ")
  252. }
  253. cond := strings.TrimSuffix(buf.String(), " OR ")
  254. ius := make([]*IssueUser, 0, 10)
  255. sess := x.Limit(20, (page-1)*20).Where("is_closed=?", isClosed)
  256. if len(cond) > 0 {
  257. sess.And(cond)
  258. }
  259. err := sess.Find(&ius)
  260. return ius, err
  261. }
  262. // GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
  263. func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
  264. ius := make([]*IssueUser, 0, 10)
  265. sess := x.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
  266. if rid > 0 {
  267. sess.And("repo_id=?", rid)
  268. }
  269. switch filterMode {
  270. case FM_ASSIGN:
  271. sess.And("is_assigned=?", true)
  272. case FM_CREATE:
  273. sess.And("is_poster=?", true)
  274. default:
  275. return ius, nil
  276. }
  277. err := sess.Find(&ius)
  278. return ius, err
  279. }
  280. // IssueStats represents issue statistic information.
  281. type IssueStats struct {
  282. OpenCount, ClosedCount int64
  283. AllCount int64
  284. AssignCount int64
  285. CreateCount int64
  286. MentionCount int64
  287. }
  288. // Filter modes.
  289. const (
  290. FM_ASSIGN = iota + 1
  291. FM_CREATE
  292. FM_MENTION
  293. )
  294. // GetIssueStats returns issue statistic information by given conditions.
  295. func GetIssueStats(rid, uid int64, isShowClosed bool, filterMode int) *IssueStats {
  296. stats := &IssueStats{}
  297. issue := new(Issue)
  298. tmpSess := &xorm.Session{}
  299. sess := x.Where("repo_id=?", rid)
  300. *tmpSess = *sess
  301. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue)
  302. *tmpSess = *sess
  303. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue)
  304. if isShowClosed {
  305. stats.AllCount = stats.ClosedCount
  306. } else {
  307. stats.AllCount = stats.OpenCount
  308. }
  309. if filterMode != FM_MENTION {
  310. sess = x.Where("repo_id=?", rid)
  311. switch filterMode {
  312. case FM_ASSIGN:
  313. sess.And("assignee_id=?", uid)
  314. case FM_CREATE:
  315. sess.And("poster_id=?", uid)
  316. default:
  317. goto nofilter
  318. }
  319. *tmpSess = *sess
  320. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue)
  321. *tmpSess = *sess
  322. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue)
  323. } else {
  324. sess := x.Where("repo_id=?", rid).And("uid=?", uid).And("is_mentioned=?", true)
  325. *tmpSess = *sess
  326. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(new(IssueUser))
  327. *tmpSess = *sess
  328. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(new(IssueUser))
  329. }
  330. nofilter:
  331. stats.AssignCount, _ = x.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("assignee_id=?", uid).Count(issue)
  332. stats.CreateCount, _ = x.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("poster_id=?", uid).Count(issue)
  333. stats.MentionCount, _ = x.Where("repo_id=?", rid).And("uid=?", uid).And("is_closed=?", isShowClosed).And("is_mentioned=?", true).Count(new(IssueUser))
  334. return stats
  335. }
  336. // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
  337. func GetUserIssueStats(uid int64, filterMode int) *IssueStats {
  338. stats := &IssueStats{}
  339. issue := new(Issue)
  340. stats.AssignCount, _ = x.Where("assignee_id=?", uid).And("is_closed=?", false).Count(issue)
  341. stats.CreateCount, _ = x.Where("poster_id=?", uid).And("is_closed=?", false).Count(issue)
  342. return stats
  343. }
  344. // UpdateIssue updates information of issue.
  345. func UpdateIssue(issue *Issue) error {
  346. _, err := x.Id(issue.Id).AllCols().Update(issue)
  347. return err
  348. }
  349. // UpdateIssueUserByStatus updates issue-user pairs by issue status.
  350. func UpdateIssueUserPairsByStatus(iid int64, isClosed bool) error {
  351. rawSql := "UPDATE `issue_user` SET is_closed = ? WHERE issue_id = ?"
  352. _, err := x.Exec(rawSql, isClosed, iid)
  353. return err
  354. }
  355. // UpdateIssueUserPairByAssignee updates issue-user pair for assigning.
  356. func UpdateIssueUserPairByAssignee(aid, iid int64) error {
  357. rawSql := "UPDATE `issue_user` SET is_assigned = ? WHERE issue_id = ?"
  358. if _, err := x.Exec(rawSql, false, iid); err != nil {
  359. return err
  360. }
  361. // Assignee ID equals to 0 means clear assignee.
  362. if aid == 0 {
  363. return nil
  364. }
  365. rawSql = "UPDATE `issue_user` SET is_assigned = true WHERE uid = ? AND issue_id = ?"
  366. _, err := x.Exec(rawSql, aid, iid)
  367. return err
  368. }
  369. // UpdateIssueUserPairByRead updates issue-user pair for reading.
  370. func UpdateIssueUserPairByRead(uid, iid int64) error {
  371. rawSql := "UPDATE `issue_user` SET is_read = ? WHERE uid = ? AND issue_id = ?"
  372. _, err := x.Exec(rawSql, true, uid, iid)
  373. return err
  374. }
  375. // UpdateIssueUserPairsByMentions updates issue-user pairs by mentioning.
  376. func UpdateIssueUserPairsByMentions(uids []int64, iid int64) error {
  377. for _, uid := range uids {
  378. iu := &IssueUser{Uid: uid, IssueId: iid}
  379. has, err := x.Get(iu)
  380. if err != nil {
  381. return err
  382. }
  383. iu.IsMentioned = true
  384. if has {
  385. _, err = x.Id(iu.Id).AllCols().Update(iu)
  386. } else {
  387. _, err = x.Insert(iu)
  388. }
  389. if err != nil {
  390. return err
  391. }
  392. }
  393. return nil
  394. }
  395. // .____ ___. .__
  396. // | | _____ \_ |__ ____ | |
  397. // | | \__ \ | __ \_/ __ \| |
  398. // | |___ / __ \| \_\ \ ___/| |__
  399. // |_______ (____ /___ /\___ >____/
  400. // \/ \/ \/ \/
  401. // Label represents a label of repository for issues.
  402. type Label struct {
  403. Id int64
  404. RepoId int64 `xorm:"INDEX"`
  405. Name string
  406. Color string `xorm:"VARCHAR(7)"`
  407. NumIssues int
  408. NumClosedIssues int
  409. NumOpenIssues int `xorm:"-"`
  410. IsChecked bool `xorm:"-"`
  411. }
  412. // CalOpenIssues calculates the open issues of label.
  413. func (m *Label) CalOpenIssues() {
  414. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  415. }
  416. // NewLabel creates new label of repository.
  417. func NewLabel(l *Label) error {
  418. _, err := x.Insert(l)
  419. return err
  420. }
  421. // GetLabelById returns a label by given ID.
  422. func GetLabelById(id int64) (*Label, error) {
  423. if id <= 0 {
  424. return nil, ErrLabelNotExist
  425. }
  426. l := &Label{Id: id}
  427. has, err := x.Get(l)
  428. if err != nil {
  429. return nil, err
  430. } else if !has {
  431. return nil, ErrLabelNotExist
  432. }
  433. return l, nil
  434. }
  435. // GetLabels returns a list of labels of given repository ID.
  436. func GetLabels(repoId int64) ([]*Label, error) {
  437. labels := make([]*Label, 0, 10)
  438. err := x.Where("repo_id=?", repoId).Find(&labels)
  439. return labels, err
  440. }
  441. // UpdateLabel updates label information.
  442. func UpdateLabel(l *Label) error {
  443. _, err := x.Id(l.Id).Update(l)
  444. return err
  445. }
  446. // DeleteLabel delete a label of given repository.
  447. func DeleteLabel(repoId int64, strId string) error {
  448. id, _ := base.StrTo(strId).Int64()
  449. l, err := GetLabelById(id)
  450. if err != nil {
  451. if err == ErrLabelNotExist {
  452. return nil
  453. }
  454. return err
  455. }
  456. issues, err := GetIssuesByLabel(repoId, strId)
  457. if err != nil {
  458. return err
  459. }
  460. sess := x.NewSession()
  461. defer sess.Close()
  462. if err = sess.Begin(); err != nil {
  463. return err
  464. }
  465. for _, issue := range issues {
  466. issue.LabelIds = strings.Replace(issue.LabelIds, "$"+strId+"|", "", -1)
  467. if _, err = sess.Id(issue.Id).AllCols().Update(issue); err != nil {
  468. sess.Rollback()
  469. return err
  470. }
  471. }
  472. if _, err = sess.Delete(l); err != nil {
  473. sess.Rollback()
  474. return err
  475. }
  476. return sess.Commit()
  477. }
  478. // _____ .__.__ __
  479. // / \ |__| | ____ _______/ |_ ____ ____ ____
  480. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  481. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  482. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  483. // \/ \/ \/ \/ \/
  484. // Milestone represents a milestone of repository.
  485. type Milestone struct {
  486. Id int64
  487. RepoId int64 `xorm:"INDEX"`
  488. Index int64
  489. Name string
  490. Content string
  491. RenderedContent string `xorm:"-"`
  492. IsClosed bool
  493. NumIssues int
  494. NumClosedIssues int
  495. NumOpenIssues int `xorm:"-"`
  496. Completeness int // Percentage(1-100).
  497. Deadline time.Time
  498. DeadlineString string `xorm:"-"`
  499. ClosedDate time.Time
  500. }
  501. // CalOpenIssues calculates the open issues of milestone.
  502. func (m *Milestone) CalOpenIssues() {
  503. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  504. }
  505. // NewMilestone creates new milestone of repository.
  506. func NewMilestone(m *Milestone) (err error) {
  507. sess := x.NewSession()
  508. defer sess.Close()
  509. if err = sess.Begin(); err != nil {
  510. return err
  511. }
  512. if _, err = sess.Insert(m); err != nil {
  513. sess.Rollback()
  514. return err
  515. }
  516. rawSql := "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?"
  517. if _, err = sess.Exec(rawSql, m.RepoId); err != nil {
  518. sess.Rollback()
  519. return err
  520. }
  521. return sess.Commit()
  522. }
  523. // GetMilestoneById returns the milestone by given ID.
  524. func GetMilestoneById(id int64) (*Milestone, error) {
  525. m := &Milestone{Id: id}
  526. has, err := x.Get(m)
  527. if err != nil {
  528. return nil, err
  529. } else if !has {
  530. return nil, ErrMilestoneNotExist
  531. }
  532. return m, nil
  533. }
  534. // GetMilestoneByIndex returns the milestone of given repository and index.
  535. func GetMilestoneByIndex(repoId, idx int64) (*Milestone, error) {
  536. m := &Milestone{RepoId: repoId, Index: idx}
  537. has, err := x.Get(m)
  538. if err != nil {
  539. return nil, err
  540. } else if !has {
  541. return nil, ErrMilestoneNotExist
  542. }
  543. return m, nil
  544. }
  545. // GetMilestones returns a list of milestones of given repository and status.
  546. func GetMilestones(repoId int64, isClosed bool) ([]*Milestone, error) {
  547. miles := make([]*Milestone, 0, 10)
  548. err := x.Where("repo_id=?", repoId).And("is_closed=?", isClosed).Find(&miles)
  549. return miles, err
  550. }
  551. // UpdateMilestone updates information of given milestone.
  552. func UpdateMilestone(m *Milestone) error {
  553. _, err := x.Id(m.Id).Update(m)
  554. return err
  555. }
  556. // ChangeMilestoneStatus changes the milestone open/closed status.
  557. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  558. repo, err := GetRepositoryById(m.RepoId)
  559. if err != nil {
  560. return err
  561. }
  562. sess := x.NewSession()
  563. defer sess.Close()
  564. if err = sess.Begin(); err != nil {
  565. return err
  566. }
  567. m.IsClosed = isClosed
  568. if _, err = sess.Id(m.Id).AllCols().Update(m); err != nil {
  569. sess.Rollback()
  570. return err
  571. }
  572. if isClosed {
  573. repo.NumClosedMilestones++
  574. } else {
  575. repo.NumClosedMilestones--
  576. }
  577. if _, err = sess.Id(repo.Id).Update(repo); err != nil {
  578. sess.Rollback()
  579. return err
  580. }
  581. return sess.Commit()
  582. }
  583. // ChangeMilestoneAssign changes assignment of milestone for issue.
  584. func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) {
  585. sess := x.NewSession()
  586. defer sess.Close()
  587. if err = sess.Begin(); err != nil {
  588. return err
  589. }
  590. if oldMid > 0 {
  591. m, err := GetMilestoneById(oldMid)
  592. if err != nil {
  593. return err
  594. }
  595. m.NumIssues--
  596. if issue.IsClosed {
  597. m.NumClosedIssues--
  598. }
  599. if m.NumIssues > 0 {
  600. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  601. } else {
  602. m.Completeness = 0
  603. }
  604. if _, err = sess.Id(m.Id).Update(m); err != nil {
  605. sess.Rollback()
  606. return err
  607. }
  608. rawSql := "UPDATE `issue_user` SET milestone_id = 0 WHERE issue_id = ?"
  609. if _, err = sess.Exec(rawSql, issue.Id); err != nil {
  610. sess.Rollback()
  611. return err
  612. }
  613. }
  614. if mid > 0 {
  615. m, err := GetMilestoneById(mid)
  616. if err != nil {
  617. return err
  618. }
  619. m.NumIssues++
  620. if issue.IsClosed {
  621. m.NumClosedIssues++
  622. }
  623. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  624. if _, err = sess.Id(m.Id).Update(m); err != nil {
  625. sess.Rollback()
  626. return err
  627. }
  628. rawSql := "UPDATE `issue_user` SET milestone_id = ? WHERE issue_id = ?"
  629. if _, err = sess.Exec(rawSql, m.Id, issue.Id); err != nil {
  630. sess.Rollback()
  631. return err
  632. }
  633. }
  634. return sess.Commit()
  635. }
  636. // DeleteMilestone deletes a milestone.
  637. func DeleteMilestone(m *Milestone) (err error) {
  638. sess := x.NewSession()
  639. defer sess.Close()
  640. if err = sess.Begin(); err != nil {
  641. return err
  642. }
  643. if _, err = sess.Delete(m); err != nil {
  644. sess.Rollback()
  645. return err
  646. }
  647. rawSql := "UPDATE `repository` SET num_milestones = num_milestones - 1 WHERE id = ?"
  648. if _, err = sess.Exec(rawSql, m.RepoId); err != nil {
  649. sess.Rollback()
  650. return err
  651. }
  652. rawSql = "UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?"
  653. if _, err = sess.Exec(rawSql, m.Id); err != nil {
  654. sess.Rollback()
  655. return err
  656. }
  657. rawSql = "UPDATE `issue_user` SET milestone_id = 0 WHERE milestone_id = ?"
  658. if _, err = sess.Exec(rawSql, m.Id); err != nil {
  659. sess.Rollback()
  660. return err
  661. }
  662. return sess.Commit()
  663. }
  664. // _________ __
  665. // \_ ___ \ ____ _____ _____ ____ _____/ |_
  666. // / \ \/ / _ \ / \ / \_/ __ \ / \ __\
  667. // \ \___( <_> ) Y Y \ Y Y \ ___/| | \ |
  668. // \______ /\____/|__|_| /__|_| /\___ >___| /__|
  669. // \/ \/ \/ \/ \/
  670. // Issue types.
  671. const (
  672. IT_PLAIN = iota // Pure comment.
  673. IT_REOPEN // Issue reopen status change prompt.
  674. IT_CLOSE // Issue close status change prompt.
  675. )
  676. // Comment represents a comment in commit and issue page.
  677. type Comment struct {
  678. Id int64
  679. Type int
  680. PosterId int64
  681. Poster *User `xorm:"-"`
  682. IssueId int64
  683. CommitId int64
  684. Line int64
  685. Content string `xorm:"TEXT"`
  686. Created time.Time `xorm:"CREATED"`
  687. }
  688. // CreateComment creates comment of issue or commit.
  689. func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, content string) error {
  690. sess := x.NewSession()
  691. defer sess.Close()
  692. if err := sess.Begin(); err != nil {
  693. return err
  694. }
  695. if _, err := sess.Insert(&Comment{PosterId: userId, Type: cmtType, IssueId: issueId,
  696. CommitId: commitId, Line: line, Content: content}); err != nil {
  697. sess.Rollback()
  698. return err
  699. }
  700. // Check comment type.
  701. switch cmtType {
  702. case IT_PLAIN:
  703. rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
  704. if _, err := sess.Exec(rawSql, issueId); err != nil {
  705. sess.Rollback()
  706. return err
  707. }
  708. case IT_REOPEN:
  709. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?"
  710. if _, err := sess.Exec(rawSql, repoId); err != nil {
  711. sess.Rollback()
  712. return err
  713. }
  714. case IT_CLOSE:
  715. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?"
  716. if _, err := sess.Exec(rawSql, repoId); err != nil {
  717. sess.Rollback()
  718. return err
  719. }
  720. }
  721. return sess.Commit()
  722. }
  723. // GetIssueComments returns list of comment by given issue id.
  724. func GetIssueComments(issueId int64) ([]Comment, error) {
  725. comments := make([]Comment, 0, 10)
  726. err := x.Asc("created").Find(&comments, &Comment{IssueId: issueId})
  727. return comments, err
  728. }