issue.go 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  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. "fmt"
  9. "io"
  10. "mime/multipart"
  11. "os"
  12. "path"
  13. "strings"
  14. "time"
  15. "github.com/Unknwon/com"
  16. "github.com/go-xorm/xorm"
  17. gouuid "github.com/satori/go.uuid"
  18. "github.com/gogits/gogs/modules/base"
  19. "github.com/gogits/gogs/modules/log"
  20. "github.com/gogits/gogs/modules/setting"
  21. )
  22. var (
  23. ErrWrongIssueCounter = errors.New("Invalid number of issues for this milestone")
  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. Labels []*Label `xorm:"-"`
  37. MilestoneID int64
  38. Milestone *Milestone `xorm:"-"`
  39. AssigneeID int64
  40. Assignee *User `xorm:"-"`
  41. IsRead bool `xorm:"-"`
  42. IsPull bool // Indicates whether is a pull request or not.
  43. *PullRequest `xorm:"-"`
  44. IsClosed bool
  45. Content string `xorm:"TEXT"`
  46. RenderedContent string `xorm:"-"`
  47. Priority int
  48. NumComments int
  49. Deadline time.Time
  50. Created time.Time `xorm:"CREATED"`
  51. Updated time.Time `xorm:"UPDATED"`
  52. Attachments []*Attachment `xorm:"-"`
  53. Comments []*Comment `xorm:"-"`
  54. }
  55. func (i *Issue) AfterSet(colName string, _ xorm.Cell) {
  56. var err error
  57. switch colName {
  58. case "id":
  59. i.Attachments, err = GetAttachmentsByIssueID(i.ID)
  60. if err != nil {
  61. log.Error(3, "GetAttachmentsByIssueID[%d]: %v", i.ID, err)
  62. }
  63. i.Comments, err = GetCommentsByIssueID(i.ID)
  64. if err != nil {
  65. log.Error(3, "GetCommentsByIssueID[%d]: %v", i.ID, err)
  66. }
  67. case "milestone_id":
  68. if i.MilestoneID == 0 {
  69. return
  70. }
  71. i.Milestone, err = GetMilestoneByID(i.MilestoneID)
  72. if err != nil {
  73. log.Error(3, "GetMilestoneById[%d]: %v", i.ID, err)
  74. }
  75. case "assignee_id":
  76. if i.AssigneeID == 0 {
  77. return
  78. }
  79. i.Assignee, err = GetUserByID(i.AssigneeID)
  80. if err != nil {
  81. log.Error(3, "GetUserByID[%d]: %v", i.ID, err)
  82. }
  83. case "created":
  84. i.Created = regulateTimeZone(i.Created)
  85. }
  86. }
  87. // HashTag returns unique hash tag for issue.
  88. func (i *Issue) HashTag() string {
  89. return "issue-" + com.ToStr(i.ID)
  90. }
  91. // IsPoster returns true if given user by ID is the poster.
  92. func (i *Issue) IsPoster(uid int64) bool {
  93. return i.PosterID == uid
  94. }
  95. func (i *Issue) GetPoster() (err error) {
  96. i.Poster, err = GetUserByID(i.PosterID)
  97. if IsErrUserNotExist(err) {
  98. i.PosterID = -1
  99. i.Poster = NewFakeUser()
  100. return nil
  101. }
  102. return err
  103. }
  104. func (i *Issue) hasLabel(e Engine, labelID int64) bool {
  105. return hasIssueLabel(e, i.ID, labelID)
  106. }
  107. // HasLabel returns true if issue has been labeled by given ID.
  108. func (i *Issue) HasLabel(labelID int64) bool {
  109. return i.hasLabel(x, labelID)
  110. }
  111. func (i *Issue) addLabel(e *xorm.Session, label *Label) error {
  112. return newIssueLabel(e, i, label)
  113. }
  114. // AddLabel adds new label to issue by given ID.
  115. func (i *Issue) AddLabel(label *Label) (err error) {
  116. sess := x.NewSession()
  117. defer sessionRelease(sess)
  118. if err = sess.Begin(); err != nil {
  119. return err
  120. }
  121. if err = i.addLabel(sess, label); err != nil {
  122. return err
  123. }
  124. return sess.Commit()
  125. }
  126. func (i *Issue) getLabels(e Engine) (err error) {
  127. if len(i.Labels) > 0 {
  128. return nil
  129. }
  130. i.Labels, err = getLabelsByIssueID(e, i.ID)
  131. if err != nil {
  132. return fmt.Errorf("getLabelsByIssueID: %v", err)
  133. }
  134. return nil
  135. }
  136. // GetLabels retrieves all labels of issue and assign to corresponding field.
  137. func (i *Issue) GetLabels() error {
  138. return i.getLabels(x)
  139. }
  140. func (i *Issue) removeLabel(e *xorm.Session, label *Label) error {
  141. return deleteIssueLabel(e, i, label)
  142. }
  143. // RemoveLabel removes a label from issue by given ID.
  144. func (i *Issue) RemoveLabel(label *Label) (err error) {
  145. sess := x.NewSession()
  146. defer sessionRelease(sess)
  147. if err = sess.Begin(); err != nil {
  148. return err
  149. }
  150. if err = i.removeLabel(sess, label); err != nil {
  151. return err
  152. }
  153. return sess.Commit()
  154. }
  155. func (i *Issue) ClearLabels() (err error) {
  156. sess := x.NewSession()
  157. defer sessionRelease(sess)
  158. if err = sess.Begin(); err != nil {
  159. return err
  160. }
  161. if err = i.getLabels(sess); err != nil {
  162. return err
  163. }
  164. for idx := range i.Labels {
  165. if err = i.removeLabel(sess, i.Labels[idx]); err != nil {
  166. return err
  167. }
  168. }
  169. return sess.Commit()
  170. }
  171. func (i *Issue) GetAssignee() (err error) {
  172. if i.AssigneeID == 0 || i.Assignee != nil {
  173. return nil
  174. }
  175. i.Assignee, err = GetUserByID(i.AssigneeID)
  176. if IsErrUserNotExist(err) {
  177. return nil
  178. }
  179. return err
  180. }
  181. // ReadBy sets issue to be read by given user.
  182. func (i *Issue) ReadBy(uid int64) error {
  183. return UpdateIssueUserByRead(uid, i.ID)
  184. }
  185. func (i *Issue) changeStatus(e *xorm.Session, doer *User, repo *Repository, isClosed bool) (err error) {
  186. // Nothing should be performed if current status is same as target status
  187. if i.IsClosed == isClosed {
  188. return nil
  189. }
  190. i.IsClosed = isClosed
  191. if err = updateIssueCols(e, i, "is_closed"); err != nil {
  192. return err
  193. } else if err = updateIssueUsersByStatus(e, i.ID, isClosed); err != nil {
  194. return err
  195. }
  196. // Update issue count of labels
  197. if err = i.getLabels(e); err != nil {
  198. return err
  199. }
  200. for idx := range i.Labels {
  201. if i.IsClosed {
  202. i.Labels[idx].NumClosedIssues++
  203. } else {
  204. i.Labels[idx].NumClosedIssues--
  205. }
  206. if err = updateLabel(e, i.Labels[idx]); err != nil {
  207. return err
  208. }
  209. }
  210. // Update issue count of milestone
  211. if err = changeMilestoneIssueStats(e, i); err != nil {
  212. return err
  213. }
  214. // New action comment
  215. if _, err = createStatusComment(e, doer, repo, i); err != nil {
  216. return err
  217. }
  218. return nil
  219. }
  220. // ChangeStatus changes issue status to open or closed.
  221. func (i *Issue) ChangeStatus(doer *User, repo *Repository, isClosed bool) (err error) {
  222. sess := x.NewSession()
  223. defer sessionRelease(sess)
  224. if err = sess.Begin(); err != nil {
  225. return err
  226. }
  227. if err = i.changeStatus(sess, doer, repo, isClosed); err != nil {
  228. return err
  229. }
  230. return sess.Commit()
  231. }
  232. func (i *Issue) GetPullRequest() (err error) {
  233. if i.PullRequest != nil {
  234. return nil
  235. }
  236. i.PullRequest, err = GetPullRequestByIssueID(i.ID)
  237. return err
  238. }
  239. // It's caller's responsibility to create action.
  240. func newIssue(e *xorm.Session, repo *Repository, issue *Issue, labelIDs []int64, uuids []string, isPull bool) (err error) {
  241. if _, err = e.Insert(issue); err != nil {
  242. return err
  243. }
  244. if isPull {
  245. _, err = e.Exec("UPDATE `repository` SET num_pulls=num_pulls+1 WHERE id=?", issue.RepoID)
  246. } else {
  247. _, err = e.Exec("UPDATE `repository` SET num_issues=num_issues+1 WHERE id=?", issue.RepoID)
  248. }
  249. if err != nil {
  250. return err
  251. }
  252. if len(labelIDs) > 0 {
  253. // During the session, SQLite3 dirver cannot handle retrieve objects after update something.
  254. // So we have to get all needed labels first.
  255. labels := make([]*Label, 0, len(labelIDs))
  256. if err = e.In("id", labelIDs).Find(&labels); err != nil {
  257. return fmt.Errorf("find all labels: %v", err)
  258. }
  259. for _, label := range labels {
  260. if err = issue.addLabel(e, label); err != nil {
  261. return fmt.Errorf("addLabel: %v", err)
  262. }
  263. }
  264. }
  265. if issue.MilestoneID > 0 {
  266. if err = changeMilestoneAssign(e, 0, issue); err != nil {
  267. return err
  268. }
  269. }
  270. if err = newIssueUsers(e, repo, issue); err != nil {
  271. return err
  272. }
  273. // Check attachments.
  274. attachments := make([]*Attachment, 0, len(uuids))
  275. for _, uuid := range uuids {
  276. attach, err := getAttachmentByUUID(e, uuid)
  277. if err != nil {
  278. if IsErrAttachmentNotExist(err) {
  279. continue
  280. }
  281. return fmt.Errorf("getAttachmentByUUID[%s]: %v", uuid, err)
  282. }
  283. attachments = append(attachments, attach)
  284. }
  285. for i := range attachments {
  286. attachments[i].IssueID = issue.ID
  287. // No assign value could be 0, so ignore AllCols().
  288. if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
  289. return fmt.Errorf("update attachment[%d]: %v", attachments[i].ID, err)
  290. }
  291. }
  292. return nil
  293. }
  294. // NewIssue creates new issue with labels for repository.
  295. func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
  296. sess := x.NewSession()
  297. defer sessionRelease(sess)
  298. if err = sess.Begin(); err != nil {
  299. return err
  300. }
  301. if err = newIssue(sess, repo, issue, labelIDs, uuids, false); err != nil {
  302. return fmt.Errorf("newIssue: %v", err)
  303. }
  304. // Notify watchers.
  305. act := &Action{
  306. ActUserID: issue.Poster.Id,
  307. ActUserName: issue.Poster.Name,
  308. ActEmail: issue.Poster.Email,
  309. OpType: ACTION_CREATE_ISSUE,
  310. Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name),
  311. RepoID: repo.ID,
  312. RepoUserName: repo.Owner.Name,
  313. RepoName: repo.Name,
  314. IsPrivate: repo.IsPrivate,
  315. }
  316. if err = notifyWatchers(sess, act); err != nil {
  317. return err
  318. }
  319. return sess.Commit()
  320. }
  321. // GetIssueByRef returns an Issue specified by a GFM reference.
  322. // See https://help.github.com/articles/writing-on-github#references for more information on the syntax.
  323. func GetIssueByRef(ref string) (*Issue, error) {
  324. n := strings.IndexByte(ref, byte('#'))
  325. if n == -1 {
  326. return nil, ErrMissingIssueNumber
  327. }
  328. index, err := com.StrTo(ref[n+1:]).Int64()
  329. if err != nil {
  330. return nil, err
  331. }
  332. repo, err := GetRepositoryByRef(ref[:n])
  333. if err != nil {
  334. return nil, err
  335. }
  336. issue, err := GetIssueByIndex(repo.ID, index)
  337. if err != nil {
  338. return nil, err
  339. }
  340. issue.Repo = repo
  341. return issue, nil
  342. }
  343. // GetIssueByIndex returns issue by given index in repository.
  344. func GetIssueByIndex(repoID, index int64) (*Issue, error) {
  345. issue := &Issue{
  346. RepoID: repoID,
  347. Index: index,
  348. }
  349. has, err := x.Get(issue)
  350. if err != nil {
  351. return nil, err
  352. } else if !has {
  353. return nil, ErrIssueNotExist{0, repoID, index}
  354. }
  355. return issue, nil
  356. }
  357. // GetIssueByID returns an issue by given ID.
  358. func GetIssueByID(id int64) (*Issue, error) {
  359. issue := new(Issue)
  360. has, err := x.Id(id).Get(issue)
  361. if err != nil {
  362. return nil, err
  363. } else if !has {
  364. return nil, ErrIssueNotExist{id, 0, 0}
  365. }
  366. return issue, nil
  367. }
  368. type IssuesOptions struct {
  369. UserID int64
  370. AssigneeID int64
  371. RepoID int64
  372. PosterID int64
  373. MilestoneID int64
  374. RepoIDs []int64
  375. Page int
  376. IsClosed bool
  377. IsMention bool
  378. IsPull bool
  379. Labels string
  380. SortType string
  381. }
  382. // Issues returns a list of issues by given conditions.
  383. func Issues(opts *IssuesOptions) ([]*Issue, error) {
  384. sess := x.Limit(setting.IssuePagingNum, (opts.Page-1)*setting.IssuePagingNum)
  385. if opts.RepoID > 0 {
  386. sess.Where("issue.repo_id=?", opts.RepoID).And("issue.is_closed=?", opts.IsClosed)
  387. } else if opts.RepoIDs != nil {
  388. // In case repository IDs are provided but actually no repository has issue.
  389. if len(opts.RepoIDs) == 0 {
  390. return make([]*Issue, 0), nil
  391. }
  392. sess.Where("issue.repo_id IN ("+strings.Join(base.Int64sToStrings(opts.RepoIDs), ",")+")").And("issue.is_closed=?", opts.IsClosed)
  393. } else {
  394. sess.Where("issue.is_closed=?", opts.IsClosed)
  395. }
  396. if opts.AssigneeID > 0 {
  397. sess.And("issue.assignee_id=?", opts.AssigneeID)
  398. } else if opts.PosterID > 0 {
  399. sess.And("issue.poster_id=?", opts.PosterID)
  400. }
  401. if opts.MilestoneID > 0 {
  402. sess.And("issue.milestone_id=?", opts.MilestoneID)
  403. }
  404. sess.And("issue.is_pull=?", opts.IsPull)
  405. switch opts.SortType {
  406. case "oldest":
  407. sess.Asc("created")
  408. case "recentupdate":
  409. sess.Desc("updated")
  410. case "leastupdate":
  411. sess.Asc("updated")
  412. case "mostcomment":
  413. sess.Desc("num_comments")
  414. case "leastcomment":
  415. sess.Asc("num_comments")
  416. case "priority":
  417. sess.Desc("priority")
  418. default:
  419. sess.Desc("created")
  420. }
  421. labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
  422. if len(labelIDs) > 0 {
  423. validJoin := false
  424. queryStr := "issue.id=issue_label.issue_id"
  425. for _, id := range labelIDs {
  426. if id == 0 {
  427. continue
  428. }
  429. validJoin = true
  430. queryStr += " AND issue_label.label_id=" + com.ToStr(id)
  431. }
  432. if validJoin {
  433. sess.Join("INNER", "issue_label", queryStr)
  434. }
  435. }
  436. if opts.IsMention {
  437. queryStr := "issue.id=issue_user.issue_id AND issue_user.is_mentioned=1"
  438. if opts.UserID > 0 {
  439. queryStr += " AND issue_user.uid=" + com.ToStr(opts.UserID)
  440. }
  441. sess.Join("INNER", "issue_user", queryStr)
  442. }
  443. issues := make([]*Issue, 0, setting.IssuePagingNum)
  444. return issues, sess.Find(&issues)
  445. }
  446. type IssueStatus int
  447. const (
  448. IS_OPEN = iota + 1
  449. IS_CLOSE
  450. )
  451. // GetIssueCountByPoster returns number of issues of repository by poster.
  452. func GetIssueCountByPoster(uid, rid int64, isClosed bool) int64 {
  453. count, _ := x.Where("repo_id=?", rid).And("poster_id=?", uid).And("is_closed=?", isClosed).Count(new(Issue))
  454. return count
  455. }
  456. // .___ ____ ___
  457. // | | ______ ________ __ ____ | | \______ ___________
  458. // | |/ ___// ___/ | \_/ __ \| | / ___// __ \_ __ \
  459. // | |\___ \ \___ \| | /\ ___/| | /\___ \\ ___/| | \/
  460. // |___/____ >____ >____/ \___ >______//____ >\___ >__|
  461. // \/ \/ \/ \/ \/
  462. // IssueUser represents an issue-user relation.
  463. type IssueUser struct {
  464. ID int64 `xorm:"pk autoincr"`
  465. UID int64 `xorm:"INDEX"` // User ID.
  466. IssueID int64
  467. RepoID int64 `xorm:"INDEX"`
  468. MilestoneID int64
  469. IsRead bool
  470. IsAssigned bool
  471. IsMentioned bool
  472. IsPoster bool
  473. IsClosed bool
  474. }
  475. func newIssueUsers(e *xorm.Session, repo *Repository, issue *Issue) error {
  476. users, err := repo.GetAssignees()
  477. if err != nil {
  478. return err
  479. }
  480. iu := &IssueUser{
  481. IssueID: issue.ID,
  482. RepoID: repo.ID,
  483. }
  484. // Poster can be anyone.
  485. isNeedAddPoster := true
  486. for _, u := range users {
  487. iu.ID = 0
  488. iu.UID = u.Id
  489. iu.IsPoster = iu.UID == issue.PosterID
  490. if isNeedAddPoster && iu.IsPoster {
  491. isNeedAddPoster = false
  492. }
  493. iu.IsAssigned = iu.UID == issue.AssigneeID
  494. if _, err = e.Insert(iu); err != nil {
  495. return err
  496. }
  497. }
  498. if isNeedAddPoster {
  499. iu.ID = 0
  500. iu.UID = issue.PosterID
  501. iu.IsPoster = true
  502. if _, err = e.Insert(iu); err != nil {
  503. return err
  504. }
  505. }
  506. return nil
  507. }
  508. // NewIssueUsers adds new issue-user relations for new issue of repository.
  509. func NewIssueUsers(repo *Repository, issue *Issue) (err error) {
  510. sess := x.NewSession()
  511. defer sessionRelease(sess)
  512. if err = sess.Begin(); err != nil {
  513. return err
  514. }
  515. if err = newIssueUsers(sess, repo, issue); err != nil {
  516. return err
  517. }
  518. return sess.Commit()
  519. }
  520. // PairsContains returns true when pairs list contains given issue.
  521. func PairsContains(ius []*IssueUser, issueId, uid int64) int {
  522. for i := range ius {
  523. if ius[i].IssueID == issueId &&
  524. ius[i].UID == uid {
  525. return i
  526. }
  527. }
  528. return -1
  529. }
  530. // GetIssueUsers returns issue-user pairs by given repository and user.
  531. func GetIssueUsers(rid, uid int64, isClosed bool) ([]*IssueUser, error) {
  532. ius := make([]*IssueUser, 0, 10)
  533. err := x.Where("is_closed=?", isClosed).Find(&ius, &IssueUser{RepoID: rid, UID: uid})
  534. return ius, err
  535. }
  536. // GetIssueUserPairsByRepoIds returns issue-user pairs by given repository IDs.
  537. func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*IssueUser, error) {
  538. if len(rids) == 0 {
  539. return []*IssueUser{}, nil
  540. }
  541. buf := bytes.NewBufferString("")
  542. for _, rid := range rids {
  543. buf.WriteString("repo_id=")
  544. buf.WriteString(com.ToStr(rid))
  545. buf.WriteString(" OR ")
  546. }
  547. cond := strings.TrimSuffix(buf.String(), " OR ")
  548. ius := make([]*IssueUser, 0, 10)
  549. sess := x.Limit(20, (page-1)*20).Where("is_closed=?", isClosed)
  550. if len(cond) > 0 {
  551. sess.And(cond)
  552. }
  553. err := sess.Find(&ius)
  554. return ius, err
  555. }
  556. // GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
  557. func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
  558. ius := make([]*IssueUser, 0, 10)
  559. sess := x.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
  560. if rid > 0 {
  561. sess.And("repo_id=?", rid)
  562. }
  563. switch filterMode {
  564. case FM_ASSIGN:
  565. sess.And("is_assigned=?", true)
  566. case FM_CREATE:
  567. sess.And("is_poster=?", true)
  568. default:
  569. return ius, nil
  570. }
  571. err := sess.Find(&ius)
  572. return ius, err
  573. }
  574. func UpdateMentions(userNames []string, issueId int64) error {
  575. for i := range userNames {
  576. userNames[i] = strings.ToLower(userNames[i])
  577. }
  578. users := make([]*User, 0, len(userNames))
  579. if err := x.Where("lower_name IN (?)", strings.Join(userNames, "\",\"")).OrderBy("lower_name ASC").Find(&users); err != nil {
  580. return err
  581. }
  582. ids := make([]int64, 0, len(userNames))
  583. for _, user := range users {
  584. ids = append(ids, user.Id)
  585. if !user.IsOrganization() {
  586. continue
  587. }
  588. if user.NumMembers == 0 {
  589. continue
  590. }
  591. tempIds := make([]int64, 0, user.NumMembers)
  592. orgUsers, err := GetOrgUsersByOrgId(user.Id)
  593. if err != nil {
  594. return err
  595. }
  596. for _, orgUser := range orgUsers {
  597. tempIds = append(tempIds, orgUser.ID)
  598. }
  599. ids = append(ids, tempIds...)
  600. }
  601. if err := UpdateIssueUsersByMentions(ids, issueId); err != nil {
  602. return err
  603. }
  604. return nil
  605. }
  606. // IssueStats represents issue statistic information.
  607. type IssueStats struct {
  608. OpenCount, ClosedCount int64
  609. AllCount int64
  610. AssignCount int64
  611. CreateCount int64
  612. MentionCount int64
  613. }
  614. // Filter modes.
  615. const (
  616. FM_ALL = iota
  617. FM_ASSIGN
  618. FM_CREATE
  619. FM_MENTION
  620. )
  621. func parseCountResult(results []map[string][]byte) int64 {
  622. if len(results) == 0 {
  623. return 0
  624. }
  625. for _, result := range results[0] {
  626. return com.StrTo(string(result)).MustInt64()
  627. }
  628. return 0
  629. }
  630. type IssueStatsOptions struct {
  631. RepoID int64
  632. UserID int64
  633. LabelID int64
  634. MilestoneID int64
  635. AssigneeID int64
  636. FilterMode int
  637. IsPull bool
  638. }
  639. // GetIssueStats returns issue statistic information by given conditions.
  640. func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
  641. stats := &IssueStats{}
  642. queryStr := "SELECT COUNT(*) FROM `issue` "
  643. if opts.LabelID > 0 {
  644. queryStr += "INNER JOIN `issue_label` ON `issue`.id=`issue_label`.issue_id AND `issue_label`.label_id=" + com.ToStr(opts.LabelID)
  645. }
  646. baseCond := " WHERE issue.repo_id=" + com.ToStr(opts.RepoID) + " AND issue.is_closed=?"
  647. if opts.MilestoneID > 0 {
  648. baseCond += " AND issue.milestone_id=" + com.ToStr(opts.MilestoneID)
  649. }
  650. if opts.AssigneeID > 0 {
  651. baseCond += " AND assignee_id=" + com.ToStr(opts.AssigneeID)
  652. }
  653. baseCond += " AND issue.is_pull=?"
  654. switch opts.FilterMode {
  655. case FM_ALL, FM_ASSIGN:
  656. results, _ := x.Query(queryStr+baseCond, false, opts.IsPull)
  657. stats.OpenCount = parseCountResult(results)
  658. results, _ = x.Query(queryStr+baseCond, true, opts.IsPull)
  659. stats.ClosedCount = parseCountResult(results)
  660. case FM_CREATE:
  661. baseCond += " AND poster_id=?"
  662. results, _ := x.Query(queryStr+baseCond, false, opts.IsPull, opts.UserID)
  663. stats.OpenCount = parseCountResult(results)
  664. results, _ = x.Query(queryStr+baseCond, true, opts.IsPull, opts.UserID)
  665. stats.ClosedCount = parseCountResult(results)
  666. case FM_MENTION:
  667. queryStr += " INNER JOIN `issue_user` ON `issue`.id=`issue_user`.issue_id"
  668. baseCond += " AND `issue_user`.uid=? AND `issue_user`.is_mentioned=?"
  669. results, _ := x.Query(queryStr+baseCond, false, opts.IsPull, opts.UserID, true)
  670. stats.OpenCount = parseCountResult(results)
  671. results, _ = x.Query(queryStr+baseCond, true, opts.IsPull, opts.UserID, true)
  672. stats.ClosedCount = parseCountResult(results)
  673. }
  674. return stats
  675. }
  676. // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
  677. func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int, isPull bool) *IssueStats {
  678. stats := &IssueStats{}
  679. queryStr := "SELECT COUNT(*) FROM `issue` "
  680. baseCond := " WHERE issue.is_closed=?"
  681. if repoID > 0 || len(repoIDs) == 0 {
  682. baseCond += " AND issue.repo_id=" + com.ToStr(repoID)
  683. } else {
  684. baseCond += " AND issue.repo_id IN (" + strings.Join(base.Int64sToStrings(repoIDs), ",") + ")"
  685. }
  686. if isPull {
  687. baseCond += " AND issue.is_pull=1"
  688. } else {
  689. baseCond += " AND issue.is_pull=0"
  690. }
  691. results, _ := x.Query(queryStr+baseCond+" AND assignee_id=?", false, uid)
  692. stats.AssignCount = parseCountResult(results)
  693. results, _ = x.Query(queryStr+baseCond+" AND poster_id=?", false, uid)
  694. stats.CreateCount = parseCountResult(results)
  695. switch filterMode {
  696. case FM_ASSIGN:
  697. baseCond += " AND assignee_id=" + com.ToStr(uid)
  698. case FM_CREATE:
  699. baseCond += " AND poster_id=" + com.ToStr(uid)
  700. }
  701. results, _ = x.Query(queryStr+baseCond, false)
  702. stats.OpenCount = parseCountResult(results)
  703. results, _ = x.Query(queryStr+baseCond, true)
  704. stats.ClosedCount = parseCountResult(results)
  705. return stats
  706. }
  707. // GetRepoIssueStats returns number of open and closed repository issues by given filter mode.
  708. func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen int64, numClosed int64) {
  709. queryStr := "SELECT COUNT(*) FROM `issue` "
  710. baseCond := " WHERE issue.repo_id=? AND issue.is_closed=?"
  711. if isPull {
  712. baseCond += " AND issue.is_pull=1"
  713. } else {
  714. baseCond += " AND issue.is_pull=0"
  715. }
  716. switch filterMode {
  717. case FM_ASSIGN:
  718. baseCond += " AND assignee_id=" + com.ToStr(uid)
  719. case FM_CREATE:
  720. baseCond += " AND poster_id=" + com.ToStr(uid)
  721. }
  722. results, _ := x.Query(queryStr+baseCond, repoID, false)
  723. numOpen = parseCountResult(results)
  724. results, _ = x.Query(queryStr+baseCond, repoID, true)
  725. numClosed = parseCountResult(results)
  726. return numOpen, numClosed
  727. }
  728. func updateIssue(e Engine, issue *Issue) error {
  729. _, err := e.Id(issue.ID).AllCols().Update(issue)
  730. return err
  731. }
  732. // UpdateIssue updates all fields of given issue.
  733. func UpdateIssue(issue *Issue) error {
  734. return updateIssue(x, issue)
  735. }
  736. // updateIssueCols only updates values of specific columns for given issue.
  737. func updateIssueCols(e Engine, issue *Issue, cols ...string) error {
  738. _, err := e.Id(issue.ID).Cols(cols...).Update(issue)
  739. return err
  740. }
  741. func updateIssueUsersByStatus(e Engine, issueID int64, isClosed bool) error {
  742. _, err := e.Exec("UPDATE `issue_user` SET is_closed=? WHERE issue_id=?", isClosed, issueID)
  743. return err
  744. }
  745. // UpdateIssueUsersByStatus updates issue-user relations by issue status.
  746. func UpdateIssueUsersByStatus(issueID int64, isClosed bool) error {
  747. return updateIssueUsersByStatus(x, issueID, isClosed)
  748. }
  749. func updateIssueUserByAssignee(e *xorm.Session, issue *Issue) (err error) {
  750. if _, err = e.Exec("UPDATE `issue_user` SET is_assigned=? WHERE issue_id=?", false, issue.ID); err != nil {
  751. return err
  752. }
  753. // Assignee ID equals to 0 means clear assignee.
  754. if issue.AssigneeID > 0 {
  755. if _, err = e.Exec("UPDATE `issue_user` SET is_assigned=? WHERE uid=? AND issue_id=?", true, issue.AssigneeID, issue.ID); err != nil {
  756. return err
  757. }
  758. }
  759. return updateIssue(e, issue)
  760. }
  761. // UpdateIssueUserByAssignee updates issue-user relation for assignee.
  762. func UpdateIssueUserByAssignee(issue *Issue) (err error) {
  763. sess := x.NewSession()
  764. defer sessionRelease(sess)
  765. if err = sess.Begin(); err != nil {
  766. return err
  767. }
  768. if err = updateIssueUserByAssignee(sess, issue); err != nil {
  769. return err
  770. }
  771. return sess.Commit()
  772. }
  773. // UpdateIssueUserByRead updates issue-user relation for reading.
  774. func UpdateIssueUserByRead(uid, issueID int64) error {
  775. _, err := x.Exec("UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?", true, uid, issueID)
  776. return err
  777. }
  778. // UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
  779. func UpdateIssueUsersByMentions(uids []int64, iid int64) error {
  780. for _, uid := range uids {
  781. iu := &IssueUser{UID: uid, IssueID: iid}
  782. has, err := x.Get(iu)
  783. if err != nil {
  784. return err
  785. }
  786. iu.IsMentioned = true
  787. if has {
  788. _, err = x.Id(iu.ID).AllCols().Update(iu)
  789. } else {
  790. _, err = x.Insert(iu)
  791. }
  792. if err != nil {
  793. return err
  794. }
  795. }
  796. return nil
  797. }
  798. // _____ .__.__ __
  799. // / \ |__| | ____ _______/ |_ ____ ____ ____
  800. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  801. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  802. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  803. // \/ \/ \/ \/ \/
  804. // Milestone represents a milestone of repository.
  805. type Milestone struct {
  806. ID int64 `xorm:"pk autoincr"`
  807. RepoID int64 `xorm:"INDEX"`
  808. Name string
  809. Content string `xorm:"TEXT"`
  810. RenderedContent string `xorm:"-"`
  811. IsClosed bool
  812. NumIssues int
  813. NumClosedIssues int
  814. NumOpenIssues int `xorm:"-"`
  815. Completeness int // Percentage(1-100).
  816. Deadline time.Time
  817. DeadlineString string `xorm:"-"`
  818. IsOverDue bool `xorm:"-"`
  819. ClosedDate time.Time
  820. }
  821. func (m *Milestone) BeforeUpdate() {
  822. if m.NumIssues > 0 {
  823. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  824. } else {
  825. m.Completeness = 0
  826. }
  827. }
  828. func (m *Milestone) AfterSet(colName string, _ xorm.Cell) {
  829. if colName == "deadline" {
  830. if m.Deadline.Year() == 9999 {
  831. return
  832. }
  833. m.Deadline = regulateTimeZone(m.Deadline)
  834. m.DeadlineString = m.Deadline.Format("2006-01-02")
  835. if time.Now().After(m.Deadline) {
  836. m.IsOverDue = true
  837. }
  838. }
  839. }
  840. // CalOpenIssues calculates the open issues of milestone.
  841. func (m *Milestone) CalOpenIssues() {
  842. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  843. }
  844. // NewMilestone creates new milestone of repository.
  845. func NewMilestone(m *Milestone) (err error) {
  846. sess := x.NewSession()
  847. defer sessionRelease(sess)
  848. if err = sess.Begin(); err != nil {
  849. return err
  850. }
  851. if _, err = sess.Insert(m); err != nil {
  852. return err
  853. }
  854. if _, err = sess.Exec("UPDATE `repository` SET num_milestones=num_milestones+1 WHERE id=?", m.RepoID); err != nil {
  855. return err
  856. }
  857. return sess.Commit()
  858. }
  859. func getMilestoneByID(e Engine, id int64) (*Milestone, error) {
  860. m := &Milestone{ID: id}
  861. has, err := e.Get(m)
  862. if err != nil {
  863. return nil, err
  864. } else if !has {
  865. return nil, ErrMilestoneNotExist{id, 0}
  866. }
  867. return m, nil
  868. }
  869. // GetMilestoneByID returns the milestone of given ID.
  870. func GetMilestoneByID(id int64) (*Milestone, error) {
  871. return getMilestoneByID(x, id)
  872. }
  873. // GetRepoMilestoneByID returns the milestone of given ID and repository.
  874. func GetRepoMilestoneByID(repoID, milestoneID int64) (*Milestone, error) {
  875. m := &Milestone{ID: milestoneID, RepoID: repoID}
  876. has, err := x.Get(m)
  877. if err != nil {
  878. return nil, err
  879. } else if !has {
  880. return nil, ErrMilestoneNotExist{milestoneID, repoID}
  881. }
  882. return m, nil
  883. }
  884. // GetAllRepoMilestones returns all milestones of given repository.
  885. func GetAllRepoMilestones(repoID int64) ([]*Milestone, error) {
  886. miles := make([]*Milestone, 0, 10)
  887. return miles, x.Where("repo_id=?", repoID).Find(&miles)
  888. }
  889. // GetMilestones returns a list of milestones of given repository and status.
  890. func GetMilestones(repoID int64, page int, isClosed bool) ([]*Milestone, error) {
  891. miles := make([]*Milestone, 0, setting.IssuePagingNum)
  892. sess := x.Where("repo_id=? AND is_closed=?", repoID, isClosed)
  893. if page > 0 {
  894. sess = sess.Limit(setting.IssuePagingNum, (page-1)*setting.IssuePagingNum)
  895. }
  896. return miles, sess.Find(&miles)
  897. }
  898. func updateMilestone(e Engine, m *Milestone) error {
  899. _, err := e.Id(m.ID).AllCols().Update(m)
  900. return err
  901. }
  902. // UpdateMilestone updates information of given milestone.
  903. func UpdateMilestone(m *Milestone) error {
  904. return updateMilestone(x, m)
  905. }
  906. func countRepoMilestones(e Engine, repoID int64) int64 {
  907. count, _ := e.Where("repo_id=?", repoID).Count(new(Milestone))
  908. return count
  909. }
  910. // CountRepoMilestones returns number of milestones in given repository.
  911. func CountRepoMilestones(repoID int64) int64 {
  912. return countRepoMilestones(x, repoID)
  913. }
  914. func countRepoClosedMilestones(e Engine, repoID int64) int64 {
  915. closed, _ := e.Where("repo_id=? AND is_closed=?", repoID, true).Count(new(Milestone))
  916. return closed
  917. }
  918. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  919. func CountRepoClosedMilestones(repoID int64) int64 {
  920. return countRepoClosedMilestones(x, repoID)
  921. }
  922. // MilestoneStats returns number of open and closed milestones of given repository.
  923. func MilestoneStats(repoID int64) (open int64, closed int64) {
  924. open, _ = x.Where("repo_id=? AND is_closed=?", repoID, false).Count(new(Milestone))
  925. return open, CountRepoClosedMilestones(repoID)
  926. }
  927. // ChangeMilestoneStatus changes the milestone open/closed status.
  928. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  929. repo, err := GetRepositoryByID(m.RepoID)
  930. if err != nil {
  931. return err
  932. }
  933. sess := x.NewSession()
  934. defer sessionRelease(sess)
  935. if err = sess.Begin(); err != nil {
  936. return err
  937. }
  938. m.IsClosed = isClosed
  939. if err = updateMilestone(sess, m); err != nil {
  940. return err
  941. }
  942. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  943. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  944. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  945. return err
  946. }
  947. return sess.Commit()
  948. }
  949. func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error {
  950. if issue.MilestoneID == 0 {
  951. return nil
  952. }
  953. m, err := getMilestoneByID(e, issue.MilestoneID)
  954. if err != nil {
  955. return err
  956. }
  957. if issue.IsClosed {
  958. m.NumOpenIssues--
  959. m.NumClosedIssues++
  960. } else {
  961. m.NumOpenIssues++
  962. m.NumClosedIssues--
  963. }
  964. return updateMilestone(e, m)
  965. }
  966. // ChangeMilestoneIssueStats updates the open/closed issues counter and progress
  967. // for the milestone associated with the given issue.
  968. func ChangeMilestoneIssueStats(issue *Issue) (err error) {
  969. sess := x.NewSession()
  970. defer sessionRelease(sess)
  971. if err = sess.Begin(); err != nil {
  972. return err
  973. }
  974. if err = changeMilestoneIssueStats(sess, issue); err != nil {
  975. return err
  976. }
  977. return sess.Commit()
  978. }
  979. func changeMilestoneAssign(e *xorm.Session, oldMid int64, issue *Issue) error {
  980. if oldMid > 0 {
  981. m, err := getMilestoneByID(e, oldMid)
  982. if err != nil {
  983. return err
  984. }
  985. m.NumIssues--
  986. if issue.IsClosed {
  987. m.NumClosedIssues--
  988. }
  989. if err = updateMilestone(e, m); err != nil {
  990. return err
  991. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id=0 WHERE issue_id=?", issue.ID); err != nil {
  992. return err
  993. }
  994. }
  995. if issue.MilestoneID > 0 {
  996. m, err := getMilestoneByID(e, issue.MilestoneID)
  997. if err != nil {
  998. return err
  999. }
  1000. m.NumIssues++
  1001. if issue.IsClosed {
  1002. m.NumClosedIssues++
  1003. }
  1004. if m.NumIssues == 0 {
  1005. return ErrWrongIssueCounter
  1006. }
  1007. if err = updateMilestone(e, m); err != nil {
  1008. return err
  1009. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id=? WHERE issue_id=?", m.ID, issue.ID); err != nil {
  1010. return err
  1011. }
  1012. }
  1013. return updateIssue(e, issue)
  1014. }
  1015. // ChangeMilestoneAssign changes assignment of milestone for issue.
  1016. func ChangeMilestoneAssign(oldMid int64, issue *Issue) (err error) {
  1017. sess := x.NewSession()
  1018. defer sess.Close()
  1019. if err = sess.Begin(); err != nil {
  1020. return err
  1021. }
  1022. if err = changeMilestoneAssign(sess, oldMid, issue); err != nil {
  1023. return err
  1024. }
  1025. return sess.Commit()
  1026. }
  1027. // DeleteMilestoneByID deletes a milestone by given ID.
  1028. func DeleteMilestoneByID(id int64) error {
  1029. m, err := GetMilestoneByID(id)
  1030. if err != nil {
  1031. if IsErrMilestoneNotExist(err) {
  1032. return nil
  1033. }
  1034. return err
  1035. }
  1036. repo, err := GetRepositoryByID(m.RepoID)
  1037. if err != nil {
  1038. return err
  1039. }
  1040. sess := x.NewSession()
  1041. defer sessionRelease(sess)
  1042. if err = sess.Begin(); err != nil {
  1043. return err
  1044. }
  1045. if _, err = sess.Id(m.ID).Delete(new(Milestone)); err != nil {
  1046. return err
  1047. }
  1048. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  1049. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  1050. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  1051. return err
  1052. }
  1053. if _, err = sess.Exec("UPDATE `issue` SET milestone_id=0 WHERE milestone_id=?", m.ID); err != nil {
  1054. return err
  1055. } else if _, err = sess.Exec("UPDATE `issue_user` SET milestone_id=0 WHERE milestone_id=?", m.ID); err != nil {
  1056. return err
  1057. }
  1058. return sess.Commit()
  1059. }
  1060. // Attachment represent a attachment of issue/comment/release.
  1061. type Attachment struct {
  1062. ID int64 `xorm:"pk autoincr"`
  1063. UUID string `xorm:"uuid UNIQUE"`
  1064. IssueID int64 `xorm:"INDEX"`
  1065. CommentID int64
  1066. ReleaseID int64 `xorm:"INDEX"`
  1067. Name string
  1068. Created time.Time `xorm:"CREATED"`
  1069. }
  1070. // AttachmentLocalPath returns where attachment is stored in local file system based on given UUID.
  1071. func AttachmentLocalPath(uuid string) string {
  1072. return path.Join(setting.AttachmentPath, uuid[0:1], uuid[1:2], uuid)
  1073. }
  1074. // LocalPath returns where attachment is stored in local file system.
  1075. func (attach *Attachment) LocalPath() string {
  1076. return AttachmentLocalPath(attach.UUID)
  1077. }
  1078. // NewAttachment creates a new attachment object.
  1079. func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
  1080. attach := &Attachment{
  1081. UUID: gouuid.NewV4().String(),
  1082. Name: name,
  1083. }
  1084. if err = os.MkdirAll(path.Dir(attach.LocalPath()), os.ModePerm); err != nil {
  1085. return nil, fmt.Errorf("MkdirAll: %v", err)
  1086. }
  1087. fw, err := os.Create(attach.LocalPath())
  1088. if err != nil {
  1089. return nil, fmt.Errorf("Create: %v", err)
  1090. }
  1091. defer fw.Close()
  1092. if _, err = fw.Write(buf); err != nil {
  1093. return nil, fmt.Errorf("Write: %v", err)
  1094. } else if _, err = io.Copy(fw, file); err != nil {
  1095. return nil, fmt.Errorf("Copy: %v", err)
  1096. }
  1097. sess := x.NewSession()
  1098. defer sessionRelease(sess)
  1099. if err := sess.Begin(); err != nil {
  1100. return nil, err
  1101. }
  1102. if _, err := sess.Insert(attach); err != nil {
  1103. return nil, err
  1104. }
  1105. return attach, sess.Commit()
  1106. }
  1107. func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
  1108. attach := &Attachment{UUID: uuid}
  1109. has, err := x.Get(attach)
  1110. if err != nil {
  1111. return nil, err
  1112. } else if !has {
  1113. return nil, ErrAttachmentNotExist{0, uuid}
  1114. }
  1115. return attach, nil
  1116. }
  1117. // GetAttachmentByUUID returns attachment by given UUID.
  1118. func GetAttachmentByUUID(uuid string) (*Attachment, error) {
  1119. return getAttachmentByUUID(x, uuid)
  1120. }
  1121. // GetAttachmentsByIssueID returns all attachments for given issue by ID.
  1122. func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
  1123. attachments := make([]*Attachment, 0, 10)
  1124. return attachments, x.Where("issue_id=? AND comment_id=0", issueID).Find(&attachments)
  1125. }
  1126. // GetAttachmentsByCommentID returns all attachments if comment by given ID.
  1127. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
  1128. attachments := make([]*Attachment, 0, 10)
  1129. return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
  1130. }
  1131. // DeleteAttachment deletes the given attachment and optionally the associated file.
  1132. func DeleteAttachment(a *Attachment, remove bool) error {
  1133. _, err := DeleteAttachments([]*Attachment{a}, remove)
  1134. return err
  1135. }
  1136. // DeleteAttachments deletes the given attachments and optionally the associated files.
  1137. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  1138. for i, a := range attachments {
  1139. if remove {
  1140. if err := os.Remove(a.LocalPath()); err != nil {
  1141. return i, err
  1142. }
  1143. }
  1144. if _, err := x.Delete(a.ID); err != nil {
  1145. return i, err
  1146. }
  1147. }
  1148. return len(attachments), nil
  1149. }
  1150. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  1151. func DeleteAttachmentsByIssue(issueId int64, remove bool) (int, error) {
  1152. attachments, err := GetAttachmentsByIssueID(issueId)
  1153. if err != nil {
  1154. return 0, err
  1155. }
  1156. return DeleteAttachments(attachments, remove)
  1157. }
  1158. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  1159. func DeleteAttachmentsByComment(commentId int64, remove bool) (int, error) {
  1160. attachments, err := GetAttachmentsByCommentID(commentId)
  1161. if err != nil {
  1162. return 0, err
  1163. }
  1164. return DeleteAttachments(attachments, remove)
  1165. }