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.

issue.go 28KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. "html/template"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/Unknwon/com"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. var (
  18. ErrIssueNotExist = errors.New("Issue does not exist")
  19. ErrLabelNotExist = errors.New("Label does not exist")
  20. ErrMilestoneNotExist = errors.New("Milestone does not exist")
  21. ErrWrongIssueCounter = errors.New("Invalid number of issues for this milestone")
  22. ErrAttachmentNotExist = errors.New("Attachment does not exist")
  23. ErrAttachmentNotLinked = errors.New("Attachment does not belong to this issue")
  24. ErrMissingIssueNumber = errors.New("No issue number specified")
  25. )
  26. // Issue represents an issue or pull request of repository.
  27. type Issue struct {
  28. ID int64 `xorm:"pk autoincr"`
  29. RepoId int64 `xorm:"INDEX"`
  30. Index int64 // Index in one repository.
  31. Name string
  32. Repo *Repository `xorm:"-"`
  33. PosterId int64
  34. Poster *User `xorm:"-"`
  35. LabelIds string `xorm:"TEXT"`
  36. Labels []*Label `xorm:"-"`
  37. MilestoneId int64
  38. AssigneeId int64
  39. Assignee *User `xorm:"-"`
  40. IsRead bool `xorm:"-"`
  41. IsPull bool // Indicates whether is a pull request or not.
  42. IsClosed bool
  43. Content string `xorm:"TEXT"`
  44. RenderedContent string `xorm:"-"`
  45. Priority int
  46. NumComments int
  47. Deadline time.Time
  48. Created time.Time `xorm:"CREATED"`
  49. Updated time.Time `xorm:"UPDATED"`
  50. }
  51. func (i *Issue) GetPoster() (err error) {
  52. i.Poster, err = GetUserById(i.PosterId)
  53. if IsErrUserNotExist(err) {
  54. i.Poster = &User{Name: "FakeUser"}
  55. return nil
  56. }
  57. return err
  58. }
  59. func (i *Issue) GetLabels() error {
  60. if len(i.LabelIds) < 3 {
  61. return nil
  62. }
  63. strIds := strings.Split(strings.TrimSuffix(i.LabelIds[1:], "|"), "|$")
  64. i.Labels = make([]*Label, 0, len(strIds))
  65. for _, strId := range strIds {
  66. id := com.StrTo(strId).MustInt64()
  67. if id > 0 {
  68. l, err := GetLabelById(id)
  69. if err != nil {
  70. if err == ErrLabelNotExist {
  71. continue
  72. }
  73. return err
  74. }
  75. i.Labels = append(i.Labels, l)
  76. }
  77. }
  78. return nil
  79. }
  80. func (i *Issue) GetAssignee() (err error) {
  81. if i.AssigneeId == 0 {
  82. return nil
  83. }
  84. i.Assignee, err = GetUserById(i.AssigneeId)
  85. if IsErrUserNotExist(err) {
  86. return nil
  87. }
  88. return err
  89. }
  90. func (i *Issue) Attachments() []*Attachment {
  91. a, _ := GetAttachmentsForIssue(i.ID)
  92. return a
  93. }
  94. func (i *Issue) AfterDelete() {
  95. _, err := DeleteAttachmentsByIssue(i.ID, true)
  96. if err != nil {
  97. log.Info("Could not delete files for issue #%d: %s", i.ID, err)
  98. }
  99. }
  100. // CreateIssue creates new issue for repository.
  101. func NewIssue(issue *Issue) (err error) {
  102. sess := x.NewSession()
  103. defer sessionRelease(sess)
  104. if err = sess.Begin(); err != nil {
  105. return err
  106. }
  107. if _, err = sess.Insert(issue); err != nil {
  108. return err
  109. } else if _, err = sess.Exec("UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?", issue.RepoId); err != nil {
  110. return err
  111. }
  112. if err = sess.Commit(); err != nil {
  113. return err
  114. }
  115. if issue.MilestoneId > 0 {
  116. // FIXES(280): Update milestone counter.
  117. return ChangeMilestoneAssign(0, issue.MilestoneId, issue)
  118. }
  119. return
  120. }
  121. // GetIssueByRef returns an Issue specified by a GFM reference.
  122. // See https://help.github.com/articles/writing-on-github#references for more information on the syntax.
  123. func GetIssueByRef(ref string) (issue *Issue, err error) {
  124. var issueNumber int64
  125. var repo *Repository
  126. n := strings.IndexByte(ref, byte('#'))
  127. if n == -1 {
  128. return nil, ErrMissingIssueNumber
  129. }
  130. if issueNumber, err = strconv.ParseInt(ref[n+1:], 10, 64); err != nil {
  131. return
  132. }
  133. if repo, err = GetRepositoryByRef(ref[:n]); err != nil {
  134. return
  135. }
  136. return GetIssueByIndex(repo.Id, issueNumber)
  137. }
  138. // GetIssueByIndex returns issue by given index in repository.
  139. func GetIssueByIndex(rid, index int64) (*Issue, error) {
  140. issue := &Issue{RepoId: rid, Index: index}
  141. has, err := x.Get(issue)
  142. if err != nil {
  143. return nil, err
  144. } else if !has {
  145. return nil, ErrIssueNotExist
  146. }
  147. return issue, nil
  148. }
  149. // GetIssueById returns an issue by ID.
  150. func GetIssueById(id int64) (*Issue, error) {
  151. issue := &Issue{ID: id}
  152. has, err := x.Get(issue)
  153. if err != nil {
  154. return nil, err
  155. } else if !has {
  156. return nil, ErrIssueNotExist
  157. }
  158. return issue, nil
  159. }
  160. // GetIssues returns a list of issues by given conditions.
  161. func GetIssues(uid, assigneeID, repoID, posterID, milestoneID int64, page int, isClosed, isMention bool, labelIds, sortType string) ([]Issue, error) {
  162. sess := x.Limit(setting.IssuePagingNum, (page-1)*setting.IssuePagingNum)
  163. if repoID > 0 {
  164. sess.Where("issue.repo_id=?", repoID).And("issue.is_closed=?", isClosed)
  165. } else {
  166. sess.Where("issue.is_closed=?", isClosed)
  167. }
  168. if assigneeID > 0 {
  169. sess.And("issue.assignee_id=?", assigneeID)
  170. } else if posterID > 0 {
  171. sess.And("issue.poster_id=?", posterID)
  172. }
  173. if milestoneID > 0 {
  174. sess.And("issue.milestone_id=?", milestoneID)
  175. }
  176. if len(labelIds) > 0 {
  177. for _, label := range strings.Split(labelIds, ",") {
  178. if com.StrTo(label).MustInt() > 0 {
  179. sess.And("label_ids like ?", "%$"+label+"|%")
  180. }
  181. }
  182. }
  183. switch sortType {
  184. case "oldest":
  185. sess.Asc("created")
  186. case "recentupdate":
  187. sess.Desc("updated")
  188. case "leastupdate":
  189. sess.Asc("updated")
  190. case "mostcomment":
  191. sess.Desc("num_comments")
  192. case "leastcomment":
  193. sess.Asc("num_comments")
  194. case "priority":
  195. sess.Desc("priority")
  196. default:
  197. sess.Desc("created")
  198. }
  199. if isMention {
  200. queryStr := "issue.id = issue_user.issue_id AND issue_user.is_mentioned=1"
  201. if uid > 0 {
  202. queryStr += " AND issue_user.uid = " + com.ToStr(uid)
  203. }
  204. sess.Join("INNER", "issue_user", queryStr)
  205. }
  206. var issues []Issue
  207. return issues, sess.Find(&issues)
  208. }
  209. type IssueStatus int
  210. const (
  211. IS_OPEN = iota + 1
  212. IS_CLOSE
  213. )
  214. // GetIssuesByLabel returns a list of issues by given label and repository.
  215. func GetIssuesByLabel(repoID, labelID int64) ([]*Issue, error) {
  216. issues := make([]*Issue, 0, 10)
  217. return issues, x.Where("repo_id=?", repoID).And("label_ids like '%$" + com.ToStr(labelID) + "|%'").Find(&issues)
  218. }
  219. // GetIssueCountByPoster returns number of issues of repository by poster.
  220. func GetIssueCountByPoster(uid, rid int64, isClosed bool) int64 {
  221. count, _ := x.Where("repo_id=?", rid).And("poster_id=?", uid).And("is_closed=?", isClosed).Count(new(Issue))
  222. return count
  223. }
  224. // .___ ____ ___
  225. // | | ______ ________ __ ____ | | \______ ___________
  226. // | |/ ___// ___/ | \_/ __ \| | / ___// __ \_ __ \
  227. // | |\___ \ \___ \| | /\ ___/| | /\___ \\ ___/| | \/
  228. // |___/____ >____ >____/ \___ >______//____ >\___ >__|
  229. // \/ \/ \/ \/ \/
  230. // IssueUser represents an issue-user relation.
  231. type IssueUser struct {
  232. Id int64
  233. Uid int64 `xorm:"INDEX"` // User ID.
  234. IssueId int64
  235. RepoId int64 `xorm:"INDEX"`
  236. MilestoneId int64
  237. IsRead bool
  238. IsAssigned bool
  239. IsMentioned bool
  240. IsPoster bool
  241. IsClosed bool
  242. }
  243. // FIXME: organization
  244. // NewIssueUserPairs adds new issue-user pairs for new issue of repository.
  245. func NewIssueUserPairs(repo *Repository, issueID, orgID, posterID, assigneeID int64) error {
  246. users, err := repo.GetCollaborators()
  247. if err != nil {
  248. return err
  249. }
  250. iu := &IssueUser{
  251. IssueId: issueID,
  252. RepoId: repo.Id,
  253. }
  254. isNeedAddPoster := true
  255. for _, u := range users {
  256. iu.Id = 0
  257. iu.Uid = u.Id
  258. iu.IsPoster = iu.Uid == posterID
  259. if isNeedAddPoster && iu.IsPoster {
  260. isNeedAddPoster = false
  261. }
  262. iu.IsAssigned = iu.Uid == assigneeID
  263. if _, err = x.Insert(iu); err != nil {
  264. return err
  265. }
  266. }
  267. if isNeedAddPoster {
  268. iu.Id = 0
  269. iu.Uid = posterID
  270. iu.IsPoster = true
  271. iu.IsAssigned = iu.Uid == assigneeID
  272. if _, err = x.Insert(iu); err != nil {
  273. return err
  274. }
  275. }
  276. // Add owner's as well.
  277. if repo.OwnerId != posterID {
  278. iu.Id = 0
  279. iu.Uid = repo.OwnerId
  280. iu.IsAssigned = iu.Uid == assigneeID
  281. if _, err = x.Insert(iu); err != nil {
  282. return err
  283. }
  284. }
  285. return nil
  286. }
  287. // PairsContains returns true when pairs list contains given issue.
  288. func PairsContains(ius []*IssueUser, issueId, uid int64) int {
  289. for i := range ius {
  290. if ius[i].IssueId == issueId &&
  291. ius[i].Uid == uid {
  292. return i
  293. }
  294. }
  295. return -1
  296. }
  297. // GetIssueUserPairs returns issue-user pairs by given repository and user.
  298. func GetIssueUserPairs(rid, uid int64, isClosed bool) ([]*IssueUser, error) {
  299. ius := make([]*IssueUser, 0, 10)
  300. err := x.Where("is_closed=?", isClosed).Find(&ius, &IssueUser{RepoId: rid, Uid: uid})
  301. return ius, err
  302. }
  303. // GetIssueUserPairsByRepoIds returns issue-user pairs by given repository IDs.
  304. func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*IssueUser, error) {
  305. if len(rids) == 0 {
  306. return []*IssueUser{}, nil
  307. }
  308. buf := bytes.NewBufferString("")
  309. for _, rid := range rids {
  310. buf.WriteString("repo_id=")
  311. buf.WriteString(com.ToStr(rid))
  312. buf.WriteString(" OR ")
  313. }
  314. cond := strings.TrimSuffix(buf.String(), " OR ")
  315. ius := make([]*IssueUser, 0, 10)
  316. sess := x.Limit(20, (page-1)*20).Where("is_closed=?", isClosed)
  317. if len(cond) > 0 {
  318. sess.And(cond)
  319. }
  320. err := sess.Find(&ius)
  321. return ius, err
  322. }
  323. // GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
  324. func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
  325. ius := make([]*IssueUser, 0, 10)
  326. sess := x.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
  327. if rid > 0 {
  328. sess.And("repo_id=?", rid)
  329. }
  330. switch filterMode {
  331. case FM_ASSIGN:
  332. sess.And("is_assigned=?", true)
  333. case FM_CREATE:
  334. sess.And("is_poster=?", true)
  335. default:
  336. return ius, nil
  337. }
  338. err := sess.Find(&ius)
  339. return ius, err
  340. }
  341. // IssueStats represents issue statistic information.
  342. type IssueStats struct {
  343. OpenCount, ClosedCount int64
  344. AllCount int64
  345. AssignCount int64
  346. CreateCount int64
  347. MentionCount int64
  348. }
  349. // Filter modes.
  350. const (
  351. FM_ALL = iota
  352. FM_ASSIGN
  353. FM_CREATE
  354. FM_MENTION
  355. )
  356. // GetIssueStats returns issue statistic information by given conditions.
  357. func GetIssueStats(repoID, uid, labelID int64, isShowClosed bool, filterMode int) *IssueStats {
  358. stats := &IssueStats{}
  359. issue := new(Issue)
  360. queryStr := "issue.repo_id=? AND issue.is_closed=?"
  361. if labelID > 0 {
  362. queryStr += " AND issue.label_ids like '%$" + com.ToStr(labelID) + "|%'"
  363. }
  364. switch filterMode {
  365. case FM_ALL:
  366. stats.OpenCount, _ = x.Where(queryStr, repoID, false).Count(issue)
  367. stats.ClosedCount, _ = x.Where(queryStr, repoID, true).Count(issue)
  368. return stats
  369. case FM_ASSIGN:
  370. queryStr += " AND assignee_id=?"
  371. stats.OpenCount, _ = x.Where(queryStr, repoID, false, uid).Count(issue)
  372. stats.ClosedCount, _ = x.Where(queryStr, repoID, true, uid).Count(issue)
  373. return stats
  374. case FM_CREATE:
  375. queryStr += " AND poster_id=?"
  376. stats.OpenCount, _ = x.Where(queryStr, repoID, false, uid).Count(issue)
  377. stats.ClosedCount, _ = x.Where(queryStr, repoID, true, uid).Count(issue)
  378. return stats
  379. case FM_MENTION:
  380. queryStr += " AND uid=? AND is_mentioned=?"
  381. if labelID > 0 {
  382. stats.OpenCount, _ = x.Where(queryStr, repoID, false, uid, true).
  383. Join("INNER", "issue", "issue.id = issue_id").Count(new(IssueUser))
  384. stats.ClosedCount, _ = x.Where(queryStr, repoID, true, uid, true).
  385. Join("INNER", "issue", "issue.id = issue_id").Count(new(IssueUser))
  386. return stats
  387. }
  388. queryStr = strings.Replace(queryStr, "issue.", "", 2)
  389. stats.OpenCount, _ = x.Where(queryStr, repoID, false, uid, true).Count(new(IssueUser))
  390. stats.ClosedCount, _ = x.Where(queryStr, repoID, true, uid, true).Count(new(IssueUser))
  391. return stats
  392. }
  393. return stats
  394. }
  395. // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
  396. func GetUserIssueStats(uid int64, filterMode int) *IssueStats {
  397. stats := &IssueStats{}
  398. issue := new(Issue)
  399. stats.AssignCount, _ = x.Where("assignee_id=?", uid).And("is_closed=?", false).Count(issue)
  400. stats.CreateCount, _ = x.Where("poster_id=?", uid).And("is_closed=?", false).Count(issue)
  401. return stats
  402. }
  403. // UpdateIssue updates information of issue.
  404. func UpdateIssue(issue *Issue) error {
  405. _, err := x.Id(issue.ID).AllCols().Update(issue)
  406. if err != nil {
  407. return err
  408. }
  409. return err
  410. }
  411. // UpdateIssueUserByStatus updates issue-user pairs by issue status.
  412. func UpdateIssueUserPairsByStatus(iid int64, isClosed bool) error {
  413. rawSql := "UPDATE `issue_user` SET is_closed = ? WHERE issue_id = ?"
  414. _, err := x.Exec(rawSql, isClosed, iid)
  415. return err
  416. }
  417. // UpdateIssueUserPairByAssignee updates issue-user pair for assigning.
  418. func UpdateIssueUserPairByAssignee(aid, iid int64) error {
  419. rawSql := "UPDATE `issue_user` SET is_assigned = ? WHERE issue_id = ?"
  420. if _, err := x.Exec(rawSql, false, iid); err != nil {
  421. return err
  422. }
  423. // Assignee ID equals to 0 means clear assignee.
  424. if aid == 0 {
  425. return nil
  426. }
  427. rawSql = "UPDATE `issue_user` SET is_assigned = ? WHERE uid = ? AND issue_id = ?"
  428. _, err := x.Exec(rawSql, true, aid, iid)
  429. return err
  430. }
  431. // UpdateIssueUserPairByRead updates issue-user pair for reading.
  432. func UpdateIssueUserPairByRead(uid, iid int64) error {
  433. rawSql := "UPDATE `issue_user` SET is_read = ? WHERE uid = ? AND issue_id = ?"
  434. _, err := x.Exec(rawSql, true, uid, iid)
  435. return err
  436. }
  437. // UpdateIssueUserPairsByMentions updates issue-user pairs by mentioning.
  438. func UpdateIssueUserPairsByMentions(uids []int64, iid int64) error {
  439. for _, uid := range uids {
  440. iu := &IssueUser{Uid: uid, IssueId: iid}
  441. has, err := x.Get(iu)
  442. if err != nil {
  443. return err
  444. }
  445. iu.IsMentioned = true
  446. if has {
  447. _, err = x.Id(iu.Id).AllCols().Update(iu)
  448. } else {
  449. _, err = x.Insert(iu)
  450. }
  451. if err != nil {
  452. return err
  453. }
  454. }
  455. return nil
  456. }
  457. // .____ ___. .__
  458. // | | _____ \_ |__ ____ | |
  459. // | | \__ \ | __ \_/ __ \| |
  460. // | |___ / __ \| \_\ \ ___/| |__
  461. // |_______ (____ /___ /\___ >____/
  462. // \/ \/ \/ \/
  463. // Label represents a label of repository for issues.
  464. type Label struct {
  465. ID int64 `xorm:"pk autoincr"`
  466. RepoId int64 `xorm:"INDEX"`
  467. Name string
  468. Color string `xorm:"VARCHAR(7)"`
  469. NumIssues int
  470. NumClosedIssues int
  471. NumOpenIssues int `xorm:"-"`
  472. IsChecked bool `xorm:"-"`
  473. }
  474. // CalOpenIssues calculates the open issues of label.
  475. func (m *Label) CalOpenIssues() {
  476. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  477. }
  478. // NewLabel creates new label of repository.
  479. func NewLabel(l *Label) error {
  480. _, err := x.Insert(l)
  481. return err
  482. }
  483. // GetLabelById returns a label by given ID.
  484. func GetLabelById(id int64) (*Label, error) {
  485. if id <= 0 {
  486. return nil, ErrLabelNotExist
  487. }
  488. l := &Label{ID: id}
  489. has, err := x.Get(l)
  490. if err != nil {
  491. return nil, err
  492. } else if !has {
  493. return nil, ErrLabelNotExist
  494. }
  495. return l, nil
  496. }
  497. // GetLabels returns a list of labels of given repository ID.
  498. func GetLabels(repoId int64) ([]*Label, error) {
  499. labels := make([]*Label, 0, 10)
  500. err := x.Where("repo_id=?", repoId).Find(&labels)
  501. return labels, err
  502. }
  503. // UpdateLabel updates label information.
  504. func UpdateLabel(l *Label) error {
  505. _, err := x.Id(l.ID).AllCols().Update(l)
  506. return err
  507. }
  508. // DeleteLabel delete a label of given repository.
  509. func DeleteLabel(repoID, labelID int64) error {
  510. l, err := GetLabelById(labelID)
  511. if err != nil {
  512. if err == ErrLabelNotExist {
  513. return nil
  514. }
  515. return err
  516. }
  517. issues, err := GetIssuesByLabel(repoID, labelID)
  518. if err != nil {
  519. return err
  520. }
  521. sess := x.NewSession()
  522. defer sessionRelease(sess)
  523. if err = sess.Begin(); err != nil {
  524. return err
  525. }
  526. for _, issue := range issues {
  527. issue.LabelIds = strings.Replace(issue.LabelIds, "$"+com.ToStr(labelID)+"|", "", -1)
  528. if _, err = sess.Id(issue.ID).AllCols().Update(issue); err != nil {
  529. return err
  530. }
  531. }
  532. if _, err = sess.Delete(l); err != nil {
  533. return err
  534. }
  535. return sess.Commit()
  536. }
  537. // _____ .__.__ __
  538. // / \ |__| | ____ _______/ |_ ____ ____ ____
  539. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  540. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  541. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  542. // \/ \/ \/ \/ \/
  543. // Milestone represents a milestone of repository.
  544. type Milestone struct {
  545. Id int64
  546. RepoId int64 `xorm:"INDEX"`
  547. Index int64
  548. Name string
  549. Content string `xorm:"TEXT"`
  550. RenderedContent string `xorm:"-"`
  551. IsClosed bool
  552. NumIssues int
  553. NumClosedIssues int
  554. NumOpenIssues int `xorm:"-"`
  555. Completeness int // Percentage(1-100).
  556. Deadline time.Time
  557. DeadlineString string `xorm:"-"`
  558. ClosedDate time.Time
  559. }
  560. // CalOpenIssues calculates the open issues of milestone.
  561. func (m *Milestone) CalOpenIssues() {
  562. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  563. }
  564. // NewMilestone creates new milestone of repository.
  565. func NewMilestone(m *Milestone) (err error) {
  566. sess := x.NewSession()
  567. defer sess.Close()
  568. if err = sess.Begin(); err != nil {
  569. return err
  570. }
  571. if _, err = sess.Insert(m); err != nil {
  572. sess.Rollback()
  573. return err
  574. }
  575. rawSql := "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?"
  576. if _, err = sess.Exec(rawSql, m.RepoId); err != nil {
  577. sess.Rollback()
  578. return err
  579. }
  580. return sess.Commit()
  581. }
  582. // GetMilestoneById returns the milestone by given ID.
  583. func GetMilestoneById(id int64) (*Milestone, error) {
  584. m := &Milestone{Id: id}
  585. has, err := x.Get(m)
  586. if err != nil {
  587. return nil, err
  588. } else if !has {
  589. return nil, ErrMilestoneNotExist
  590. }
  591. return m, nil
  592. }
  593. // GetMilestoneByIndex returns the milestone of given repository and index.
  594. func GetMilestoneByIndex(repoId, idx int64) (*Milestone, error) {
  595. m := &Milestone{RepoId: repoId, Index: idx}
  596. has, err := x.Get(m)
  597. if err != nil {
  598. return nil, err
  599. } else if !has {
  600. return nil, ErrMilestoneNotExist
  601. }
  602. return m, nil
  603. }
  604. // GetMilestones returns a list of milestones of given repository and status.
  605. func GetMilestones(repoId int64, isClosed bool) ([]*Milestone, error) {
  606. miles := make([]*Milestone, 0, 10)
  607. err := x.Where("repo_id=?", repoId).And("is_closed=?", isClosed).Find(&miles)
  608. return miles, err
  609. }
  610. // UpdateMilestone updates information of given milestone.
  611. func UpdateMilestone(m *Milestone) error {
  612. _, err := x.Id(m.Id).Update(m)
  613. return err
  614. }
  615. // ChangeMilestoneStatus changes the milestone open/closed status.
  616. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  617. repo, err := GetRepositoryById(m.RepoId)
  618. if err != nil {
  619. return err
  620. }
  621. sess := x.NewSession()
  622. defer sess.Close()
  623. if err = sess.Begin(); err != nil {
  624. return err
  625. }
  626. m.IsClosed = isClosed
  627. if _, err = sess.Id(m.Id).AllCols().Update(m); err != nil {
  628. sess.Rollback()
  629. return err
  630. }
  631. if isClosed {
  632. repo.NumClosedMilestones++
  633. } else {
  634. repo.NumClosedMilestones--
  635. }
  636. if _, err = sess.Id(repo.Id).Update(repo); err != nil {
  637. sess.Rollback()
  638. return err
  639. }
  640. return sess.Commit()
  641. }
  642. // ChangeMilestoneIssueStats updates the open/closed issues counter and progress for the
  643. // milestone associated witht the given issue.
  644. func ChangeMilestoneIssueStats(issue *Issue) error {
  645. if issue.MilestoneId == 0 {
  646. return nil
  647. }
  648. m, err := GetMilestoneById(issue.MilestoneId)
  649. if err != nil {
  650. return err
  651. }
  652. if issue.IsClosed {
  653. m.NumOpenIssues--
  654. m.NumClosedIssues++
  655. } else {
  656. m.NumOpenIssues++
  657. m.NumClosedIssues--
  658. }
  659. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  660. return UpdateMilestone(m)
  661. }
  662. // ChangeMilestoneAssign changes assignment of milestone for issue.
  663. func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) {
  664. sess := x.NewSession()
  665. defer sess.Close()
  666. if err = sess.Begin(); err != nil {
  667. return err
  668. }
  669. if oldMid > 0 {
  670. m, err := GetMilestoneById(oldMid)
  671. if err != nil {
  672. return err
  673. }
  674. m.NumIssues--
  675. if issue.IsClosed {
  676. m.NumClosedIssues--
  677. }
  678. if m.NumIssues > 0 {
  679. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  680. } else {
  681. m.Completeness = 0
  682. }
  683. if _, err = sess.Id(m.Id).Cols("num_issues,num_completeness,num_closed_issues").Update(m); err != nil {
  684. sess.Rollback()
  685. return err
  686. }
  687. rawSql := "UPDATE `issue_user` SET milestone_id = 0 WHERE issue_id = ?"
  688. if _, err = sess.Exec(rawSql, issue.ID); err != nil {
  689. sess.Rollback()
  690. return err
  691. }
  692. }
  693. if mid > 0 {
  694. m, err := GetMilestoneById(mid)
  695. if err != nil {
  696. return err
  697. }
  698. m.NumIssues++
  699. if issue.IsClosed {
  700. m.NumClosedIssues++
  701. }
  702. if m.NumIssues == 0 {
  703. return ErrWrongIssueCounter
  704. }
  705. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  706. if _, err = sess.Id(m.Id).Cols("num_issues,num_completeness,num_closed_issues").Update(m); err != nil {
  707. sess.Rollback()
  708. return err
  709. }
  710. rawSql := "UPDATE `issue_user` SET milestone_id = ? WHERE issue_id = ?"
  711. if _, err = sess.Exec(rawSql, m.Id, issue.ID); err != nil {
  712. sess.Rollback()
  713. return err
  714. }
  715. }
  716. return sess.Commit()
  717. }
  718. // DeleteMilestone deletes a milestone.
  719. func DeleteMilestone(m *Milestone) (err error) {
  720. sess := x.NewSession()
  721. defer sess.Close()
  722. if err = sess.Begin(); err != nil {
  723. return err
  724. }
  725. if _, err = sess.Delete(m); err != nil {
  726. sess.Rollback()
  727. return err
  728. }
  729. rawSql := "UPDATE `repository` SET num_milestones = num_milestones - 1 WHERE id = ?"
  730. if _, err = sess.Exec(rawSql, m.RepoId); err != nil {
  731. sess.Rollback()
  732. return err
  733. }
  734. rawSql = "UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?"
  735. if _, err = sess.Exec(rawSql, m.Id); err != nil {
  736. sess.Rollback()
  737. return err
  738. }
  739. rawSql = "UPDATE `issue_user` SET milestone_id = 0 WHERE milestone_id = ?"
  740. if _, err = sess.Exec(rawSql, m.Id); err != nil {
  741. sess.Rollback()
  742. return err
  743. }
  744. return sess.Commit()
  745. }
  746. // _________ __
  747. // \_ ___ \ ____ _____ _____ ____ _____/ |_
  748. // / \ \/ / _ \ / \ / \_/ __ \ / \ __\
  749. // \ \___( <_> ) Y Y \ Y Y \ ___/| | \ |
  750. // \______ /\____/|__|_| /__|_| /\___ >___| /__|
  751. // \/ \/ \/ \/ \/
  752. // CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
  753. type CommentType int
  754. const (
  755. // Plain comment, can be associated with a commit (CommitId > 0) and a line (Line > 0)
  756. COMMENT_TYPE_COMMENT CommentType = iota
  757. COMMENT_TYPE_REOPEN
  758. COMMENT_TYPE_CLOSE
  759. // References.
  760. COMMENT_TYPE_ISSUE
  761. // Reference from some commit (not part of a pull request)
  762. COMMENT_TYPE_COMMIT
  763. // Reference from some pull request
  764. COMMENT_TYPE_PULL
  765. )
  766. // Comment represents a comment in commit and issue page.
  767. type Comment struct {
  768. Id int64
  769. Type CommentType
  770. PosterId int64
  771. Poster *User `xorm:"-"`
  772. IssueId int64
  773. CommitId int64
  774. Line int64
  775. Content string `xorm:"TEXT"`
  776. Created time.Time `xorm:"CREATED"`
  777. }
  778. // CreateComment creates comment of issue or commit.
  779. func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType CommentType, content string, attachments []int64) (*Comment, error) {
  780. sess := x.NewSession()
  781. defer sessionRelease(sess)
  782. if err := sess.Begin(); err != nil {
  783. return nil, err
  784. }
  785. comment := &Comment{PosterId: userId, Type: cmtType, IssueId: issueId,
  786. CommitId: commitId, Line: line, Content: content}
  787. if _, err := sess.Insert(comment); err != nil {
  788. return nil, err
  789. }
  790. // Check comment type.
  791. switch cmtType {
  792. case COMMENT_TYPE_COMMENT:
  793. rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
  794. if _, err := sess.Exec(rawSql, issueId); err != nil {
  795. return nil, err
  796. }
  797. if len(attachments) > 0 {
  798. rawSql = "UPDATE `attachment` SET comment_id = ? WHERE id IN (?)"
  799. astrs := make([]string, 0, len(attachments))
  800. for _, a := range attachments {
  801. astrs = append(astrs, strconv.FormatInt(a, 10))
  802. }
  803. if _, err := sess.Exec(rawSql, comment.Id, strings.Join(astrs, ",")); err != nil {
  804. return nil, err
  805. }
  806. }
  807. case COMMENT_TYPE_REOPEN:
  808. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?"
  809. if _, err := sess.Exec(rawSql, repoId); err != nil {
  810. return nil, err
  811. }
  812. case COMMENT_TYPE_CLOSE:
  813. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?"
  814. if _, err := sess.Exec(rawSql, repoId); err != nil {
  815. return nil, err
  816. }
  817. }
  818. return comment, sess.Commit()
  819. }
  820. // GetCommentById returns the comment with the given id
  821. func GetCommentById(commentId int64) (*Comment, error) {
  822. c := &Comment{Id: commentId}
  823. _, err := x.Get(c)
  824. return c, err
  825. }
  826. func (c *Comment) ContentHtml() template.HTML {
  827. return template.HTML(c.Content)
  828. }
  829. // GetIssueComments returns list of comment by given issue id.
  830. func GetIssueComments(issueId int64) ([]Comment, error) {
  831. comments := make([]Comment, 0, 10)
  832. err := x.Asc("created").Find(&comments, &Comment{IssueId: issueId})
  833. return comments, err
  834. }
  835. // Attachments returns the attachments for this comment.
  836. func (c *Comment) Attachments() []*Attachment {
  837. a, _ := GetAttachmentsByComment(c.Id)
  838. return a
  839. }
  840. func (c *Comment) AfterDelete() {
  841. _, err := DeleteAttachmentsByComment(c.Id, true)
  842. if err != nil {
  843. log.Info("Could not delete files for comment %d on issue #%d: %s", c.Id, c.IssueId, err)
  844. }
  845. }
  846. type Attachment struct {
  847. Id int64
  848. IssueId int64
  849. CommentId int64
  850. Name string
  851. Path string `xorm:"TEXT"`
  852. Created time.Time `xorm:"CREATED"`
  853. }
  854. // CreateAttachment creates a new attachment inside the database and
  855. func CreateAttachment(issueId, commentId int64, name, path string) (*Attachment, error) {
  856. sess := x.NewSession()
  857. defer sess.Close()
  858. if err := sess.Begin(); err != nil {
  859. return nil, err
  860. }
  861. a := &Attachment{IssueId: issueId, CommentId: commentId, Name: name, Path: path}
  862. if _, err := sess.Insert(a); err != nil {
  863. sess.Rollback()
  864. return nil, err
  865. }
  866. return a, sess.Commit()
  867. }
  868. // Attachment returns the attachment by given ID.
  869. func GetAttachmentById(id int64) (*Attachment, error) {
  870. m := &Attachment{Id: id}
  871. has, err := x.Get(m)
  872. if err != nil {
  873. return nil, err
  874. }
  875. if !has {
  876. return nil, ErrAttachmentNotExist
  877. }
  878. return m, nil
  879. }
  880. func GetAttachmentsForIssue(issueId int64) ([]*Attachment, error) {
  881. attachments := make([]*Attachment, 0, 10)
  882. err := x.Where("issue_id = ?", issueId).And("comment_id = 0").Find(&attachments)
  883. return attachments, err
  884. }
  885. // GetAttachmentsByIssue returns a list of attachments for the given issue
  886. func GetAttachmentsByIssue(issueId int64) ([]*Attachment, error) {
  887. attachments := make([]*Attachment, 0, 10)
  888. err := x.Where("issue_id = ?", issueId).And("comment_id > 0").Find(&attachments)
  889. return attachments, err
  890. }
  891. // GetAttachmentsByComment returns a list of attachments for the given comment
  892. func GetAttachmentsByComment(commentId int64) ([]*Attachment, error) {
  893. attachments := make([]*Attachment, 0, 10)
  894. err := x.Where("comment_id = ?", commentId).Find(&attachments)
  895. return attachments, err
  896. }
  897. // DeleteAttachment deletes the given attachment and optionally the associated file.
  898. func DeleteAttachment(a *Attachment, remove bool) error {
  899. _, err := DeleteAttachments([]*Attachment{a}, remove)
  900. return err
  901. }
  902. // DeleteAttachments deletes the given attachments and optionally the associated files.
  903. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  904. for i, a := range attachments {
  905. if remove {
  906. if err := os.Remove(a.Path); err != nil {
  907. return i, err
  908. }
  909. }
  910. if _, err := x.Delete(a.Id); err != nil {
  911. return i, err
  912. }
  913. }
  914. return len(attachments), nil
  915. }
  916. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  917. func DeleteAttachmentsByIssue(issueId int64, remove bool) (int, error) {
  918. attachments, err := GetAttachmentsByIssue(issueId)
  919. if err != nil {
  920. return 0, err
  921. }
  922. return DeleteAttachments(attachments, remove)
  923. }
  924. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  925. func DeleteAttachmentsByComment(commentId int64, remove bool) (int, error) {
  926. attachments, err := GetAttachmentsByComment(commentId)
  927. if err != nil {
  928. return 0, err
  929. }
  930. return DeleteAttachments(attachments, remove)
  931. }