issue.go 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  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. "io/ioutil"
  11. "mime/multipart"
  12. "os"
  13. "path"
  14. "strings"
  15. "time"
  16. "github.com/Unknwon/com"
  17. "github.com/go-xorm/xorm"
  18. "github.com/gogits/gogs/modules/base"
  19. "github.com/gogits/gogs/modules/git"
  20. "github.com/gogits/gogs/modules/log"
  21. "github.com/gogits/gogs/modules/process"
  22. "github.com/gogits/gogs/modules/setting"
  23. gouuid "github.com/gogits/gogs/modules/uuid"
  24. )
  25. var (
  26. ErrWrongIssueCounter = errors.New("Invalid number of issues for this milestone")
  27. ErrAttachmentNotLinked = errors.New("Attachment does not belong to this issue")
  28. ErrMissingIssueNumber = errors.New("No issue number specified")
  29. )
  30. // Issue represents an issue or pull request of repository.
  31. type Issue struct {
  32. ID int64 `xorm:"pk autoincr"`
  33. RepoID int64 `xorm:"INDEX"`
  34. Index int64 // Index in one repository.
  35. Name string
  36. Repo *Repository `xorm:"-"`
  37. PosterID int64
  38. Poster *User `xorm:"-"`
  39. Labels []*Label `xorm:"-"`
  40. MilestoneID int64
  41. Milestone *Milestone `xorm:"-"`
  42. AssigneeID int64
  43. Assignee *User `xorm:"-"`
  44. IsRead bool `xorm:"-"`
  45. IsPull bool // Indicates whether is a pull request or not.
  46. *PullRequest `xorm:"-"`
  47. IsClosed bool
  48. Content string `xorm:"TEXT"`
  49. RenderedContent string `xorm:"-"`
  50. Priority int
  51. NumComments int
  52. Deadline time.Time
  53. Created time.Time `xorm:"CREATED"`
  54. Updated time.Time `xorm:"UPDATED"`
  55. Attachments []*Attachment `xorm:"-"`
  56. Comments []*Comment `xorm:"-"`
  57. }
  58. func (i *Issue) AfterSet(colName string, _ xorm.Cell) {
  59. var err error
  60. switch colName {
  61. case "id":
  62. i.Attachments, err = GetAttachmentsByIssueID(i.ID)
  63. if err != nil {
  64. log.Error(3, "GetAttachmentsByIssueID[%d]: %v", i.ID, err)
  65. }
  66. i.Comments, err = GetCommentsByIssueID(i.ID)
  67. if err != nil {
  68. log.Error(3, "GetCommentsByIssueID[%d]: %v", i.ID, err)
  69. }
  70. case "milestone_id":
  71. if i.MilestoneID == 0 {
  72. return
  73. }
  74. i.Milestone, err = GetMilestoneByID(i.MilestoneID)
  75. if err != nil {
  76. log.Error(3, "GetMilestoneById[%d]: %v", i.ID, err)
  77. }
  78. case "assignee_id":
  79. if i.AssigneeID == 0 {
  80. return
  81. }
  82. i.Assignee, err = GetUserByID(i.AssigneeID)
  83. if err != nil {
  84. log.Error(3, "GetUserByID[%d]: %v", i.ID, err)
  85. }
  86. case "is_pull":
  87. if !i.IsPull {
  88. return
  89. }
  90. i.PullRequest, err = GetPullRequestByPullID(i.ID)
  91. if err != nil {
  92. log.Error(3, "GetPullRequestByPullID[%d]: %v", i.ID, err)
  93. }
  94. case "created":
  95. i.Created = regulateTimeZone(i.Created)
  96. }
  97. }
  98. // HashTag returns unique hash tag for issue.
  99. func (i *Issue) HashTag() string {
  100. return "issue-" + com.ToStr(i.ID)
  101. }
  102. // IsPoster returns true if given user by ID is the poster.
  103. func (i *Issue) IsPoster(uid int64) bool {
  104. return i.PosterID == uid
  105. }
  106. func (i *Issue) GetPoster() (err error) {
  107. i.Poster, err = GetUserByID(i.PosterID)
  108. if IsErrUserNotExist(err) {
  109. i.PosterID = -1
  110. i.Poster = NewFakeUser()
  111. return nil
  112. }
  113. return err
  114. }
  115. func (i *Issue) hasLabel(e Engine, labelID int64) bool {
  116. return hasIssueLabel(e, i.ID, labelID)
  117. }
  118. // HasLabel returns true if issue has been labeled by given ID.
  119. func (i *Issue) HasLabel(labelID int64) bool {
  120. return i.hasLabel(x, labelID)
  121. }
  122. func (i *Issue) addLabel(e *xorm.Session, label *Label) error {
  123. return newIssueLabel(e, i, label)
  124. }
  125. // AddLabel adds new label to issue by given ID.
  126. func (i *Issue) AddLabel(label *Label) (err error) {
  127. sess := x.NewSession()
  128. defer sessionRelease(sess)
  129. if err = sess.Begin(); err != nil {
  130. return err
  131. }
  132. if err = i.addLabel(sess, label); err != nil {
  133. return err
  134. }
  135. return sess.Commit()
  136. }
  137. func (i *Issue) getLabels(e Engine) (err error) {
  138. if len(i.Labels) > 0 {
  139. return nil
  140. }
  141. i.Labels, err = getLabelsByIssueID(e, i.ID)
  142. if err != nil {
  143. return fmt.Errorf("getLabelsByIssueID: %v", err)
  144. }
  145. return nil
  146. }
  147. // GetLabels retrieves all labels of issue and assign to corresponding field.
  148. func (i *Issue) GetLabels() error {
  149. return i.getLabels(x)
  150. }
  151. func (i *Issue) removeLabel(e *xorm.Session, label *Label) error {
  152. return deleteIssueLabel(e, i, label)
  153. }
  154. // RemoveLabel removes a label from issue by given ID.
  155. func (i *Issue) RemoveLabel(label *Label) (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.removeLabel(sess, label); err != nil {
  162. return err
  163. }
  164. return sess.Commit()
  165. }
  166. func (i *Issue) ClearLabels() (err error) {
  167. sess := x.NewSession()
  168. defer sessionRelease(sess)
  169. if err = sess.Begin(); err != nil {
  170. return err
  171. }
  172. if err = i.getLabels(sess); err != nil {
  173. return err
  174. }
  175. for idx := range i.Labels {
  176. if err = i.removeLabel(sess, i.Labels[idx]); err != nil {
  177. return err
  178. }
  179. }
  180. return sess.Commit()
  181. }
  182. func (i *Issue) GetAssignee() (err error) {
  183. if i.AssigneeID == 0 || i.Assignee != nil {
  184. return nil
  185. }
  186. i.Assignee, err = GetUserByID(i.AssigneeID)
  187. if IsErrUserNotExist(err) {
  188. return nil
  189. }
  190. return err
  191. }
  192. // ReadBy sets issue to be read by given user.
  193. func (i *Issue) ReadBy(uid int64) error {
  194. return UpdateIssueUserByRead(uid, i.ID)
  195. }
  196. func (i *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (err error) {
  197. if i.IsClosed == isClosed {
  198. return nil
  199. }
  200. i.IsClosed = isClosed
  201. if err = updateIssue(e, i); err != nil {
  202. return err
  203. } else if err = updateIssueUsersByStatus(e, i.ID, isClosed); err != nil {
  204. return err
  205. }
  206. // Update labels.
  207. if err = i.getLabels(e); err != nil {
  208. return err
  209. }
  210. for idx := range i.Labels {
  211. if i.IsClosed {
  212. i.Labels[idx].NumClosedIssues++
  213. } else {
  214. i.Labels[idx].NumClosedIssues--
  215. }
  216. if err = updateLabel(e, i.Labels[idx]); err != nil {
  217. return err
  218. }
  219. }
  220. // Update milestone.
  221. if err = changeMilestoneIssueStats(e, i); err != nil {
  222. return err
  223. }
  224. // New action comment.
  225. if _, err = createStatusComment(e, doer, i.Repo, i); err != nil {
  226. return err
  227. }
  228. return nil
  229. }
  230. // ChangeStatus changes issue status to open/closed.
  231. func (i *Issue) ChangeStatus(doer *User, isClosed bool) (err error) {
  232. sess := x.NewSession()
  233. defer sessionRelease(sess)
  234. if err = sess.Begin(); err != nil {
  235. return err
  236. }
  237. if err = i.changeStatus(sess, doer, isClosed); err != nil {
  238. return err
  239. }
  240. return sess.Commit()
  241. }
  242. // It's caller's responsibility to create action.
  243. func newIssue(e *xorm.Session, repo *Repository, issue *Issue, labelIDs []int64, uuids []string, isPull bool) (err error) {
  244. if _, err = e.Insert(issue); err != nil {
  245. return err
  246. }
  247. if isPull {
  248. _, err = e.Exec("UPDATE `repository` SET num_pulls=num_pulls+1 WHERE id=?", issue.RepoID)
  249. } else {
  250. _, err = e.Exec("UPDATE `repository` SET num_issues=num_issues+1 WHERE id=?", issue.RepoID)
  251. }
  252. if err != nil {
  253. return err
  254. }
  255. var label *Label
  256. for _, id := range labelIDs {
  257. if id == 0 {
  258. continue
  259. }
  260. label, err = getLabelByID(e, id)
  261. if err != nil {
  262. return err
  263. }
  264. if err = issue.addLabel(e, label); err != nil {
  265. return fmt.Errorf("addLabel: %v", err)
  266. }
  267. }
  268. if issue.MilestoneID > 0 {
  269. if err = changeMilestoneAssign(e, 0, issue); err != nil {
  270. return err
  271. }
  272. }
  273. if err = newIssueUsers(e, repo, issue); err != nil {
  274. return err
  275. }
  276. // Check attachments.
  277. attachments := make([]*Attachment, 0, len(uuids))
  278. for _, uuid := range uuids {
  279. attach, err := getAttachmentByUUID(e, uuid)
  280. if err != nil {
  281. if IsErrAttachmentNotExist(err) {
  282. continue
  283. }
  284. return fmt.Errorf("getAttachmentByUUID[%s]: %v", uuid, err)
  285. }
  286. attachments = append(attachments, attach)
  287. }
  288. for i := range attachments {
  289. attachments[i].IssueID = issue.ID
  290. // No assign value could be 0, so ignore AllCols().
  291. if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
  292. return fmt.Errorf("update attachment[%d]: %v", attachments[i].ID, err)
  293. }
  294. }
  295. return nil
  296. }
  297. // NewIssue creates new issue with labels for repository.
  298. func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
  299. sess := x.NewSession()
  300. defer sessionRelease(sess)
  301. if err = sess.Begin(); err != nil {
  302. return err
  303. }
  304. if err = newIssue(sess, repo, issue, labelIDs, uuids, false); err != nil {
  305. return fmt.Errorf("newIssue: %v", err)
  306. }
  307. // Notify watchers.
  308. act := &Action{
  309. ActUserID: issue.Poster.Id,
  310. ActUserName: issue.Poster.Name,
  311. ActEmail: issue.Poster.Email,
  312. OpType: CREATE_ISSUE,
  313. Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name),
  314. RepoID: repo.ID,
  315. RepoUserName: repo.Owner.Name,
  316. RepoName: repo.Name,
  317. IsPrivate: repo.IsPrivate,
  318. }
  319. if err = notifyWatchers(sess, act); err != nil {
  320. return err
  321. }
  322. return sess.Commit()
  323. }
  324. // GetIssueByRef returns an Issue specified by a GFM reference.
  325. // See https://help.github.com/articles/writing-on-github#references for more information on the syntax.
  326. func GetIssueByRef(ref string) (*Issue, error) {
  327. n := strings.IndexByte(ref, byte('#'))
  328. if n == -1 {
  329. return nil, ErrMissingIssueNumber
  330. }
  331. index, err := com.StrTo(ref[n+1:]).Int64()
  332. if err != nil {
  333. return nil, err
  334. }
  335. repo, err := GetRepositoryByRef(ref[:n])
  336. if err != nil {
  337. return nil, err
  338. }
  339. issue, err := GetIssueByIndex(repo.ID, index)
  340. if err != nil {
  341. return nil, err
  342. }
  343. issue.Repo = repo
  344. return issue, nil
  345. }
  346. // GetIssueByIndex returns issue by given index in repository.
  347. func GetIssueByIndex(repoID, index int64) (*Issue, error) {
  348. issue := &Issue{
  349. RepoID: repoID,
  350. Index: index,
  351. }
  352. has, err := x.Get(issue)
  353. if err != nil {
  354. return nil, err
  355. } else if !has {
  356. return nil, ErrIssueNotExist{0, repoID, index}
  357. }
  358. return issue, nil
  359. }
  360. // GetIssueByID returns an issue by given ID.
  361. func GetIssueByID(id int64) (*Issue, error) {
  362. issue := new(Issue)
  363. has, err := x.Id(id).Get(issue)
  364. if err != nil {
  365. return nil, err
  366. } else if !has {
  367. return nil, ErrIssueNotExist{id, 0, 0}
  368. }
  369. return issue, nil
  370. }
  371. type IssuesOptions struct {
  372. UserID int64
  373. AssigneeID int64
  374. RepoID int64
  375. PosterID int64
  376. MilestoneID int64
  377. RepoIDs []int64
  378. Page int
  379. IsClosed bool
  380. IsMention bool
  381. IsPull bool
  382. Labels string
  383. SortType string
  384. }
  385. // Issues returns a list of issues by given conditions.
  386. func Issues(opts *IssuesOptions) ([]*Issue, error) {
  387. sess := x.Limit(setting.IssuePagingNum, (opts.Page-1)*setting.IssuePagingNum)
  388. if opts.RepoID > 0 {
  389. sess.Where("issue.repo_id=?", opts.RepoID).And("issue.is_closed=?", opts.IsClosed)
  390. } else if opts.RepoIDs != nil {
  391. // In case repository IDs are provided but actually no repository has issue.
  392. if len(opts.RepoIDs) == 0 {
  393. return make([]*Issue, 0), nil
  394. }
  395. sess.Where("issue.repo_id IN ("+strings.Join(base.Int64sToStrings(opts.RepoIDs), ",")+")").And("issue.is_closed=?", opts.IsClosed)
  396. } else {
  397. sess.Where("issue.is_closed=?", opts.IsClosed)
  398. }
  399. if opts.AssigneeID > 0 {
  400. sess.And("issue.assignee_id=?", opts.AssigneeID)
  401. } else if opts.PosterID > 0 {
  402. sess.And("issue.poster_id=?", opts.PosterID)
  403. }
  404. if opts.MilestoneID > 0 {
  405. sess.And("issue.milestone_id=?", opts.MilestoneID)
  406. }
  407. sess.And("issue.is_pull=?", opts.IsPull)
  408. switch opts.SortType {
  409. case "oldest":
  410. sess.Asc("created")
  411. case "recentupdate":
  412. sess.Desc("updated")
  413. case "leastupdate":
  414. sess.Asc("updated")
  415. case "mostcomment":
  416. sess.Desc("num_comments")
  417. case "leastcomment":
  418. sess.Asc("num_comments")
  419. case "priority":
  420. sess.Desc("priority")
  421. default:
  422. sess.Desc("created")
  423. }
  424. labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
  425. if len(labelIDs) > 0 {
  426. validJoin := false
  427. queryStr := "issue.id=issue_label.issue_id"
  428. for _, id := range labelIDs {
  429. if id == 0 {
  430. continue
  431. }
  432. validJoin = true
  433. queryStr += " AND issue_label.label_id=" + com.ToStr(id)
  434. }
  435. if validJoin {
  436. sess.Join("INNER", "issue_label", queryStr)
  437. }
  438. }
  439. if opts.IsMention {
  440. queryStr := "issue.id=issue_user.issue_id AND issue_user.is_mentioned=1"
  441. if opts.UserID > 0 {
  442. queryStr += " AND issue_user.uid=" + com.ToStr(opts.UserID)
  443. }
  444. sess.Join("INNER", "issue_user", queryStr)
  445. }
  446. issues := make([]*Issue, 0, setting.IssuePagingNum)
  447. return issues, sess.Find(&issues)
  448. }
  449. type IssueStatus int
  450. const (
  451. IS_OPEN = iota + 1
  452. IS_CLOSE
  453. )
  454. // GetIssueCountByPoster returns number of issues of repository by poster.
  455. func GetIssueCountByPoster(uid, rid int64, isClosed bool) int64 {
  456. count, _ := x.Where("repo_id=?", rid).And("poster_id=?", uid).And("is_closed=?", isClosed).Count(new(Issue))
  457. return count
  458. }
  459. // .___ ____ ___
  460. // | | ______ ________ __ ____ | | \______ ___________
  461. // | |/ ___// ___/ | \_/ __ \| | / ___// __ \_ __ \
  462. // | |\___ \ \___ \| | /\ ___/| | /\___ \\ ___/| | \/
  463. // |___/____ >____ >____/ \___ >______//____ >\___ >__|
  464. // \/ \/ \/ \/ \/
  465. // IssueUser represents an issue-user relation.
  466. type IssueUser struct {
  467. ID int64 `xorm:"pk autoincr"`
  468. UID int64 `xorm:"INDEX"` // User ID.
  469. IssueID int64
  470. RepoID int64 `xorm:"INDEX"`
  471. MilestoneID int64
  472. IsRead bool
  473. IsAssigned bool
  474. IsMentioned bool
  475. IsPoster bool
  476. IsClosed bool
  477. }
  478. func newIssueUsers(e *xorm.Session, repo *Repository, issue *Issue) error {
  479. users, err := repo.GetAssignees()
  480. if err != nil {
  481. return err
  482. }
  483. iu := &IssueUser{
  484. IssueID: issue.ID,
  485. RepoID: repo.ID,
  486. }
  487. // Poster can be anyone.
  488. isNeedAddPoster := true
  489. for _, u := range users {
  490. iu.ID = 0
  491. iu.UID = u.Id
  492. iu.IsPoster = iu.UID == issue.PosterID
  493. if isNeedAddPoster && iu.IsPoster {
  494. isNeedAddPoster = false
  495. }
  496. iu.IsAssigned = iu.UID == issue.AssigneeID
  497. if _, err = e.Insert(iu); err != nil {
  498. return err
  499. }
  500. }
  501. if isNeedAddPoster {
  502. iu.ID = 0
  503. iu.UID = issue.PosterID
  504. iu.IsPoster = true
  505. if _, err = e.Insert(iu); err != nil {
  506. return err
  507. }
  508. }
  509. return nil
  510. }
  511. // NewIssueUsers adds new issue-user relations for new issue of repository.
  512. func NewIssueUsers(repo *Repository, issue *Issue) (err error) {
  513. sess := x.NewSession()
  514. defer sessionRelease(sess)
  515. if err = sess.Begin(); err != nil {
  516. return err
  517. }
  518. if err = newIssueUsers(sess, repo, issue); err != nil {
  519. return err
  520. }
  521. return sess.Commit()
  522. }
  523. // PairsContains returns true when pairs list contains given issue.
  524. func PairsContains(ius []*IssueUser, issueId, uid int64) int {
  525. for i := range ius {
  526. if ius[i].IssueID == issueId &&
  527. ius[i].UID == uid {
  528. return i
  529. }
  530. }
  531. return -1
  532. }
  533. // GetIssueUsers returns issue-user pairs by given repository and user.
  534. func GetIssueUsers(rid, uid int64, isClosed bool) ([]*IssueUser, error) {
  535. ius := make([]*IssueUser, 0, 10)
  536. err := x.Where("is_closed=?", isClosed).Find(&ius, &IssueUser{RepoID: rid, UID: uid})
  537. return ius, err
  538. }
  539. // GetIssueUserPairsByRepoIds returns issue-user pairs by given repository IDs.
  540. func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*IssueUser, error) {
  541. if len(rids) == 0 {
  542. return []*IssueUser{}, nil
  543. }
  544. buf := bytes.NewBufferString("")
  545. for _, rid := range rids {
  546. buf.WriteString("repo_id=")
  547. buf.WriteString(com.ToStr(rid))
  548. buf.WriteString(" OR ")
  549. }
  550. cond := strings.TrimSuffix(buf.String(), " OR ")
  551. ius := make([]*IssueUser, 0, 10)
  552. sess := x.Limit(20, (page-1)*20).Where("is_closed=?", isClosed)
  553. if len(cond) > 0 {
  554. sess.And(cond)
  555. }
  556. err := sess.Find(&ius)
  557. return ius, err
  558. }
  559. // GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
  560. func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
  561. ius := make([]*IssueUser, 0, 10)
  562. sess := x.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
  563. if rid > 0 {
  564. sess.And("repo_id=?", rid)
  565. }
  566. switch filterMode {
  567. case FM_ASSIGN:
  568. sess.And("is_assigned=?", true)
  569. case FM_CREATE:
  570. sess.And("is_poster=?", true)
  571. default:
  572. return ius, nil
  573. }
  574. err := sess.Find(&ius)
  575. return ius, err
  576. }
  577. // IssueStats represents issue statistic information.
  578. type IssueStats struct {
  579. OpenCount, ClosedCount int64
  580. AllCount int64
  581. AssignCount int64
  582. CreateCount int64
  583. MentionCount int64
  584. }
  585. // Filter modes.
  586. const (
  587. FM_ALL = iota
  588. FM_ASSIGN
  589. FM_CREATE
  590. FM_MENTION
  591. )
  592. func parseCountResult(results []map[string][]byte) int64 {
  593. if len(results) == 0 {
  594. return 0
  595. }
  596. for _, result := range results[0] {
  597. return com.StrTo(string(result)).MustInt64()
  598. }
  599. return 0
  600. }
  601. type IssueStatsOptions struct {
  602. RepoID int64
  603. UserID int64
  604. LabelID int64
  605. MilestoneID int64
  606. AssigneeID int64
  607. FilterMode int
  608. IsPull bool
  609. }
  610. // GetIssueStats returns issue statistic information by given conditions.
  611. func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
  612. stats := &IssueStats{}
  613. queryStr := "SELECT COUNT(*) FROM `issue` "
  614. if opts.LabelID > 0 {
  615. queryStr += "INNER JOIN `issue_label` ON `issue`.id=`issue_label`.issue_id AND `issue_label`.label_id=" + com.ToStr(opts.LabelID)
  616. }
  617. baseCond := " WHERE issue.repo_id=" + com.ToStr(opts.RepoID) + " AND issue.is_closed=?"
  618. if opts.MilestoneID > 0 {
  619. baseCond += " AND issue.milestone_id=" + com.ToStr(opts.MilestoneID)
  620. }
  621. if opts.AssigneeID > 0 {
  622. baseCond += " AND assignee_id=" + com.ToStr(opts.AssigneeID)
  623. }
  624. if opts.IsPull {
  625. baseCond += " AND issue.is_pull=1"
  626. } else {
  627. baseCond += " AND issue.is_pull=0"
  628. }
  629. switch opts.FilterMode {
  630. case FM_ALL, FM_ASSIGN:
  631. results, _ := x.Query(queryStr+baseCond, false)
  632. stats.OpenCount = parseCountResult(results)
  633. results, _ = x.Query(queryStr+baseCond, true)
  634. stats.ClosedCount = parseCountResult(results)
  635. case FM_CREATE:
  636. baseCond += " AND poster_id=?"
  637. results, _ := x.Query(queryStr+baseCond, false, opts.UserID)
  638. stats.OpenCount = parseCountResult(results)
  639. results, _ = x.Query(queryStr+baseCond, true, opts.UserID)
  640. stats.ClosedCount = parseCountResult(results)
  641. case FM_MENTION:
  642. queryStr += " INNER JOIN `issue_user` ON `issue`.id=`issue_user`.issue_id"
  643. baseCond += " AND `issue_user`.uid=? AND `issue_user`.is_mentioned=?"
  644. results, _ := x.Query(queryStr+baseCond, false, opts.UserID, true)
  645. stats.OpenCount = parseCountResult(results)
  646. results, _ = x.Query(queryStr+baseCond, true, opts.UserID, true)
  647. stats.ClosedCount = parseCountResult(results)
  648. }
  649. return stats
  650. }
  651. // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
  652. func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int, isPull bool) *IssueStats {
  653. stats := &IssueStats{}
  654. queryStr := "SELECT COUNT(*) FROM `issue` "
  655. baseCond := " WHERE issue.is_closed=?"
  656. if repoID > 0 || len(repoIDs) == 0 {
  657. baseCond += " AND issue.repo_id=" + com.ToStr(repoID)
  658. } else {
  659. baseCond += " AND issue.repo_id IN (" + strings.Join(base.Int64sToStrings(repoIDs), ",") + ")"
  660. }
  661. if isPull {
  662. baseCond += " AND issue.is_pull=1"
  663. } else {
  664. baseCond += " AND issue.is_pull=0"
  665. }
  666. results, _ := x.Query(queryStr+baseCond+" AND assignee_id=?", false, uid)
  667. stats.AssignCount = parseCountResult(results)
  668. results, _ = x.Query(queryStr+baseCond+" AND poster_id=?", false, uid)
  669. stats.CreateCount = parseCountResult(results)
  670. switch filterMode {
  671. case FM_ASSIGN:
  672. baseCond += " AND assignee_id=" + com.ToStr(uid)
  673. case FM_CREATE:
  674. baseCond += " AND poster_id=" + com.ToStr(uid)
  675. }
  676. results, _ = x.Query(queryStr+baseCond, false)
  677. stats.OpenCount = parseCountResult(results)
  678. results, _ = x.Query(queryStr+baseCond, true)
  679. stats.ClosedCount = parseCountResult(results)
  680. return stats
  681. }
  682. // GetRepoIssueStats returns number of open and closed repository issues by given filter mode.
  683. func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen int64, numClosed int64) {
  684. queryStr := "SELECT COUNT(*) FROM `issue` "
  685. baseCond := " WHERE issue.repo_id=? AND issue.is_closed=?"
  686. if isPull {
  687. baseCond += " AND issue.is_pull=1"
  688. } else {
  689. baseCond += " AND issue.is_pull=0"
  690. }
  691. switch filterMode {
  692. case FM_ASSIGN:
  693. baseCond += " AND assignee_id=" + com.ToStr(uid)
  694. case FM_CREATE:
  695. baseCond += " AND poster_id=" + com.ToStr(uid)
  696. }
  697. results, _ := x.Query(queryStr+baseCond, repoID, false)
  698. numOpen = parseCountResult(results)
  699. results, _ = x.Query(queryStr+baseCond, repoID, true)
  700. numClosed = parseCountResult(results)
  701. return numOpen, numClosed
  702. }
  703. func updateIssue(e Engine, issue *Issue) error {
  704. _, err := e.Id(issue.ID).AllCols().Update(issue)
  705. return err
  706. }
  707. // UpdateIssue updates information of issue.
  708. func UpdateIssue(issue *Issue) error {
  709. return updateIssue(x, issue)
  710. }
  711. func updateIssueUsersByStatus(e Engine, issueID int64, isClosed bool) error {
  712. _, err := e.Exec("UPDATE `issue_user` SET is_closed=? WHERE issue_id=?", isClosed, issueID)
  713. return err
  714. }
  715. // UpdateIssueUsersByStatus updates issue-user relations by issue status.
  716. func UpdateIssueUsersByStatus(issueID int64, isClosed bool) error {
  717. return updateIssueUsersByStatus(x, issueID, isClosed)
  718. }
  719. func updateIssueUserByAssignee(e *xorm.Session, issue *Issue) (err error) {
  720. if _, err = e.Exec("UPDATE `issue_user` SET is_assigned=? WHERE issue_id=?", false, issue.ID); err != nil {
  721. return err
  722. }
  723. // Assignee ID equals to 0 means clear assignee.
  724. if issue.AssigneeID > 0 {
  725. if _, err = e.Exec("UPDATE `issue_user` SET is_assigned=? WHERE uid=? AND issue_id=?", true, issue.AssigneeID, issue.ID); err != nil {
  726. return err
  727. }
  728. }
  729. return updateIssue(e, issue)
  730. }
  731. // UpdateIssueUserByAssignee updates issue-user relation for assignee.
  732. func UpdateIssueUserByAssignee(issue *Issue) (err error) {
  733. sess := x.NewSession()
  734. defer sessionRelease(sess)
  735. if err = sess.Begin(); err != nil {
  736. return err
  737. }
  738. if err = updateIssueUserByAssignee(sess, issue); err != nil {
  739. return err
  740. }
  741. return sess.Commit()
  742. }
  743. // UpdateIssueUserByRead updates issue-user relation for reading.
  744. func UpdateIssueUserByRead(uid, issueID int64) error {
  745. _, err := x.Exec("UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?", true, uid, issueID)
  746. return err
  747. }
  748. // UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
  749. func UpdateIssueUsersByMentions(uids []int64, iid int64) error {
  750. for _, uid := range uids {
  751. iu := &IssueUser{UID: uid, IssueID: iid}
  752. has, err := x.Get(iu)
  753. if err != nil {
  754. return err
  755. }
  756. iu.IsMentioned = true
  757. if has {
  758. _, err = x.Id(iu.ID).AllCols().Update(iu)
  759. } else {
  760. _, err = x.Insert(iu)
  761. }
  762. if err != nil {
  763. return err
  764. }
  765. }
  766. return nil
  767. }
  768. // __________ .__ .__ __________ __
  769. // \______ \__ __| | | |\______ \ ____ ________ __ ____ _______/ |_
  770. // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
  771. // | | | | / |_| |_| | \ ___< <_| | | /\ ___/ \___ \ | |
  772. // |____| |____/|____/____/____|_ /\___ >__ |____/ \___ >____ > |__|
  773. // \/ \/ |__| \/ \/
  774. type PullRequestType int
  775. const (
  776. PULL_REQUEST_GOGS = iota
  777. PLLL_ERQUEST_GIT
  778. )
  779. // PullRequest represents relation between pull request and repositories.
  780. type PullRequest struct {
  781. ID int64 `xorm:"pk autoincr"`
  782. PullID int64 `xorm:"INDEX"`
  783. Pull *Issue `xorm:"-"`
  784. PullIndex int64
  785. HeadRepoID int64
  786. HeadRepo *Repository `xorm:"-"`
  787. BaseRepoID int64
  788. HeadUserName string
  789. HeadBarcnh string
  790. BaseBranch string
  791. MergeBase string `xorm:"VARCHAR(40)"`
  792. MergedCommitID string `xorm:"VARCHAR(40)"`
  793. Type PullRequestType
  794. CanAutoMerge bool
  795. HasMerged bool
  796. Merged time.Time
  797. MergerID int64
  798. Merger *User `xorm:"-"`
  799. }
  800. // Note: don't try to get Pull because will end up recursive querying.
  801. func (pr *PullRequest) AfterSet(colName string, _ xorm.Cell) {
  802. var err error
  803. switch colName {
  804. case "head_repo_id":
  805. // FIXME: shouldn't show error if it's known that head repository has been removed.
  806. pr.HeadRepo, err = GetRepositoryByID(pr.HeadRepoID)
  807. if err != nil {
  808. log.Error(3, "GetRepositoryByID[%d]: %v", pr.ID, err)
  809. }
  810. case "merger_id":
  811. if !pr.HasMerged {
  812. return
  813. }
  814. pr.Merger, err = GetUserByID(pr.MergerID)
  815. if err != nil {
  816. if IsErrUserNotExist(err) {
  817. pr.MergerID = -1
  818. pr.Merger = NewFakeUser()
  819. } else {
  820. log.Error(3, "GetUserByID[%d]: %v", pr.ID, err)
  821. }
  822. }
  823. case "merged":
  824. if !pr.HasMerged {
  825. return
  826. }
  827. pr.Merged = regulateTimeZone(pr.Merged)
  828. }
  829. }
  830. // Merge merges pull request to base repository.
  831. func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository) (err error) {
  832. sess := x.NewSession()
  833. defer sessionRelease(sess)
  834. if err = sess.Begin(); err != nil {
  835. return err
  836. }
  837. if err = pr.Pull.changeStatus(sess, doer, true); err != nil {
  838. return fmt.Errorf("Pull.changeStatus: %v", err)
  839. }
  840. headRepoPath := RepoPath(pr.HeadUserName, pr.HeadRepo.Name)
  841. headGitRepo, err := git.OpenRepository(headRepoPath)
  842. if err != nil {
  843. return fmt.Errorf("OpenRepository: %v", err)
  844. }
  845. pr.MergedCommitID, err = headGitRepo.GetCommitIdOfBranch(pr.HeadBarcnh)
  846. if err != nil {
  847. return fmt.Errorf("GetCommitIdOfBranch: %v", err)
  848. }
  849. if err = mergePullRequestAction(sess, doer, pr.Pull.Repo, pr.Pull); err != nil {
  850. return fmt.Errorf("mergePullRequestAction: %v", err)
  851. }
  852. pr.HasMerged = true
  853. pr.Merged = time.Now()
  854. pr.MergerID = doer.Id
  855. if _, err = sess.Id(pr.ID).AllCols().Update(pr); err != nil {
  856. return fmt.Errorf("update pull request: %v", err)
  857. }
  858. // Clone base repo.
  859. tmpBasePath := path.Join("data/tmp/repos", com.ToStr(time.Now().Nanosecond())+".git")
  860. os.MkdirAll(path.Dir(tmpBasePath), os.ModePerm)
  861. defer os.RemoveAll(path.Dir(tmpBasePath))
  862. var stderr string
  863. if _, stderr, err = process.ExecTimeout(5*time.Minute,
  864. fmt.Sprintf("PullRequest.Merge(git clone): %s", tmpBasePath),
  865. "git", "clone", baseGitRepo.Path, tmpBasePath); err != nil {
  866. return fmt.Errorf("git clone: %s", stderr)
  867. }
  868. // Check out base branch.
  869. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  870. fmt.Sprintf("PullRequest.Merge(git checkout): %s", tmpBasePath),
  871. "git", "checkout", pr.BaseBranch); err != nil {
  872. return fmt.Errorf("git checkout: %s", stderr)
  873. }
  874. // Pull commits.
  875. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  876. fmt.Sprintf("PullRequest.Merge(git pull): %s", tmpBasePath),
  877. "git", "pull", headRepoPath, pr.HeadBarcnh); err != nil {
  878. return fmt.Errorf("git pull[%s / %s -> %s]: %s", headRepoPath, pr.HeadBarcnh, tmpBasePath, stderr)
  879. }
  880. // Push back to upstream.
  881. if _, stderr, err = process.ExecDir(-1, tmpBasePath,
  882. fmt.Sprintf("PullRequest.Merge(git push): %s", tmpBasePath),
  883. "git", "push", baseGitRepo.Path, pr.BaseBranch); err != nil {
  884. return fmt.Errorf("git push: %s", stderr)
  885. }
  886. return sess.Commit()
  887. }
  888. // NewPullRequest creates new pull request with labels for repository.
  889. func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte) (err error) {
  890. sess := x.NewSession()
  891. defer sessionRelease(sess)
  892. if err = sess.Begin(); err != nil {
  893. return err
  894. }
  895. if err = newIssue(sess, repo, pull, labelIDs, uuids, true); err != nil {
  896. return fmt.Errorf("newIssue: %v", err)
  897. }
  898. // Notify watchers.
  899. act := &Action{
  900. ActUserID: pull.Poster.Id,
  901. ActUserName: pull.Poster.Name,
  902. ActEmail: pull.Poster.Email,
  903. OpType: CREATE_PULL_REQUEST,
  904. Content: fmt.Sprintf("%d|%s", pull.Index, pull.Name),
  905. RepoID: repo.ID,
  906. RepoUserName: repo.Owner.Name,
  907. RepoName: repo.Name,
  908. IsPrivate: repo.IsPrivate,
  909. }
  910. if err = notifyWatchers(sess, act); err != nil {
  911. return err
  912. }
  913. // Test apply patch.
  914. if err = repo.UpdateLocalCopy(); err != nil {
  915. return fmt.Errorf("UpdateLocalCopy: %v", err)
  916. }
  917. repoPath, err := repo.RepoPath()
  918. if err != nil {
  919. return fmt.Errorf("RepoPath: %v", err)
  920. }
  921. patchPath := path.Join(repoPath, "pulls", com.ToStr(pull.ID)+".patch")
  922. os.MkdirAll(path.Dir(patchPath), os.ModePerm)
  923. if err = ioutil.WriteFile(patchPath, patch, 0644); err != nil {
  924. return fmt.Errorf("save patch: %v", err)
  925. }
  926. pr.CanAutoMerge = true
  927. _, stderr, err := process.ExecDir(-1, repo.LocalCopyPath(),
  928. fmt.Sprintf("NewPullRequest(git apply --check): %d", repo.ID),
  929. "git", "apply", "--check", patchPath)
  930. if err != nil {
  931. if strings.Contains(stderr, "patch does not apply") {
  932. pr.CanAutoMerge = false
  933. } else {
  934. return fmt.Errorf("git apply --check: %v - %s", err, stderr)
  935. }
  936. }
  937. pr.PullID = pull.ID
  938. pr.PullIndex = pull.Index
  939. if _, err = sess.Insert(pr); err != nil {
  940. return fmt.Errorf("insert pull repo: %v", err)
  941. }
  942. return sess.Commit()
  943. }
  944. // GetUnmergedPullRequest returnss a pull request hasn't been merged by given info.
  945. func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) {
  946. pr := &PullRequest{
  947. HeadRepoID: headRepoID,
  948. BaseRepoID: baseRepoID,
  949. HeadBarcnh: headBranch,
  950. BaseBranch: baseBranch,
  951. }
  952. has, err := x.Where("has_merged=?", false).Get(pr)
  953. if err != nil {
  954. return nil, err
  955. } else if !has {
  956. return nil, ErrPullRequestNotExist{0, 0, headRepoID, baseRepoID, headBranch, baseBranch}
  957. }
  958. return pr, nil
  959. }
  960. // GetPullRequestByPullID returns pull repo by given pull ID.
  961. func GetPullRequestByPullID(pullID int64) (*PullRequest, error) {
  962. pr := new(PullRequest)
  963. has, err := x.Where("pull_id=?", pullID).Get(pr)
  964. if err != nil {
  965. return nil, err
  966. } else if !has {
  967. return nil, ErrPullRequestNotExist{0, pullID, 0, 0, "", ""}
  968. }
  969. return pr, nil
  970. }
  971. // .____ ___. .__
  972. // | | _____ \_ |__ ____ | |
  973. // | | \__ \ | __ \_/ __ \| |
  974. // | |___ / __ \| \_\ \ ___/| |__
  975. // |_______ (____ /___ /\___ >____/
  976. // \/ \/ \/ \/
  977. // Label represents a label of repository for issues.
  978. type Label struct {
  979. ID int64 `xorm:"pk autoincr"`
  980. RepoID int64 `xorm:"INDEX"`
  981. Name string
  982. Color string `xorm:"VARCHAR(7)"`
  983. NumIssues int
  984. NumClosedIssues int
  985. NumOpenIssues int `xorm:"-"`
  986. IsChecked bool `xorm:"-"`
  987. }
  988. // CalOpenIssues calculates the open issues of label.
  989. func (m *Label) CalOpenIssues() {
  990. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  991. }
  992. // NewLabel creates new label of repository.
  993. func NewLabel(l *Label) error {
  994. _, err := x.Insert(l)
  995. return err
  996. }
  997. func getLabelByID(e Engine, id int64) (*Label, error) {
  998. if id <= 0 {
  999. return nil, ErrLabelNotExist{id}
  1000. }
  1001. l := &Label{ID: id}
  1002. has, err := x.Get(l)
  1003. if err != nil {
  1004. return nil, err
  1005. } else if !has {
  1006. return nil, ErrLabelNotExist{l.ID}
  1007. }
  1008. return l, nil
  1009. }
  1010. // GetLabelByID returns a label by given ID.
  1011. func GetLabelByID(id int64) (*Label, error) {
  1012. return getLabelByID(x, id)
  1013. }
  1014. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
  1015. func GetLabelsByRepoID(repoID int64) ([]*Label, error) {
  1016. labels := make([]*Label, 0, 10)
  1017. return labels, x.Where("repo_id=?", repoID).Find(&labels)
  1018. }
  1019. func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
  1020. issueLabels, err := getIssueLabels(e, issueID)
  1021. if err != nil {
  1022. return nil, fmt.Errorf("getIssueLabels: %v", err)
  1023. }
  1024. var label *Label
  1025. labels := make([]*Label, 0, len(issueLabels))
  1026. for idx := range issueLabels {
  1027. label, err = getLabelByID(e, issueLabels[idx].LabelID)
  1028. if err != nil && !IsErrLabelNotExist(err) {
  1029. return nil, fmt.Errorf("getLabelByID: %v", err)
  1030. }
  1031. labels = append(labels, label)
  1032. }
  1033. return labels, nil
  1034. }
  1035. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
  1036. func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
  1037. return getLabelsByIssueID(x, issueID)
  1038. }
  1039. func updateLabel(e Engine, l *Label) error {
  1040. _, err := e.Id(l.ID).AllCols().Update(l)
  1041. return err
  1042. }
  1043. // UpdateLabel updates label information.
  1044. func UpdateLabel(l *Label) error {
  1045. return updateLabel(x, l)
  1046. }
  1047. // DeleteLabel delete a label of given repository.
  1048. func DeleteLabel(repoID, labelID int64) error {
  1049. l, err := GetLabelByID(labelID)
  1050. if err != nil {
  1051. if IsErrLabelNotExist(err) {
  1052. return nil
  1053. }
  1054. return err
  1055. }
  1056. sess := x.NewSession()
  1057. defer sessionRelease(sess)
  1058. if err = sess.Begin(); err != nil {
  1059. return err
  1060. }
  1061. if _, err = x.Where("label_id=?", labelID).Delete(new(IssueLabel)); err != nil {
  1062. return err
  1063. } else if _, err = sess.Delete(l); err != nil {
  1064. return err
  1065. }
  1066. return sess.Commit()
  1067. }
  1068. // .___ .____ ___. .__
  1069. // | | ______ ________ __ ____ | | _____ \_ |__ ____ | |
  1070. // | |/ ___// ___/ | \_/ __ \| | \__ \ | __ \_/ __ \| |
  1071. // | |\___ \ \___ \| | /\ ___/| |___ / __ \| \_\ \ ___/| |__
  1072. // |___/____ >____ >____/ \___ >_______ (____ /___ /\___ >____/
  1073. // \/ \/ \/ \/ \/ \/ \/
  1074. // IssueLabel represetns an issue-lable relation.
  1075. type IssueLabel struct {
  1076. ID int64 `xorm:"pk autoincr"`
  1077. IssueID int64 `xorm:"UNIQUE(s)"`
  1078. LabelID int64 `xorm:"UNIQUE(s)"`
  1079. }
  1080. func hasIssueLabel(e Engine, issueID, labelID int64) bool {
  1081. has, _ := e.Where("issue_id=? AND label_id=?", issueID, labelID).Get(new(IssueLabel))
  1082. return has
  1083. }
  1084. // HasIssueLabel returns true if issue has been labeled.
  1085. func HasIssueLabel(issueID, labelID int64) bool {
  1086. return hasIssueLabel(x, issueID, labelID)
  1087. }
  1088. func newIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  1089. if _, err = e.Insert(&IssueLabel{
  1090. IssueID: issue.ID,
  1091. LabelID: label.ID,
  1092. }); err != nil {
  1093. return err
  1094. }
  1095. label.NumIssues++
  1096. if issue.IsClosed {
  1097. label.NumClosedIssues++
  1098. }
  1099. return updateLabel(e, label)
  1100. }
  1101. // NewIssueLabel creates a new issue-label relation.
  1102. func NewIssueLabel(issue *Issue, label *Label) (err error) {
  1103. sess := x.NewSession()
  1104. defer sessionRelease(sess)
  1105. if err = sess.Begin(); err != nil {
  1106. return err
  1107. }
  1108. if err = newIssueLabel(sess, issue, label); err != nil {
  1109. return err
  1110. }
  1111. return sess.Commit()
  1112. }
  1113. func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
  1114. issueLabels := make([]*IssueLabel, 0, 10)
  1115. return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels)
  1116. }
  1117. // GetIssueLabels returns all issue-label relations of given issue by ID.
  1118. func GetIssueLabels(issueID int64) ([]*IssueLabel, error) {
  1119. return getIssueLabels(x, issueID)
  1120. }
  1121. func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  1122. if _, err = e.Delete(&IssueLabel{
  1123. IssueID: issue.ID,
  1124. LabelID: label.ID,
  1125. }); err != nil {
  1126. return err
  1127. }
  1128. label.NumIssues--
  1129. if issue.IsClosed {
  1130. label.NumClosedIssues--
  1131. }
  1132. return updateLabel(e, label)
  1133. }
  1134. // DeleteIssueLabel deletes issue-label relation.
  1135. func DeleteIssueLabel(issue *Issue, label *Label) (err error) {
  1136. sess := x.NewSession()
  1137. defer sessionRelease(sess)
  1138. if err = sess.Begin(); err != nil {
  1139. return err
  1140. }
  1141. if err = deleteIssueLabel(sess, issue, label); err != nil {
  1142. return err
  1143. }
  1144. return sess.Commit()
  1145. }
  1146. // _____ .__.__ __
  1147. // / \ |__| | ____ _______/ |_ ____ ____ ____
  1148. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  1149. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  1150. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  1151. // \/ \/ \/ \/ \/
  1152. // Milestone represents a milestone of repository.
  1153. type Milestone struct {
  1154. ID int64 `xorm:"pk autoincr"`
  1155. RepoID int64 `xorm:"INDEX"`
  1156. Name string
  1157. Content string `xorm:"TEXT"`
  1158. RenderedContent string `xorm:"-"`
  1159. IsClosed bool
  1160. NumIssues int
  1161. NumClosedIssues int
  1162. NumOpenIssues int `xorm:"-"`
  1163. Completeness int // Percentage(1-100).
  1164. Deadline time.Time
  1165. DeadlineString string `xorm:"-"`
  1166. IsOverDue bool `xorm:"-"`
  1167. ClosedDate time.Time
  1168. }
  1169. func (m *Milestone) BeforeUpdate() {
  1170. if m.NumIssues > 0 {
  1171. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  1172. } else {
  1173. m.Completeness = 0
  1174. }
  1175. }
  1176. func (m *Milestone) AfterSet(colName string, _ xorm.Cell) {
  1177. if colName == "deadline" {
  1178. if m.Deadline.Year() == 9999 {
  1179. return
  1180. }
  1181. m.DeadlineString = m.Deadline.Format("2006-01-02")
  1182. if time.Now().After(m.Deadline) {
  1183. m.IsOverDue = true
  1184. }
  1185. }
  1186. }
  1187. // CalOpenIssues calculates the open issues of milestone.
  1188. func (m *Milestone) CalOpenIssues() {
  1189. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  1190. }
  1191. // NewMilestone creates new milestone of repository.
  1192. func NewMilestone(m *Milestone) (err error) {
  1193. sess := x.NewSession()
  1194. defer sessionRelease(sess)
  1195. if err = sess.Begin(); err != nil {
  1196. return err
  1197. }
  1198. if _, err = sess.Insert(m); err != nil {
  1199. return err
  1200. }
  1201. if _, err = sess.Exec("UPDATE `repository` SET num_milestones=num_milestones+1 WHERE id=?", m.RepoID); err != nil {
  1202. return err
  1203. }
  1204. return sess.Commit()
  1205. }
  1206. func getMilestoneByID(e Engine, id int64) (*Milestone, error) {
  1207. m := &Milestone{ID: id}
  1208. has, err := e.Get(m)
  1209. if err != nil {
  1210. return nil, err
  1211. } else if !has {
  1212. return nil, ErrMilestoneNotExist{id, 0}
  1213. }
  1214. return m, nil
  1215. }
  1216. // GetMilestoneByID returns the milestone of given ID.
  1217. func GetMilestoneByID(id int64) (*Milestone, error) {
  1218. return getMilestoneByID(x, id)
  1219. }
  1220. // GetRepoMilestoneByID returns the milestone of given ID and repository.
  1221. func GetRepoMilestoneByID(repoID, milestoneID int64) (*Milestone, error) {
  1222. m := &Milestone{ID: milestoneID, RepoID: repoID}
  1223. has, err := x.Get(m)
  1224. if err != nil {
  1225. return nil, err
  1226. } else if !has {
  1227. return nil, ErrMilestoneNotExist{milestoneID, repoID}
  1228. }
  1229. return m, nil
  1230. }
  1231. // GetAllRepoMilestones returns all milestones of given repository.
  1232. func GetAllRepoMilestones(repoID int64) ([]*Milestone, error) {
  1233. miles := make([]*Milestone, 0, 10)
  1234. return miles, x.Where("repo_id=?", repoID).Find(&miles)
  1235. }
  1236. // GetMilestones returns a list of milestones of given repository and status.
  1237. func GetMilestones(repoID int64, page int, isClosed bool) ([]*Milestone, error) {
  1238. miles := make([]*Milestone, 0, setting.IssuePagingNum)
  1239. sess := x.Where("repo_id=? AND is_closed=?", repoID, isClosed)
  1240. if page > 0 {
  1241. sess = sess.Limit(setting.IssuePagingNum, (page-1)*setting.IssuePagingNum)
  1242. }
  1243. return miles, sess.Find(&miles)
  1244. }
  1245. func updateMilestone(e Engine, m *Milestone) error {
  1246. _, err := e.Id(m.ID).AllCols().Update(m)
  1247. return err
  1248. }
  1249. // UpdateMilestone updates information of given milestone.
  1250. func UpdateMilestone(m *Milestone) error {
  1251. return updateMilestone(x, m)
  1252. }
  1253. func countRepoMilestones(e Engine, repoID int64) int64 {
  1254. count, _ := e.Where("repo_id=?", repoID).Count(new(Milestone))
  1255. return count
  1256. }
  1257. // CountRepoMilestones returns number of milestones in given repository.
  1258. func CountRepoMilestones(repoID int64) int64 {
  1259. return countRepoMilestones(x, repoID)
  1260. }
  1261. func countRepoClosedMilestones(e Engine, repoID int64) int64 {
  1262. closed, _ := e.Where("repo_id=? AND is_closed=?", repoID, true).Count(new(Milestone))
  1263. return closed
  1264. }
  1265. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  1266. func CountRepoClosedMilestones(repoID int64) int64 {
  1267. return countRepoClosedMilestones(x, repoID)
  1268. }
  1269. // MilestoneStats returns number of open and closed milestones of given repository.
  1270. func MilestoneStats(repoID int64) (open int64, closed int64) {
  1271. open, _ = x.Where("repo_id=? AND is_closed=?", repoID, false).Count(new(Milestone))
  1272. return open, CountRepoClosedMilestones(repoID)
  1273. }
  1274. // ChangeMilestoneStatus changes the milestone open/closed status.
  1275. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  1276. repo, err := GetRepositoryByID(m.RepoID)
  1277. if err != nil {
  1278. return err
  1279. }
  1280. sess := x.NewSession()
  1281. defer sessionRelease(sess)
  1282. if err = sess.Begin(); err != nil {
  1283. return err
  1284. }
  1285. m.IsClosed = isClosed
  1286. if err = updateMilestone(sess, m); err != nil {
  1287. return err
  1288. }
  1289. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  1290. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  1291. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  1292. return err
  1293. }
  1294. return sess.Commit()
  1295. }
  1296. func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error {
  1297. if issue.MilestoneID == 0 {
  1298. return nil
  1299. }
  1300. m, err := getMilestoneByID(e, issue.MilestoneID)
  1301. if err != nil {
  1302. return err
  1303. }
  1304. if issue.IsClosed {
  1305. m.NumOpenIssues--
  1306. m.NumClosedIssues++
  1307. } else {
  1308. m.NumOpenIssues++
  1309. m.NumClosedIssues--
  1310. }
  1311. return updateMilestone(e, m)
  1312. }
  1313. // ChangeMilestoneIssueStats updates the open/closed issues counter and progress
  1314. // for the milestone associated witht the given issue.
  1315. func ChangeMilestoneIssueStats(issue *Issue) (err error) {
  1316. sess := x.NewSession()
  1317. defer sessionRelease(sess)
  1318. if err = sess.Begin(); err != nil {
  1319. return err
  1320. }
  1321. if err = changeMilestoneIssueStats(sess, issue); err != nil {
  1322. return err
  1323. }
  1324. return sess.Commit()
  1325. }
  1326. func changeMilestoneAssign(e *xorm.Session, oldMid int64, issue *Issue) error {
  1327. if oldMid > 0 {
  1328. m, err := getMilestoneByID(e, oldMid)
  1329. if err != nil {
  1330. return err
  1331. }
  1332. m.NumIssues--
  1333. if issue.IsClosed {
  1334. m.NumClosedIssues--
  1335. }
  1336. if err = updateMilestone(e, m); err != nil {
  1337. return err
  1338. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id=0 WHERE issue_id=?", issue.ID); err != nil {
  1339. return err
  1340. }
  1341. }
  1342. if issue.MilestoneID > 0 {
  1343. m, err := getMilestoneByID(e, issue.MilestoneID)
  1344. if err != nil {
  1345. return err
  1346. }
  1347. m.NumIssues++
  1348. if issue.IsClosed {
  1349. m.NumClosedIssues++
  1350. }
  1351. if m.NumIssues == 0 {
  1352. return ErrWrongIssueCounter
  1353. }
  1354. if err = updateMilestone(e, m); err != nil {
  1355. return err
  1356. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id=? WHERE issue_id=?", m.ID, issue.ID); err != nil {
  1357. return err
  1358. }
  1359. }
  1360. return updateIssue(e, issue)
  1361. }
  1362. // ChangeMilestoneAssign changes assignment of milestone for issue.
  1363. func ChangeMilestoneAssign(oldMid int64, issue *Issue) (err error) {
  1364. sess := x.NewSession()
  1365. defer sess.Close()
  1366. if err = sess.Begin(); err != nil {
  1367. return err
  1368. }
  1369. if err = changeMilestoneAssign(sess, oldMid, issue); err != nil {
  1370. return err
  1371. }
  1372. return sess.Commit()
  1373. }
  1374. // DeleteMilestoneByID deletes a milestone by given ID.
  1375. func DeleteMilestoneByID(mid int64) error {
  1376. m, err := GetMilestoneByID(mid)
  1377. if err != nil {
  1378. if IsErrMilestoneNotExist(err) {
  1379. return nil
  1380. }
  1381. return err
  1382. }
  1383. repo, err := GetRepositoryByID(m.RepoID)
  1384. if err != nil {
  1385. return err
  1386. }
  1387. sess := x.NewSession()
  1388. defer sessionRelease(sess)
  1389. if err = sess.Begin(); err != nil {
  1390. return err
  1391. }
  1392. if _, err = sess.Id(m.ID).Delete(new(Milestone)); err != nil {
  1393. return err
  1394. }
  1395. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  1396. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  1397. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  1398. return err
  1399. }
  1400. if _, err = sess.Exec("UPDATE `issue` SET milestone_id=0 WHERE milestone_id=?", m.ID); err != nil {
  1401. return err
  1402. } else if _, err = sess.Exec("UPDATE `issue_user` SET milestone_id=0 WHERE milestone_id=?", m.ID); err != nil {
  1403. return err
  1404. }
  1405. return sess.Commit()
  1406. }
  1407. // _________ __
  1408. // \_ ___ \ ____ _____ _____ ____ _____/ |_
  1409. // / \ \/ / _ \ / \ / \_/ __ \ / \ __\
  1410. // \ \___( <_> ) Y Y \ Y Y \ ___/| | \ |
  1411. // \______ /\____/|__|_| /__|_| /\___ >___| /__|
  1412. // \/ \/ \/ \/ \/
  1413. // CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
  1414. type CommentType int
  1415. const (
  1416. // Plain comment, can be associated with a commit (CommitId > 0) and a line (Line > 0)
  1417. COMMENT_TYPE_COMMENT CommentType = iota
  1418. COMMENT_TYPE_REOPEN
  1419. COMMENT_TYPE_CLOSE
  1420. // References.
  1421. COMMENT_TYPE_ISSUE_REF
  1422. // Reference from a commit (not part of a pull request)
  1423. COMMENT_TYPE_COMMIT_REF
  1424. // Reference from a comment
  1425. COMMENT_TYPE_COMMENT_REF
  1426. // Reference from a pull request
  1427. COMMENT_TYPE_PULL_REF
  1428. )
  1429. type CommentTag int
  1430. const (
  1431. COMMENT_TAG_NONE CommentTag = iota
  1432. COMMENT_TAG_POSTER
  1433. COMMENT_TAG_ADMIN
  1434. COMMENT_TAG_OWNER
  1435. )
  1436. // Comment represents a comment in commit and issue page.
  1437. type Comment struct {
  1438. ID int64 `xorm:"pk autoincr"`
  1439. Type CommentType
  1440. PosterID int64
  1441. Poster *User `xorm:"-"`
  1442. IssueID int64 `xorm:"INDEX"`
  1443. CommitID int64
  1444. Line int64
  1445. Content string `xorm:"TEXT"`
  1446. RenderedContent string `xorm:"-"`
  1447. Created time.Time `xorm:"CREATED"`
  1448. // Reference issue in commit message
  1449. CommitSHA string `xorm:"VARCHAR(40)"`
  1450. Attachments []*Attachment `xorm:"-"`
  1451. // For view issue page.
  1452. ShowTag CommentTag `xorm:"-"`
  1453. }
  1454. func (c *Comment) AfterSet(colName string, _ xorm.Cell) {
  1455. var err error
  1456. switch colName {
  1457. case "id":
  1458. c.Attachments, err = GetAttachmentsByCommentID(c.ID)
  1459. if err != nil {
  1460. log.Error(3, "GetAttachmentsByCommentID[%d]: %v", c.ID, err)
  1461. }
  1462. case "poster_id":
  1463. c.Poster, err = GetUserByID(c.PosterID)
  1464. if err != nil {
  1465. if IsErrUserNotExist(err) {
  1466. c.PosterID = -1
  1467. c.Poster = NewFakeUser()
  1468. } else {
  1469. log.Error(3, "GetUserByID[%d]: %v", c.ID, err)
  1470. }
  1471. }
  1472. case "created":
  1473. c.Created = regulateTimeZone(c.Created)
  1474. }
  1475. }
  1476. func (c *Comment) AfterDelete() {
  1477. _, err := DeleteAttachmentsByComment(c.ID, true)
  1478. if err != nil {
  1479. log.Info("Could not delete files for comment %d on issue #%d: %s", c.ID, c.IssueID, err)
  1480. }
  1481. }
  1482. // HashTag returns unique hash tag for comment.
  1483. func (c *Comment) HashTag() string {
  1484. return "issuecomment-" + com.ToStr(c.ID)
  1485. }
  1486. // EventTag returns unique event hash tag for comment.
  1487. func (c *Comment) EventTag() string {
  1488. return "event-" + com.ToStr(c.ID)
  1489. }
  1490. func createComment(e *xorm.Session, u *User, repo *Repository, issue *Issue, commitID, line int64, cmtType CommentType, content, commitSHA string, uuids []string) (_ *Comment, err error) {
  1491. comment := &Comment{
  1492. PosterID: u.Id,
  1493. Type: cmtType,
  1494. IssueID: issue.ID,
  1495. CommitID: commitID,
  1496. Line: line,
  1497. Content: content,
  1498. CommitSHA: commitSHA,
  1499. }
  1500. if _, err = e.Insert(comment); err != nil {
  1501. return nil, err
  1502. }
  1503. // Check comment type.
  1504. switch cmtType {
  1505. case COMMENT_TYPE_COMMENT:
  1506. if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", issue.ID); err != nil {
  1507. return nil, err
  1508. }
  1509. // Check attachments.
  1510. attachments := make([]*Attachment, 0, len(uuids))
  1511. for _, uuid := range uuids {
  1512. attach, err := getAttachmentByUUID(e, uuid)
  1513. if err != nil {
  1514. if IsErrAttachmentNotExist(err) {
  1515. continue
  1516. }
  1517. return nil, fmt.Errorf("getAttachmentByUUID[%s]: %v", uuid, err)
  1518. }
  1519. attachments = append(attachments, attach)
  1520. }
  1521. for i := range attachments {
  1522. attachments[i].IssueID = issue.ID
  1523. attachments[i].CommentID = comment.ID
  1524. // No assign value could be 0, so ignore AllCols().
  1525. if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
  1526. return nil, fmt.Errorf("update attachment[%d]: %v", attachments[i].ID, err)
  1527. }
  1528. }
  1529. // Notify watchers.
  1530. act := &Action{
  1531. ActUserID: u.Id,
  1532. ActUserName: u.LowerName,
  1533. ActEmail: u.Email,
  1534. OpType: COMMENT_ISSUE,
  1535. Content: fmt.Sprintf("%d|%s", issue.Index, strings.Split(content, "\n")[0]),
  1536. RepoID: repo.ID,
  1537. RepoUserName: repo.Owner.LowerName,
  1538. RepoName: repo.LowerName,
  1539. IsPrivate: repo.IsPrivate,
  1540. }
  1541. if err = notifyWatchers(e, act); err != nil {
  1542. return nil, err
  1543. }
  1544. case COMMENT_TYPE_REOPEN:
  1545. if issue.IsPull {
  1546. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls-1 WHERE id=?", repo.ID)
  1547. } else {
  1548. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", repo.ID)
  1549. }
  1550. if err != nil {
  1551. return nil, err
  1552. }
  1553. case COMMENT_TYPE_CLOSE:
  1554. if issue.IsPull {
  1555. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls+1 WHERE id=?", repo.ID)
  1556. } else {
  1557. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", repo.ID)
  1558. }
  1559. if err != nil {
  1560. return nil, err
  1561. }
  1562. }
  1563. return comment, nil
  1564. }
  1565. func createStatusComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue) (*Comment, error) {
  1566. cmtType := COMMENT_TYPE_CLOSE
  1567. if !issue.IsClosed {
  1568. cmtType = COMMENT_TYPE_REOPEN
  1569. }
  1570. return createComment(e, doer, repo, issue, 0, 0, cmtType, "", "", nil)
  1571. }
  1572. // CreateComment creates comment of issue or commit.
  1573. func CreateComment(doer *User, repo *Repository, issue *Issue, commitID, line int64, cmtType CommentType, content, commitSHA string, attachments []string) (comment *Comment, err error) {
  1574. sess := x.NewSession()
  1575. defer sessionRelease(sess)
  1576. if err = sess.Begin(); err != nil {
  1577. return nil, err
  1578. }
  1579. comment, err = createComment(sess, doer, repo, issue, commitID, line, cmtType, content, commitSHA, attachments)
  1580. if err != nil {
  1581. return nil, err
  1582. }
  1583. return comment, sess.Commit()
  1584. }
  1585. // CreateIssueComment creates a plain issue comment.
  1586. func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) {
  1587. return CreateComment(doer, repo, issue, 0, 0, COMMENT_TYPE_COMMENT, content, "", attachments)
  1588. }
  1589. // CreateRefComment creates a commit reference comment to issue.
  1590. func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
  1591. if len(commitSHA) == 0 {
  1592. return fmt.Errorf("cannot create reference with empty commit SHA")
  1593. }
  1594. // Check if same reference from same commit has already existed.
  1595. has, err := x.Get(&Comment{
  1596. Type: COMMENT_TYPE_COMMIT_REF,
  1597. IssueID: issue.ID,
  1598. CommitSHA: commitSHA,
  1599. })
  1600. if err != nil {
  1601. return fmt.Errorf("check reference comment: %v", err)
  1602. } else if has {
  1603. return nil
  1604. }
  1605. _, err = CreateComment(doer, repo, issue, 0, 0, COMMENT_TYPE_COMMIT_REF, content, commitSHA, nil)
  1606. return err
  1607. }
  1608. // GetCommentByID returns the comment by given ID.
  1609. func GetCommentByID(id int64) (*Comment, error) {
  1610. c := new(Comment)
  1611. has, err := x.Id(id).Get(c)
  1612. if err != nil {
  1613. return nil, err
  1614. } else if !has {
  1615. return nil, ErrCommentNotExist{id}
  1616. }
  1617. return c, nil
  1618. }
  1619. // GetCommentsByIssueID returns all comments of issue by given ID.
  1620. func GetCommentsByIssueID(issueID int64) ([]*Comment, error) {
  1621. comments := make([]*Comment, 0, 10)
  1622. return comments, x.Where("issue_id=?", issueID).Asc("created").Find(&comments)
  1623. }
  1624. // UpdateComment updates information of comment.
  1625. func UpdateComment(c *Comment) error {
  1626. _, err := x.Id(c.ID).AllCols().Update(c)
  1627. return err
  1628. }
  1629. // Attachment represent a attachment of issue/comment/release.
  1630. type Attachment struct {
  1631. ID int64 `xorm:"pk autoincr"`
  1632. UUID string `xorm:"uuid UNIQUE"`
  1633. IssueID int64 `xorm:"INDEX"`
  1634. CommentID int64
  1635. ReleaseID int64 `xorm:"INDEX"`
  1636. Name string
  1637. Created time.Time `xorm:"CREATED"`
  1638. }
  1639. // AttachmentLocalPath returns where attachment is stored in local file system based on given UUID.
  1640. func AttachmentLocalPath(uuid string) string {
  1641. return path.Join(setting.AttachmentPath, uuid[0:1], uuid[1:2], uuid)
  1642. }
  1643. // LocalPath returns where attachment is stored in local file system.
  1644. func (attach *Attachment) LocalPath() string {
  1645. return AttachmentLocalPath(attach.UUID)
  1646. }
  1647. // NewAttachment creates a new attachment object.
  1648. func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
  1649. attach := &Attachment{
  1650. UUID: gouuid.NewV4().String(),
  1651. Name: name,
  1652. }
  1653. if err = os.MkdirAll(path.Dir(attach.LocalPath()), os.ModePerm); err != nil {
  1654. return nil, fmt.Errorf("MkdirAll: %v", err)
  1655. }
  1656. fw, err := os.Create(attach.LocalPath())
  1657. if err != nil {
  1658. return nil, fmt.Errorf("Create: %v", err)
  1659. }
  1660. defer fw.Close()
  1661. if _, err = fw.Write(buf); err != nil {
  1662. return nil, fmt.Errorf("Write: %v", err)
  1663. } else if _, err = io.Copy(fw, file); err != nil {
  1664. return nil, fmt.Errorf("Copy: %v", err)
  1665. }
  1666. sess := x.NewSession()
  1667. defer sessionRelease(sess)
  1668. if err := sess.Begin(); err != nil {
  1669. return nil, err
  1670. }
  1671. if _, err := sess.Insert(attach); err != nil {
  1672. return nil, err
  1673. }
  1674. return attach, sess.Commit()
  1675. }
  1676. func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
  1677. attach := &Attachment{UUID: uuid}
  1678. has, err := x.Get(attach)
  1679. if err != nil {
  1680. return nil, err
  1681. } else if !has {
  1682. return nil, ErrAttachmentNotExist{0, uuid}
  1683. }
  1684. return attach, nil
  1685. }
  1686. // GetAttachmentByUUID returns attachment by given UUID.
  1687. func GetAttachmentByUUID(uuid string) (*Attachment, error) {
  1688. return getAttachmentByUUID(x, uuid)
  1689. }
  1690. // GetAttachmentsByIssueID returns all attachments for given issue by ID.
  1691. func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
  1692. attachments := make([]*Attachment, 0, 10)
  1693. return attachments, x.Where("issue_id=? AND comment_id=0", issueID).Find(&attachments)
  1694. }
  1695. // GetAttachmentsByCommentID returns all attachments if comment by given ID.
  1696. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
  1697. attachments := make([]*Attachment, 0, 10)
  1698. return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
  1699. }
  1700. // DeleteAttachment deletes the given attachment and optionally the associated file.
  1701. func DeleteAttachment(a *Attachment, remove bool) error {
  1702. _, err := DeleteAttachments([]*Attachment{a}, remove)
  1703. return err
  1704. }
  1705. // DeleteAttachments deletes the given attachments and optionally the associated files.
  1706. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  1707. for i, a := range attachments {
  1708. if remove {
  1709. if err := os.Remove(a.LocalPath()); err != nil {
  1710. return i, err
  1711. }
  1712. }
  1713. if _, err := x.Delete(a.ID); err != nil {
  1714. return i, err
  1715. }
  1716. }
  1717. return len(attachments), nil
  1718. }
  1719. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  1720. func DeleteAttachmentsByIssue(issueId int64, remove bool) (int, error) {
  1721. attachments, err := GetAttachmentsByIssueID(issueId)
  1722. if err != nil {
  1723. return 0, err
  1724. }
  1725. return DeleteAttachments(attachments, remove)
  1726. }
  1727. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  1728. func DeleteAttachmentsByComment(commentId int64, remove bool) (int, error) {
  1729. attachments, err := GetAttachmentsByCommentID(commentId)
  1730. if err != nil {
  1731. return 0, err
  1732. }
  1733. return DeleteAttachments(attachments, remove)
  1734. }