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.

issue.go 50KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2020 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "regexp"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/structs"
  16. api "code.gitea.io/gitea/modules/structs"
  17. "code.gitea.io/gitea/modules/timeutil"
  18. "code.gitea.io/gitea/modules/util"
  19. "github.com/unknwon/com"
  20. "xorm.io/builder"
  21. "xorm.io/xorm"
  22. )
  23. // Issue represents an issue or pull request of repository.
  24. type Issue struct {
  25. ID int64 `xorm:"pk autoincr"`
  26. RepoID int64 `xorm:"INDEX UNIQUE(repo_index)"`
  27. Repo *Repository `xorm:"-"`
  28. Index int64 `xorm:"UNIQUE(repo_index)"` // Index in one repository.
  29. PosterID int64 `xorm:"INDEX"`
  30. Poster *User `xorm:"-"`
  31. OriginalAuthor string
  32. OriginalAuthorID int64 `xorm:"index"`
  33. Title string `xorm:"name"`
  34. Content string `xorm:"TEXT"`
  35. RenderedContent string `xorm:"-"`
  36. Labels []*Label `xorm:"-"`
  37. MilestoneID int64 `xorm:"INDEX"`
  38. Milestone *Milestone `xorm:"-"`
  39. Priority int
  40. AssigneeID int64 `xorm:"-"`
  41. Assignee *User `xorm:"-"`
  42. IsClosed bool `xorm:"INDEX"`
  43. IsRead bool `xorm:"-"`
  44. IsPull bool `xorm:"INDEX"` // Indicates whether is a pull request or not.
  45. PullRequest *PullRequest `xorm:"-"`
  46. NumComments int
  47. Ref string
  48. DeadlineUnix timeutil.TimeStamp `xorm:"INDEX"`
  49. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  50. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  51. ClosedUnix timeutil.TimeStamp `xorm:"INDEX"`
  52. Attachments []*Attachment `xorm:"-"`
  53. Comments []*Comment `xorm:"-"`
  54. Reactions ReactionList `xorm:"-"`
  55. TotalTrackedTime int64 `xorm:"-"`
  56. Assignees []*User `xorm:"-"`
  57. // IsLocked limits commenting abilities to users on an issue
  58. // with write access
  59. IsLocked bool `xorm:"NOT NULL DEFAULT false"`
  60. }
  61. var (
  62. issueTasksPat *regexp.Regexp
  63. issueTasksDonePat *regexp.Regexp
  64. )
  65. const issueTasksRegexpStr = `(^\s*[-*]\s\[[\sx]\]\s.)|(\n\s*[-*]\s\[[\sx]\]\s.)`
  66. const issueTasksDoneRegexpStr = `(^\s*[-*]\s\[[x]\]\s.)|(\n\s*[-*]\s\[[x]\]\s.)`
  67. const issueMaxDupIndexAttempts = 3
  68. func init() {
  69. issueTasksPat = regexp.MustCompile(issueTasksRegexpStr)
  70. issueTasksDonePat = regexp.MustCompile(issueTasksDoneRegexpStr)
  71. }
  72. func (issue *Issue) loadTotalTimes(e Engine) (err error) {
  73. opts := FindTrackedTimesOptions{IssueID: issue.ID}
  74. issue.TotalTrackedTime, err = opts.ToSession(e).SumInt(&TrackedTime{}, "time")
  75. if err != nil {
  76. return err
  77. }
  78. return nil
  79. }
  80. // IsOverdue checks if the issue is overdue
  81. func (issue *Issue) IsOverdue() bool {
  82. return timeutil.TimeStampNow() >= issue.DeadlineUnix
  83. }
  84. // LoadRepo loads issue's repository
  85. func (issue *Issue) LoadRepo() error {
  86. return issue.loadRepo(x)
  87. }
  88. func (issue *Issue) loadRepo(e Engine) (err error) {
  89. if issue.Repo == nil {
  90. issue.Repo, err = getRepositoryByID(e, issue.RepoID)
  91. if err != nil {
  92. return fmt.Errorf("getRepositoryByID [%d]: %v", issue.RepoID, err)
  93. }
  94. }
  95. return nil
  96. }
  97. // IsTimetrackerEnabled returns true if the repo enables timetracking
  98. func (issue *Issue) IsTimetrackerEnabled() bool {
  99. return issue.isTimetrackerEnabled(x)
  100. }
  101. func (issue *Issue) isTimetrackerEnabled(e Engine) bool {
  102. if err := issue.loadRepo(e); err != nil {
  103. log.Error(fmt.Sprintf("loadRepo: %v", err))
  104. return false
  105. }
  106. return issue.Repo.IsTimetrackerEnabled()
  107. }
  108. // GetPullRequest returns the issue pull request
  109. func (issue *Issue) GetPullRequest() (pr *PullRequest, err error) {
  110. if !issue.IsPull {
  111. return nil, fmt.Errorf("Issue is not a pull request")
  112. }
  113. pr, err = getPullRequestByIssueID(x, issue.ID)
  114. if err != nil {
  115. return nil, err
  116. }
  117. pr.Issue = issue
  118. return
  119. }
  120. func (issue *Issue) loadLabels(e Engine) (err error) {
  121. if issue.Labels == nil {
  122. issue.Labels, err = getLabelsByIssueID(e, issue.ID)
  123. if err != nil {
  124. return fmt.Errorf("getLabelsByIssueID [%d]: %v", issue.ID, err)
  125. }
  126. }
  127. return nil
  128. }
  129. // LoadPoster loads poster
  130. func (issue *Issue) LoadPoster() error {
  131. return issue.loadPoster(x)
  132. }
  133. func (issue *Issue) loadPoster(e Engine) (err error) {
  134. if issue.Poster == nil {
  135. issue.Poster, err = getUserByID(e, issue.PosterID)
  136. if err != nil {
  137. issue.PosterID = -1
  138. issue.Poster = NewGhostUser()
  139. if !IsErrUserNotExist(err) {
  140. return fmt.Errorf("getUserByID.(poster) [%d]: %v", issue.PosterID, err)
  141. }
  142. err = nil
  143. return
  144. }
  145. }
  146. return
  147. }
  148. func (issue *Issue) loadPullRequest(e Engine) (err error) {
  149. if issue.IsPull && issue.PullRequest == nil {
  150. issue.PullRequest, err = getPullRequestByIssueID(e, issue.ID)
  151. if err != nil {
  152. if IsErrPullRequestNotExist(err) {
  153. return err
  154. }
  155. return fmt.Errorf("getPullRequestByIssueID [%d]: %v", issue.ID, err)
  156. }
  157. issue.PullRequest.Issue = issue
  158. }
  159. return nil
  160. }
  161. // LoadPullRequest loads pull request info
  162. func (issue *Issue) LoadPullRequest() error {
  163. return issue.loadPullRequest(x)
  164. }
  165. func (issue *Issue) loadComments(e Engine) (err error) {
  166. return issue.loadCommentsByType(e, CommentTypeUnknown)
  167. }
  168. // LoadDiscussComments loads discuss comments
  169. func (issue *Issue) LoadDiscussComments() error {
  170. return issue.loadCommentsByType(x, CommentTypeComment)
  171. }
  172. func (issue *Issue) loadCommentsByType(e Engine, tp CommentType) (err error) {
  173. if issue.Comments != nil {
  174. return nil
  175. }
  176. issue.Comments, err = findComments(e, FindCommentsOptions{
  177. IssueID: issue.ID,
  178. Type: tp,
  179. })
  180. return err
  181. }
  182. func (issue *Issue) loadReactions(e Engine) (err error) {
  183. if issue.Reactions != nil {
  184. return nil
  185. }
  186. reactions, err := findReactions(e, FindReactionsOptions{
  187. IssueID: issue.ID,
  188. })
  189. if err != nil {
  190. return err
  191. }
  192. if err = issue.loadRepo(e); err != nil {
  193. return err
  194. }
  195. // Load reaction user data
  196. if _, err := ReactionList(reactions).loadUsers(e, issue.Repo); err != nil {
  197. return err
  198. }
  199. // Cache comments to map
  200. comments := make(map[int64]*Comment)
  201. for _, comment := range issue.Comments {
  202. comments[comment.ID] = comment
  203. }
  204. // Add reactions either to issue or comment
  205. for _, react := range reactions {
  206. if react.CommentID == 0 {
  207. issue.Reactions = append(issue.Reactions, react)
  208. } else if comment, ok := comments[react.CommentID]; ok {
  209. comment.Reactions = append(comment.Reactions, react)
  210. }
  211. }
  212. return nil
  213. }
  214. func (issue *Issue) loadMilestone(e Engine) (err error) {
  215. if issue.Milestone == nil && issue.MilestoneID > 0 {
  216. issue.Milestone, err = getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  217. if err != nil && !IsErrMilestoneNotExist(err) {
  218. return fmt.Errorf("getMilestoneByRepoID [repo_id: %d, milestone_id: %d]: %v", issue.RepoID, issue.MilestoneID, err)
  219. }
  220. }
  221. return nil
  222. }
  223. func (issue *Issue) loadAttributes(e Engine) (err error) {
  224. if err = issue.loadRepo(e); err != nil {
  225. return
  226. }
  227. if err = issue.loadPoster(e); err != nil {
  228. return
  229. }
  230. if err = issue.loadLabels(e); err != nil {
  231. return
  232. }
  233. if err = issue.loadMilestone(e); err != nil {
  234. return
  235. }
  236. if err = issue.loadAssignees(e); err != nil {
  237. return
  238. }
  239. if err = issue.loadPullRequest(e); err != nil && !IsErrPullRequestNotExist(err) {
  240. // It is possible pull request is not yet created.
  241. return err
  242. }
  243. if issue.Attachments == nil {
  244. issue.Attachments, err = getAttachmentsByIssueID(e, issue.ID)
  245. if err != nil {
  246. return fmt.Errorf("getAttachmentsByIssueID [%d]: %v", issue.ID, err)
  247. }
  248. }
  249. if err = issue.loadComments(e); err != nil {
  250. return err
  251. }
  252. if err = CommentList(issue.Comments).loadAttributes(e); err != nil {
  253. return err
  254. }
  255. if issue.isTimetrackerEnabled(e) {
  256. if err = issue.loadTotalTimes(e); err != nil {
  257. return err
  258. }
  259. }
  260. return issue.loadReactions(e)
  261. }
  262. // LoadAttributes loads the attribute of this issue.
  263. func (issue *Issue) LoadAttributes() error {
  264. return issue.loadAttributes(x)
  265. }
  266. // LoadMilestone load milestone of this issue.
  267. func (issue *Issue) LoadMilestone() error {
  268. return issue.loadMilestone(x)
  269. }
  270. // GetIsRead load the `IsRead` field of the issue
  271. func (issue *Issue) GetIsRead(userID int64) error {
  272. issueUser := &IssueUser{IssueID: issue.ID, UID: userID}
  273. if has, err := x.Get(issueUser); err != nil {
  274. return err
  275. } else if !has {
  276. issue.IsRead = false
  277. return nil
  278. }
  279. issue.IsRead = issueUser.IsRead
  280. return nil
  281. }
  282. // APIURL returns the absolute APIURL to this issue.
  283. func (issue *Issue) APIURL() string {
  284. return fmt.Sprintf("%s/issues/%d", issue.Repo.APIURL(), issue.Index)
  285. }
  286. // HTMLURL returns the absolute URL to this issue.
  287. func (issue *Issue) HTMLURL() string {
  288. var path string
  289. if issue.IsPull {
  290. path = "pulls"
  291. } else {
  292. path = "issues"
  293. }
  294. return fmt.Sprintf("%s/%s/%d", issue.Repo.HTMLURL(), path, issue.Index)
  295. }
  296. // DiffURL returns the absolute URL to this diff
  297. func (issue *Issue) DiffURL() string {
  298. if issue.IsPull {
  299. return fmt.Sprintf("%s/pulls/%d.diff", issue.Repo.HTMLURL(), issue.Index)
  300. }
  301. return ""
  302. }
  303. // PatchURL returns the absolute URL to this patch
  304. func (issue *Issue) PatchURL() string {
  305. if issue.IsPull {
  306. return fmt.Sprintf("%s/pulls/%d.patch", issue.Repo.HTMLURL(), issue.Index)
  307. }
  308. return ""
  309. }
  310. // State returns string representation of issue status.
  311. func (issue *Issue) State() api.StateType {
  312. if issue.IsClosed {
  313. return api.StateClosed
  314. }
  315. return api.StateOpen
  316. }
  317. // APIFormat assumes some fields assigned with values:
  318. // Required - Poster, Labels,
  319. // Optional - Milestone, Assignee, PullRequest
  320. func (issue *Issue) APIFormat() *api.Issue {
  321. return issue.apiFormat(x)
  322. }
  323. func (issue *Issue) apiFormat(e Engine) *api.Issue {
  324. issue.loadLabels(e)
  325. apiLabels := make([]*api.Label, len(issue.Labels))
  326. for i := range issue.Labels {
  327. apiLabels[i] = issue.Labels[i].APIFormat()
  328. }
  329. issue.loadPoster(e)
  330. issue.loadRepo(e)
  331. apiIssue := &api.Issue{
  332. ID: issue.ID,
  333. URL: issue.APIURL(),
  334. HTMLURL: issue.HTMLURL(),
  335. Index: issue.Index,
  336. Poster: issue.Poster.APIFormat(),
  337. Title: issue.Title,
  338. Body: issue.Content,
  339. Labels: apiLabels,
  340. State: issue.State(),
  341. Comments: issue.NumComments,
  342. Created: issue.CreatedUnix.AsTime(),
  343. Updated: issue.UpdatedUnix.AsTime(),
  344. }
  345. apiIssue.Repo = &api.RepositoryMeta{
  346. ID: issue.Repo.ID,
  347. Name: issue.Repo.Name,
  348. FullName: issue.Repo.FullName(),
  349. }
  350. if issue.ClosedUnix != 0 {
  351. apiIssue.Closed = issue.ClosedUnix.AsTimePtr()
  352. }
  353. issue.loadMilestone(e)
  354. if issue.Milestone != nil {
  355. apiIssue.Milestone = issue.Milestone.APIFormat()
  356. }
  357. issue.loadAssignees(e)
  358. if len(issue.Assignees) > 0 {
  359. for _, assignee := range issue.Assignees {
  360. apiIssue.Assignees = append(apiIssue.Assignees, assignee.APIFormat())
  361. }
  362. apiIssue.Assignee = issue.Assignees[0].APIFormat() // For compatibility, we're keeping the first assignee as `apiIssue.Assignee`
  363. }
  364. if issue.IsPull {
  365. issue.loadPullRequest(e)
  366. apiIssue.PullRequest = &api.PullRequestMeta{
  367. HasMerged: issue.PullRequest.HasMerged,
  368. }
  369. if issue.PullRequest.HasMerged {
  370. apiIssue.PullRequest.Merged = issue.PullRequest.MergedUnix.AsTimePtr()
  371. }
  372. }
  373. if issue.DeadlineUnix != 0 {
  374. apiIssue.Deadline = issue.DeadlineUnix.AsTimePtr()
  375. }
  376. return apiIssue
  377. }
  378. // HashTag returns unique hash tag for issue.
  379. func (issue *Issue) HashTag() string {
  380. return "issue-" + com.ToStr(issue.ID)
  381. }
  382. // IsPoster returns true if given user by ID is the poster.
  383. func (issue *Issue) IsPoster(uid int64) bool {
  384. return issue.OriginalAuthorID == 0 && issue.PosterID == uid
  385. }
  386. func (issue *Issue) hasLabel(e Engine, labelID int64) bool {
  387. return hasIssueLabel(e, issue.ID, labelID)
  388. }
  389. // HasLabel returns true if issue has been labeled by given ID.
  390. func (issue *Issue) HasLabel(labelID int64) bool {
  391. return issue.hasLabel(x, labelID)
  392. }
  393. // ReplyReference returns tokenized address to use for email reply headers
  394. func (issue *Issue) ReplyReference() string {
  395. var path string
  396. if issue.IsPull {
  397. path = "pulls"
  398. } else {
  399. path = "issues"
  400. }
  401. return fmt.Sprintf("%s/%s/%d@%s", issue.Repo.FullName(), path, issue.Index, setting.Domain)
  402. }
  403. func (issue *Issue) addLabel(e *xorm.Session, label *Label, doer *User) error {
  404. return newIssueLabel(e, issue, label, doer)
  405. }
  406. func (issue *Issue) addLabels(e *xorm.Session, labels []*Label, doer *User) error {
  407. return newIssueLabels(e, issue, labels, doer)
  408. }
  409. func (issue *Issue) getLabels(e Engine) (err error) {
  410. if len(issue.Labels) > 0 {
  411. return nil
  412. }
  413. issue.Labels, err = getLabelsByIssueID(e, issue.ID)
  414. if err != nil {
  415. return fmt.Errorf("getLabelsByIssueID: %v", err)
  416. }
  417. return nil
  418. }
  419. func (issue *Issue) removeLabel(e *xorm.Session, doer *User, label *Label) error {
  420. return deleteIssueLabel(e, issue, label, doer)
  421. }
  422. func (issue *Issue) clearLabels(e *xorm.Session, doer *User) (err error) {
  423. if err = issue.getLabels(e); err != nil {
  424. return fmt.Errorf("getLabels: %v", err)
  425. }
  426. for i := range issue.Labels {
  427. if err = issue.removeLabel(e, doer, issue.Labels[i]); err != nil {
  428. return fmt.Errorf("removeLabel: %v", err)
  429. }
  430. }
  431. return nil
  432. }
  433. // ClearLabels removes all issue labels as the given user.
  434. // Triggers appropriate WebHooks, if any.
  435. func (issue *Issue) ClearLabels(doer *User) (err error) {
  436. sess := x.NewSession()
  437. defer sess.Close()
  438. if err = sess.Begin(); err != nil {
  439. return err
  440. }
  441. if err := issue.loadRepo(sess); err != nil {
  442. return err
  443. } else if err = issue.loadPullRequest(sess); err != nil {
  444. return err
  445. }
  446. perm, err := getUserRepoPermission(sess, issue.Repo, doer)
  447. if err != nil {
  448. return err
  449. }
  450. if !perm.CanWriteIssuesOrPulls(issue.IsPull) {
  451. return ErrLabelNotExist{}
  452. }
  453. if err = issue.clearLabels(sess, doer); err != nil {
  454. return err
  455. }
  456. if err = sess.Commit(); err != nil {
  457. return fmt.Errorf("Commit: %v", err)
  458. }
  459. return nil
  460. }
  461. type labelSorter []*Label
  462. func (ts labelSorter) Len() int {
  463. return len([]*Label(ts))
  464. }
  465. func (ts labelSorter) Less(i, j int) bool {
  466. return []*Label(ts)[i].ID < []*Label(ts)[j].ID
  467. }
  468. func (ts labelSorter) Swap(i, j int) {
  469. []*Label(ts)[i], []*Label(ts)[j] = []*Label(ts)[j], []*Label(ts)[i]
  470. }
  471. // ReplaceLabels removes all current labels and add new labels to the issue.
  472. // Triggers appropriate WebHooks, if any.
  473. func (issue *Issue) ReplaceLabels(labels []*Label, doer *User) (err error) {
  474. sess := x.NewSession()
  475. defer sess.Close()
  476. if err = sess.Begin(); err != nil {
  477. return err
  478. }
  479. if err = issue.loadLabels(sess); err != nil {
  480. return err
  481. }
  482. sort.Sort(labelSorter(labels))
  483. sort.Sort(labelSorter(issue.Labels))
  484. var toAdd, toRemove []*Label
  485. addIndex, removeIndex := 0, 0
  486. for addIndex < len(labels) && removeIndex < len(issue.Labels) {
  487. addLabel := labels[addIndex]
  488. removeLabel := issue.Labels[removeIndex]
  489. if addLabel.ID == removeLabel.ID {
  490. addIndex++
  491. removeIndex++
  492. } else if addLabel.ID < removeLabel.ID {
  493. toAdd = append(toAdd, addLabel)
  494. addIndex++
  495. } else {
  496. toRemove = append(toRemove, removeLabel)
  497. removeIndex++
  498. }
  499. }
  500. toAdd = append(toAdd, labels[addIndex:]...)
  501. toRemove = append(toRemove, issue.Labels[removeIndex:]...)
  502. if len(toAdd) > 0 {
  503. if err = issue.addLabels(sess, toAdd, doer); err != nil {
  504. return fmt.Errorf("addLabels: %v", err)
  505. }
  506. }
  507. for _, l := range toRemove {
  508. if err = issue.removeLabel(sess, doer, l); err != nil {
  509. return fmt.Errorf("removeLabel: %v", err)
  510. }
  511. }
  512. return sess.Commit()
  513. }
  514. // ReadBy sets issue to be read by given user.
  515. func (issue *Issue) ReadBy(userID int64) error {
  516. if err := UpdateIssueUserByRead(userID, issue.ID); err != nil {
  517. return err
  518. }
  519. return setNotificationStatusReadIfUnread(x, userID, issue.ID)
  520. }
  521. func updateIssueCols(e Engine, issue *Issue, cols ...string) error {
  522. if _, err := e.ID(issue.ID).Cols(cols...).Update(issue); err != nil {
  523. return err
  524. }
  525. return nil
  526. }
  527. func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (*Comment, error) {
  528. // Reload the issue
  529. currentIssue, err := getIssueByID(e, issue.ID)
  530. if err != nil {
  531. return nil, err
  532. }
  533. // Nothing should be performed if current status is same as target status
  534. if currentIssue.IsClosed == isClosed {
  535. if !issue.IsPull {
  536. return nil, ErrIssueWasClosed{
  537. ID: issue.ID,
  538. }
  539. }
  540. return nil, ErrPullWasClosed{
  541. ID: issue.ID,
  542. }
  543. }
  544. // Check for open dependencies
  545. if isClosed && issue.Repo.isDependenciesEnabled(e) {
  546. // only check if dependencies are enabled and we're about to close an issue, otherwise reopening an issue would fail when there are unsatisfied dependencies
  547. noDeps, err := issueNoDependenciesLeft(e, issue)
  548. if err != nil {
  549. return nil, err
  550. }
  551. if !noDeps {
  552. return nil, ErrDependenciesLeft{issue.ID}
  553. }
  554. }
  555. issue.IsClosed = isClosed
  556. if isClosed {
  557. issue.ClosedUnix = timeutil.TimeStampNow()
  558. } else {
  559. issue.ClosedUnix = 0
  560. }
  561. if err = updateIssueCols(e, issue, "is_closed", "closed_unix"); err != nil {
  562. return nil, err
  563. }
  564. // Update issue count of labels
  565. if err = issue.getLabels(e); err != nil {
  566. return nil, err
  567. }
  568. for idx := range issue.Labels {
  569. if err = updateLabel(e, issue.Labels[idx]); err != nil {
  570. return nil, err
  571. }
  572. }
  573. // Update issue count of milestone
  574. if err := updateMilestoneClosedNum(e, issue.MilestoneID); err != nil {
  575. return nil, err
  576. }
  577. // New action comment
  578. cmtType := CommentTypeClose
  579. if !issue.IsClosed {
  580. cmtType = CommentTypeReopen
  581. }
  582. return createComment(e, &CreateCommentOptions{
  583. Type: cmtType,
  584. Doer: doer,
  585. Repo: issue.Repo,
  586. Issue: issue,
  587. })
  588. }
  589. // ChangeStatus changes issue status to open or closed.
  590. func (issue *Issue) ChangeStatus(doer *User, isClosed bool) (*Comment, error) {
  591. sess := x.NewSession()
  592. defer sess.Close()
  593. if err := sess.Begin(); err != nil {
  594. return nil, err
  595. }
  596. if err := issue.loadRepo(sess); err != nil {
  597. return nil, err
  598. }
  599. if err := issue.loadPoster(sess); err != nil {
  600. return nil, err
  601. }
  602. comment, err := issue.changeStatus(sess, doer, isClosed)
  603. if err != nil {
  604. return nil, err
  605. }
  606. if err = sess.Commit(); err != nil {
  607. return nil, fmt.Errorf("Commit: %v", err)
  608. }
  609. return comment, nil
  610. }
  611. // ChangeTitle changes the title of this issue, as the given user.
  612. func (issue *Issue) ChangeTitle(doer *User, oldTitle string) (err error) {
  613. sess := x.NewSession()
  614. defer sess.Close()
  615. if err = sess.Begin(); err != nil {
  616. return err
  617. }
  618. if err = updateIssueCols(sess, issue, "name"); err != nil {
  619. return fmt.Errorf("updateIssueCols: %v", err)
  620. }
  621. if err = issue.loadRepo(sess); err != nil {
  622. return fmt.Errorf("loadRepo: %v", err)
  623. }
  624. var opts = &CreateCommentOptions{
  625. Type: CommentTypeChangeTitle,
  626. Doer: doer,
  627. Repo: issue.Repo,
  628. Issue: issue,
  629. OldTitle: oldTitle,
  630. NewTitle: issue.Title,
  631. }
  632. if _, err = createComment(sess, opts); err != nil {
  633. return fmt.Errorf("createComment: %v", err)
  634. }
  635. if err = issue.addCrossReferences(sess, doer, true); err != nil {
  636. return err
  637. }
  638. return sess.Commit()
  639. }
  640. // AddDeletePRBranchComment adds delete branch comment for pull request issue
  641. func AddDeletePRBranchComment(doer *User, repo *Repository, issueID int64, branchName string) error {
  642. issue, err := getIssueByID(x, issueID)
  643. if err != nil {
  644. return err
  645. }
  646. sess := x.NewSession()
  647. defer sess.Close()
  648. if err := sess.Begin(); err != nil {
  649. return err
  650. }
  651. var opts = &CreateCommentOptions{
  652. Type: CommentTypeDeleteBranch,
  653. Doer: doer,
  654. Repo: repo,
  655. Issue: issue,
  656. CommitSHA: branchName,
  657. }
  658. if _, err = createComment(sess, opts); err != nil {
  659. return err
  660. }
  661. return sess.Commit()
  662. }
  663. // UpdateAttachments update attachments by UUIDs for the issue
  664. func (issue *Issue) UpdateAttachments(uuids []string) (err error) {
  665. sess := x.NewSession()
  666. defer sess.Close()
  667. if err = sess.Begin(); err != nil {
  668. return err
  669. }
  670. attachments, err := getAttachmentsByUUIDs(sess, uuids)
  671. if err != nil {
  672. return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %v", uuids, err)
  673. }
  674. for i := 0; i < len(attachments); i++ {
  675. attachments[i].IssueID = issue.ID
  676. if err := updateAttachment(sess, attachments[i]); err != nil {
  677. return fmt.Errorf("update attachment [id: %d]: %v", attachments[i].ID, err)
  678. }
  679. }
  680. return sess.Commit()
  681. }
  682. // ChangeContent changes issue content, as the given user.
  683. func (issue *Issue) ChangeContent(doer *User, content string) (err error) {
  684. issue.Content = content
  685. sess := x.NewSession()
  686. defer sess.Close()
  687. if err = sess.Begin(); err != nil {
  688. return err
  689. }
  690. if err = updateIssueCols(sess, issue, "content"); err != nil {
  691. return fmt.Errorf("UpdateIssueCols: %v", err)
  692. }
  693. if err = issue.addCrossReferences(sess, doer, true); err != nil {
  694. return err
  695. }
  696. return sess.Commit()
  697. }
  698. // GetTasks returns the amount of tasks in the issues content
  699. func (issue *Issue) GetTasks() int {
  700. return len(issueTasksPat.FindAllStringIndex(issue.Content, -1))
  701. }
  702. // GetTasksDone returns the amount of completed tasks in the issues content
  703. func (issue *Issue) GetTasksDone() int {
  704. return len(issueTasksDonePat.FindAllStringIndex(issue.Content, -1))
  705. }
  706. // GetLastEventTimestamp returns the last user visible event timestamp, either the creation of this issue or the close.
  707. func (issue *Issue) GetLastEventTimestamp() timeutil.TimeStamp {
  708. if issue.IsClosed {
  709. return issue.ClosedUnix
  710. }
  711. return issue.CreatedUnix
  712. }
  713. // GetLastEventLabel returns the localization label for the current issue.
  714. func (issue *Issue) GetLastEventLabel() string {
  715. if issue.IsClosed {
  716. if issue.IsPull && issue.PullRequest.HasMerged {
  717. return "repo.pulls.merged_by"
  718. }
  719. return "repo.issues.closed_by"
  720. }
  721. return "repo.issues.opened_by"
  722. }
  723. // GetLastComment return last comment for the current issue.
  724. func (issue *Issue) GetLastComment() (*Comment, error) {
  725. var c Comment
  726. exist, err := x.Where("type = ?", CommentTypeComment).
  727. And("issue_id = ?", issue.ID).Desc("id").Get(&c)
  728. if err != nil {
  729. return nil, err
  730. }
  731. if !exist {
  732. return nil, nil
  733. }
  734. return &c, nil
  735. }
  736. // GetLastEventLabelFake returns the localization label for the current issue without providing a link in the username.
  737. func (issue *Issue) GetLastEventLabelFake() string {
  738. if issue.IsClosed {
  739. if issue.IsPull && issue.PullRequest.HasMerged {
  740. return "repo.pulls.merged_by_fake"
  741. }
  742. return "repo.issues.closed_by_fake"
  743. }
  744. return "repo.issues.opened_by_fake"
  745. }
  746. // NewIssueOptions represents the options of a new issue.
  747. type NewIssueOptions struct {
  748. Repo *Repository
  749. Issue *Issue
  750. LabelIDs []int64
  751. Attachments []string // In UUID format.
  752. IsPull bool
  753. }
  754. func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
  755. opts.Issue.Title = strings.TrimSpace(opts.Issue.Title)
  756. if opts.Issue.MilestoneID > 0 {
  757. milestone, err := getMilestoneByRepoID(e, opts.Issue.RepoID, opts.Issue.MilestoneID)
  758. if err != nil && !IsErrMilestoneNotExist(err) {
  759. return fmt.Errorf("getMilestoneByID: %v", err)
  760. }
  761. // Assume milestone is invalid and drop silently.
  762. opts.Issue.MilestoneID = 0
  763. if milestone != nil {
  764. opts.Issue.MilestoneID = milestone.ID
  765. opts.Issue.Milestone = milestone
  766. }
  767. }
  768. // Milestone validation should happen before insert actual object.
  769. if _, err := e.SetExpr("`index`", "coalesce(MAX(`index`),0)+1").
  770. Where("repo_id=?", opts.Issue.RepoID).
  771. Insert(opts.Issue); err != nil {
  772. return ErrNewIssueInsert{err}
  773. }
  774. inserted, err := getIssueByID(e, opts.Issue.ID)
  775. if err != nil {
  776. return err
  777. }
  778. // Patch Index with the value calculated by the database
  779. opts.Issue.Index = inserted.Index
  780. if opts.Issue.MilestoneID > 0 {
  781. if _, err = e.Exec("UPDATE `milestone` SET num_issues=num_issues+1 WHERE id=?", opts.Issue.MilestoneID); err != nil {
  782. return err
  783. }
  784. var opts = &CreateCommentOptions{
  785. Type: CommentTypeMilestone,
  786. Doer: doer,
  787. Repo: opts.Repo,
  788. Issue: opts.Issue,
  789. OldMilestoneID: 0,
  790. MilestoneID: opts.Issue.MilestoneID,
  791. }
  792. if _, err = createComment(e, opts); err != nil {
  793. return err
  794. }
  795. }
  796. if opts.IsPull {
  797. _, err = e.Exec("UPDATE `repository` SET num_pulls = num_pulls + 1 WHERE id = ?", opts.Issue.RepoID)
  798. } else {
  799. _, err = e.Exec("UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?", opts.Issue.RepoID)
  800. }
  801. if err != nil {
  802. return err
  803. }
  804. if len(opts.LabelIDs) > 0 {
  805. // During the session, SQLite3 driver cannot handle retrieve objects after update something.
  806. // So we have to get all needed labels first.
  807. labels := make([]*Label, 0, len(opts.LabelIDs))
  808. if err = e.In("id", opts.LabelIDs).Find(&labels); err != nil {
  809. return fmt.Errorf("find all labels [label_ids: %v]: %v", opts.LabelIDs, err)
  810. }
  811. if err = opts.Issue.loadPoster(e); err != nil {
  812. return err
  813. }
  814. for _, label := range labels {
  815. // Silently drop invalid labels.
  816. if label.RepoID != opts.Repo.ID {
  817. continue
  818. }
  819. if err = opts.Issue.addLabel(e, label, opts.Issue.Poster); err != nil {
  820. return fmt.Errorf("addLabel [id: %d]: %v", label.ID, err)
  821. }
  822. }
  823. }
  824. if err = newIssueUsers(e, opts.Repo, opts.Issue); err != nil {
  825. return err
  826. }
  827. if len(opts.Attachments) > 0 {
  828. attachments, err := getAttachmentsByUUIDs(e, opts.Attachments)
  829. if err != nil {
  830. return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %v", opts.Attachments, err)
  831. }
  832. for i := 0; i < len(attachments); i++ {
  833. attachments[i].IssueID = opts.Issue.ID
  834. if _, err = e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
  835. return fmt.Errorf("update attachment [id: %d]: %v", attachments[i].ID, err)
  836. }
  837. }
  838. }
  839. if err = opts.Issue.loadAttributes(e); err != nil {
  840. return err
  841. }
  842. return opts.Issue.addCrossReferences(e, doer, false)
  843. }
  844. // NewIssue creates new issue with labels for repository.
  845. func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
  846. // Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887
  847. i := 0
  848. for {
  849. if err = newIssueAttempt(repo, issue, labelIDs, uuids); err == nil {
  850. return nil
  851. }
  852. if !IsErrNewIssueInsert(err) {
  853. return err
  854. }
  855. if i++; i == issueMaxDupIndexAttempts {
  856. break
  857. }
  858. log.Error("NewIssue: error attempting to insert the new issue; will retry. Original error: %v", err)
  859. }
  860. return fmt.Errorf("NewIssue: too many errors attempting to insert the new issue. Last error was: %v", err)
  861. }
  862. func newIssueAttempt(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
  863. sess := x.NewSession()
  864. defer sess.Close()
  865. if err = sess.Begin(); err != nil {
  866. return err
  867. }
  868. if err = newIssue(sess, issue.Poster, NewIssueOptions{
  869. Repo: repo,
  870. Issue: issue,
  871. LabelIDs: labelIDs,
  872. Attachments: uuids,
  873. }); err != nil {
  874. if IsErrUserDoesNotHaveAccessToRepo(err) || IsErrNewIssueInsert(err) {
  875. return err
  876. }
  877. return fmt.Errorf("newIssue: %v", err)
  878. }
  879. if err = sess.Commit(); err != nil {
  880. return fmt.Errorf("Commit: %v", err)
  881. }
  882. return nil
  883. }
  884. // GetIssueByIndex returns raw issue without loading attributes by index in a repository.
  885. func GetIssueByIndex(repoID, index int64) (*Issue, error) {
  886. issue := &Issue{
  887. RepoID: repoID,
  888. Index: index,
  889. }
  890. has, err := x.Get(issue)
  891. if err != nil {
  892. return nil, err
  893. } else if !has {
  894. return nil, ErrIssueNotExist{0, repoID, index}
  895. }
  896. return issue, nil
  897. }
  898. // GetIssueWithAttrsByIndex returns issue by index in a repository.
  899. func GetIssueWithAttrsByIndex(repoID, index int64) (*Issue, error) {
  900. issue, err := GetIssueByIndex(repoID, index)
  901. if err != nil {
  902. return nil, err
  903. }
  904. return issue, issue.LoadAttributes()
  905. }
  906. func getIssueByID(e Engine, id int64) (*Issue, error) {
  907. issue := new(Issue)
  908. has, err := e.ID(id).Get(issue)
  909. if err != nil {
  910. return nil, err
  911. } else if !has {
  912. return nil, ErrIssueNotExist{id, 0, 0}
  913. }
  914. return issue, nil
  915. }
  916. // GetIssueWithAttrsByID returns an issue with attributes by given ID.
  917. func GetIssueWithAttrsByID(id int64) (*Issue, error) {
  918. issue, err := getIssueByID(x, id)
  919. if err != nil {
  920. return nil, err
  921. }
  922. return issue, issue.loadAttributes(x)
  923. }
  924. // GetIssueByID returns an issue by given ID.
  925. func GetIssueByID(id int64) (*Issue, error) {
  926. return getIssueByID(x, id)
  927. }
  928. func getIssuesByIDs(e Engine, issueIDs []int64) ([]*Issue, error) {
  929. issues := make([]*Issue, 0, 10)
  930. return issues, e.In("id", issueIDs).Find(&issues)
  931. }
  932. func getIssueIDsByRepoID(e Engine, repoID int64) ([]int64, error) {
  933. var ids = make([]int64, 0, 10)
  934. err := e.Table("issue").Where("repo_id = ?", repoID).Find(&ids)
  935. return ids, err
  936. }
  937. // GetIssueIDsByRepoID returns all issue ids by repo id
  938. func GetIssueIDsByRepoID(repoID int64) ([]int64, error) {
  939. return getIssueIDsByRepoID(x, repoID)
  940. }
  941. // GetIssuesByIDs return issues with the given IDs.
  942. func GetIssuesByIDs(issueIDs []int64) ([]*Issue, error) {
  943. return getIssuesByIDs(x, issueIDs)
  944. }
  945. // IssuesOptions represents options of an issue.
  946. type IssuesOptions struct {
  947. RepoIDs []int64 // include all repos if empty
  948. AssigneeID int64
  949. PosterID int64
  950. MentionedID int64
  951. MilestoneID int64
  952. Page int
  953. PageSize int
  954. IsClosed util.OptionalBool
  955. IsPull util.OptionalBool
  956. LabelIDs []int64
  957. SortType string
  958. IssueIDs []int64
  959. // prioritize issues from this repo
  960. PriorityRepoID int64
  961. }
  962. // sortIssuesSession sort an issues-related session based on the provided
  963. // sortType string
  964. func sortIssuesSession(sess *xorm.Session, sortType string, priorityRepoID int64) {
  965. switch sortType {
  966. case "oldest":
  967. sess.Asc("issue.created_unix")
  968. case "recentupdate":
  969. sess.Desc("issue.updated_unix")
  970. case "leastupdate":
  971. sess.Asc("issue.updated_unix")
  972. case "mostcomment":
  973. sess.Desc("issue.num_comments")
  974. case "leastcomment":
  975. sess.Asc("issue.num_comments")
  976. case "priority":
  977. sess.Desc("issue.priority")
  978. case "nearduedate":
  979. // 253370764800 is 01/01/9999 @ 12:00am (UTC)
  980. sess.OrderBy("CASE WHEN issue.deadline_unix = 0 THEN 253370764800 ELSE issue.deadline_unix END ASC")
  981. case "farduedate":
  982. sess.Desc("issue.deadline_unix")
  983. case "priorityrepo":
  984. sess.OrderBy("CASE WHEN issue.repo_id = " + strconv.FormatInt(priorityRepoID, 10) + " THEN 1 ELSE 2 END, issue.created_unix DESC")
  985. default:
  986. sess.Desc("issue.created_unix")
  987. }
  988. }
  989. func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
  990. if opts.Page >= 0 && opts.PageSize > 0 {
  991. var start int
  992. if opts.Page == 0 {
  993. start = 0
  994. } else {
  995. start = (opts.Page - 1) * opts.PageSize
  996. }
  997. sess.Limit(opts.PageSize, start)
  998. }
  999. if len(opts.IssueIDs) > 0 {
  1000. sess.In("issue.id", opts.IssueIDs)
  1001. }
  1002. if len(opts.RepoIDs) > 0 {
  1003. // In case repository IDs are provided but actually no repository has issue.
  1004. sess.In("issue.repo_id", opts.RepoIDs)
  1005. }
  1006. switch opts.IsClosed {
  1007. case util.OptionalBoolTrue:
  1008. sess.And("issue.is_closed=?", true)
  1009. case util.OptionalBoolFalse:
  1010. sess.And("issue.is_closed=?", false)
  1011. }
  1012. if opts.AssigneeID > 0 {
  1013. sess.Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
  1014. And("issue_assignees.assignee_id = ?", opts.AssigneeID)
  1015. }
  1016. if opts.PosterID > 0 {
  1017. sess.And("issue.poster_id=?", opts.PosterID)
  1018. }
  1019. if opts.MentionedID > 0 {
  1020. sess.Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
  1021. And("issue_user.is_mentioned = ?", true).
  1022. And("issue_user.uid = ?", opts.MentionedID)
  1023. }
  1024. if opts.MilestoneID > 0 {
  1025. sess.And("issue.milestone_id=?", opts.MilestoneID)
  1026. }
  1027. switch opts.IsPull {
  1028. case util.OptionalBoolTrue:
  1029. sess.And("issue.is_pull=?", true)
  1030. case util.OptionalBoolFalse:
  1031. sess.And("issue.is_pull=?", false)
  1032. }
  1033. if opts.LabelIDs != nil {
  1034. for i, labelID := range opts.LabelIDs {
  1035. if labelID > 0 {
  1036. sess.Join("INNER", fmt.Sprintf("issue_label il%d", i),
  1037. fmt.Sprintf("issue.id = il%[1]d.issue_id AND il%[1]d.label_id = %[2]d", i, labelID))
  1038. } else {
  1039. sess.Where("issue.id not in (select issue_id from issue_label where label_id = ?)", -labelID)
  1040. }
  1041. }
  1042. }
  1043. }
  1044. // CountIssuesByRepo map from repoID to number of issues matching the options
  1045. func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
  1046. sess := x.NewSession()
  1047. defer sess.Close()
  1048. opts.setupSession(sess)
  1049. countsSlice := make([]*struct {
  1050. RepoID int64
  1051. Count int64
  1052. }, 0, 10)
  1053. if err := sess.GroupBy("issue.repo_id").
  1054. Select("issue.repo_id AS repo_id, COUNT(*) AS count").
  1055. Table("issue").
  1056. Find(&countsSlice); err != nil {
  1057. return nil, err
  1058. }
  1059. countMap := make(map[int64]int64, len(countsSlice))
  1060. for _, c := range countsSlice {
  1061. countMap[c.RepoID] = c.Count
  1062. }
  1063. return countMap, nil
  1064. }
  1065. // Issues returns a list of issues by given conditions.
  1066. func Issues(opts *IssuesOptions) ([]*Issue, error) {
  1067. sess := x.NewSession()
  1068. defer sess.Close()
  1069. opts.setupSession(sess)
  1070. sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID)
  1071. issues := make([]*Issue, 0, setting.UI.IssuePagingNum)
  1072. if err := sess.Find(&issues); err != nil {
  1073. return nil, fmt.Errorf("Find: %v", err)
  1074. }
  1075. sess.Close()
  1076. if err := IssueList(issues).LoadAttributes(); err != nil {
  1077. return nil, fmt.Errorf("LoadAttributes: %v", err)
  1078. }
  1079. return issues, nil
  1080. }
  1081. // GetParticipantsIDsByIssueID returns the IDs of all users who participated in comments of an issue,
  1082. // but skips joining with `user` for performance reasons.
  1083. // User permissions must be verified elsewhere if required.
  1084. func GetParticipantsIDsByIssueID(issueID int64) ([]int64, error) {
  1085. userIDs := make([]int64, 0, 5)
  1086. return userIDs, x.Table("comment").
  1087. Cols("poster_id").
  1088. Where("issue_id = ?", issueID).
  1089. And("type in (?,?,?)", CommentTypeComment, CommentTypeCode, CommentTypeReview).
  1090. Distinct("poster_id").
  1091. Find(&userIDs)
  1092. }
  1093. // GetParticipantsByIssueID returns all users who are participated in comments of an issue.
  1094. func GetParticipantsByIssueID(issueID int64) ([]*User, error) {
  1095. return getParticipantsByIssueID(x, issueID)
  1096. }
  1097. func getParticipantsByIssueID(e Engine, issueID int64) ([]*User, error) {
  1098. userIDs := make([]int64, 0, 5)
  1099. if err := e.Table("comment").Cols("poster_id").
  1100. Where("`comment`.issue_id = ?", issueID).
  1101. And("`comment`.type in (?,?,?)", CommentTypeComment, CommentTypeCode, CommentTypeReview).
  1102. And("`user`.is_active = ?", true).
  1103. And("`user`.prohibit_login = ?", false).
  1104. Join("INNER", "`user`", "`user`.id = `comment`.poster_id").
  1105. Distinct("poster_id").
  1106. Find(&userIDs); err != nil {
  1107. return nil, fmt.Errorf("get poster IDs: %v", err)
  1108. }
  1109. if len(userIDs) == 0 {
  1110. return nil, nil
  1111. }
  1112. users := make([]*User, 0, len(userIDs))
  1113. return users, e.In("id", userIDs).Find(&users)
  1114. }
  1115. // UpdateIssueMentions updates issue-user relations for mentioned users.
  1116. func UpdateIssueMentions(ctx DBContext, issueID int64, mentions []*User) error {
  1117. if len(mentions) == 0 {
  1118. return nil
  1119. }
  1120. ids := make([]int64, len(mentions))
  1121. for i, u := range mentions {
  1122. ids[i] = u.ID
  1123. }
  1124. if err := UpdateIssueUsersByMentions(ctx, issueID, ids); err != nil {
  1125. return fmt.Errorf("UpdateIssueUsersByMentions: %v", err)
  1126. }
  1127. return nil
  1128. }
  1129. // IssueStats represents issue statistic information.
  1130. type IssueStats struct {
  1131. OpenCount, ClosedCount int64
  1132. YourRepositoriesCount int64
  1133. AssignCount int64
  1134. CreateCount int64
  1135. MentionCount int64
  1136. }
  1137. // Filter modes.
  1138. const (
  1139. FilterModeAll = iota
  1140. FilterModeAssign
  1141. FilterModeCreate
  1142. FilterModeMention
  1143. )
  1144. func parseCountResult(results []map[string][]byte) int64 {
  1145. if len(results) == 0 {
  1146. return 0
  1147. }
  1148. for _, result := range results[0] {
  1149. return com.StrTo(string(result)).MustInt64()
  1150. }
  1151. return 0
  1152. }
  1153. // IssueStatsOptions contains parameters accepted by GetIssueStats.
  1154. type IssueStatsOptions struct {
  1155. RepoID int64
  1156. Labels string
  1157. MilestoneID int64
  1158. AssigneeID int64
  1159. MentionedID int64
  1160. PosterID int64
  1161. IsPull util.OptionalBool
  1162. IssueIDs []int64
  1163. }
  1164. // GetIssueStats returns issue statistic information by given conditions.
  1165. func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
  1166. stats := &IssueStats{}
  1167. countSession := func(opts *IssueStatsOptions) *xorm.Session {
  1168. sess := x.
  1169. Where("issue.repo_id = ?", opts.RepoID)
  1170. if len(opts.IssueIDs) > 0 {
  1171. sess.In("issue.id", opts.IssueIDs)
  1172. }
  1173. if len(opts.Labels) > 0 && opts.Labels != "0" {
  1174. labelIDs, err := base.StringsToInt64s(strings.Split(opts.Labels, ","))
  1175. if err != nil {
  1176. log.Warn("Malformed Labels argument: %s", opts.Labels)
  1177. } else {
  1178. for i, labelID := range labelIDs {
  1179. if labelID > 0 {
  1180. sess.Join("INNER", fmt.Sprintf("issue_label il%d", i),
  1181. fmt.Sprintf("issue.id = il%[1]d.issue_id AND il%[1]d.label_id = %[2]d", i, labelID))
  1182. } else {
  1183. sess.Where("issue.id NOT IN (SELECT issue_id FROM issue_label WHERE label_id = ?)", -labelID)
  1184. }
  1185. }
  1186. }
  1187. }
  1188. if opts.MilestoneID > 0 {
  1189. sess.And("issue.milestone_id = ?", opts.MilestoneID)
  1190. }
  1191. if opts.AssigneeID > 0 {
  1192. sess.Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
  1193. And("issue_assignees.assignee_id = ?", opts.AssigneeID)
  1194. }
  1195. if opts.PosterID > 0 {
  1196. sess.And("issue.poster_id = ?", opts.PosterID)
  1197. }
  1198. if opts.MentionedID > 0 {
  1199. sess.Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
  1200. And("issue_user.uid = ?", opts.MentionedID).
  1201. And("issue_user.is_mentioned = ?", true)
  1202. }
  1203. switch opts.IsPull {
  1204. case util.OptionalBoolTrue:
  1205. sess.And("issue.is_pull=?", true)
  1206. case util.OptionalBoolFalse:
  1207. sess.And("issue.is_pull=?", false)
  1208. }
  1209. return sess
  1210. }
  1211. var err error
  1212. stats.OpenCount, err = countSession(opts).
  1213. And("issue.is_closed = ?", false).
  1214. Count(new(Issue))
  1215. if err != nil {
  1216. return stats, err
  1217. }
  1218. stats.ClosedCount, err = countSession(opts).
  1219. And("issue.is_closed = ?", true).
  1220. Count(new(Issue))
  1221. return stats, err
  1222. }
  1223. // UserIssueStatsOptions contains parameters accepted by GetUserIssueStats.
  1224. type UserIssueStatsOptions struct {
  1225. UserID int64
  1226. RepoIDs []int64
  1227. UserRepoIDs []int64
  1228. FilterMode int
  1229. IsPull bool
  1230. IsClosed bool
  1231. }
  1232. // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
  1233. func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
  1234. var err error
  1235. stats := &IssueStats{}
  1236. cond := builder.NewCond()
  1237. cond = cond.And(builder.Eq{"issue.is_pull": opts.IsPull})
  1238. if len(opts.RepoIDs) > 0 {
  1239. cond = cond.And(builder.In("issue.repo_id", opts.RepoIDs))
  1240. }
  1241. switch opts.FilterMode {
  1242. case FilterModeAll:
  1243. stats.OpenCount, err = x.Where(cond).And("issue.is_closed = ?", false).
  1244. And(builder.In("issue.repo_id", opts.UserRepoIDs)).
  1245. Count(new(Issue))
  1246. if err != nil {
  1247. return nil, err
  1248. }
  1249. stats.ClosedCount, err = x.Where(cond).And("issue.is_closed = ?", true).
  1250. And(builder.In("issue.repo_id", opts.UserRepoIDs)).
  1251. Count(new(Issue))
  1252. if err != nil {
  1253. return nil, err
  1254. }
  1255. case FilterModeAssign:
  1256. stats.OpenCount, err = x.Where(cond).And("issue.is_closed = ?", false).
  1257. Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
  1258. And("issue_assignees.assignee_id = ?", opts.UserID).
  1259. Count(new(Issue))
  1260. if err != nil {
  1261. return nil, err
  1262. }
  1263. stats.ClosedCount, err = x.Where(cond).And("issue.is_closed = ?", true).
  1264. Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
  1265. And("issue_assignees.assignee_id = ?", opts.UserID).
  1266. Count(new(Issue))
  1267. if err != nil {
  1268. return nil, err
  1269. }
  1270. case FilterModeCreate:
  1271. stats.OpenCount, err = x.Where(cond).And("issue.is_closed = ?", false).
  1272. And("issue.poster_id = ?", opts.UserID).
  1273. Count(new(Issue))
  1274. if err != nil {
  1275. return nil, err
  1276. }
  1277. stats.ClosedCount, err = x.Where(cond).And("issue.is_closed = ?", true).
  1278. And("issue.poster_id = ?", opts.UserID).
  1279. Count(new(Issue))
  1280. if err != nil {
  1281. return nil, err
  1282. }
  1283. case FilterModeMention:
  1284. stats.OpenCount, err = x.Where(cond).And("issue.is_closed = ?", false).
  1285. Join("INNER", "issue_user", "issue.id = issue_user.issue_id and issue_user.is_mentioned = ?", true).
  1286. And("issue_user.uid = ?", opts.UserID).
  1287. Count(new(Issue))
  1288. if err != nil {
  1289. return nil, err
  1290. }
  1291. stats.ClosedCount, err = x.Where(cond).And("issue.is_closed = ?", true).
  1292. Join("INNER", "issue_user", "issue.id = issue_user.issue_id and issue_user.is_mentioned = ?", true).
  1293. And("issue_user.uid = ?", opts.UserID).
  1294. Count(new(Issue))
  1295. if err != nil {
  1296. return nil, err
  1297. }
  1298. }
  1299. cond = cond.And(builder.Eq{"issue.is_closed": opts.IsClosed})
  1300. stats.AssignCount, err = x.Where(cond).
  1301. Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
  1302. And("issue_assignees.assignee_id = ?", opts.UserID).
  1303. Count(new(Issue))
  1304. if err != nil {
  1305. return nil, err
  1306. }
  1307. stats.CreateCount, err = x.Where(cond).
  1308. And("poster_id = ?", opts.UserID).
  1309. Count(new(Issue))
  1310. if err != nil {
  1311. return nil, err
  1312. }
  1313. stats.MentionCount, err = x.Where(cond).
  1314. Join("INNER", "issue_user", "issue.id = issue_user.issue_id and issue_user.is_mentioned = ?", true).
  1315. And("issue_user.uid = ?", opts.UserID).
  1316. Count(new(Issue))
  1317. if err != nil {
  1318. return nil, err
  1319. }
  1320. stats.YourRepositoriesCount, err = x.Where(cond).
  1321. And(builder.In("issue.repo_id", opts.UserRepoIDs)).
  1322. Count(new(Issue))
  1323. if err != nil {
  1324. return nil, err
  1325. }
  1326. return stats, nil
  1327. }
  1328. // GetRepoIssueStats returns number of open and closed repository issues by given filter mode.
  1329. func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen int64, numClosed int64) {
  1330. countSession := func(isClosed, isPull bool, repoID int64) *xorm.Session {
  1331. sess := x.
  1332. Where("is_closed = ?", isClosed).
  1333. And("is_pull = ?", isPull).
  1334. And("repo_id = ?", repoID)
  1335. return sess
  1336. }
  1337. openCountSession := countSession(false, isPull, repoID)
  1338. closedCountSession := countSession(true, isPull, repoID)
  1339. switch filterMode {
  1340. case FilterModeAssign:
  1341. openCountSession.Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
  1342. And("issue_assignees.assignee_id = ?", uid)
  1343. closedCountSession.Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
  1344. And("issue_assignees.assignee_id = ?", uid)
  1345. case FilterModeCreate:
  1346. openCountSession.And("poster_id = ?", uid)
  1347. closedCountSession.And("poster_id = ?", uid)
  1348. }
  1349. openResult, _ := openCountSession.Count(new(Issue))
  1350. closedResult, _ := closedCountSession.Count(new(Issue))
  1351. return openResult, closedResult
  1352. }
  1353. // SearchIssueIDsByKeyword search issues on database
  1354. func SearchIssueIDsByKeyword(kw string, repoIDs []int64, limit, start int) (int64, []int64, error) {
  1355. var repoCond = builder.In("repo_id", repoIDs)
  1356. var subQuery = builder.Select("id").From("issue").Where(repoCond)
  1357. var cond = builder.And(
  1358. repoCond,
  1359. builder.Or(
  1360. builder.Like{"name", kw},
  1361. builder.Like{"content", kw},
  1362. builder.In("id", builder.Select("issue_id").
  1363. From("comment").
  1364. Where(builder.And(
  1365. builder.Eq{"type": CommentTypeComment},
  1366. builder.In("issue_id", subQuery),
  1367. builder.Like{"content", kw},
  1368. )),
  1369. ),
  1370. ),
  1371. )
  1372. var ids = make([]int64, 0, limit)
  1373. err := x.Distinct("id").Table("issue").Where(cond).Limit(limit, start).Find(&ids)
  1374. if err != nil {
  1375. return 0, nil, err
  1376. }
  1377. total, err := x.Distinct("id").Table("issue").Where(cond).Count()
  1378. if err != nil {
  1379. return 0, nil, err
  1380. }
  1381. return total, ids, nil
  1382. }
  1383. // UpdateIssueByAPI updates all allowed fields of given issue.
  1384. func UpdateIssueByAPI(issue *Issue) error {
  1385. sess := x.NewSession()
  1386. defer sess.Close()
  1387. if err := sess.Begin(); err != nil {
  1388. return err
  1389. }
  1390. if _, err := sess.ID(issue.ID).Cols(
  1391. "name", "is_closed", "content", "milestone_id", "priority",
  1392. "deadline_unix", "updated_unix", "closed_unix", "is_locked").
  1393. Update(issue); err != nil {
  1394. return err
  1395. }
  1396. if err := issue.loadPoster(sess); err != nil {
  1397. return err
  1398. }
  1399. if err := issue.addCrossReferences(sess, issue.Poster, true); err != nil {
  1400. return err
  1401. }
  1402. return sess.Commit()
  1403. }
  1404. // UpdateIssueDeadline updates an issue deadline and adds comments. Setting a deadline to 0 means deleting it.
  1405. func UpdateIssueDeadline(issue *Issue, deadlineUnix timeutil.TimeStamp, doer *User) (err error) {
  1406. // if the deadline hasn't changed do nothing
  1407. if issue.DeadlineUnix == deadlineUnix {
  1408. return nil
  1409. }
  1410. sess := x.NewSession()
  1411. defer sess.Close()
  1412. if err := sess.Begin(); err != nil {
  1413. return err
  1414. }
  1415. // Update the deadline
  1416. if err = updateIssueCols(sess, &Issue{ID: issue.ID, DeadlineUnix: deadlineUnix}, "deadline_unix"); err != nil {
  1417. return err
  1418. }
  1419. // Make the comment
  1420. if _, err = createDeadlineComment(sess, doer, issue, deadlineUnix); err != nil {
  1421. return fmt.Errorf("createRemovedDueDateComment: %v", err)
  1422. }
  1423. return sess.Commit()
  1424. }
  1425. // DependencyInfo represents high level information about an issue which is a dependency of another issue.
  1426. type DependencyInfo struct {
  1427. Issue `xorm:"extends"`
  1428. Repository `xorm:"extends"`
  1429. }
  1430. // Get Blocked By Dependencies, aka all issues this issue is blocked by.
  1431. func (issue *Issue) getBlockedByDependencies(e Engine) (issueDeps []*DependencyInfo, err error) {
  1432. return issueDeps, e.
  1433. Table("issue").
  1434. Join("INNER", "repository", "repository.id = issue.repo_id").
  1435. Join("INNER", "issue_dependency", "issue_dependency.dependency_id = issue.id").
  1436. Where("issue_id = ?", issue.ID).
  1437. //sort by repo id then created date, with the issues of the same repo at the beginning of the list
  1438. OrderBy("CASE WHEN issue.repo_id = " + strconv.FormatInt(issue.RepoID, 10) + " THEN 0 ELSE issue.repo_id END, issue.created_unix DESC").
  1439. Find(&issueDeps)
  1440. }
  1441. // Get Blocking Dependencies, aka all issues this issue blocks.
  1442. func (issue *Issue) getBlockingDependencies(e Engine) (issueDeps []*DependencyInfo, err error) {
  1443. return issueDeps, e.
  1444. Table("issue").
  1445. Join("INNER", "repository", "repository.id = issue.repo_id").
  1446. Join("INNER", "issue_dependency", "issue_dependency.issue_id = issue.id").
  1447. Where("dependency_id = ?", issue.ID).
  1448. //sort by repo id then created date, with the issues of the same repo at the beginning of the list
  1449. OrderBy("CASE WHEN issue.repo_id = " + strconv.FormatInt(issue.RepoID, 10) + " THEN 0 ELSE issue.repo_id END, issue.created_unix DESC").
  1450. Find(&issueDeps)
  1451. }
  1452. // BlockedByDependencies finds all Dependencies an issue is blocked by
  1453. func (issue *Issue) BlockedByDependencies() ([]*DependencyInfo, error) {
  1454. return issue.getBlockedByDependencies(x)
  1455. }
  1456. // BlockingDependencies returns all blocking dependencies, aka all other issues a given issue blocks
  1457. func (issue *Issue) BlockingDependencies() ([]*DependencyInfo, error) {
  1458. return issue.getBlockingDependencies(x)
  1459. }
  1460. func (issue *Issue) updateClosedNum(e Engine) (err error) {
  1461. if issue.IsPull {
  1462. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=(SELECT count(*) FROM issue WHERE repo_id=? AND is_pull=? AND is_closed=?) WHERE id=?",
  1463. issue.RepoID,
  1464. true,
  1465. true,
  1466. issue.RepoID,
  1467. )
  1468. } else {
  1469. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=(SELECT count(*) FROM issue WHERE repo_id=? AND is_pull=? AND is_closed=?) WHERE id=?",
  1470. issue.RepoID,
  1471. false,
  1472. true,
  1473. issue.RepoID,
  1474. )
  1475. }
  1476. return
  1477. }
  1478. // ResolveMentionsByVisibility returns the users mentioned in an issue, removing those that
  1479. // don't have access to reading it. Teams are expanded into their users, but organizations are ignored.
  1480. func (issue *Issue) ResolveMentionsByVisibility(ctx DBContext, doer *User, mentions []string) (users []*User, err error) {
  1481. if len(mentions) == 0 {
  1482. return
  1483. }
  1484. if err = issue.loadRepo(ctx.e); err != nil {
  1485. return
  1486. }
  1487. resolved := make(map[string]bool, 20)
  1488. names := make([]string, 0, 20)
  1489. resolved[doer.LowerName] = true
  1490. for _, name := range mentions {
  1491. name := strings.ToLower(name)
  1492. if _, ok := resolved[name]; ok {
  1493. continue
  1494. }
  1495. resolved[name] = false
  1496. names = append(names, name)
  1497. }
  1498. if err := issue.Repo.getOwner(ctx.e); err != nil {
  1499. return nil, err
  1500. }
  1501. if issue.Repo.Owner.IsOrganization() {
  1502. // Since there can be users with names that match the name of a team,
  1503. // if the team exists and can read the issue, the team takes precedence.
  1504. teams := make([]*Team, 0, len(names))
  1505. if err := ctx.e.
  1506. Join("INNER", "team_repo", "team_repo.team_id = team.id").
  1507. Where("team_repo.repo_id=?", issue.Repo.ID).
  1508. In("team.lower_name", names).
  1509. Find(&teams); err != nil {
  1510. return nil, fmt.Errorf("find mentioned teams: %v", err)
  1511. }
  1512. if len(teams) != 0 {
  1513. checked := make([]int64, 0, len(teams))
  1514. unittype := UnitTypeIssues
  1515. if issue.IsPull {
  1516. unittype = UnitTypePullRequests
  1517. }
  1518. for _, team := range teams {
  1519. if team.Authorize >= AccessModeOwner {
  1520. checked = append(checked, team.ID)
  1521. resolved[team.LowerName] = true
  1522. continue
  1523. }
  1524. has, err := ctx.e.Get(&TeamUnit{OrgID: issue.Repo.Owner.ID, TeamID: team.ID, Type: unittype})
  1525. if err != nil {
  1526. return nil, fmt.Errorf("get team units (%d): %v", team.ID, err)
  1527. }
  1528. if has {
  1529. checked = append(checked, team.ID)
  1530. resolved[team.LowerName] = true
  1531. }
  1532. }
  1533. if len(checked) != 0 {
  1534. teamusers := make([]*User, 0, 20)
  1535. if err := ctx.e.
  1536. Join("INNER", "team_user", "team_user.uid = `user`.id").
  1537. In("`team_user`.team_id", checked).
  1538. And("`user`.is_active = ?", true).
  1539. And("`user`.prohibit_login = ?", false).
  1540. Find(&teamusers); err != nil {
  1541. return nil, fmt.Errorf("get teams users: %v", err)
  1542. }
  1543. if len(teamusers) > 0 {
  1544. users = make([]*User, 0, len(teamusers))
  1545. for _, user := range teamusers {
  1546. if already, ok := resolved[user.LowerName]; !ok || !already {
  1547. users = append(users, user)
  1548. resolved[user.LowerName] = true
  1549. }
  1550. }
  1551. }
  1552. }
  1553. }
  1554. // Remove names already in the list to avoid querying the database if pending names remain
  1555. names = make([]string, 0, len(resolved))
  1556. for name, already := range resolved {
  1557. if !already {
  1558. names = append(names, name)
  1559. }
  1560. }
  1561. if len(names) == 0 {
  1562. return
  1563. }
  1564. }
  1565. unchecked := make([]*User, 0, len(names))
  1566. if err := ctx.e.
  1567. Where("`user`.is_active = ?", true).
  1568. And("`user`.prohibit_login = ?", false).
  1569. In("`user`.lower_name", names).
  1570. Find(&unchecked); err != nil {
  1571. return nil, fmt.Errorf("find mentioned users: %v", err)
  1572. }
  1573. for _, user := range unchecked {
  1574. if already := resolved[user.LowerName]; already || user.IsOrganization() {
  1575. continue
  1576. }
  1577. // Normal users must have read access to the referencing issue
  1578. perm, err := getUserRepoPermission(ctx.e, issue.Repo, user)
  1579. if err != nil {
  1580. return nil, fmt.Errorf("getUserRepoPermission [%d]: %v", user.ID, err)
  1581. }
  1582. if !perm.CanReadIssuesOrPulls(issue.IsPull) {
  1583. continue
  1584. }
  1585. users = append(users, user)
  1586. }
  1587. return
  1588. }
  1589. // UpdateIssuesMigrationsByType updates all migrated repositories' issues from gitServiceType to replace originalAuthorID to posterID
  1590. func UpdateIssuesMigrationsByType(gitServiceType structs.GitServiceType, originalAuthorID string, posterID int64) error {
  1591. _, err := x.Table("issue").
  1592. Where("repo_id IN (SELECT id FROM repository WHERE original_service_type = ?)", gitServiceType).
  1593. And("original_author_id = ?", originalAuthorID).
  1594. Update(map[string]interface{}{
  1595. "poster_id": posterID,
  1596. "original_author": "",
  1597. "original_author_id": 0,
  1598. })
  1599. return err
  1600. }
  1601. // UpdateReactionsMigrationsByType updates all migrated repositories' reactions from gitServiceType to replace originalAuthorID to posterID
  1602. func UpdateReactionsMigrationsByType(gitServiceType structs.GitServiceType, originalAuthorID string, userID int64) error {
  1603. _, err := x.Table("reaction").
  1604. Join("INNER", "issue", "issue.id = reaction.issue_id").
  1605. Where("issue.repo_id IN (SELECT id FROM repository WHERE original_service_type = ?)", gitServiceType).
  1606. And("reaction.original_author_id = ?", originalAuthorID).
  1607. Update(map[string]interface{}{
  1608. "user_id": userID,
  1609. "original_author": "",
  1610. "original_author_id": 0,
  1611. })
  1612. return err
  1613. }