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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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/go-xorm/xorm"
  15. "github.com/gogits/gogs/modules/log"
  16. "github.com/gogits/gogs/modules/setting"
  17. )
  18. var (
  19. ErrIssueNotExist = errors.New("Issue does not exist")
  20. ErrLabelNotExist = errors.New("Label does not exist")
  21. ErrMilestoneNotExist = errors.New("Milestone does not exist")
  22. ErrWrongIssueCounter = errors.New("Invalid number of issues for this milestone")
  23. ErrAttachmentNotExist = errors.New("Attachment does not exist")
  24. ErrAttachmentNotLinked = errors.New("Attachment does not belong to this issue")
  25. ErrMissingIssueNumber = errors.New("No issue number specified")
  26. )
  27. // Issue represents an issue or pull request of repository.
  28. type Issue struct {
  29. ID int64 `xorm:"pk autoincr"`
  30. RepoId int64 `xorm:"INDEX"`
  31. Index int64 // Index in one repository.
  32. Name string
  33. Repo *Repository `xorm:"-"`
  34. PosterId int64
  35. Poster *User `xorm:"-"`
  36. LabelIds string `xorm:"TEXT"`
  37. Labels []*Label `xorm:"-"`
  38. MilestoneId int64
  39. AssigneeId int64
  40. Assignee *User `xorm:"-"`
  41. IsRead bool `xorm:"-"`
  42. IsPull bool // Indicates whether is a pull request or not.
  43. IsClosed bool
  44. Content string `xorm:"TEXT"`
  45. RenderedContent string `xorm:"-"`
  46. Priority int
  47. NumComments int
  48. Deadline time.Time
  49. Created time.Time `xorm:"CREATED"`
  50. Updated time.Time `xorm:"UPDATED"`
  51. }
  52. func (i *Issue) GetPoster() (err error) {
  53. i.Poster, err = GetUserById(i.PosterId)
  54. if err == ErrUserNotExist {
  55. i.Poster = &User{Name: "FakeUser"}
  56. return nil
  57. }
  58. return err
  59. }
  60. func (i *Issue) GetLabels() error {
  61. if len(i.LabelIds) < 3 {
  62. return nil
  63. }
  64. strIds := strings.Split(strings.TrimSuffix(i.LabelIds[1:], "|"), "|$")
  65. i.Labels = make([]*Label, 0, len(strIds))
  66. for _, strId := range strIds {
  67. id, _ := com.StrTo(strId).Int64()
  68. if id > 0 {
  69. l, err := GetLabelById(id)
  70. if err != nil {
  71. if err == ErrLabelNotExist {
  72. continue
  73. }
  74. return err
  75. }
  76. i.Labels = append(i.Labels, l)
  77. }
  78. }
  79. return nil
  80. }
  81. func (i *Issue) GetAssignee() (err error) {
  82. if i.AssigneeId == 0 {
  83. return nil
  84. }
  85. i.Assignee, err = GetUserById(i.AssigneeId)
  86. if err == ErrUserNotExist {
  87. return nil
  88. }
  89. return err
  90. }
  91. func (i *Issue) Attachments() []*Attachment {
  92. a, _ := GetAttachmentsForIssue(i.ID)
  93. return a
  94. }
  95. func (i *Issue) AfterDelete() {
  96. _, err := DeleteAttachmentsByIssue(i.ID, true)
  97. if err != nil {
  98. log.Info("Could not delete files for issue #%d: %s", i.ID, err)
  99. }
  100. }
  101. // CreateIssue creates new issue for repository.
  102. func NewIssue(issue *Issue) (err error) {
  103. sess := x.NewSession()
  104. defer sessionRelease(sess)
  105. if err = sess.Begin(); err != nil {
  106. return err
  107. }
  108. if _, err = sess.Insert(issue); err != nil {
  109. return err
  110. } else if _, err = sess.Exec("UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?", issue.RepoId); err != nil {
  111. return err
  112. }
  113. if err = sess.Commit(); err != nil {
  114. return err
  115. }
  116. if issue.MilestoneId > 0 {
  117. // FIXES(280): Update milestone counter.
  118. return ChangeMilestoneAssign(0, issue.MilestoneId, issue)
  119. }
  120. return
  121. }
  122. // GetIssueByRef returns an Issue specified by a GFM reference.
  123. // See https://help.github.com/articles/writing-on-github#references for more information on the syntax.
  124. func GetIssueByRef(ref string) (issue *Issue, err error) {
  125. var issueNumber int64
  126. var repo *Repository
  127. n := strings.IndexByte(ref, byte('#'))
  128. if n == -1 {
  129. return nil, ErrMissingIssueNumber
  130. }
  131. if issueNumber, err = strconv.ParseInt(ref[n+1:], 10, 64); err != nil {
  132. return
  133. }
  134. if repo, err = GetRepositoryByRef(ref[:n]); err != nil {
  135. return
  136. }
  137. return GetIssueByIndex(repo.Id, issueNumber)
  138. }
  139. // GetIssueByIndex returns issue by given index in repository.
  140. func GetIssueByIndex(rid, index int64) (*Issue, error) {
  141. issue := &Issue{RepoId: rid, Index: index}
  142. has, err := x.Get(issue)
  143. if err != nil {
  144. return nil, err
  145. } else if !has {
  146. return nil, ErrIssueNotExist
  147. }
  148. return issue, nil
  149. }
  150. // GetIssueById returns an issue by ID.
  151. func GetIssueById(id int64) (*Issue, error) {
  152. issue := &Issue{ID: id}
  153. has, err := x.Get(issue)
  154. if err != nil {
  155. return nil, err
  156. } else if !has {
  157. return nil, ErrIssueNotExist
  158. }
  159. return issue, nil
  160. }
  161. // GetIssues returns a list of issues by given conditions.
  162. func GetIssues(uid, rid, pid, mid int64, page int, isClosed bool, labelIds, sortType string) ([]Issue, error) {
  163. sess := x.Limit(setting.IssuePagingNum, (page-1)*setting.IssuePagingNum)
  164. if rid > 0 {
  165. sess.Where("repo_id=?", rid).And("is_closed=?", isClosed)
  166. } else {
  167. sess.Where("is_closed=?", isClosed)
  168. }
  169. if uid > 0 {
  170. sess.And("assignee_id=?", uid)
  171. } else if pid > 0 {
  172. sess.And("poster_id=?", pid)
  173. }
  174. if mid > 0 {
  175. sess.And("milestone_id=?", mid)
  176. }
  177. if len(labelIds) > 0 {
  178. for _, label := range strings.Split(labelIds, ",") {
  179. if com.StrTo(label).MustInt() > 0 {
  180. sess.And("label_ids like ?", "'%$"+label+"|%'")
  181. }
  182. }
  183. }
  184. switch sortType {
  185. case "oldest":
  186. sess.Asc("created")
  187. case "recentupdate":
  188. sess.Desc("updated")
  189. case "leastupdate":
  190. sess.Asc("updated")
  191. case "mostcomment":
  192. sess.Desc("num_comments")
  193. case "leastcomment":
  194. sess.Asc("num_comments")
  195. case "priority":
  196. sess.Desc("priority")
  197. default:
  198. sess.Desc("created")
  199. }
  200. var issues []Issue
  201. return issues, sess.Find(&issues)
  202. }
  203. type IssueStatus int
  204. const (
  205. IS_OPEN = iota + 1
  206. IS_CLOSE
  207. )
  208. // GetIssuesByLabel returns a list of issues by given label and repository.
  209. func GetIssuesByLabel(repoId int64, label string) ([]*Issue, error) {
  210. issues := make([]*Issue, 0, 10)
  211. err := x.Where("repo_id=?", repoId).And("label_ids like '%$" + label + "|%'").Find(&issues)
  212. return issues, err
  213. }
  214. // GetIssueCountByPoster returns number of issues of repository by poster.
  215. func GetIssueCountByPoster(uid, rid int64, isClosed bool) int64 {
  216. count, _ := x.Where("repo_id=?", rid).And("poster_id=?", uid).And("is_closed=?", isClosed).Count(new(Issue))
  217. return count
  218. }
  219. // .___ ____ ___
  220. // | | ______ ________ __ ____ | | \______ ___________
  221. // | |/ ___// ___/ | \_/ __ \| | / ___// __ \_ __ \
  222. // | |\___ \ \___ \| | /\ ___/| | /\___ \\ ___/| | \/
  223. // |___/____ >____ >____/ \___ >______//____ >\___ >__|
  224. // \/ \/ \/ \/ \/
  225. // IssueUser represents an issue-user relation.
  226. type IssueUser struct {
  227. Id int64
  228. Uid int64 `xorm:"INDEX"` // User ID.
  229. IssueId int64
  230. RepoId int64 `xorm:"INDEX"`
  231. MilestoneId int64
  232. IsRead bool
  233. IsAssigned bool
  234. IsMentioned bool
  235. IsPoster bool
  236. IsClosed bool
  237. }
  238. // FIXME: organization
  239. // NewIssueUserPairs adds new issue-user pairs for new issue of repository.
  240. func NewIssueUserPairs(repo *Repository, issueID, orgID, posterID, assigneeID int64) error {
  241. users, err := repo.GetCollaborators()
  242. if err != nil {
  243. return err
  244. }
  245. iu := &IssueUser{
  246. IssueId: issueID,
  247. RepoId: repo.Id,
  248. }
  249. isNeedAddPoster := true
  250. for _, u := range users {
  251. iu.Id = 0
  252. iu.Uid = u.Id
  253. iu.IsPoster = iu.Uid == posterID
  254. if isNeedAddPoster && iu.IsPoster {
  255. isNeedAddPoster = false
  256. }
  257. iu.IsAssigned = iu.Uid == assigneeID
  258. if _, err = x.Insert(iu); err != nil {
  259. return err
  260. }
  261. }
  262. if isNeedAddPoster {
  263. iu.Id = 0
  264. iu.Uid = posterID
  265. iu.IsPoster = true
  266. iu.IsAssigned = iu.Uid == assigneeID
  267. if _, err = x.Insert(iu); err != nil {
  268. return err
  269. }
  270. }
  271. // Add owner's as well.
  272. if repo.OwnerId != posterID {
  273. iu.Id = 0
  274. iu.Uid = repo.OwnerId
  275. iu.IsAssigned = iu.Uid == assigneeID
  276. if _, err = x.Insert(iu); err != nil {
  277. return err
  278. }
  279. }
  280. return nil
  281. }
  282. // PairsContains returns true when pairs list contains given issue.
  283. func PairsContains(ius []*IssueUser, issueId, uid int64) int {
  284. for i := range ius {
  285. if ius[i].IssueId == issueId &&
  286. ius[i].Uid == uid {
  287. return i
  288. }
  289. }
  290. return -1
  291. }
  292. // GetIssueUserPairs returns issue-user pairs by given repository and user.
  293. func GetIssueUserPairs(rid, uid int64, isClosed bool) ([]*IssueUser, error) {
  294. ius := make([]*IssueUser, 0, 10)
  295. err := x.Where("is_closed=?", isClosed).Find(&ius, &IssueUser{RepoId: rid, Uid: uid})
  296. return ius, err
  297. }
  298. // GetIssueUserPairsByRepoIds returns issue-user pairs by given repository IDs.
  299. func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*IssueUser, error) {
  300. if len(rids) == 0 {
  301. return []*IssueUser{}, nil
  302. }
  303. buf := bytes.NewBufferString("")
  304. for _, rid := range rids {
  305. buf.WriteString("repo_id=")
  306. buf.WriteString(com.ToStr(rid))
  307. buf.WriteString(" OR ")
  308. }
  309. cond := strings.TrimSuffix(buf.String(), " OR ")
  310. ius := make([]*IssueUser, 0, 10)
  311. sess := x.Limit(20, (page-1)*20).Where("is_closed=?", isClosed)
  312. if len(cond) > 0 {
  313. sess.And(cond)
  314. }
  315. err := sess.Find(&ius)
  316. return ius, err
  317. }
  318. // GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
  319. func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
  320. ius := make([]*IssueUser, 0, 10)
  321. sess := x.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
  322. if rid > 0 {
  323. sess.And("repo_id=?", rid)
  324. }
  325. switch filterMode {
  326. case FM_ASSIGN:
  327. sess.And("is_assigned=?", true)
  328. case FM_CREATE:
  329. sess.And("is_poster=?", true)
  330. default:
  331. return ius, nil
  332. }
  333. err := sess.Find(&ius)
  334. return ius, err
  335. }
  336. // IssueStats represents issue statistic information.
  337. type IssueStats struct {
  338. OpenCount, ClosedCount int64
  339. AllCount int64
  340. AssignCount int64
  341. CreateCount int64
  342. MentionCount int64
  343. }
  344. // Filter modes.
  345. const (
  346. FM_ASSIGN = iota + 1
  347. FM_CREATE
  348. FM_MENTION
  349. )
  350. // GetIssueStats returns issue statistic information by given conditions.
  351. func GetIssueStats(rid, uid int64, isShowClosed bool, filterMode int) *IssueStats {
  352. stats := &IssueStats{}
  353. issue := new(Issue)
  354. tmpSess := &xorm.Session{}
  355. sess := x.Where("repo_id=?", rid)
  356. *tmpSess = *sess
  357. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue)
  358. *tmpSess = *sess
  359. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue)
  360. if isShowClosed {
  361. stats.AllCount = stats.ClosedCount
  362. } else {
  363. stats.AllCount = stats.OpenCount
  364. }
  365. if filterMode != FM_MENTION {
  366. sess = x.Where("repo_id=?", rid)
  367. switch filterMode {
  368. case FM_ASSIGN:
  369. sess.And("assignee_id=?", uid)
  370. case FM_CREATE:
  371. sess.And("poster_id=?", uid)
  372. default:
  373. goto nofilter
  374. }
  375. *tmpSess = *sess
  376. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue)
  377. *tmpSess = *sess
  378. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue)
  379. } else {
  380. sess := x.Where("repo_id=?", rid).And("uid=?", uid).And("is_mentioned=?", true)
  381. *tmpSess = *sess
  382. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(new(IssueUser))
  383. *tmpSess = *sess
  384. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(new(IssueUser))
  385. }
  386. nofilter:
  387. stats.AssignCount, _ = x.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("assignee_id=?", uid).Count(issue)
  388. stats.CreateCount, _ = x.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("poster_id=?", uid).Count(issue)
  389. stats.MentionCount, _ = x.Where("repo_id=?", rid).And("uid=?", uid).And("is_closed=?", isShowClosed).And("is_mentioned=?", true).Count(new(IssueUser))
  390. return stats
  391. }
  392. // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
  393. func GetUserIssueStats(uid int64, filterMode int) *IssueStats {
  394. stats := &IssueStats{}
  395. issue := new(Issue)
  396. stats.AssignCount, _ = x.Where("assignee_id=?", uid).And("is_closed=?", false).Count(issue)
  397. stats.CreateCount, _ = x.Where("poster_id=?", uid).And("is_closed=?", false).Count(issue)
  398. return stats
  399. }
  400. // UpdateIssue updates information of issue.
  401. func UpdateIssue(issue *Issue) error {
  402. _, err := x.Id(issue.ID).AllCols().Update(issue)
  403. if err != nil {
  404. return err
  405. }
  406. return err
  407. }
  408. // UpdateIssueUserByStatus updates issue-user pairs by issue status.
  409. func UpdateIssueUserPairsByStatus(iid int64, isClosed bool) error {
  410. rawSql := "UPDATE `issue_user` SET is_closed = ? WHERE issue_id = ?"
  411. _, err := x.Exec(rawSql, isClosed, iid)
  412. return err
  413. }
  414. // UpdateIssueUserPairByAssignee updates issue-user pair for assigning.
  415. func UpdateIssueUserPairByAssignee(aid, iid int64) error {
  416. rawSql := "UPDATE `issue_user` SET is_assigned = ? WHERE issue_id = ?"
  417. if _, err := x.Exec(rawSql, false, iid); err != nil {
  418. return err
  419. }
  420. // Assignee ID equals to 0 means clear assignee.
  421. if aid == 0 {
  422. return nil
  423. }
  424. rawSql = "UPDATE `issue_user` SET is_assigned = ? WHERE uid = ? AND issue_id = ?"
  425. _, err := x.Exec(rawSql, true, aid, iid)
  426. return err
  427. }
  428. // UpdateIssueUserPairByRead updates issue-user pair for reading.
  429. func UpdateIssueUserPairByRead(uid, iid int64) error {
  430. rawSql := "UPDATE `issue_user` SET is_read = ? WHERE uid = ? AND issue_id = ?"
  431. _, err := x.Exec(rawSql, true, uid, iid)
  432. return err
  433. }
  434. // UpdateIssueUserPairsByMentions updates issue-user pairs by mentioning.
  435. func UpdateIssueUserPairsByMentions(uids []int64, iid int64) error {
  436. for _, uid := range uids {
  437. iu := &IssueUser{Uid: uid, IssueId: iid}
  438. has, err := x.Get(iu)
  439. if err != nil {
  440. return err
  441. }
  442. iu.IsMentioned = true
  443. if has {
  444. _, err = x.Id(iu.Id).AllCols().Update(iu)
  445. } else {
  446. _, err = x.Insert(iu)
  447. }
  448. if err != nil {
  449. return err
  450. }
  451. }
  452. return nil
  453. }
  454. // .____ ___. .__
  455. // | | _____ \_ |__ ____ | |
  456. // | | \__ \ | __ \_/ __ \| |
  457. // | |___ / __ \| \_\ \ ___/| |__
  458. // |_______ (____ /___ /\___ >____/
  459. // \/ \/ \/ \/
  460. // Label represents a label of repository for issues.
  461. type Label struct {
  462. ID int64 `xorm:"pk autoincr"`
  463. RepoId int64 `xorm:"INDEX"`
  464. Name string
  465. Color string `xorm:"VARCHAR(7)"`
  466. NumIssues int
  467. NumClosedIssues int
  468. NumOpenIssues int `xorm:"-"`
  469. IsChecked bool `xorm:"-"`
  470. }
  471. // CalOpenIssues calculates the open issues of label.
  472. func (m *Label) CalOpenIssues() {
  473. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  474. }
  475. // NewLabel creates new label of repository.
  476. func NewLabel(l *Label) error {
  477. _, err := x.Insert(l)
  478. return err
  479. }
  480. // GetLabelById returns a label by given ID.
  481. func GetLabelById(id int64) (*Label, error) {
  482. if id <= 0 {
  483. return nil, ErrLabelNotExist
  484. }
  485. l := &Label{ID: id}
  486. has, err := x.Get(l)
  487. if err != nil {
  488. return nil, err
  489. } else if !has {
  490. return nil, ErrLabelNotExist
  491. }
  492. return l, nil
  493. }
  494. // GetLabels returns a list of labels of given repository ID.
  495. func GetLabels(repoId int64) ([]*Label, error) {
  496. labels := make([]*Label, 0, 10)
  497. err := x.Where("repo_id=?", repoId).Find(&labels)
  498. return labels, err
  499. }
  500. // UpdateLabel updates label information.
  501. func UpdateLabel(l *Label) error {
  502. _, err := x.Id(l.ID).AllCols().Update(l)
  503. return err
  504. }
  505. // DeleteLabel delete a label of given repository.
  506. func DeleteLabel(repoId int64, strId string) error {
  507. id, _ := com.StrTo(strId).Int64()
  508. l, err := GetLabelById(id)
  509. if err != nil {
  510. if err == ErrLabelNotExist {
  511. return nil
  512. }
  513. return err
  514. }
  515. issues, err := GetIssuesByLabel(repoId, strId)
  516. if err != nil {
  517. return err
  518. }
  519. sess := x.NewSession()
  520. defer sess.Close()
  521. if err = sess.Begin(); err != nil {
  522. return err
  523. }
  524. for _, issue := range issues {
  525. issue.LabelIds = strings.Replace(issue.LabelIds, "$"+strId+"|", "", -1)
  526. if _, err = sess.Id(issue.ID).AllCols().Update(issue); err != nil {
  527. sess.Rollback()
  528. return err
  529. }
  530. }
  531. if _, err = sess.Delete(l); err != nil {
  532. sess.Rollback()
  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 sess.Close()
  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. sess.Rollback()
  789. return nil, err
  790. }
  791. // Check comment type.
  792. switch cmtType {
  793. case COMMENT_TYPE_COMMENT:
  794. rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
  795. if _, err := sess.Exec(rawSql, issueId); err != nil {
  796. sess.Rollback()
  797. return nil, err
  798. }
  799. if len(attachments) > 0 {
  800. rawSql = "UPDATE `attachment` SET comment_id = ? WHERE id IN (?)"
  801. astrs := make([]string, 0, len(attachments))
  802. for _, a := range attachments {
  803. astrs = append(astrs, strconv.FormatInt(a, 10))
  804. }
  805. if _, err := sess.Exec(rawSql, comment.Id, strings.Join(astrs, ",")); err != nil {
  806. sess.Rollback()
  807. return nil, err
  808. }
  809. }
  810. case COMMENT_TYPE_REOPEN:
  811. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?"
  812. if _, err := sess.Exec(rawSql, repoId); err != nil {
  813. sess.Rollback()
  814. return nil, err
  815. }
  816. case COMMENT_TYPE_CLOSE:
  817. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?"
  818. if _, err := sess.Exec(rawSql, repoId); err != nil {
  819. sess.Rollback()
  820. return nil, err
  821. }
  822. }
  823. return comment, sess.Commit()
  824. }
  825. // GetCommentById returns the comment with the given id
  826. func GetCommentById(commentId int64) (*Comment, error) {
  827. c := &Comment{Id: commentId}
  828. _, err := x.Get(c)
  829. return c, err
  830. }
  831. func (c *Comment) ContentHtml() template.HTML {
  832. return template.HTML(c.Content)
  833. }
  834. // GetIssueComments returns list of comment by given issue id.
  835. func GetIssueComments(issueId int64) ([]Comment, error) {
  836. comments := make([]Comment, 0, 10)
  837. err := x.Asc("created").Find(&comments, &Comment{IssueId: issueId})
  838. return comments, err
  839. }
  840. // Attachments returns the attachments for this comment.
  841. func (c *Comment) Attachments() []*Attachment {
  842. a, _ := GetAttachmentsByComment(c.Id)
  843. return a
  844. }
  845. func (c *Comment) AfterDelete() {
  846. _, err := DeleteAttachmentsByComment(c.Id, true)
  847. if err != nil {
  848. log.Info("Could not delete files for comment %d on issue #%d: %s", c.Id, c.IssueId, err)
  849. }
  850. }
  851. type Attachment struct {
  852. Id int64
  853. IssueId int64
  854. CommentId int64
  855. Name string
  856. Path string `xorm:"TEXT"`
  857. Created time.Time `xorm:"CREATED"`
  858. }
  859. // CreateAttachment creates a new attachment inside the database and
  860. func CreateAttachment(issueId, commentId int64, name, path string) (*Attachment, error) {
  861. sess := x.NewSession()
  862. defer sess.Close()
  863. if err := sess.Begin(); err != nil {
  864. return nil, err
  865. }
  866. a := &Attachment{IssueId: issueId, CommentId: commentId, Name: name, Path: path}
  867. if _, err := sess.Insert(a); err != nil {
  868. sess.Rollback()
  869. return nil, err
  870. }
  871. return a, sess.Commit()
  872. }
  873. // Attachment returns the attachment by given ID.
  874. func GetAttachmentById(id int64) (*Attachment, error) {
  875. m := &Attachment{Id: id}
  876. has, err := x.Get(m)
  877. if err != nil {
  878. return nil, err
  879. }
  880. if !has {
  881. return nil, ErrAttachmentNotExist
  882. }
  883. return m, nil
  884. }
  885. func GetAttachmentsForIssue(issueId int64) ([]*Attachment, error) {
  886. attachments := make([]*Attachment, 0, 10)
  887. err := x.Where("issue_id = ?", issueId).And("comment_id = 0").Find(&attachments)
  888. return attachments, err
  889. }
  890. // GetAttachmentsByIssue returns a list of attachments for the given issue
  891. func GetAttachmentsByIssue(issueId int64) ([]*Attachment, error) {
  892. attachments := make([]*Attachment, 0, 10)
  893. err := x.Where("issue_id = ?", issueId).And("comment_id > 0").Find(&attachments)
  894. return attachments, err
  895. }
  896. // GetAttachmentsByComment returns a list of attachments for the given comment
  897. func GetAttachmentsByComment(commentId int64) ([]*Attachment, error) {
  898. attachments := make([]*Attachment, 0, 10)
  899. err := x.Where("comment_id = ?", commentId).Find(&attachments)
  900. return attachments, err
  901. }
  902. // DeleteAttachment deletes the given attachment and optionally the associated file.
  903. func DeleteAttachment(a *Attachment, remove bool) error {
  904. _, err := DeleteAttachments([]*Attachment{a}, remove)
  905. return err
  906. }
  907. // DeleteAttachments deletes the given attachments and optionally the associated files.
  908. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  909. for i, a := range attachments {
  910. if remove {
  911. if err := os.Remove(a.Path); err != nil {
  912. return i, err
  913. }
  914. }
  915. if _, err := x.Delete(a.Id); err != nil {
  916. return i, err
  917. }
  918. }
  919. return len(attachments), nil
  920. }
  921. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  922. func DeleteAttachmentsByIssue(issueId int64, remove bool) (int, error) {
  923. attachments, err := GetAttachmentsByIssue(issueId)
  924. if err != nil {
  925. return 0, err
  926. }
  927. return DeleteAttachments(attachments, remove)
  928. }
  929. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  930. func DeleteAttachmentsByComment(commentId int64, remove bool) (int, error) {
  931. attachments, err := GetAttachmentsByComment(commentId)
  932. if err != nil {
  933. return 0, err
  934. }
  935. return DeleteAttachments(attachments, remove)
  936. }