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.

пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 9 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. )
  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
  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).Int64()
  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 sess.Close()
  104. if err = sess.Begin(); err != nil {
  105. return err
  106. }
  107. if _, err = sess.Insert(issue); err != nil {
  108. sess.Rollback()
  109. return err
  110. }
  111. rawSql := "UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?"
  112. if _, err = sess.Exec(rawSql, issue.RepoId); err != nil {
  113. sess.Rollback()
  114. return err
  115. }
  116. if err = sess.Commit(); err != nil {
  117. return err
  118. }
  119. if issue.MilestoneId > 0 {
  120. // FIXES(280): Update milestone counter.
  121. return ChangeMilestoneAssign(0, issue.MilestoneId, issue)
  122. }
  123. return
  124. }
  125. // GetIssueByRef returns an Issue specified by a GFM reference.
  126. // See https://help.github.com/articles/writing-on-github#references for more information on the syntax.
  127. func GetIssueByRef(ref string) (issue *Issue, err error) {
  128. var issueNumber int64
  129. var repo *Repository
  130. n := strings.IndexByte(ref, byte('#'))
  131. if n == -1 {
  132. return nil, ErrMissingIssueNumber
  133. }
  134. if issueNumber, err = strconv.ParseInt(ref[n+1:], 10, 64); err != nil {
  135. return
  136. }
  137. if repo, err = GetRepositoryByRef(ref[:n]); err != nil {
  138. return
  139. }
  140. return GetIssueByIndex(repo.Id, issueNumber)
  141. }
  142. // GetIssueByIndex returns issue by given index in repository.
  143. func GetIssueByIndex(rid, index int64) (*Issue, error) {
  144. issue := &Issue{RepoId: rid, Index: index}
  145. has, err := x.Get(issue)
  146. if err != nil {
  147. return nil, err
  148. } else if !has {
  149. return nil, ErrIssueNotExist
  150. }
  151. return issue, nil
  152. }
  153. // GetIssueById returns an issue by ID.
  154. func GetIssueById(id int64) (*Issue, error) {
  155. issue := &Issue{Id: id}
  156. has, err := x.Get(issue)
  157. if err != nil {
  158. return nil, err
  159. } else if !has {
  160. return nil, ErrIssueNotExist
  161. }
  162. return issue, nil
  163. }
  164. // GetIssues returns a list of issues by given conditions.
  165. func GetIssues(uid, rid, pid, mid int64, page int, isClosed bool, labelIds, sortType string) ([]Issue, error) {
  166. sess := x.Limit(20, (page-1)*20)
  167. if rid > 0 {
  168. sess.Where("repo_id=?", rid).And("is_closed=?", isClosed)
  169. } else {
  170. sess.Where("is_closed=?", isClosed)
  171. }
  172. if uid > 0 {
  173. sess.And("assignee_id=?", uid)
  174. } else if pid > 0 {
  175. sess.And("poster_id=?", pid)
  176. }
  177. if mid > 0 {
  178. sess.And("milestone_id=?", mid)
  179. }
  180. if len(labelIds) > 0 {
  181. for _, label := range strings.Split(labelIds, ",") {
  182. // Prevent SQL inject.
  183. if com.StrTo(label).MustInt() > 0 {
  184. sess.And("label_ids like '%$" + label + "|%'")
  185. }
  186. }
  187. }
  188. switch sortType {
  189. case "oldest":
  190. sess.Asc("created")
  191. case "recentupdate":
  192. sess.Desc("updated")
  193. case "leastupdate":
  194. sess.Asc("updated")
  195. case "mostcomment":
  196. sess.Desc("num_comments")
  197. case "leastcomment":
  198. sess.Asc("num_comments")
  199. case "priority":
  200. sess.Desc("priority")
  201. default:
  202. sess.Desc("created")
  203. }
  204. var issues []Issue
  205. err := sess.Find(&issues)
  206. return issues, err
  207. }
  208. type IssueStatus int
  209. const (
  210. IS_OPEN = iota + 1
  211. IS_CLOSE
  212. )
  213. // GetIssuesByLabel returns a list of issues by given label and repository.
  214. func GetIssuesByLabel(repoId int64, label string) ([]*Issue, error) {
  215. issues := make([]*Issue, 0, 10)
  216. err := x.Where("repo_id=?", repoId).And("label_ids like '%$" + label + "|%'").Find(&issues)
  217. return issues, err
  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. // NewIssueUserPairs adds new issue-user pairs for new issue of repository.
  244. func NewIssueUserPairs(repo *Repository, iid, oid, pid, aid int64) (err error) {
  245. iu := &IssueUser{IssueId: iid, RepoId: repo.Id}
  246. us, err := repo.GetCollaborators()
  247. if err != nil {
  248. return err
  249. }
  250. isNeedAddPoster := true
  251. for _, u := range us {
  252. iu.Uid = u.Id
  253. iu.IsPoster = iu.Uid == pid
  254. if isNeedAddPoster && iu.IsPoster {
  255. isNeedAddPoster = false
  256. }
  257. iu.IsAssigned = iu.Uid == aid
  258. if _, err = x.Insert(iu); err != nil {
  259. return err
  260. }
  261. }
  262. if isNeedAddPoster {
  263. iu.Uid = pid
  264. iu.IsPoster = true
  265. iu.IsAssigned = iu.Uid == aid
  266. if _, err = x.Insert(iu); err != nil {
  267. return err
  268. }
  269. }
  270. return nil
  271. }
  272. // PairsContains returns true when pairs list contains given issue.
  273. func PairsContains(ius []*IssueUser, issueId int64) int {
  274. for i := range ius {
  275. if ius[i].IssueId == issueId {
  276. return i
  277. }
  278. }
  279. return -1
  280. }
  281. // GetIssueUserPairs returns issue-user pairs by given repository and user.
  282. func GetIssueUserPairs(rid, uid int64, isClosed bool) ([]*IssueUser, error) {
  283. ius := make([]*IssueUser, 0, 10)
  284. err := x.Where("is_closed=?", isClosed).Find(&ius, &IssueUser{RepoId: rid, Uid: uid})
  285. return ius, err
  286. }
  287. // GetIssueUserPairsByRepoIds returns issue-user pairs by given repository IDs.
  288. func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*IssueUser, error) {
  289. if len(rids) == 0 {
  290. return []*IssueUser{}, nil
  291. }
  292. buf := bytes.NewBufferString("")
  293. for _, rid := range rids {
  294. buf.WriteString("repo_id=")
  295. buf.WriteString(com.ToStr(rid))
  296. buf.WriteString(" OR ")
  297. }
  298. cond := strings.TrimSuffix(buf.String(), " OR ")
  299. ius := make([]*IssueUser, 0, 10)
  300. sess := x.Limit(20, (page-1)*20).Where("is_closed=?", isClosed)
  301. if len(cond) > 0 {
  302. sess.And(cond)
  303. }
  304. err := sess.Find(&ius)
  305. return ius, err
  306. }
  307. // GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
  308. func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
  309. ius := make([]*IssueUser, 0, 10)
  310. sess := x.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
  311. if rid > 0 {
  312. sess.And("repo_id=?", rid)
  313. }
  314. switch filterMode {
  315. case FM_ASSIGN:
  316. sess.And("is_assigned=?", true)
  317. case FM_CREATE:
  318. sess.And("is_poster=?", true)
  319. default:
  320. return ius, nil
  321. }
  322. err := sess.Find(&ius)
  323. return ius, err
  324. }
  325. // IssueStats represents issue statistic information.
  326. type IssueStats struct {
  327. OpenCount, ClosedCount int64
  328. AllCount int64
  329. AssignCount int64
  330. CreateCount int64
  331. MentionCount int64
  332. }
  333. // Filter modes.
  334. const (
  335. FM_ASSIGN = iota + 1
  336. FM_CREATE
  337. FM_MENTION
  338. )
  339. // GetIssueStats returns issue statistic information by given conditions.
  340. func GetIssueStats(rid, uid int64, isShowClosed bool, filterMode int) *IssueStats {
  341. stats := &IssueStats{}
  342. issue := new(Issue)
  343. tmpSess := &xorm.Session{}
  344. sess := x.Where("repo_id=?", rid)
  345. *tmpSess = *sess
  346. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue)
  347. *tmpSess = *sess
  348. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue)
  349. if isShowClosed {
  350. stats.AllCount = stats.ClosedCount
  351. } else {
  352. stats.AllCount = stats.OpenCount
  353. }
  354. if filterMode != FM_MENTION {
  355. sess = x.Where("repo_id=?", rid)
  356. switch filterMode {
  357. case FM_ASSIGN:
  358. sess.And("assignee_id=?", uid)
  359. case FM_CREATE:
  360. sess.And("poster_id=?", uid)
  361. default:
  362. goto nofilter
  363. }
  364. *tmpSess = *sess
  365. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue)
  366. *tmpSess = *sess
  367. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue)
  368. } else {
  369. sess := x.Where("repo_id=?", rid).And("uid=?", uid).And("is_mentioned=?", true)
  370. *tmpSess = *sess
  371. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(new(IssueUser))
  372. *tmpSess = *sess
  373. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(new(IssueUser))
  374. }
  375. nofilter:
  376. stats.AssignCount, _ = x.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("assignee_id=?", uid).Count(issue)
  377. stats.CreateCount, _ = x.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("poster_id=?", uid).Count(issue)
  378. stats.MentionCount, _ = x.Where("repo_id=?", rid).And("uid=?", uid).And("is_closed=?", isShowClosed).And("is_mentioned=?", true).Count(new(IssueUser))
  379. return stats
  380. }
  381. // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
  382. func GetUserIssueStats(uid int64, filterMode int) *IssueStats {
  383. stats := &IssueStats{}
  384. issue := new(Issue)
  385. stats.AssignCount, _ = x.Where("assignee_id=?", uid).And("is_closed=?", false).Count(issue)
  386. stats.CreateCount, _ = x.Where("poster_id=?", uid).And("is_closed=?", false).Count(issue)
  387. return stats
  388. }
  389. // UpdateIssue updates information of issue.
  390. func UpdateIssue(issue *Issue) error {
  391. _, err := x.Id(issue.Id).AllCols().Update(issue)
  392. if err != nil {
  393. return err
  394. }
  395. return err
  396. }
  397. // UpdateIssueUserByStatus updates issue-user pairs by issue status.
  398. func UpdateIssueUserPairsByStatus(iid int64, isClosed bool) error {
  399. rawSql := "UPDATE `issue_user` SET is_closed = ? WHERE issue_id = ?"
  400. _, err := x.Exec(rawSql, isClosed, iid)
  401. return err
  402. }
  403. // UpdateIssueUserPairByAssignee updates issue-user pair for assigning.
  404. func UpdateIssueUserPairByAssignee(aid, iid int64) error {
  405. rawSql := "UPDATE `issue_user` SET is_assigned = ? WHERE issue_id = ?"
  406. if _, err := x.Exec(rawSql, false, iid); err != nil {
  407. return err
  408. }
  409. // Assignee ID equals to 0 means clear assignee.
  410. if aid == 0 {
  411. return nil
  412. }
  413. rawSql = "UPDATE `issue_user` SET is_assigned = ? WHERE uid = ? AND issue_id = ?"
  414. _, err := x.Exec(rawSql, true, aid, iid)
  415. return err
  416. }
  417. // UpdateIssueUserPairByRead updates issue-user pair for reading.
  418. func UpdateIssueUserPairByRead(uid, iid int64) error {
  419. rawSql := "UPDATE `issue_user` SET is_read = ? WHERE uid = ? AND issue_id = ?"
  420. _, err := x.Exec(rawSql, true, uid, iid)
  421. return err
  422. }
  423. // UpdateIssueUserPairsByMentions updates issue-user pairs by mentioning.
  424. func UpdateIssueUserPairsByMentions(uids []int64, iid int64) error {
  425. for _, uid := range uids {
  426. iu := &IssueUser{Uid: uid, IssueId: iid}
  427. has, err := x.Get(iu)
  428. if err != nil {
  429. return err
  430. }
  431. iu.IsMentioned = true
  432. if has {
  433. _, err = x.Id(iu.Id).AllCols().Update(iu)
  434. } else {
  435. _, err = x.Insert(iu)
  436. }
  437. if err != nil {
  438. return err
  439. }
  440. }
  441. return nil
  442. }
  443. // .____ ___. .__
  444. // | | _____ \_ |__ ____ | |
  445. // | | \__ \ | __ \_/ __ \| |
  446. // | |___ / __ \| \_\ \ ___/| |__
  447. // |_______ (____ /___ /\___ >____/
  448. // \/ \/ \/ \/
  449. // Label represents a label of repository for issues.
  450. type Label struct {
  451. Id int64
  452. RepoId int64 `xorm:"INDEX"`
  453. Name string
  454. Color string `xorm:"VARCHAR(7)"`
  455. NumIssues int
  456. NumClosedIssues int
  457. NumOpenIssues int `xorm:"-"`
  458. IsChecked bool `xorm:"-"`
  459. }
  460. // CalOpenIssues calculates the open issues of label.
  461. func (m *Label) CalOpenIssues() {
  462. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  463. }
  464. // NewLabel creates new label of repository.
  465. func NewLabel(l *Label) error {
  466. _, err := x.Insert(l)
  467. return err
  468. }
  469. // GetLabelById returns a label by given ID.
  470. func GetLabelById(id int64) (*Label, error) {
  471. if id <= 0 {
  472. return nil, ErrLabelNotExist
  473. }
  474. l := &Label{Id: id}
  475. has, err := x.Get(l)
  476. if err != nil {
  477. return nil, err
  478. } else if !has {
  479. return nil, ErrLabelNotExist
  480. }
  481. return l, nil
  482. }
  483. // GetLabels returns a list of labels of given repository ID.
  484. func GetLabels(repoId int64) ([]*Label, error) {
  485. labels := make([]*Label, 0, 10)
  486. err := x.Where("repo_id=?", repoId).Find(&labels)
  487. return labels, err
  488. }
  489. // UpdateLabel updates label information.
  490. func UpdateLabel(l *Label) error {
  491. _, err := x.Id(l.Id).Update(l)
  492. return err
  493. }
  494. // DeleteLabel delete a label of given repository.
  495. func DeleteLabel(repoId int64, strId string) error {
  496. id, _ := com.StrTo(strId).Int64()
  497. l, err := GetLabelById(id)
  498. if err != nil {
  499. if err == ErrLabelNotExist {
  500. return nil
  501. }
  502. return err
  503. }
  504. issues, err := GetIssuesByLabel(repoId, strId)
  505. if err != nil {
  506. return err
  507. }
  508. sess := x.NewSession()
  509. defer sess.Close()
  510. if err = sess.Begin(); err != nil {
  511. return err
  512. }
  513. for _, issue := range issues {
  514. issue.LabelIds = strings.Replace(issue.LabelIds, "$"+strId+"|", "", -1)
  515. if _, err = sess.Id(issue.Id).AllCols().Update(issue); err != nil {
  516. sess.Rollback()
  517. return err
  518. }
  519. }
  520. if _, err = sess.Delete(l); err != nil {
  521. sess.Rollback()
  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 CommentType = iota
  746. // Reopen action
  747. REOPEN
  748. // Close action
  749. CLOSE
  750. // Reference from another issue
  751. ISSUE
  752. // Reference from some commit (not part of a pull request)
  753. COMMIT
  754. // Reference from some pull request
  755. PULL
  756. )
  757. // Comment represents a comment in commit and issue page.
  758. type Comment struct {
  759. Id int64
  760. Type CommentType
  761. PosterId int64
  762. Poster *User `xorm:"-"`
  763. IssueId int64
  764. CommitId int64
  765. Line int64
  766. Content string `xorm:"TEXT"`
  767. Created time.Time `xorm:"CREATED"`
  768. }
  769. // CreateComment creates comment of issue or commit.
  770. func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType CommentType, content string, attachments []int64) (*Comment, error) {
  771. sess := x.NewSession()
  772. defer sess.Close()
  773. if err := sess.Begin(); err != nil {
  774. return nil, err
  775. }
  776. comment := &Comment{PosterId: userId, Type: cmtType, IssueId: issueId,
  777. CommitId: commitId, Line: line, Content: content}
  778. if _, err := sess.Insert(comment); err != nil {
  779. sess.Rollback()
  780. return nil, err
  781. }
  782. // Check comment type.
  783. switch cmtType {
  784. case COMMENT:
  785. rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
  786. if _, err := sess.Exec(rawSql, issueId); err != nil {
  787. sess.Rollback()
  788. return nil, err
  789. }
  790. if len(attachments) > 0 {
  791. rawSql = "UPDATE `attachment` SET comment_id = ? WHERE id IN (?)"
  792. astrs := make([]string, 0, len(attachments))
  793. for _, a := range attachments {
  794. astrs = append(astrs, strconv.FormatInt(a, 10))
  795. }
  796. if _, err := sess.Exec(rawSql, comment.Id, strings.Join(astrs, ",")); err != nil {
  797. sess.Rollback()
  798. return nil, err
  799. }
  800. }
  801. case REOPEN:
  802. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?"
  803. if _, err := sess.Exec(rawSql, repoId); err != nil {
  804. sess.Rollback()
  805. return nil, err
  806. }
  807. case CLOSE:
  808. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?"
  809. if _, err := sess.Exec(rawSql, repoId); err != nil {
  810. sess.Rollback()
  811. return nil, err
  812. }
  813. }
  814. return comment, sess.Commit()
  815. }
  816. // GetCommentById returns the comment with the given id
  817. func GetCommentById(commentId int64) (*Comment, error) {
  818. c := &Comment{Id: commentId}
  819. _, err := x.Get(c)
  820. return c, err
  821. }
  822. func (c *Comment) ContentHtml() template.HTML {
  823. return template.HTML(c.Content)
  824. }
  825. // GetIssueComments returns list of comment by given issue id.
  826. func GetIssueComments(issueId int64) ([]Comment, error) {
  827. comments := make([]Comment, 0, 10)
  828. err := x.Asc("created").Find(&comments, &Comment{IssueId: issueId})
  829. return comments, err
  830. }
  831. // Attachments returns the attachments for this comment.
  832. func (c *Comment) Attachments() []*Attachment {
  833. a, _ := GetAttachmentsByComment(c.Id)
  834. return a
  835. }
  836. func (c *Comment) AfterDelete() {
  837. _, err := DeleteAttachmentsByComment(c.Id, true)
  838. if err != nil {
  839. log.Info("Could not delete files for comment %d on issue #%d: %s", c.Id, c.IssueId, err)
  840. }
  841. }
  842. type Attachment struct {
  843. Id int64
  844. IssueId int64
  845. CommentId int64
  846. Name string
  847. Path string `xorm:"TEXT"`
  848. Created time.Time `xorm:"CREATED"`
  849. }
  850. // CreateAttachment creates a new attachment inside the database and
  851. func CreateAttachment(issueId, commentId int64, name, path string) (*Attachment, error) {
  852. sess := x.NewSession()
  853. defer sess.Close()
  854. if err := sess.Begin(); err != nil {
  855. return nil, err
  856. }
  857. a := &Attachment{IssueId: issueId, CommentId: commentId, Name: name, Path: path}
  858. if _, err := sess.Insert(a); err != nil {
  859. sess.Rollback()
  860. return nil, err
  861. }
  862. return a, sess.Commit()
  863. }
  864. // Attachment returns the attachment by given ID.
  865. func GetAttachmentById(id int64) (*Attachment, error) {
  866. m := &Attachment{Id: id}
  867. has, err := x.Get(m)
  868. if err != nil {
  869. return nil, err
  870. }
  871. if !has {
  872. return nil, ErrAttachmentNotExist
  873. }
  874. return m, nil
  875. }
  876. func GetAttachmentsForIssue(issueId int64) ([]*Attachment, error) {
  877. attachments := make([]*Attachment, 0, 10)
  878. err := x.Where("issue_id = ?", issueId).And("comment_id = 0").Find(&attachments)
  879. return attachments, err
  880. }
  881. // GetAttachmentsByIssue returns a list of attachments for the given issue
  882. func GetAttachmentsByIssue(issueId int64) ([]*Attachment, error) {
  883. attachments := make([]*Attachment, 0, 10)
  884. err := x.Where("issue_id = ?", issueId).And("comment_id > 0").Find(&attachments)
  885. return attachments, err
  886. }
  887. // GetAttachmentsByComment returns a list of attachments for the given comment
  888. func GetAttachmentsByComment(commentId int64) ([]*Attachment, error) {
  889. attachments := make([]*Attachment, 0, 10)
  890. err := x.Where("comment_id = ?", commentId).Find(&attachments)
  891. return attachments, err
  892. }
  893. // DeleteAttachment deletes the given attachment and optionally the associated file.
  894. func DeleteAttachment(a *Attachment, remove bool) error {
  895. _, err := DeleteAttachments([]*Attachment{a}, remove)
  896. return err
  897. }
  898. // DeleteAttachments deletes the given attachments and optionally the associated files.
  899. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  900. for i, a := range attachments {
  901. if remove {
  902. if err := os.Remove(a.Path); err != nil {
  903. return i, err
  904. }
  905. }
  906. if _, err := x.Delete(a.Id); err != nil {
  907. return i, err
  908. }
  909. }
  910. return len(attachments), nil
  911. }
  912. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  913. func DeleteAttachmentsByIssue(issueId int64, remove bool) (int, error) {
  914. attachments, err := GetAttachmentsByIssue(issueId)
  915. if err != nil {
  916. return 0, err
  917. }
  918. return DeleteAttachments(attachments, remove)
  919. }
  920. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  921. func DeleteAttachmentsByComment(commentId int64, remove bool) (int, error) {
  922. attachments, err := GetAttachmentsByComment(commentId)
  923. if err != nil {
  924. return 0, err
  925. }
  926. return DeleteAttachments(attachments, remove)
  927. }