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.

1452 lines
36 KiB

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