issue.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 := orm.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 := orm.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 := orm.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 := orm.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 := orm.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, _ := orm.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 // User ID.
  189. IssueId int64
  190. RepoId int64
  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 = orm.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 = orm.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 := orm.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. buf := bytes.NewBufferString("")
  245. for _, rid := range rids {
  246. buf.WriteString("repo_id=")
  247. buf.WriteString(base.ToStr(rid))
  248. buf.WriteString(" OR ")
  249. }
  250. cond := strings.TrimSuffix(buf.String(), " OR ")
  251. ius := make([]*IssueUser, 0, 10)
  252. sess := orm.Limit(20, (page-1)*20).Where("is_closed=?", isClosed)
  253. if len(cond) > 0 {
  254. sess.And(cond)
  255. }
  256. err := sess.Find(&ius)
  257. return ius, err
  258. }
  259. // GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
  260. func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
  261. ius := make([]*IssueUser, 0, 10)
  262. sess := orm.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
  263. if rid > 0 {
  264. sess.And("repo_id=?", rid)
  265. }
  266. switch filterMode {
  267. case FM_ASSIGN:
  268. sess.And("is_assigned=?", true)
  269. case FM_CREATE:
  270. sess.And("is_poster=?", true)
  271. default:
  272. return ius, nil
  273. }
  274. err := sess.Find(&ius)
  275. return ius, err
  276. }
  277. // IssueStats represents issue statistic information.
  278. type IssueStats struct {
  279. OpenCount, ClosedCount int64
  280. AllCount int64
  281. AssignCount int64
  282. CreateCount int64
  283. MentionCount int64
  284. }
  285. // Filter modes.
  286. const (
  287. FM_ASSIGN = iota + 1
  288. FM_CREATE
  289. FM_MENTION
  290. )
  291. // GetIssueStats returns issue statistic information by given conditions.
  292. func GetIssueStats(rid, uid int64, isShowClosed bool, filterMode int) *IssueStats {
  293. stats := &IssueStats{}
  294. issue := new(Issue)
  295. tmpSess := &xorm.Session{}
  296. sess := orm.Where("repo_id=?", rid)
  297. *tmpSess = *sess
  298. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue)
  299. *tmpSess = *sess
  300. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue)
  301. if isShowClosed {
  302. stats.AllCount = stats.ClosedCount
  303. } else {
  304. stats.AllCount = stats.OpenCount
  305. }
  306. if filterMode != FM_MENTION {
  307. sess = orm.Where("repo_id=?", rid)
  308. switch filterMode {
  309. case FM_ASSIGN:
  310. sess.And("assignee_id=?", uid)
  311. case FM_CREATE:
  312. sess.And("poster_id=?", uid)
  313. default:
  314. goto nofilter
  315. }
  316. *tmpSess = *sess
  317. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue)
  318. *tmpSess = *sess
  319. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue)
  320. } else {
  321. sess := orm.Where("repo_id=?", rid).And("uid=?", uid).And("is_mentioned=?", true)
  322. *tmpSess = *sess
  323. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(new(IssueUser))
  324. *tmpSess = *sess
  325. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(new(IssueUser))
  326. }
  327. nofilter:
  328. stats.AssignCount, _ = orm.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("assignee_id=?", uid).Count(issue)
  329. stats.CreateCount, _ = orm.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("poster_id=?", uid).Count(issue)
  330. stats.MentionCount, _ = orm.Where("repo_id=?", rid).And("uid=?", uid).And("is_closed=?", isShowClosed).And("is_mentioned=?", true).Count(new(IssueUser))
  331. return stats
  332. }
  333. // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
  334. func GetUserIssueStats(uid int64, filterMode int) *IssueStats {
  335. stats := &IssueStats{}
  336. issue := new(Issue)
  337. stats.AssignCount, _ = orm.Where("assignee_id=?", uid).And("is_closed=?", false).Count(issue)
  338. stats.CreateCount, _ = orm.Where("poster_id=?", uid).And("is_closed=?", false).Count(issue)
  339. return stats
  340. }
  341. // UpdateIssue updates information of issue.
  342. func UpdateIssue(issue *Issue) error {
  343. _, err := orm.Id(issue.Id).AllCols().Update(issue)
  344. return err
  345. }
  346. // UpdateIssueUserByStatus updates issue-user pairs by issue status.
  347. func UpdateIssueUserPairsByStatus(iid int64, isClosed bool) error {
  348. rawSql := "UPDATE `issue_user` SET is_closed = ? WHERE issue_id = ?"
  349. _, err := orm.Exec(rawSql, isClosed, iid)
  350. return err
  351. }
  352. // UpdateIssueUserPairByAssignee updates issue-user pair for assigning.
  353. func UpdateIssueUserPairByAssignee(aid, iid int64) error {
  354. rawSql := "UPDATE `issue_user` SET is_assigned = ? WHERE issue_id = ?"
  355. if _, err := orm.Exec(rawSql, false, iid); err != nil {
  356. return err
  357. }
  358. // Assignee ID equals to 0 means clear assignee.
  359. if aid == 0 {
  360. return nil
  361. }
  362. rawSql = "UPDATE `issue_user` SET is_assigned = true WHERE uid = ? AND issue_id = ?"
  363. _, err := orm.Exec(rawSql, aid, iid)
  364. return err
  365. }
  366. // UpdateIssueUserPairByRead updates issue-user pair for reading.
  367. func UpdateIssueUserPairByRead(uid, iid int64) error {
  368. rawSql := "UPDATE `issue_user` SET is_read = ? WHERE uid = ? AND issue_id = ?"
  369. _, err := orm.Exec(rawSql, true, uid, iid)
  370. return err
  371. }
  372. // UpdateIssueUserPairsByMentions updates issue-user pairs by mentioning.
  373. func UpdateIssueUserPairsByMentions(uids []int64, iid int64) error {
  374. for _, uid := range uids {
  375. iu := &IssueUser{Uid: uid, IssueId: iid}
  376. has, err := orm.Get(iu)
  377. if err != nil {
  378. return err
  379. }
  380. iu.IsMentioned = true
  381. if has {
  382. _, err = orm.Id(iu.Id).AllCols().Update(iu)
  383. } else {
  384. _, err = orm.Insert(iu)
  385. }
  386. if err != nil {
  387. return err
  388. }
  389. }
  390. return nil
  391. }
  392. // .____ ___. .__
  393. // | | _____ \_ |__ ____ | |
  394. // | | \__ \ | __ \_/ __ \| |
  395. // | |___ / __ \| \_\ \ ___/| |__
  396. // |_______ (____ /___ /\___ >____/
  397. // \/ \/ \/ \/
  398. // Label represents a label of repository for issues.
  399. type Label struct {
  400. Id int64
  401. RepoId int64 `xorm:"INDEX"`
  402. Name string
  403. Color string `xorm:"VARCHAR(7)"`
  404. NumIssues int
  405. NumClosedIssues int
  406. NumOpenIssues int `xorm:"-"`
  407. IsChecked bool `xorm:"-"`
  408. }
  409. // CalOpenIssues calculates the open issues of label.
  410. func (m *Label) CalOpenIssues() {
  411. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  412. }
  413. // NewLabel creates new label of repository.
  414. func NewLabel(l *Label) error {
  415. _, err := orm.Insert(l)
  416. return err
  417. }
  418. // GetLabelById returns a label by given ID.
  419. func GetLabelById(id int64) (*Label, error) {
  420. l := &Label{Id: id}
  421. has, err := orm.Get(l)
  422. if err != nil {
  423. return nil, err
  424. } else if !has {
  425. return nil, ErrLabelNotExist
  426. }
  427. return l, nil
  428. }
  429. // GetLabels returns a list of labels of given repository ID.
  430. func GetLabels(repoId int64) ([]*Label, error) {
  431. labels := make([]*Label, 0, 10)
  432. err := orm.Where("repo_id=?", repoId).Find(&labels)
  433. return labels, err
  434. }
  435. // UpdateLabel updates label information.
  436. func UpdateLabel(l *Label) error {
  437. _, err := orm.Id(l.Id).Update(l)
  438. return err
  439. }
  440. // DeleteLabel delete a label of given repository.
  441. func DeleteLabel(repoId int64, strId string) error {
  442. id, _ := base.StrTo(strId).Int64()
  443. l, err := GetLabelById(id)
  444. if err != nil {
  445. if err == ErrLabelNotExist {
  446. return nil
  447. }
  448. return err
  449. }
  450. issues, err := GetIssuesByLabel(repoId, strId)
  451. if err != nil {
  452. return err
  453. }
  454. sess := orm.NewSession()
  455. defer sess.Close()
  456. if err = sess.Begin(); err != nil {
  457. return err
  458. }
  459. for _, issue := range issues {
  460. issue.LabelIds = strings.Replace(issue.LabelIds, "$"+strId+"|", "", -1)
  461. if _, err = sess.Id(issue.Id).AllCols().Update(issue); err != nil {
  462. sess.Rollback()
  463. return err
  464. }
  465. }
  466. if _, err = sess.Delete(l); err != nil {
  467. sess.Rollback()
  468. return err
  469. }
  470. return sess.Commit()
  471. }
  472. // _____ .__.__ __
  473. // / \ |__| | ____ _______/ |_ ____ ____ ____
  474. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  475. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  476. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  477. // \/ \/ \/ \/ \/
  478. // Milestone represents a milestone of repository.
  479. type Milestone struct {
  480. Id int64
  481. RepoId int64 `xorm:"INDEX"`
  482. Index int64
  483. Name string
  484. Content string
  485. RenderedContent string `xorm:"-"`
  486. IsClosed bool
  487. NumIssues int
  488. NumClosedIssues int
  489. NumOpenIssues int `xorm:"-"`
  490. Completeness int // Percentage(1-100).
  491. Deadline time.Time
  492. DeadlineString string `xorm:"-"`
  493. ClosedDate time.Time
  494. }
  495. // CalOpenIssues calculates the open issues of milestone.
  496. func (m *Milestone) CalOpenIssues() {
  497. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  498. }
  499. // NewMilestone creates new milestone of repository.
  500. func NewMilestone(m *Milestone) (err error) {
  501. sess := orm.NewSession()
  502. defer sess.Close()
  503. if err = sess.Begin(); err != nil {
  504. return err
  505. }
  506. if _, err = sess.Insert(m); err != nil {
  507. sess.Rollback()
  508. return err
  509. }
  510. rawSql := "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?"
  511. if _, err = sess.Exec(rawSql, m.RepoId); err != nil {
  512. sess.Rollback()
  513. return err
  514. }
  515. return sess.Commit()
  516. }
  517. // GetMilestoneById returns the milestone by given ID.
  518. func GetMilestoneById(id int64) (*Milestone, error) {
  519. m := &Milestone{Id: id}
  520. has, err := orm.Get(m)
  521. if err != nil {
  522. return nil, err
  523. } else if !has {
  524. return nil, ErrMilestoneNotExist
  525. }
  526. return m, nil
  527. }
  528. // GetMilestoneByIndex returns the milestone of given repository and index.
  529. func GetMilestoneByIndex(repoId, idx int64) (*Milestone, error) {
  530. m := &Milestone{RepoId: repoId, Index: idx}
  531. has, err := orm.Get(m)
  532. if err != nil {
  533. return nil, err
  534. } else if !has {
  535. return nil, ErrMilestoneNotExist
  536. }
  537. return m, nil
  538. }
  539. // GetMilestones returns a list of milestones of given repository and status.
  540. func GetMilestones(repoId int64, isClosed bool) ([]*Milestone, error) {
  541. miles := make([]*Milestone, 0, 10)
  542. err := orm.Where("repo_id=?", repoId).And("is_closed=?", isClosed).Find(&miles)
  543. return miles, err
  544. }
  545. // UpdateMilestone updates information of given milestone.
  546. func UpdateMilestone(m *Milestone) error {
  547. _, err := orm.Id(m.Id).Update(m)
  548. return err
  549. }
  550. // ChangeMilestoneStatus changes the milestone open/closed status.
  551. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  552. repo, err := GetRepositoryById(m.RepoId)
  553. if err != nil {
  554. return err
  555. }
  556. sess := orm.NewSession()
  557. defer sess.Close()
  558. if err = sess.Begin(); err != nil {
  559. return err
  560. }
  561. m.IsClosed = isClosed
  562. if _, err = sess.Id(m.Id).AllCols().Update(m); err != nil {
  563. sess.Rollback()
  564. return err
  565. }
  566. if isClosed {
  567. repo.NumClosedMilestones++
  568. } else {
  569. repo.NumClosedMilestones--
  570. }
  571. if _, err = sess.Id(repo.Id).Update(repo); err != nil {
  572. sess.Rollback()
  573. return err
  574. }
  575. return sess.Commit()
  576. }
  577. // ChangeMilestoneAssign changes assignment of milestone for issue.
  578. func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) {
  579. sess := orm.NewSession()
  580. defer sess.Close()
  581. if err = sess.Begin(); err != nil {
  582. return err
  583. }
  584. if oldMid > 0 {
  585. m, err := GetMilestoneById(oldMid)
  586. if err != nil {
  587. return err
  588. }
  589. m.NumIssues--
  590. if issue.IsClosed {
  591. m.NumClosedIssues--
  592. }
  593. if m.NumIssues > 0 {
  594. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  595. } else {
  596. m.Completeness = 0
  597. }
  598. if _, err = sess.Id(m.Id).Update(m); err != nil {
  599. sess.Rollback()
  600. return err
  601. }
  602. rawSql := "UPDATE `issue_user` SET milestone_id = 0 WHERE issue_id = ?"
  603. if _, err = sess.Exec(rawSql, issue.Id); err != nil {
  604. sess.Rollback()
  605. return err
  606. }
  607. }
  608. if mid > 0 {
  609. m, err := GetMilestoneById(mid)
  610. if err != nil {
  611. return err
  612. }
  613. m.NumIssues++
  614. if issue.IsClosed {
  615. m.NumClosedIssues++
  616. }
  617. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  618. if _, err = sess.Id(m.Id).Update(m); err != nil {
  619. sess.Rollback()
  620. return err
  621. }
  622. rawSql := "UPDATE `issue_user` SET milestone_id = ? WHERE issue_id = ?"
  623. if _, err = sess.Exec(rawSql, m.Id, issue.Id); err != nil {
  624. sess.Rollback()
  625. return err
  626. }
  627. }
  628. return sess.Commit()
  629. }
  630. // DeleteMilestone deletes a milestone.
  631. func DeleteMilestone(m *Milestone) (err error) {
  632. sess := orm.NewSession()
  633. defer sess.Close()
  634. if err = sess.Begin(); err != nil {
  635. return err
  636. }
  637. if _, err = sess.Delete(m); err != nil {
  638. sess.Rollback()
  639. return err
  640. }
  641. rawSql := "UPDATE `repository` SET num_milestones = num_milestones - 1 WHERE id = ?"
  642. if _, err = sess.Exec(rawSql, m.RepoId); err != nil {
  643. sess.Rollback()
  644. return err
  645. }
  646. rawSql = "UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?"
  647. if _, err = sess.Exec(rawSql, m.Id); err != nil {
  648. sess.Rollback()
  649. return err
  650. }
  651. rawSql = "UPDATE `issue_user` SET milestone_id = 0 WHERE milestone_id = ?"
  652. if _, err = sess.Exec(rawSql, m.Id); err != nil {
  653. sess.Rollback()
  654. return err
  655. }
  656. return sess.Commit()
  657. }
  658. // _________ __
  659. // \_ ___ \ ____ _____ _____ ____ _____/ |_
  660. // / \ \/ / _ \ / \ / \_/ __ \ / \ __\
  661. // \ \___( <_> ) Y Y \ Y Y \ ___/| | \ |
  662. // \______ /\____/|__|_| /__|_| /\___ >___| /__|
  663. // \/ \/ \/ \/ \/
  664. // Issue types.
  665. const (
  666. IT_PLAIN = iota // Pure comment.
  667. IT_REOPEN // Issue reopen status change prompt.
  668. IT_CLOSE // Issue close status change prompt.
  669. )
  670. // Comment represents a comment in commit and issue page.
  671. type Comment struct {
  672. Id int64
  673. Type int
  674. PosterId int64
  675. Poster *User `xorm:"-"`
  676. IssueId int64
  677. CommitId int64
  678. Line int64
  679. Content string
  680. Created time.Time `xorm:"CREATED"`
  681. }
  682. // CreateComment creates comment of issue or commit.
  683. func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, content string) error {
  684. sess := orm.NewSession()
  685. defer sess.Close()
  686. if err := sess.Begin(); err != nil {
  687. return err
  688. }
  689. if _, err := sess.Insert(&Comment{PosterId: userId, Type: cmtType, IssueId: issueId,
  690. CommitId: commitId, Line: line, Content: content}); err != nil {
  691. sess.Rollback()
  692. return err
  693. }
  694. // Check comment type.
  695. switch cmtType {
  696. case IT_PLAIN:
  697. rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
  698. if _, err := sess.Exec(rawSql, issueId); err != nil {
  699. sess.Rollback()
  700. return err
  701. }
  702. case IT_REOPEN:
  703. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?"
  704. if _, err := sess.Exec(rawSql, repoId); err != nil {
  705. sess.Rollback()
  706. return err
  707. }
  708. case IT_CLOSE:
  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. }
  715. return sess.Commit()
  716. }
  717. // GetIssueComments returns list of comment by given issue id.
  718. func GetIssueComments(issueId int64) ([]Comment, error) {
  719. comments := make([]Comment, 0, 10)
  720. err := orm.Asc("created").Find(&comments, &Comment{IssueId: issueId})
  721. return comments, err
  722. }