You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue.go 28 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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 err == ErrUserNotExist {
  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 err == ErrUserNotExist {
  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 := "repo_id=? AND is_closed=?"
  361. switch filterMode {
  362. case FM_ALL:
  363. stats.OpenCount, _ = x.Where(queryStr, repoID, false).Count(issue)
  364. stats.ClosedCount, _ = x.Where(queryStr, repoID, true).Count(issue)
  365. return stats
  366. case FM_ASSIGN:
  367. queryStr += " AND assignee_id=?"
  368. stats.OpenCount, _ = x.Where(queryStr, repoID, false, uid).Count(issue)
  369. stats.ClosedCount, _ = x.Where(queryStr, repoID, true, uid).Count(issue)
  370. return stats
  371. case FM_CREATE:
  372. queryStr += " AND poster_id=?"
  373. stats.OpenCount, _ = x.Where(queryStr, repoID, false, uid).Count(issue)
  374. stats.ClosedCount, _ = x.Where(queryStr, repoID, true, uid).Count(issue)
  375. return stats
  376. case FM_MENTION:
  377. queryStr += " AND uid=? AND is_mentioned=?"
  378. stats.OpenCount, _ = x.Where(queryStr, repoID, false, uid, true).Count(new(IssueUser))
  379. stats.ClosedCount, _ = x.Where(queryStr, repoID, true, uid, true).Count(new(IssueUser))
  380. return stats
  381. }
  382. return stats
  383. }
  384. // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
  385. func GetUserIssueStats(uid int64, filterMode int) *IssueStats {
  386. stats := &IssueStats{}
  387. issue := new(Issue)
  388. stats.AssignCount, _ = x.Where("assignee_id=?", uid).And("is_closed=?", false).Count(issue)
  389. stats.CreateCount, _ = x.Where("poster_id=?", uid).And("is_closed=?", false).Count(issue)
  390. return stats
  391. }
  392. // UpdateIssue updates information of issue.
  393. func UpdateIssue(issue *Issue) error {
  394. _, err := x.Id(issue.ID).AllCols().Update(issue)
  395. if err != nil {
  396. return err
  397. }
  398. return err
  399. }
  400. // UpdateIssueUserByStatus updates issue-user pairs by issue status.
  401. func UpdateIssueUserPairsByStatus(iid int64, isClosed bool) error {
  402. rawSql := "UPDATE `issue_user` SET is_closed = ? WHERE issue_id = ?"
  403. _, err := x.Exec(rawSql, isClosed, iid)
  404. return err
  405. }
  406. // UpdateIssueUserPairByAssignee updates issue-user pair for assigning.
  407. func UpdateIssueUserPairByAssignee(aid, iid int64) error {
  408. rawSql := "UPDATE `issue_user` SET is_assigned = ? WHERE issue_id = ?"
  409. if _, err := x.Exec(rawSql, false, iid); err != nil {
  410. return err
  411. }
  412. // Assignee ID equals to 0 means clear assignee.
  413. if aid == 0 {
  414. return nil
  415. }
  416. rawSql = "UPDATE `issue_user` SET is_assigned = ? WHERE uid = ? AND issue_id = ?"
  417. _, err := x.Exec(rawSql, true, aid, iid)
  418. return err
  419. }
  420. // UpdateIssueUserPairByRead updates issue-user pair for reading.
  421. func UpdateIssueUserPairByRead(uid, iid int64) error {
  422. rawSql := "UPDATE `issue_user` SET is_read = ? WHERE uid = ? AND issue_id = ?"
  423. _, err := x.Exec(rawSql, true, uid, iid)
  424. return err
  425. }
  426. // UpdateIssueUserPairsByMentions updates issue-user pairs by mentioning.
  427. func UpdateIssueUserPairsByMentions(uids []int64, iid int64) error {
  428. for _, uid := range uids {
  429. iu := &IssueUser{Uid: uid, IssueId: iid}
  430. has, err := x.Get(iu)
  431. if err != nil {
  432. return err
  433. }
  434. iu.IsMentioned = true
  435. if has {
  436. _, err = x.Id(iu.Id).AllCols().Update(iu)
  437. } else {
  438. _, err = x.Insert(iu)
  439. }
  440. if err != nil {
  441. return err
  442. }
  443. }
  444. return nil
  445. }
  446. // .____ ___. .__
  447. // | | _____ \_ |__ ____ | |
  448. // | | \__ \ | __ \_/ __ \| |
  449. // | |___ / __ \| \_\ \ ___/| |__
  450. // |_______ (____ /___ /\___ >____/
  451. // \/ \/ \/ \/
  452. // Label represents a label of repository for issues.
  453. type Label struct {
  454. ID int64 `xorm:"pk autoincr"`
  455. RepoId int64 `xorm:"INDEX"`
  456. Name string
  457. Color string `xorm:"VARCHAR(7)"`
  458. NumIssues int
  459. NumClosedIssues int
  460. NumOpenIssues int `xorm:"-"`
  461. IsChecked bool `xorm:"-"`
  462. }
  463. // CalOpenIssues calculates the open issues of label.
  464. func (m *Label) CalOpenIssues() {
  465. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  466. }
  467. // NewLabel creates new label of repository.
  468. func NewLabel(l *Label) error {
  469. _, err := x.Insert(l)
  470. return err
  471. }
  472. // GetLabelById returns a label by given ID.
  473. func GetLabelById(id int64) (*Label, error) {
  474. if id <= 0 {
  475. return nil, ErrLabelNotExist
  476. }
  477. l := &Label{ID: id}
  478. has, err := x.Get(l)
  479. if err != nil {
  480. return nil, err
  481. } else if !has {
  482. return nil, ErrLabelNotExist
  483. }
  484. return l, nil
  485. }
  486. // GetLabels returns a list of labels of given repository ID.
  487. func GetLabels(repoId int64) ([]*Label, error) {
  488. labels := make([]*Label, 0, 10)
  489. err := x.Where("repo_id=?", repoId).Find(&labels)
  490. return labels, err
  491. }
  492. // UpdateLabel updates label information.
  493. func UpdateLabel(l *Label) error {
  494. _, err := x.Id(l.ID).AllCols().Update(l)
  495. return err
  496. }
  497. // DeleteLabel delete a label of given repository.
  498. func DeleteLabel(repoID, labelID int64) error {
  499. l, err := GetLabelById(labelID)
  500. if err != nil {
  501. if err == ErrLabelNotExist {
  502. return nil
  503. }
  504. return err
  505. }
  506. issues, err := GetIssuesByLabel(repoID, labelID)
  507. if err != nil {
  508. return err
  509. }
  510. sess := x.NewSession()
  511. defer sessionRelease(sess)
  512. if err = sess.Begin(); err != nil {
  513. return err
  514. }
  515. for _, issue := range issues {
  516. issue.LabelIds = strings.Replace(issue.LabelIds, "$"+com.ToStr(labelID)+"|", "", -1)
  517. if _, err = sess.Id(issue.ID).AllCols().Update(issue); err != nil {
  518. return err
  519. }
  520. }
  521. if _, err = sess.Delete(l); err != nil {
  522. return err
  523. }
  524. return sess.Commit()
  525. }
  526. // _____ .__.__ __
  527. // / \ |__| | ____ _______/ |_ ____ ____ ____
  528. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  529. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  530. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  531. // \/ \/ \/ \/ \/
  532. // Milestone represents a milestone of repository.
  533. type Milestone struct {
  534. Id int64
  535. RepoId int64 `xorm:"INDEX"`
  536. Index int64
  537. Name string
  538. Content string `xorm:"TEXT"`
  539. RenderedContent string `xorm:"-"`
  540. IsClosed bool
  541. NumIssues int
  542. NumClosedIssues int
  543. NumOpenIssues int `xorm:"-"`
  544. Completeness int // Percentage(1-100).
  545. Deadline time.Time
  546. DeadlineString string `xorm:"-"`
  547. ClosedDate time.Time
  548. }
  549. // CalOpenIssues calculates the open issues of milestone.
  550. func (m *Milestone) CalOpenIssues() {
  551. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  552. }
  553. // NewMilestone creates new milestone of repository.
  554. func NewMilestone(m *Milestone) (err error) {
  555. sess := x.NewSession()
  556. defer sess.Close()
  557. if err = sess.Begin(); err != nil {
  558. return err
  559. }
  560. if _, err = sess.Insert(m); err != nil {
  561. sess.Rollback()
  562. return err
  563. }
  564. rawSql := "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?"
  565. if _, err = sess.Exec(rawSql, m.RepoId); err != nil {
  566. sess.Rollback()
  567. return err
  568. }
  569. return sess.Commit()
  570. }
  571. // GetMilestoneById returns the milestone by given ID.
  572. func GetMilestoneById(id int64) (*Milestone, error) {
  573. m := &Milestone{Id: id}
  574. has, err := x.Get(m)
  575. if err != nil {
  576. return nil, err
  577. } else if !has {
  578. return nil, ErrMilestoneNotExist
  579. }
  580. return m, nil
  581. }
  582. // GetMilestoneByIndex returns the milestone of given repository and index.
  583. func GetMilestoneByIndex(repoId, idx int64) (*Milestone, error) {
  584. m := &Milestone{RepoId: repoId, Index: idx}
  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. // GetMilestones returns a list of milestones of given repository and status.
  594. func GetMilestones(repoId int64, isClosed bool) ([]*Milestone, error) {
  595. miles := make([]*Milestone, 0, 10)
  596. err := x.Where("repo_id=?", repoId).And("is_closed=?", isClosed).Find(&miles)
  597. return miles, err
  598. }
  599. // UpdateMilestone updates information of given milestone.
  600. func UpdateMilestone(m *Milestone) error {
  601. _, err := x.Id(m.Id).Update(m)
  602. return err
  603. }
  604. // ChangeMilestoneStatus changes the milestone open/closed status.
  605. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  606. repo, err := GetRepositoryById(m.RepoId)
  607. if err != nil {
  608. return err
  609. }
  610. sess := x.NewSession()
  611. defer sess.Close()
  612. if err = sess.Begin(); err != nil {
  613. return err
  614. }
  615. m.IsClosed = isClosed
  616. if _, err = sess.Id(m.Id).AllCols().Update(m); err != nil {
  617. sess.Rollback()
  618. return err
  619. }
  620. if isClosed {
  621. repo.NumClosedMilestones++
  622. } else {
  623. repo.NumClosedMilestones--
  624. }
  625. if _, err = sess.Id(repo.Id).Update(repo); err != nil {
  626. sess.Rollback()
  627. return err
  628. }
  629. return sess.Commit()
  630. }
  631. // ChangeMilestoneIssueStats updates the open/closed issues counter and progress for the
  632. // milestone associated witht the given issue.
  633. func ChangeMilestoneIssueStats(issue *Issue) error {
  634. if issue.MilestoneId == 0 {
  635. return nil
  636. }
  637. m, err := GetMilestoneById(issue.MilestoneId)
  638. if err != nil {
  639. return err
  640. }
  641. if issue.IsClosed {
  642. m.NumOpenIssues--
  643. m.NumClosedIssues++
  644. } else {
  645. m.NumOpenIssues++
  646. m.NumClosedIssues--
  647. }
  648. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  649. return UpdateMilestone(m)
  650. }
  651. // ChangeMilestoneAssign changes assignment of milestone for issue.
  652. func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) {
  653. sess := x.NewSession()
  654. defer sess.Close()
  655. if err = sess.Begin(); err != nil {
  656. return err
  657. }
  658. if oldMid > 0 {
  659. m, err := GetMilestoneById(oldMid)
  660. if err != nil {
  661. return err
  662. }
  663. m.NumIssues--
  664. if issue.IsClosed {
  665. m.NumClosedIssues--
  666. }
  667. if m.NumIssues > 0 {
  668. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  669. } else {
  670. m.Completeness = 0
  671. }
  672. if _, err = sess.Id(m.Id).Cols("num_issues,num_completeness,num_closed_issues").Update(m); err != nil {
  673. sess.Rollback()
  674. return err
  675. }
  676. rawSql := "UPDATE `issue_user` SET milestone_id = 0 WHERE issue_id = ?"
  677. if _, err = sess.Exec(rawSql, issue.ID); err != nil {
  678. sess.Rollback()
  679. return err
  680. }
  681. }
  682. if mid > 0 {
  683. m, err := GetMilestoneById(mid)
  684. if err != nil {
  685. return err
  686. }
  687. m.NumIssues++
  688. if issue.IsClosed {
  689. m.NumClosedIssues++
  690. }
  691. if m.NumIssues == 0 {
  692. return ErrWrongIssueCounter
  693. }
  694. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  695. if _, err = sess.Id(m.Id).Cols("num_issues,num_completeness,num_closed_issues").Update(m); err != nil {
  696. sess.Rollback()
  697. return err
  698. }
  699. rawSql := "UPDATE `issue_user` SET milestone_id = ? WHERE issue_id = ?"
  700. if _, err = sess.Exec(rawSql, m.Id, issue.ID); err != nil {
  701. sess.Rollback()
  702. return err
  703. }
  704. }
  705. return sess.Commit()
  706. }
  707. // DeleteMilestone deletes a milestone.
  708. func DeleteMilestone(m *Milestone) (err error) {
  709. sess := x.NewSession()
  710. defer sess.Close()
  711. if err = sess.Begin(); err != nil {
  712. return err
  713. }
  714. if _, err = sess.Delete(m); err != nil {
  715. sess.Rollback()
  716. return err
  717. }
  718. rawSql := "UPDATE `repository` SET num_milestones = num_milestones - 1 WHERE id = ?"
  719. if _, err = sess.Exec(rawSql, m.RepoId); err != nil {
  720. sess.Rollback()
  721. return err
  722. }
  723. rawSql = "UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?"
  724. if _, err = sess.Exec(rawSql, m.Id); err != nil {
  725. sess.Rollback()
  726. return err
  727. }
  728. rawSql = "UPDATE `issue_user` SET milestone_id = 0 WHERE milestone_id = ?"
  729. if _, err = sess.Exec(rawSql, m.Id); err != nil {
  730. sess.Rollback()
  731. return err
  732. }
  733. return sess.Commit()
  734. }
  735. // _________ __
  736. // \_ ___ \ ____ _____ _____ ____ _____/ |_
  737. // / \ \/ / _ \ / \ / \_/ __ \ / \ __\
  738. // \ \___( <_> ) Y Y \ Y Y \ ___/| | \ |
  739. // \______ /\____/|__|_| /__|_| /\___ >___| /__|
  740. // \/ \/ \/ \/ \/
  741. // CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
  742. type CommentType int
  743. const (
  744. // Plain comment, can be associated with a commit (CommitId > 0) and a line (Line > 0)
  745. COMMENT_TYPE_COMMENT CommentType = iota
  746. COMMENT_TYPE_REOPEN
  747. COMMENT_TYPE_CLOSE
  748. // References.
  749. COMMENT_TYPE_ISSUE
  750. // Reference from some commit (not part of a pull request)
  751. COMMENT_TYPE_COMMIT
  752. // Reference from some pull request
  753. COMMENT_TYPE_PULL
  754. )
  755. // Comment represents a comment in commit and issue page.
  756. type Comment struct {
  757. Id int64
  758. Type CommentType
  759. PosterId int64
  760. Poster *User `xorm:"-"`
  761. IssueId int64
  762. CommitId int64
  763. Line int64
  764. Content string `xorm:"TEXT"`
  765. Created time.Time `xorm:"CREATED"`
  766. }
  767. // CreateComment creates comment of issue or commit.
  768. func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType CommentType, content string, attachments []int64) (*Comment, error) {
  769. sess := x.NewSession()
  770. defer sessionRelease(sess)
  771. if err := sess.Begin(); err != nil {
  772. return nil, err
  773. }
  774. comment := &Comment{PosterId: userId, Type: cmtType, IssueId: issueId,
  775. CommitId: commitId, Line: line, Content: content}
  776. if _, err := sess.Insert(comment); err != nil {
  777. return nil, err
  778. }
  779. // Check comment type.
  780. switch cmtType {
  781. case COMMENT_TYPE_COMMENT:
  782. rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
  783. if _, err := sess.Exec(rawSql, issueId); err != nil {
  784. return nil, err
  785. }
  786. if len(attachments) > 0 {
  787. rawSql = "UPDATE `attachment` SET comment_id = ? WHERE id IN (?)"
  788. astrs := make([]string, 0, len(attachments))
  789. for _, a := range attachments {
  790. astrs = append(astrs, strconv.FormatInt(a, 10))
  791. }
  792. if _, err := sess.Exec(rawSql, comment.Id, strings.Join(astrs, ",")); err != nil {
  793. return nil, err
  794. }
  795. }
  796. case COMMENT_TYPE_REOPEN:
  797. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?"
  798. if _, err := sess.Exec(rawSql, repoId); err != nil {
  799. return nil, err
  800. }
  801. case COMMENT_TYPE_CLOSE:
  802. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?"
  803. if _, err := sess.Exec(rawSql, repoId); err != nil {
  804. return nil, err
  805. }
  806. }
  807. return comment, sess.Commit()
  808. }
  809. // GetCommentById returns the comment with the given id
  810. func GetCommentById(commentId int64) (*Comment, error) {
  811. c := &Comment{Id: commentId}
  812. _, err := x.Get(c)
  813. return c, err
  814. }
  815. func (c *Comment) ContentHtml() template.HTML {
  816. return template.HTML(c.Content)
  817. }
  818. // GetIssueComments returns list of comment by given issue id.
  819. func GetIssueComments(issueId int64) ([]Comment, error) {
  820. comments := make([]Comment, 0, 10)
  821. err := x.Asc("created").Find(&comments, &Comment{IssueId: issueId})
  822. return comments, err
  823. }
  824. // Attachments returns the attachments for this comment.
  825. func (c *Comment) Attachments() []*Attachment {
  826. a, _ := GetAttachmentsByComment(c.Id)
  827. return a
  828. }
  829. func (c *Comment) AfterDelete() {
  830. _, err := DeleteAttachmentsByComment(c.Id, true)
  831. if err != nil {
  832. log.Info("Could not delete files for comment %d on issue #%d: %s", c.Id, c.IssueId, err)
  833. }
  834. }
  835. type Attachment struct {
  836. Id int64
  837. IssueId int64
  838. CommentId int64
  839. Name string
  840. Path string `xorm:"TEXT"`
  841. Created time.Time `xorm:"CREATED"`
  842. }
  843. // CreateAttachment creates a new attachment inside the database and
  844. func CreateAttachment(issueId, commentId int64, name, path string) (*Attachment, error) {
  845. sess := x.NewSession()
  846. defer sess.Close()
  847. if err := sess.Begin(); err != nil {
  848. return nil, err
  849. }
  850. a := &Attachment{IssueId: issueId, CommentId: commentId, Name: name, Path: path}
  851. if _, err := sess.Insert(a); err != nil {
  852. sess.Rollback()
  853. return nil, err
  854. }
  855. return a, sess.Commit()
  856. }
  857. // Attachment returns the attachment by given ID.
  858. func GetAttachmentById(id int64) (*Attachment, error) {
  859. m := &Attachment{Id: id}
  860. has, err := x.Get(m)
  861. if err != nil {
  862. return nil, err
  863. }
  864. if !has {
  865. return nil, ErrAttachmentNotExist
  866. }
  867. return m, nil
  868. }
  869. func GetAttachmentsForIssue(issueId int64) ([]*Attachment, error) {
  870. attachments := make([]*Attachment, 0, 10)
  871. err := x.Where("issue_id = ?", issueId).And("comment_id = 0").Find(&attachments)
  872. return attachments, err
  873. }
  874. // GetAttachmentsByIssue returns a list of attachments for the given issue
  875. func GetAttachmentsByIssue(issueId int64) ([]*Attachment, error) {
  876. attachments := make([]*Attachment, 0, 10)
  877. err := x.Where("issue_id = ?", issueId).And("comment_id > 0").Find(&attachments)
  878. return attachments, err
  879. }
  880. // GetAttachmentsByComment returns a list of attachments for the given comment
  881. func GetAttachmentsByComment(commentId int64) ([]*Attachment, error) {
  882. attachments := make([]*Attachment, 0, 10)
  883. err := x.Where("comment_id = ?", commentId).Find(&attachments)
  884. return attachments, err
  885. }
  886. // DeleteAttachment deletes the given attachment and optionally the associated file.
  887. func DeleteAttachment(a *Attachment, remove bool) error {
  888. _, err := DeleteAttachments([]*Attachment{a}, remove)
  889. return err
  890. }
  891. // DeleteAttachments deletes the given attachments and optionally the associated files.
  892. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  893. for i, a := range attachments {
  894. if remove {
  895. if err := os.Remove(a.Path); err != nil {
  896. return i, err
  897. }
  898. }
  899. if _, err := x.Delete(a.Id); err != nil {
  900. return i, err
  901. }
  902. }
  903. return len(attachments), nil
  904. }
  905. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  906. func DeleteAttachmentsByIssue(issueId int64, remove bool) (int, error) {
  907. attachments, err := GetAttachmentsByIssue(issueId)
  908. if err != nil {
  909. return 0, err
  910. }
  911. return DeleteAttachments(attachments, remove)
  912. }
  913. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  914. func DeleteAttachmentsByComment(commentId int64, remove bool) (int, error) {
  915. attachments, err := GetAttachmentsByComment(commentId)
  916. if err != nil {
  917. return 0, err
  918. }
  919. return DeleteAttachments(attachments, remove)
  920. }