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 49KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 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 repo
  6. import (
  7. "bytes"
  8. "errors"
  9. "fmt"
  10. "io/ioutil"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. "code.gitea.io/gitea/models"
  15. "code.gitea.io/gitea/modules/auth"
  16. "code.gitea.io/gitea/modules/base"
  17. "code.gitea.io/gitea/modules/context"
  18. "code.gitea.io/gitea/modules/git"
  19. issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
  20. "code.gitea.io/gitea/modules/log"
  21. "code.gitea.io/gitea/modules/markup"
  22. "code.gitea.io/gitea/modules/markup/markdown"
  23. "code.gitea.io/gitea/modules/setting"
  24. api "code.gitea.io/gitea/modules/structs"
  25. "code.gitea.io/gitea/modules/util"
  26. comment_service "code.gitea.io/gitea/services/comments"
  27. issue_service "code.gitea.io/gitea/services/issue"
  28. pull_service "code.gitea.io/gitea/services/pull"
  29. "github.com/unknwon/com"
  30. )
  31. const (
  32. tplAttachment base.TplName = "repo/issue/view_content/attachments"
  33. tplIssues base.TplName = "repo/issue/list"
  34. tplIssueNew base.TplName = "repo/issue/new"
  35. tplIssueView base.TplName = "repo/issue/view"
  36. tplReactions base.TplName = "repo/issue/view_content/reactions"
  37. issueTemplateKey = "IssueTemplate"
  38. )
  39. var (
  40. // ErrTooManyFiles upload too many files
  41. ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded")
  42. // IssueTemplateCandidates issue templates
  43. IssueTemplateCandidates = []string{
  44. "ISSUE_TEMPLATE.md",
  45. "issue_template.md",
  46. ".gitea/ISSUE_TEMPLATE.md",
  47. ".gitea/issue_template.md",
  48. ".github/ISSUE_TEMPLATE.md",
  49. ".github/issue_template.md",
  50. }
  51. )
  52. // MustAllowUserComment checks to make sure if an issue is locked.
  53. // If locked and user has permissions to write to the repository,
  54. // then the comment is allowed, else it is blocked
  55. func MustAllowUserComment(ctx *context.Context) {
  56. issue := GetActionIssue(ctx)
  57. if ctx.Written() {
  58. return
  59. }
  60. if issue.IsLocked && !ctx.Repo.CanWrite(models.UnitTypeIssues) && !ctx.User.IsAdmin {
  61. ctx.Flash.Error(ctx.Tr("repo.issues.comment_on_locked"))
  62. ctx.Redirect(issue.HTMLURL())
  63. return
  64. }
  65. }
  66. // MustEnableIssues check if repository enable internal issues
  67. func MustEnableIssues(ctx *context.Context) {
  68. if !ctx.Repo.CanRead(models.UnitTypeIssues) &&
  69. !ctx.Repo.CanRead(models.UnitTypeExternalTracker) {
  70. ctx.NotFound("MustEnableIssues", nil)
  71. return
  72. }
  73. unit, err := ctx.Repo.Repository.GetUnit(models.UnitTypeExternalTracker)
  74. if err == nil {
  75. ctx.Redirect(unit.ExternalTrackerConfig().ExternalTrackerURL)
  76. return
  77. }
  78. }
  79. // MustAllowPulls check if repository enable pull requests and user have right to do that
  80. func MustAllowPulls(ctx *context.Context) {
  81. if !ctx.Repo.Repository.CanEnablePulls() || !ctx.Repo.CanRead(models.UnitTypePullRequests) {
  82. ctx.NotFound("MustAllowPulls", nil)
  83. return
  84. }
  85. // User can send pull request if owns a forked repository.
  86. if ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID) {
  87. ctx.Repo.PullRequest.Allowed = true
  88. ctx.Repo.PullRequest.HeadInfo = ctx.User.Name + ":" + ctx.Repo.BranchName
  89. }
  90. }
  91. func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalBool) {
  92. var err error
  93. viewType := ctx.Query("type")
  94. sortType := ctx.Query("sort")
  95. types := []string{"all", "your_repositories", "assigned", "created_by", "mentioned"}
  96. if !com.IsSliceContainsStr(types, viewType) {
  97. viewType = "all"
  98. }
  99. var (
  100. assigneeID = ctx.QueryInt64("assignee")
  101. posterID int64
  102. mentionedID int64
  103. forceEmpty bool
  104. )
  105. if ctx.IsSigned {
  106. switch viewType {
  107. case "created_by":
  108. posterID = ctx.User.ID
  109. case "mentioned":
  110. mentionedID = ctx.User.ID
  111. }
  112. }
  113. repo := ctx.Repo.Repository
  114. var labelIDs []int64
  115. selectLabels := ctx.Query("labels")
  116. if len(selectLabels) > 0 && selectLabels != "0" {
  117. labelIDs, err = base.StringsToInt64s(strings.Split(selectLabels, ","))
  118. if err != nil {
  119. ctx.ServerError("StringsToInt64s", err)
  120. return
  121. }
  122. }
  123. isShowClosed := ctx.Query("state") == "closed"
  124. keyword := strings.Trim(ctx.Query("q"), " ")
  125. if bytes.Contains([]byte(keyword), []byte{0x00}) {
  126. keyword = ""
  127. }
  128. var issueIDs []int64
  129. if len(keyword) > 0 {
  130. issueIDs, err = issue_indexer.SearchIssuesByKeyword([]int64{repo.ID}, keyword)
  131. if err != nil {
  132. ctx.ServerError("issueIndexer.Search", err)
  133. return
  134. }
  135. if len(issueIDs) == 0 {
  136. forceEmpty = true
  137. }
  138. }
  139. var issueStats *models.IssueStats
  140. if forceEmpty {
  141. issueStats = &models.IssueStats{}
  142. } else {
  143. issueStats, err = models.GetIssueStats(&models.IssueStatsOptions{
  144. RepoID: repo.ID,
  145. Labels: selectLabels,
  146. MilestoneID: milestoneID,
  147. AssigneeID: assigneeID,
  148. MentionedID: mentionedID,
  149. PosterID: posterID,
  150. IsPull: isPullOption,
  151. IssueIDs: issueIDs,
  152. })
  153. if err != nil {
  154. ctx.ServerError("GetIssueStats", err)
  155. return
  156. }
  157. }
  158. page := ctx.QueryInt("page")
  159. if page <= 1 {
  160. page = 1
  161. }
  162. var total int
  163. if !isShowClosed {
  164. total = int(issueStats.OpenCount)
  165. } else {
  166. total = int(issueStats.ClosedCount)
  167. }
  168. pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, 5)
  169. var issues []*models.Issue
  170. if forceEmpty {
  171. issues = []*models.Issue{}
  172. } else {
  173. issues, err = models.Issues(&models.IssuesOptions{
  174. RepoIDs: []int64{repo.ID},
  175. AssigneeID: assigneeID,
  176. PosterID: posterID,
  177. MentionedID: mentionedID,
  178. MilestoneID: milestoneID,
  179. Page: pager.Paginater.Current(),
  180. PageSize: setting.UI.IssuePagingNum,
  181. IsClosed: util.OptionalBoolOf(isShowClosed),
  182. IsPull: isPullOption,
  183. LabelIDs: labelIDs,
  184. SortType: sortType,
  185. IssueIDs: issueIDs,
  186. })
  187. if err != nil {
  188. ctx.ServerError("Issues", err)
  189. return
  190. }
  191. }
  192. var commitStatus = make(map[int64]*models.CommitStatus, len(issues))
  193. // Get posters.
  194. for i := range issues {
  195. // Check read status
  196. if !ctx.IsSigned {
  197. issues[i].IsRead = true
  198. } else if err = issues[i].GetIsRead(ctx.User.ID); err != nil {
  199. ctx.ServerError("GetIsRead", err)
  200. return
  201. }
  202. if issues[i].IsPull {
  203. if err := issues[i].LoadPullRequest(); err != nil {
  204. ctx.ServerError("LoadPullRequest", err)
  205. return
  206. }
  207. commitStatus[issues[i].PullRequest.ID], _ = issues[i].PullRequest.GetLastCommitStatus()
  208. }
  209. }
  210. ctx.Data["Issues"] = issues
  211. ctx.Data["CommitStatus"] = commitStatus
  212. // Get assignees.
  213. ctx.Data["Assignees"], err = repo.GetAssignees()
  214. if err != nil {
  215. ctx.ServerError("GetAssignees", err)
  216. return
  217. }
  218. labels, err := models.GetLabelsByRepoID(repo.ID, "")
  219. if err != nil {
  220. ctx.ServerError("GetLabelsByRepoID", err)
  221. return
  222. }
  223. for _, l := range labels {
  224. l.LoadSelectedLabelsAfterClick(labelIDs)
  225. }
  226. ctx.Data["Labels"] = labels
  227. ctx.Data["NumLabels"] = len(labels)
  228. if ctx.QueryInt64("assignee") == 0 {
  229. assigneeID = 0 // Reset ID to prevent unexpected selection of assignee.
  230. }
  231. ctx.Data["IssueStats"] = issueStats
  232. ctx.Data["SelLabelIDs"] = labelIDs
  233. ctx.Data["SelectLabels"] = selectLabels
  234. ctx.Data["ViewType"] = viewType
  235. ctx.Data["SortType"] = sortType
  236. ctx.Data["MilestoneID"] = milestoneID
  237. ctx.Data["AssigneeID"] = assigneeID
  238. ctx.Data["IsShowClosed"] = isShowClosed
  239. ctx.Data["Keyword"] = keyword
  240. if isShowClosed {
  241. ctx.Data["State"] = "closed"
  242. } else {
  243. ctx.Data["State"] = "open"
  244. }
  245. pager.AddParam(ctx, "q", "Keyword")
  246. pager.AddParam(ctx, "type", "ViewType")
  247. pager.AddParam(ctx, "sort", "SortType")
  248. pager.AddParam(ctx, "state", "State")
  249. pager.AddParam(ctx, "labels", "SelectLabels")
  250. pager.AddParam(ctx, "milestone", "MilestoneID")
  251. pager.AddParam(ctx, "assignee", "AssigneeID")
  252. ctx.Data["Page"] = pager
  253. }
  254. // Issues render issues page
  255. func Issues(ctx *context.Context) {
  256. isPullList := ctx.Params(":type") == "pulls"
  257. if isPullList {
  258. MustAllowPulls(ctx)
  259. if ctx.Written() {
  260. return
  261. }
  262. ctx.Data["Title"] = ctx.Tr("repo.pulls")
  263. ctx.Data["PageIsPullList"] = true
  264. } else {
  265. MustEnableIssues(ctx)
  266. if ctx.Written() {
  267. return
  268. }
  269. ctx.Data["Title"] = ctx.Tr("repo.issues")
  270. ctx.Data["PageIsIssueList"] = true
  271. }
  272. issues(ctx, ctx.QueryInt64("milestone"), util.OptionalBoolOf(isPullList))
  273. var err error
  274. // Get milestones.
  275. ctx.Data["Milestones"], err = models.GetMilestonesByRepoID(ctx.Repo.Repository.ID, api.StateType(ctx.Query("state")))
  276. if err != nil {
  277. ctx.ServerError("GetAllRepoMilestones", err)
  278. return
  279. }
  280. perm, err := models.GetUserRepoPermission(ctx.Repo.Repository, ctx.User)
  281. if err != nil {
  282. ctx.ServerError("GetUserRepoPermission", err)
  283. return
  284. }
  285. ctx.Data["CanWriteIssuesOrPulls"] = perm.CanWriteIssuesOrPulls(isPullList)
  286. ctx.HTML(200, tplIssues)
  287. }
  288. // RetrieveRepoMilestonesAndAssignees find all the milestones and assignees of a repository
  289. func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repository) {
  290. var err error
  291. ctx.Data["OpenMilestones"], err = models.GetMilestones(repo.ID, -1, false, "")
  292. if err != nil {
  293. ctx.ServerError("GetMilestones", err)
  294. return
  295. }
  296. ctx.Data["ClosedMilestones"], err = models.GetMilestones(repo.ID, -1, true, "")
  297. if err != nil {
  298. ctx.ServerError("GetMilestones", err)
  299. return
  300. }
  301. ctx.Data["Assignees"], err = repo.GetAssignees()
  302. if err != nil {
  303. ctx.ServerError("GetAssignees", err)
  304. return
  305. }
  306. }
  307. // RetrieveRepoMetas find all the meta information of a repository
  308. func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository) []*models.Label {
  309. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  310. return nil
  311. }
  312. labels, err := models.GetLabelsByRepoID(repo.ID, "")
  313. if err != nil {
  314. ctx.ServerError("GetLabelsByRepoID", err)
  315. return nil
  316. }
  317. ctx.Data["Labels"] = labels
  318. RetrieveRepoMilestonesAndAssignees(ctx, repo)
  319. if ctx.Written() {
  320. return nil
  321. }
  322. brs, err := ctx.Repo.GitRepo.GetBranches()
  323. if err != nil {
  324. ctx.ServerError("GetBranches", err)
  325. return nil
  326. }
  327. ctx.Data["Branches"] = brs
  328. // Contains true if the user can create issue dependencies
  329. ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.User)
  330. return labels
  331. }
  332. func getFileContentFromDefaultBranch(ctx *context.Context, filename string) (string, bool) {
  333. var bytes []byte
  334. if ctx.Repo.Commit == nil {
  335. var err error
  336. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
  337. if err != nil {
  338. return "", false
  339. }
  340. }
  341. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(filename)
  342. if err != nil {
  343. return "", false
  344. }
  345. if entry.Blob().Size() >= setting.UI.MaxDisplayFileSize {
  346. return "", false
  347. }
  348. r, err := entry.Blob().DataAsync()
  349. if err != nil {
  350. return "", false
  351. }
  352. defer r.Close()
  353. bytes, err = ioutil.ReadAll(r)
  354. if err != nil {
  355. return "", false
  356. }
  357. return string(bytes), true
  358. }
  359. func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles []string) {
  360. for _, filename := range possibleFiles {
  361. content, found := getFileContentFromDefaultBranch(ctx, filename)
  362. if found {
  363. ctx.Data[ctxDataKey] = content
  364. return
  365. }
  366. }
  367. }
  368. // NewIssue render creating issue page
  369. func NewIssue(ctx *context.Context) {
  370. ctx.Data["Title"] = ctx.Tr("repo.issues.new")
  371. ctx.Data["PageIsIssueList"] = true
  372. ctx.Data["RequireHighlightJS"] = true
  373. ctx.Data["RequireSimpleMDE"] = true
  374. ctx.Data["RequireTribute"] = true
  375. ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
  376. body := ctx.Query("body")
  377. ctx.Data["BodyQuery"] = body
  378. milestoneID := ctx.QueryInt64("milestone")
  379. if milestoneID > 0 {
  380. milestone, err := models.GetMilestoneByID(milestoneID)
  381. if err != nil {
  382. log.Error("GetMilestoneByID: %d: %v", milestoneID, err)
  383. } else {
  384. ctx.Data["milestone_id"] = milestoneID
  385. ctx.Data["Milestone"] = milestone
  386. }
  387. }
  388. setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates)
  389. renderAttachmentSettings(ctx)
  390. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  391. if ctx.Written() {
  392. return
  393. }
  394. ctx.HTML(200, tplIssueNew)
  395. }
  396. // ValidateRepoMetas check and returns repository's meta informations
  397. func ValidateRepoMetas(ctx *context.Context, form auth.CreateIssueForm, isPull bool) ([]int64, []int64, int64) {
  398. var (
  399. repo = ctx.Repo.Repository
  400. err error
  401. )
  402. labels := RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  403. if ctx.Written() {
  404. return nil, nil, 0
  405. }
  406. var labelIDs []int64
  407. hasSelected := false
  408. // Check labels.
  409. if len(form.LabelIDs) > 0 {
  410. labelIDs, err = base.StringsToInt64s(strings.Split(form.LabelIDs, ","))
  411. if err != nil {
  412. return nil, nil, 0
  413. }
  414. labelIDMark := base.Int64sToMap(labelIDs)
  415. for i := range labels {
  416. if labelIDMark[labels[i].ID] {
  417. labels[i].IsChecked = true
  418. hasSelected = true
  419. }
  420. }
  421. }
  422. ctx.Data["Labels"] = labels
  423. ctx.Data["HasSelectedLabel"] = hasSelected
  424. ctx.Data["label_ids"] = form.LabelIDs
  425. // Check milestone.
  426. milestoneID := form.MilestoneID
  427. if milestoneID > 0 {
  428. ctx.Data["Milestone"], err = repo.GetMilestoneByID(milestoneID)
  429. if err != nil {
  430. ctx.ServerError("GetMilestoneByID", err)
  431. return nil, nil, 0
  432. }
  433. ctx.Data["milestone_id"] = milestoneID
  434. }
  435. // Check assignees
  436. var assigneeIDs []int64
  437. if len(form.AssigneeIDs) > 0 {
  438. assigneeIDs, err = base.StringsToInt64s(strings.Split(form.AssigneeIDs, ","))
  439. if err != nil {
  440. return nil, nil, 0
  441. }
  442. // Check if the passed assignees actually exists and is assignable
  443. for _, aID := range assigneeIDs {
  444. assignee, err := models.GetUserByID(aID)
  445. if err != nil {
  446. ctx.ServerError("GetUserByID", err)
  447. return nil, nil, 0
  448. }
  449. valid, err := models.CanBeAssigned(assignee, repo, isPull)
  450. if err != nil {
  451. ctx.ServerError("canBeAssigned", err)
  452. return nil, nil, 0
  453. }
  454. if !valid {
  455. ctx.ServerError("canBeAssigned", models.ErrUserDoesNotHaveAccessToRepo{UserID: aID, RepoName: repo.Name})
  456. return nil, nil, 0
  457. }
  458. }
  459. }
  460. // Keep the old assignee id thingy for compatibility reasons
  461. if form.AssigneeID > 0 {
  462. assigneeIDs = append(assigneeIDs, form.AssigneeID)
  463. }
  464. return labelIDs, assigneeIDs, milestoneID
  465. }
  466. // NewIssuePost response for creating new issue
  467. func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
  468. ctx.Data["Title"] = ctx.Tr("repo.issues.new")
  469. ctx.Data["PageIsIssueList"] = true
  470. ctx.Data["RequireHighlightJS"] = true
  471. ctx.Data["RequireSimpleMDE"] = true
  472. ctx.Data["ReadOnly"] = false
  473. ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
  474. renderAttachmentSettings(ctx)
  475. var (
  476. repo = ctx.Repo.Repository
  477. attachments []string
  478. )
  479. labelIDs, assigneeIDs, milestoneID := ValidateRepoMetas(ctx, form, false)
  480. if ctx.Written() {
  481. return
  482. }
  483. if setting.AttachmentEnabled {
  484. attachments = form.Files
  485. }
  486. if ctx.HasError() {
  487. ctx.HTML(200, tplIssueNew)
  488. return
  489. }
  490. if util.IsEmptyString(form.Title) {
  491. ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplIssueNew, form)
  492. return
  493. }
  494. issue := &models.Issue{
  495. RepoID: repo.ID,
  496. Title: form.Title,
  497. PosterID: ctx.User.ID,
  498. Poster: ctx.User,
  499. MilestoneID: milestoneID,
  500. Content: form.Content,
  501. Ref: form.Ref,
  502. }
  503. if err := issue_service.NewIssue(repo, issue, labelIDs, attachments, assigneeIDs); err != nil {
  504. if models.IsErrUserDoesNotHaveAccessToRepo(err) {
  505. ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error())
  506. return
  507. }
  508. ctx.ServerError("NewIssue", err)
  509. return
  510. }
  511. log.Trace("Issue created: %d/%d", repo.ID, issue.ID)
  512. ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
  513. }
  514. // commentTag returns the CommentTag for a comment in/with the given repo, poster and issue
  515. func commentTag(repo *models.Repository, poster *models.User, issue *models.Issue) (models.CommentTag, error) {
  516. perm, err := models.GetUserRepoPermission(repo, poster)
  517. if err != nil {
  518. return models.CommentTagNone, err
  519. }
  520. if perm.IsOwner() {
  521. return models.CommentTagOwner, nil
  522. } else if poster.ID == issue.PosterID {
  523. return models.CommentTagPoster, nil
  524. } else if perm.CanWrite(models.UnitTypeCode) {
  525. return models.CommentTagWriter, nil
  526. }
  527. return models.CommentTagNone, nil
  528. }
  529. func getBranchData(ctx *context.Context, issue *models.Issue) {
  530. ctx.Data["BaseBranch"] = nil
  531. ctx.Data["HeadBranch"] = nil
  532. ctx.Data["HeadUserName"] = nil
  533. ctx.Data["BaseName"] = ctx.Repo.Repository.OwnerName
  534. if issue.IsPull {
  535. pull := issue.PullRequest
  536. ctx.Data["BaseBranch"] = pull.BaseBranch
  537. ctx.Data["HeadBranch"] = pull.HeadBranch
  538. ctx.Data["HeadUserName"] = pull.MustHeadUserName()
  539. }
  540. }
  541. // ViewIssue render issue view page
  542. func ViewIssue(ctx *context.Context) {
  543. if ctx.Params(":type") == "issues" {
  544. // If issue was requested we check if repo has external tracker and redirect
  545. extIssueUnit, err := ctx.Repo.Repository.GetUnit(models.UnitTypeExternalTracker)
  546. if err == nil && extIssueUnit != nil {
  547. if extIssueUnit.ExternalTrackerConfig().ExternalTrackerStyle == markup.IssueNameStyleNumeric || extIssueUnit.ExternalTrackerConfig().ExternalTrackerStyle == "" {
  548. metas := ctx.Repo.Repository.ComposeMetas()
  549. metas["index"] = ctx.Params(":index")
  550. ctx.Redirect(com.Expand(extIssueUnit.ExternalTrackerConfig().ExternalTrackerFormat, metas))
  551. return
  552. }
  553. } else if err != nil && !models.IsErrUnitTypeNotExist(err) {
  554. ctx.ServerError("GetUnit", err)
  555. return
  556. }
  557. }
  558. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  559. if err != nil {
  560. if models.IsErrIssueNotExist(err) {
  561. ctx.NotFound("GetIssueByIndex", err)
  562. } else {
  563. ctx.ServerError("GetIssueByIndex", err)
  564. }
  565. return
  566. }
  567. // Make sure type and URL matches.
  568. if ctx.Params(":type") == "issues" && issue.IsPull {
  569. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
  570. return
  571. } else if ctx.Params(":type") == "pulls" && !issue.IsPull {
  572. ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
  573. return
  574. }
  575. if issue.IsPull {
  576. MustAllowPulls(ctx)
  577. if ctx.Written() {
  578. return
  579. }
  580. ctx.Data["PageIsPullList"] = true
  581. ctx.Data["PageIsPullConversation"] = true
  582. } else {
  583. MustEnableIssues(ctx)
  584. if ctx.Written() {
  585. return
  586. }
  587. ctx.Data["PageIsIssueList"] = true
  588. }
  589. ctx.Data["RequireHighlightJS"] = true
  590. ctx.Data["RequireDropzone"] = true
  591. ctx.Data["RequireTribute"] = true
  592. ctx.Data["RequireSimpleMDE"] = true
  593. renderAttachmentSettings(ctx)
  594. if err = issue.LoadAttributes(); err != nil {
  595. ctx.ServerError("LoadAttributes", err)
  596. return
  597. }
  598. if err = filterXRefComments(ctx, issue); err != nil {
  599. ctx.ServerError("filterXRefComments", err)
  600. return
  601. }
  602. ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title)
  603. var iw *models.IssueWatch
  604. var exists bool
  605. if ctx.User != nil {
  606. iw, exists, err = models.GetIssueWatch(ctx.User.ID, issue.ID)
  607. if err != nil {
  608. ctx.ServerError("GetIssueWatch", err)
  609. return
  610. }
  611. if !exists {
  612. iw = &models.IssueWatch{
  613. UserID: ctx.User.ID,
  614. IssueID: issue.ID,
  615. IsWatching: models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID),
  616. }
  617. }
  618. }
  619. ctx.Data["IssueWatch"] = iw
  620. issue.RenderedContent = string(markdown.Render([]byte(issue.Content), ctx.Repo.RepoLink,
  621. ctx.Repo.Repository.ComposeMetas()))
  622. repo := ctx.Repo.Repository
  623. // Get more information if it's a pull request.
  624. if issue.IsPull {
  625. if issue.PullRequest.HasMerged {
  626. ctx.Data["DisableStatusChange"] = issue.PullRequest.HasMerged
  627. PrepareMergedViewPullInfo(ctx, issue)
  628. } else {
  629. PrepareViewPullInfo(ctx, issue)
  630. ctx.Data["DisableStatusChange"] = ctx.Data["IsPullRequestBroken"] == true && issue.IsClosed
  631. }
  632. if ctx.Written() {
  633. return
  634. }
  635. }
  636. // Metas.
  637. // Check labels.
  638. labelIDMark := make(map[int64]bool)
  639. for i := range issue.Labels {
  640. labelIDMark[issue.Labels[i].ID] = true
  641. }
  642. labels, err := models.GetLabelsByRepoID(repo.ID, "")
  643. if err != nil {
  644. ctx.ServerError("GetLabelsByRepoID", err)
  645. return
  646. }
  647. hasSelected := false
  648. for i := range labels {
  649. if labelIDMark[labels[i].ID] {
  650. labels[i].IsChecked = true
  651. hasSelected = true
  652. }
  653. }
  654. ctx.Data["HasSelectedLabel"] = hasSelected
  655. ctx.Data["Labels"] = labels
  656. // Check milestone and assignee.
  657. if ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
  658. RetrieveRepoMilestonesAndAssignees(ctx, repo)
  659. if ctx.Written() {
  660. return
  661. }
  662. }
  663. if ctx.IsSigned {
  664. // Update issue-user.
  665. if err = issue.ReadBy(ctx.User.ID); err != nil {
  666. ctx.ServerError("ReadBy", err)
  667. return
  668. }
  669. }
  670. var (
  671. tag models.CommentTag
  672. ok bool
  673. marked = make(map[int64]models.CommentTag)
  674. comment *models.Comment
  675. participants = make([]*models.User, 1, 10)
  676. )
  677. if ctx.Repo.Repository.IsTimetrackerEnabled() {
  678. if ctx.IsSigned {
  679. // Deal with the stopwatch
  680. ctx.Data["IsStopwatchRunning"] = models.StopwatchExists(ctx.User.ID, issue.ID)
  681. if !ctx.Data["IsStopwatchRunning"].(bool) {
  682. var exists bool
  683. var sw *models.Stopwatch
  684. if exists, sw, err = models.HasUserStopwatch(ctx.User.ID); err != nil {
  685. ctx.ServerError("HasUserStopwatch", err)
  686. return
  687. }
  688. ctx.Data["HasUserStopwatch"] = exists
  689. if exists {
  690. // Add warning if the user has already a stopwatch
  691. var otherIssue *models.Issue
  692. if otherIssue, err = models.GetIssueByID(sw.IssueID); err != nil {
  693. ctx.ServerError("GetIssueByID", err)
  694. return
  695. }
  696. if err = otherIssue.LoadRepo(); err != nil {
  697. ctx.ServerError("LoadRepo", err)
  698. return
  699. }
  700. // Add link to the issue of the already running stopwatch
  701. ctx.Data["OtherStopwatchURL"] = otherIssue.HTMLURL()
  702. }
  703. }
  704. ctx.Data["CanUseTimetracker"] = ctx.Repo.CanUseTimetracker(issue, ctx.User)
  705. } else {
  706. ctx.Data["CanUseTimetracker"] = false
  707. }
  708. if ctx.Data["WorkingUsers"], err = models.TotalTimes(models.FindTrackedTimesOptions{IssueID: issue.ID}); err != nil {
  709. ctx.ServerError("TotalTimes", err)
  710. return
  711. }
  712. }
  713. // Check if the user can use the dependencies
  714. ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.User)
  715. // check if dependencies can be created across repositories
  716. ctx.Data["AllowCrossRepositoryDependencies"] = setting.Service.AllowCrossRepositoryDependencies
  717. // Render comments and and fetch participants.
  718. participants[0] = issue.Poster
  719. for _, comment = range issue.Comments {
  720. comment.Issue = issue
  721. if err := comment.LoadPoster(); err != nil {
  722. ctx.ServerError("LoadPoster", err)
  723. return
  724. }
  725. if comment.Type == models.CommentTypeComment {
  726. if err := comment.LoadAttachments(); err != nil {
  727. ctx.ServerError("LoadAttachments", err)
  728. return
  729. }
  730. comment.RenderedContent = string(markdown.Render([]byte(comment.Content), ctx.Repo.RepoLink,
  731. ctx.Repo.Repository.ComposeMetas()))
  732. // Check tag.
  733. tag, ok = marked[comment.PosterID]
  734. if ok {
  735. comment.ShowTag = tag
  736. continue
  737. }
  738. comment.ShowTag, err = commentTag(repo, comment.Poster, issue)
  739. if err != nil {
  740. ctx.ServerError("commentTag", err)
  741. return
  742. }
  743. marked[comment.PosterID] = comment.ShowTag
  744. participants = addParticipant(comment.Poster, participants)
  745. } else if comment.Type == models.CommentTypeLabel {
  746. if err = comment.LoadLabel(); err != nil {
  747. ctx.ServerError("LoadLabel", err)
  748. return
  749. }
  750. } else if comment.Type == models.CommentTypeMilestone {
  751. if err = comment.LoadMilestone(); err != nil {
  752. ctx.ServerError("LoadMilestone", err)
  753. return
  754. }
  755. ghostMilestone := &models.Milestone{
  756. ID: -1,
  757. Name: ctx.Tr("repo.issues.deleted_milestone"),
  758. }
  759. if comment.OldMilestoneID > 0 && comment.OldMilestone == nil {
  760. comment.OldMilestone = ghostMilestone
  761. }
  762. if comment.MilestoneID > 0 && comment.Milestone == nil {
  763. comment.Milestone = ghostMilestone
  764. }
  765. } else if comment.Type == models.CommentTypeAssignees {
  766. if err = comment.LoadAssigneeUser(); err != nil {
  767. ctx.ServerError("LoadAssigneeUser", err)
  768. return
  769. }
  770. } else if comment.Type == models.CommentTypeRemoveDependency || comment.Type == models.CommentTypeAddDependency {
  771. if err = comment.LoadDepIssueDetails(); err != nil {
  772. ctx.ServerError("LoadDepIssueDetails", err)
  773. return
  774. }
  775. } else if comment.Type == models.CommentTypeCode || comment.Type == models.CommentTypeReview {
  776. if err = comment.LoadReview(); err != nil && !models.IsErrReviewNotExist(err) {
  777. ctx.ServerError("LoadReview", err)
  778. return
  779. }
  780. participants = addParticipant(comment.Poster, participants)
  781. if comment.Review == nil {
  782. continue
  783. }
  784. if err = comment.Review.LoadAttributes(); err != nil {
  785. if !models.IsErrUserNotExist(err) {
  786. ctx.ServerError("Review.LoadAttributes", err)
  787. return
  788. }
  789. comment.Review.Reviewer = models.NewGhostUser()
  790. }
  791. if err = comment.Review.LoadCodeComments(); err != nil {
  792. ctx.ServerError("Review.LoadCodeComments", err)
  793. return
  794. }
  795. }
  796. }
  797. getBranchData(ctx, issue)
  798. if issue.IsPull {
  799. pull := issue.PullRequest
  800. pull.Issue = issue
  801. canDelete := false
  802. ctx.Data["AllowMerge"] = false
  803. if ctx.IsSigned {
  804. if err := pull.GetHeadRepo(); err != nil {
  805. log.Error("GetHeadRepo: %v", err)
  806. } else if pull.HeadRepo != nil && pull.HeadBranch != pull.HeadRepo.DefaultBranch {
  807. perm, err := models.GetUserRepoPermission(pull.HeadRepo, ctx.User)
  808. if err != nil {
  809. ctx.ServerError("GetUserRepoPermission", err)
  810. return
  811. }
  812. if perm.CanWrite(models.UnitTypeCode) {
  813. // Check if branch is not protected
  814. if protected, err := pull.HeadRepo.IsProtectedBranch(pull.HeadBranch, ctx.User); err != nil {
  815. log.Error("IsProtectedBranch: %v", err)
  816. } else if !protected {
  817. canDelete = true
  818. ctx.Data["DeleteBranchLink"] = ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index) + "/cleanup"
  819. }
  820. }
  821. }
  822. if err := pull.GetBaseRepo(); err != nil {
  823. log.Error("GetBaseRepo: %v", err)
  824. }
  825. perm, err := models.GetUserRepoPermission(pull.BaseRepo, ctx.User)
  826. if err != nil {
  827. ctx.ServerError("GetUserRepoPermission", err)
  828. return
  829. }
  830. ctx.Data["AllowMerge"], err = pull_service.IsUserAllowedToMerge(pull, perm, ctx.User)
  831. if err != nil {
  832. ctx.ServerError("IsUserAllowedToMerge", err)
  833. return
  834. }
  835. }
  836. prUnit, err := repo.GetUnit(models.UnitTypePullRequests)
  837. if err != nil {
  838. ctx.ServerError("GetUnit", err)
  839. return
  840. }
  841. prConfig := prUnit.PullRequestsConfig()
  842. // Check correct values and select default
  843. if ms, ok := ctx.Data["MergeStyle"].(models.MergeStyle); !ok ||
  844. !prConfig.IsMergeStyleAllowed(ms) {
  845. if prConfig.AllowMerge {
  846. ctx.Data["MergeStyle"] = models.MergeStyleMerge
  847. } else if prConfig.AllowRebase {
  848. ctx.Data["MergeStyle"] = models.MergeStyleRebase
  849. } else if prConfig.AllowRebaseMerge {
  850. ctx.Data["MergeStyle"] = models.MergeStyleRebaseMerge
  851. } else if prConfig.AllowSquash {
  852. ctx.Data["MergeStyle"] = models.MergeStyleSquash
  853. } else {
  854. ctx.Data["MergeStyle"] = ""
  855. }
  856. }
  857. if err = pull.LoadProtectedBranch(); err != nil {
  858. ctx.ServerError("LoadProtectedBranch", err)
  859. return
  860. }
  861. if pull.ProtectedBranch != nil {
  862. cnt := pull.ProtectedBranch.GetGrantedApprovalsCount(pull)
  863. ctx.Data["IsBlockedByApprovals"] = !pull.ProtectedBranch.HasEnoughApprovals(pull)
  864. ctx.Data["IsBlockedByRejection"] = pull.ProtectedBranch.MergeBlockedByRejectedReview(pull)
  865. ctx.Data["GrantedApprovals"] = cnt
  866. ctx.Data["RequireSigned"] = pull.ProtectedBranch.RequireSignedCommits
  867. }
  868. ctx.Data["WillSign"] = false
  869. if ctx.User != nil {
  870. sign, key, err := pull.SignMerge(ctx.User, pull.BaseRepo.RepoPath(), pull.BaseBranch, pull.GetGitRefName())
  871. ctx.Data["WillSign"] = sign
  872. ctx.Data["SigningKey"] = key
  873. if err != nil {
  874. if models.IsErrWontSign(err) {
  875. ctx.Data["WontSignReason"] = err.(*models.ErrWontSign).Reason
  876. } else {
  877. ctx.Data["WontSignReason"] = "error"
  878. log.Error("Error whilst checking if could sign pr %d in repo %s. Error: %v", pull.ID, pull.BaseRepo.FullName(), err)
  879. }
  880. }
  881. }
  882. ctx.Data["IsPullBranchDeletable"] = canDelete &&
  883. pull.HeadRepo != nil &&
  884. git.IsBranchExist(pull.HeadRepo.RepoPath(), pull.HeadBranch) &&
  885. (!pull.HasMerged || ctx.Data["HeadBranchCommitID"] == ctx.Data["PullHeadCommitID"])
  886. ctx.Data["PullReviewers"], err = models.GetReviewersByIssueID(issue.ID)
  887. if err != nil {
  888. ctx.ServerError("GetReviewersByIssueID", err)
  889. return
  890. }
  891. }
  892. // Get Dependencies
  893. ctx.Data["BlockedByDependencies"], err = issue.BlockedByDependencies()
  894. if err != nil {
  895. ctx.ServerError("BlockedByDependencies", err)
  896. return
  897. }
  898. ctx.Data["BlockingDependencies"], err = issue.BlockingDependencies()
  899. if err != nil {
  900. ctx.ServerError("BlockingDependencies", err)
  901. return
  902. }
  903. ctx.Data["Participants"] = participants
  904. ctx.Data["NumParticipants"] = len(participants)
  905. ctx.Data["Issue"] = issue
  906. ctx.Data["ReadOnly"] = true
  907. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login?redirect_to=" + ctx.Data["Link"].(string)
  908. ctx.Data["IsIssuePoster"] = ctx.IsSigned && issue.IsPoster(ctx.User.ID)
  909. ctx.Data["IsIssueWriter"] = ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)
  910. ctx.Data["IsRepoAdmin"] = ctx.IsSigned && (ctx.Repo.IsAdmin() || ctx.User.IsAdmin)
  911. ctx.Data["IsRepoIssuesWriter"] = ctx.IsSigned && (ctx.Repo.CanWrite(models.UnitTypeIssues) || ctx.User.IsAdmin)
  912. ctx.Data["LockReasons"] = setting.Repository.Issue.LockReasons
  913. ctx.HTML(200, tplIssueView)
  914. }
  915. // GetActionIssue will return the issue which is used in the context.
  916. func GetActionIssue(ctx *context.Context) *models.Issue {
  917. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  918. if err != nil {
  919. ctx.NotFoundOrServerError("GetIssueByIndex", models.IsErrIssueNotExist, err)
  920. return nil
  921. }
  922. issue.Repo = ctx.Repo.Repository
  923. checkIssueRights(ctx, issue)
  924. if ctx.Written() {
  925. return nil
  926. }
  927. if err = issue.LoadAttributes(); err != nil {
  928. ctx.ServerError("LoadAttributes", nil)
  929. return nil
  930. }
  931. return issue
  932. }
  933. func checkIssueRights(ctx *context.Context, issue *models.Issue) {
  934. if issue.IsPull && !ctx.Repo.CanRead(models.UnitTypePullRequests) ||
  935. !issue.IsPull && !ctx.Repo.CanRead(models.UnitTypeIssues) {
  936. ctx.NotFound("IssueOrPullRequestUnitNotAllowed", nil)
  937. }
  938. }
  939. func getActionIssues(ctx *context.Context) []*models.Issue {
  940. commaSeparatedIssueIDs := ctx.Query("issue_ids")
  941. if len(commaSeparatedIssueIDs) == 0 {
  942. return nil
  943. }
  944. issueIDs := make([]int64, 0, 10)
  945. for _, stringIssueID := range strings.Split(commaSeparatedIssueIDs, ",") {
  946. issueID, err := strconv.ParseInt(stringIssueID, 10, 64)
  947. if err != nil {
  948. ctx.ServerError("ParseInt", err)
  949. return nil
  950. }
  951. issueIDs = append(issueIDs, issueID)
  952. }
  953. issues, err := models.GetIssuesByIDs(issueIDs)
  954. if err != nil {
  955. ctx.ServerError("GetIssuesByIDs", err)
  956. return nil
  957. }
  958. // Check access rights for all issues
  959. issueUnitEnabled := ctx.Repo.CanRead(models.UnitTypeIssues)
  960. prUnitEnabled := ctx.Repo.CanRead(models.UnitTypePullRequests)
  961. for _, issue := range issues {
  962. if issue.IsPull && !prUnitEnabled || !issue.IsPull && !issueUnitEnabled {
  963. ctx.NotFound("IssueOrPullRequestUnitNotAllowed", nil)
  964. return nil
  965. }
  966. if err = issue.LoadAttributes(); err != nil {
  967. ctx.ServerError("LoadAttributes", err)
  968. return nil
  969. }
  970. }
  971. return issues
  972. }
  973. // UpdateIssueTitle change issue's title
  974. func UpdateIssueTitle(ctx *context.Context) {
  975. issue := GetActionIssue(ctx)
  976. if ctx.Written() {
  977. return
  978. }
  979. if !ctx.IsSigned || (!issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)) {
  980. ctx.Error(403)
  981. return
  982. }
  983. title := ctx.QueryTrim("title")
  984. if len(title) == 0 {
  985. ctx.Error(204)
  986. return
  987. }
  988. if err := issue_service.ChangeTitle(issue, ctx.User, title); err != nil {
  989. ctx.ServerError("ChangeTitle", err)
  990. return
  991. }
  992. ctx.JSON(200, map[string]interface{}{
  993. "title": issue.Title,
  994. })
  995. }
  996. // UpdateIssueContent change issue's content
  997. func UpdateIssueContent(ctx *context.Context) {
  998. issue := GetActionIssue(ctx)
  999. if ctx.Written() {
  1000. return
  1001. }
  1002. if !ctx.IsSigned || (ctx.User.ID != issue.PosterID && !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)) {
  1003. ctx.Error(403)
  1004. return
  1005. }
  1006. content := ctx.Query("content")
  1007. if err := issue_service.ChangeContent(issue, ctx.User, content); err != nil {
  1008. ctx.ServerError("ChangeContent", err)
  1009. return
  1010. }
  1011. files := ctx.QueryStrings("files[]")
  1012. if err := updateAttachments(issue, files); err != nil {
  1013. ctx.ServerError("UpdateAttachments", err)
  1014. }
  1015. ctx.JSON(200, map[string]interface{}{
  1016. "content": string(markdown.Render([]byte(issue.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
  1017. "attachments": attachmentsHTML(ctx, issue.Attachments),
  1018. })
  1019. }
  1020. // UpdateIssueMilestone change issue's milestone
  1021. func UpdateIssueMilestone(ctx *context.Context) {
  1022. issues := getActionIssues(ctx)
  1023. if ctx.Written() {
  1024. return
  1025. }
  1026. milestoneID := ctx.QueryInt64("id")
  1027. for _, issue := range issues {
  1028. oldMilestoneID := issue.MilestoneID
  1029. if oldMilestoneID == milestoneID {
  1030. continue
  1031. }
  1032. issue.MilestoneID = milestoneID
  1033. if err := issue_service.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
  1034. ctx.ServerError("ChangeMilestoneAssign", err)
  1035. return
  1036. }
  1037. }
  1038. ctx.JSON(200, map[string]interface{}{
  1039. "ok": true,
  1040. })
  1041. }
  1042. // UpdateIssueAssignee change issue's or pull's assignee
  1043. func UpdateIssueAssignee(ctx *context.Context) {
  1044. issues := getActionIssues(ctx)
  1045. if ctx.Written() {
  1046. return
  1047. }
  1048. assigneeID := ctx.QueryInt64("id")
  1049. action := ctx.Query("action")
  1050. for _, issue := range issues {
  1051. switch action {
  1052. case "clear":
  1053. if err := issue_service.DeleteNotPassedAssignee(issue, ctx.User, []*models.User{}); err != nil {
  1054. ctx.ServerError("ClearAssignees", err)
  1055. return
  1056. }
  1057. default:
  1058. assignee, err := models.GetUserByID(assigneeID)
  1059. if err != nil {
  1060. ctx.ServerError("GetUserByID", err)
  1061. return
  1062. }
  1063. valid, err := models.CanBeAssigned(assignee, issue.Repo, issue.IsPull)
  1064. if err != nil {
  1065. ctx.ServerError("canBeAssigned", err)
  1066. return
  1067. }
  1068. if !valid {
  1069. ctx.ServerError("canBeAssigned", models.ErrUserDoesNotHaveAccessToRepo{UserID: assigneeID, RepoName: issue.Repo.Name})
  1070. return
  1071. }
  1072. _, _, err = issue_service.ToggleAssignee(issue, ctx.User, assigneeID)
  1073. if err != nil {
  1074. ctx.ServerError("ToggleAssignee", err)
  1075. return
  1076. }
  1077. }
  1078. }
  1079. ctx.JSON(200, map[string]interface{}{
  1080. "ok": true,
  1081. })
  1082. }
  1083. // UpdateIssueStatus change issue's status
  1084. func UpdateIssueStatus(ctx *context.Context) {
  1085. issues := getActionIssues(ctx)
  1086. if ctx.Written() {
  1087. return
  1088. }
  1089. var isClosed bool
  1090. switch action := ctx.Query("action"); action {
  1091. case "open":
  1092. isClosed = false
  1093. case "close":
  1094. isClosed = true
  1095. default:
  1096. log.Warn("Unrecognized action: %s", action)
  1097. }
  1098. if _, err := models.IssueList(issues).LoadRepositories(); err != nil {
  1099. ctx.ServerError("LoadRepositories", err)
  1100. return
  1101. }
  1102. for _, issue := range issues {
  1103. if issue.IsClosed != isClosed {
  1104. if err := issue_service.ChangeStatus(issue, ctx.User, isClosed); err != nil {
  1105. if models.IsErrDependenciesLeft(err) {
  1106. ctx.JSON(http.StatusPreconditionFailed, map[string]interface{}{
  1107. "error": "cannot close this issue because it still has open dependencies",
  1108. })
  1109. return
  1110. }
  1111. ctx.ServerError("ChangeStatus", err)
  1112. return
  1113. }
  1114. }
  1115. }
  1116. ctx.JSON(200, map[string]interface{}{
  1117. "ok": true,
  1118. })
  1119. }
  1120. // NewComment create a comment for issue
  1121. func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
  1122. issue := GetActionIssue(ctx)
  1123. if ctx.Written() {
  1124. return
  1125. }
  1126. if !ctx.IsSigned || (ctx.User.ID != issue.PosterID && !ctx.Repo.CanReadIssuesOrPulls(issue.IsPull)) {
  1127. if log.IsTrace() {
  1128. if ctx.IsSigned {
  1129. issueType := "issues"
  1130. if issue.IsPull {
  1131. issueType = "pulls"
  1132. }
  1133. log.Trace("Permission Denied: User %-v not the Poster (ID: %d) and cannot read %s in Repo %-v.\n"+
  1134. "User in Repo has Permissions: %-+v",
  1135. ctx.User,
  1136. log.NewColoredIDValue(issue.PosterID),
  1137. issueType,
  1138. ctx.Repo.Repository,
  1139. ctx.Repo.Permission)
  1140. } else {
  1141. log.Trace("Permission Denied: Not logged in")
  1142. }
  1143. }
  1144. ctx.Error(403)
  1145. }
  1146. if issue.IsLocked && !ctx.Repo.CanWrite(models.UnitTypeIssues) && !ctx.User.IsAdmin {
  1147. ctx.Flash.Error(ctx.Tr("repo.issues.comment_on_locked"))
  1148. ctx.Redirect(issue.HTMLURL(), http.StatusSeeOther)
  1149. return
  1150. }
  1151. var attachments []string
  1152. if setting.AttachmentEnabled {
  1153. attachments = form.Files
  1154. }
  1155. if ctx.HasError() {
  1156. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  1157. ctx.Redirect(issue.HTMLURL())
  1158. return
  1159. }
  1160. var comment *models.Comment
  1161. defer func() {
  1162. // Check if issue admin/poster changes the status of issue.
  1163. if (ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) || (ctx.IsSigned && issue.IsPoster(ctx.User.ID))) &&
  1164. (form.Status == "reopen" || form.Status == "close") &&
  1165. !(issue.IsPull && issue.PullRequest.HasMerged) {
  1166. // Duplication and conflict check should apply to reopen pull request.
  1167. var pr *models.PullRequest
  1168. if form.Status == "reopen" && issue.IsPull {
  1169. pull := issue.PullRequest
  1170. var err error
  1171. pr, err = models.GetUnmergedPullRequest(pull.HeadRepoID, pull.BaseRepoID, pull.HeadBranch, pull.BaseBranch)
  1172. if err != nil {
  1173. if !models.IsErrPullRequestNotExist(err) {
  1174. ctx.ServerError("GetUnmergedPullRequest", err)
  1175. return
  1176. }
  1177. }
  1178. // Regenerate patch and test conflict.
  1179. if pr == nil {
  1180. pull_service.AddToTaskQueue(issue.PullRequest)
  1181. }
  1182. }
  1183. if pr != nil {
  1184. ctx.Flash.Info(ctx.Tr("repo.pulls.open_unmerged_pull_exists", pr.Index))
  1185. } else {
  1186. isClosed := form.Status == "close"
  1187. if err := issue_service.ChangeStatus(issue, ctx.User, isClosed); err != nil {
  1188. log.Error("ChangeStatus: %v", err)
  1189. if models.IsErrDependenciesLeft(err) {
  1190. if issue.IsPull {
  1191. ctx.Flash.Error(ctx.Tr("repo.issues.dependency.pr_close_blocked"))
  1192. ctx.Redirect(fmt.Sprintf("%s/pulls/%d", ctx.Repo.RepoLink, issue.Index), http.StatusSeeOther)
  1193. } else {
  1194. ctx.Flash.Error(ctx.Tr("repo.issues.dependency.issue_close_blocked"))
  1195. ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, issue.Index), http.StatusSeeOther)
  1196. }
  1197. return
  1198. }
  1199. } else {
  1200. if err := stopTimerIfAvailable(ctx.User, issue); err != nil {
  1201. ctx.ServerError("CreateOrStopIssueStopwatch", err)
  1202. return
  1203. }
  1204. log.Trace("Issue [%d] status changed to closed: %v", issue.ID, issue.IsClosed)
  1205. }
  1206. }
  1207. }
  1208. // Redirect to comment hashtag if there is any actual content.
  1209. typeName := "issues"
  1210. if issue.IsPull {
  1211. typeName = "pulls"
  1212. }
  1213. if comment != nil {
  1214. ctx.Redirect(fmt.Sprintf("%s/%s/%d#%s", ctx.Repo.RepoLink, typeName, issue.Index, comment.HashTag()))
  1215. } else {
  1216. ctx.Redirect(fmt.Sprintf("%s/%s/%d", ctx.Repo.RepoLink, typeName, issue.Index))
  1217. }
  1218. }()
  1219. // Fix #321: Allow empty comments, as long as we have attachments.
  1220. if len(form.Content) == 0 && len(attachments) == 0 {
  1221. return
  1222. }
  1223. comment, err := comment_service.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments)
  1224. if err != nil {
  1225. ctx.ServerError("CreateIssueComment", err)
  1226. return
  1227. }
  1228. log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
  1229. }
  1230. // UpdateCommentContent change comment of issue's content
  1231. func UpdateCommentContent(ctx *context.Context) {
  1232. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  1233. if err != nil {
  1234. ctx.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
  1235. return
  1236. }
  1237. if err := comment.LoadIssue(); err != nil {
  1238. ctx.NotFoundOrServerError("LoadIssue", models.IsErrIssueNotExist, err)
  1239. return
  1240. }
  1241. if comment.Type == models.CommentTypeComment {
  1242. if err := comment.LoadAttachments(); err != nil {
  1243. ctx.ServerError("LoadAttachments", err)
  1244. return
  1245. }
  1246. }
  1247. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.CanWriteIssuesOrPulls(comment.Issue.IsPull)) {
  1248. ctx.Error(403)
  1249. return
  1250. } else if comment.Type != models.CommentTypeComment && comment.Type != models.CommentTypeCode {
  1251. ctx.Error(204)
  1252. return
  1253. }
  1254. oldContent := comment.Content
  1255. comment.Content = ctx.Query("content")
  1256. if len(comment.Content) == 0 {
  1257. ctx.JSON(200, map[string]interface{}{
  1258. "content": "",
  1259. })
  1260. return
  1261. }
  1262. if err = comment_service.UpdateComment(comment, ctx.User, oldContent); err != nil {
  1263. ctx.ServerError("UpdateComment", err)
  1264. return
  1265. }
  1266. files := ctx.QueryStrings("files[]")
  1267. if err := updateAttachments(comment, files); err != nil {
  1268. ctx.ServerError("UpdateAttachments", err)
  1269. }
  1270. ctx.JSON(200, map[string]interface{}{
  1271. "content": string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
  1272. "attachments": attachmentsHTML(ctx, comment.Attachments),
  1273. })
  1274. }
  1275. // DeleteComment delete comment of issue
  1276. func DeleteComment(ctx *context.Context) {
  1277. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  1278. if err != nil {
  1279. ctx.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
  1280. return
  1281. }
  1282. if err := comment.LoadIssue(); err != nil {
  1283. ctx.NotFoundOrServerError("LoadIssue", models.IsErrIssueNotExist, err)
  1284. return
  1285. }
  1286. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.CanWriteIssuesOrPulls(comment.Issue.IsPull)) {
  1287. ctx.Error(403)
  1288. return
  1289. } else if comment.Type != models.CommentTypeComment && comment.Type != models.CommentTypeCode {
  1290. ctx.Error(204)
  1291. return
  1292. }
  1293. if err = comment_service.DeleteComment(comment, ctx.User); err != nil {
  1294. ctx.ServerError("DeleteCommentByID", err)
  1295. return
  1296. }
  1297. ctx.Status(200)
  1298. }
  1299. // ChangeIssueReaction create a reaction for issue
  1300. func ChangeIssueReaction(ctx *context.Context, form auth.ReactionForm) {
  1301. issue := GetActionIssue(ctx)
  1302. if ctx.Written() {
  1303. return
  1304. }
  1305. if !ctx.IsSigned || (ctx.User.ID != issue.PosterID && !ctx.Repo.CanReadIssuesOrPulls(issue.IsPull)) {
  1306. if log.IsTrace() {
  1307. if ctx.IsSigned {
  1308. issueType := "issues"
  1309. if issue.IsPull {
  1310. issueType = "pulls"
  1311. }
  1312. log.Trace("Permission Denied: User %-v not the Poster (ID: %d) and cannot read %s in Repo %-v.\n"+
  1313. "User in Repo has Permissions: %-+v",
  1314. ctx.User,
  1315. log.NewColoredIDValue(issue.PosterID),
  1316. issueType,
  1317. ctx.Repo.Repository,
  1318. ctx.Repo.Permission)
  1319. } else {
  1320. log.Trace("Permission Denied: Not logged in")
  1321. }
  1322. }
  1323. ctx.Error(403)
  1324. return
  1325. }
  1326. if ctx.HasError() {
  1327. ctx.ServerError("ChangeIssueReaction", errors.New(ctx.GetErrMsg()))
  1328. return
  1329. }
  1330. switch ctx.Params(":action") {
  1331. case "react":
  1332. reaction, err := models.CreateIssueReaction(ctx.User, issue, form.Content)
  1333. if err != nil {
  1334. if models.IsErrForbiddenIssueReaction(err) {
  1335. ctx.ServerError("ChangeIssueReaction", err)
  1336. return
  1337. }
  1338. log.Info("CreateIssueReaction: %s", err)
  1339. break
  1340. }
  1341. // Reload new reactions
  1342. issue.Reactions = nil
  1343. if err = issue.LoadAttributes(); err != nil {
  1344. log.Info("issue.LoadAttributes: %s", err)
  1345. break
  1346. }
  1347. log.Trace("Reaction for issue created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, reaction.ID)
  1348. case "unreact":
  1349. if err := models.DeleteIssueReaction(ctx.User, issue, form.Content); err != nil {
  1350. ctx.ServerError("DeleteIssueReaction", err)
  1351. return
  1352. }
  1353. // Reload new reactions
  1354. issue.Reactions = nil
  1355. if err := issue.LoadAttributes(); err != nil {
  1356. log.Info("issue.LoadAttributes: %s", err)
  1357. break
  1358. }
  1359. log.Trace("Reaction for issue removed: %d/%d", ctx.Repo.Repository.ID, issue.ID)
  1360. default:
  1361. ctx.NotFound(fmt.Sprintf("Unknown action %s", ctx.Params(":action")), nil)
  1362. return
  1363. }
  1364. if len(issue.Reactions) == 0 {
  1365. ctx.JSON(200, map[string]interface{}{
  1366. "empty": true,
  1367. "html": "",
  1368. })
  1369. return
  1370. }
  1371. html, err := ctx.HTMLString(string(tplReactions), map[string]interface{}{
  1372. "ctx": ctx.Data,
  1373. "ActionURL": fmt.Sprintf("%s/issues/%d/reactions", ctx.Repo.RepoLink, issue.Index),
  1374. "Reactions": issue.Reactions.GroupByType(),
  1375. })
  1376. if err != nil {
  1377. ctx.ServerError("ChangeIssueReaction.HTMLString", err)
  1378. return
  1379. }
  1380. ctx.JSON(200, map[string]interface{}{
  1381. "html": html,
  1382. })
  1383. }
  1384. // ChangeCommentReaction create a reaction for comment
  1385. func ChangeCommentReaction(ctx *context.Context, form auth.ReactionForm) {
  1386. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  1387. if err != nil {
  1388. ctx.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
  1389. return
  1390. }
  1391. if err := comment.LoadIssue(); err != nil {
  1392. ctx.NotFoundOrServerError("LoadIssue", models.IsErrIssueNotExist, err)
  1393. return
  1394. }
  1395. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.CanReadIssuesOrPulls(comment.Issue.IsPull)) {
  1396. if log.IsTrace() {
  1397. if ctx.IsSigned {
  1398. issueType := "issues"
  1399. if comment.Issue.IsPull {
  1400. issueType = "pulls"
  1401. }
  1402. log.Trace("Permission Denied: User %-v not the Poster (ID: %d) and cannot read %s in Repo %-v.\n"+
  1403. "User in Repo has Permissions: %-+v",
  1404. ctx.User,
  1405. log.NewColoredIDValue(comment.Issue.PosterID),
  1406. issueType,
  1407. ctx.Repo.Repository,
  1408. ctx.Repo.Permission)
  1409. } else {
  1410. log.Trace("Permission Denied: Not logged in")
  1411. }
  1412. }
  1413. ctx.Error(403)
  1414. return
  1415. } else if comment.Type != models.CommentTypeComment && comment.Type != models.CommentTypeCode {
  1416. ctx.Error(204)
  1417. return
  1418. }
  1419. switch ctx.Params(":action") {
  1420. case "react":
  1421. reaction, err := models.CreateCommentReaction(ctx.User, comment.Issue, comment, form.Content)
  1422. if err != nil {
  1423. if models.IsErrForbiddenIssueReaction(err) {
  1424. ctx.ServerError("ChangeIssueReaction", err)
  1425. return
  1426. }
  1427. log.Info("CreateCommentReaction: %s", err)
  1428. break
  1429. }
  1430. // Reload new reactions
  1431. comment.Reactions = nil
  1432. if err = comment.LoadReactions(); err != nil {
  1433. log.Info("comment.LoadReactions: %s", err)
  1434. break
  1435. }
  1436. log.Trace("Reaction for comment created: %d/%d/%d/%d", ctx.Repo.Repository.ID, comment.Issue.ID, comment.ID, reaction.ID)
  1437. case "unreact":
  1438. if err := models.DeleteCommentReaction(ctx.User, comment.Issue, comment, form.Content); err != nil {
  1439. ctx.ServerError("DeleteCommentReaction", err)
  1440. return
  1441. }
  1442. // Reload new reactions
  1443. comment.Reactions = nil
  1444. if err = comment.LoadReactions(); err != nil {
  1445. log.Info("comment.LoadReactions: %s", err)
  1446. break
  1447. }
  1448. log.Trace("Reaction for comment removed: %d/%d/%d", ctx.Repo.Repository.ID, comment.Issue.ID, comment.ID)
  1449. default:
  1450. ctx.NotFound(fmt.Sprintf("Unknown action %s", ctx.Params(":action")), nil)
  1451. return
  1452. }
  1453. if len(comment.Reactions) == 0 {
  1454. ctx.JSON(200, map[string]interface{}{
  1455. "empty": true,
  1456. "html": "",
  1457. })
  1458. return
  1459. }
  1460. html, err := ctx.HTMLString(string(tplReactions), map[string]interface{}{
  1461. "ctx": ctx.Data,
  1462. "ActionURL": fmt.Sprintf("%s/comments/%d/reactions", ctx.Repo.RepoLink, comment.ID),
  1463. "Reactions": comment.Reactions.GroupByType(),
  1464. })
  1465. if err != nil {
  1466. ctx.ServerError("ChangeCommentReaction.HTMLString", err)
  1467. return
  1468. }
  1469. ctx.JSON(200, map[string]interface{}{
  1470. "html": html,
  1471. })
  1472. }
  1473. func addParticipant(poster *models.User, participants []*models.User) []*models.User {
  1474. for _, part := range participants {
  1475. if poster.ID == part.ID {
  1476. return participants
  1477. }
  1478. }
  1479. return append(participants, poster)
  1480. }
  1481. func filterXRefComments(ctx *context.Context, issue *models.Issue) error {
  1482. // Remove comments that the user has no permissions to see
  1483. for i := 0; i < len(issue.Comments); {
  1484. c := issue.Comments[i]
  1485. if models.CommentTypeIsRef(c.Type) && c.RefRepoID != issue.RepoID && c.RefRepoID != 0 {
  1486. var err error
  1487. // Set RefRepo for description in template
  1488. c.RefRepo, err = models.GetRepositoryByID(c.RefRepoID)
  1489. if err != nil {
  1490. return err
  1491. }
  1492. perm, err := models.GetUserRepoPermission(c.RefRepo, ctx.User)
  1493. if err != nil {
  1494. return err
  1495. }
  1496. if !perm.CanReadIssuesOrPulls(c.RefIsPull) {
  1497. issue.Comments = append(issue.Comments[:i], issue.Comments[i+1:]...)
  1498. continue
  1499. }
  1500. }
  1501. i++
  1502. }
  1503. return nil
  1504. }
  1505. // GetIssueAttachments returns attachments for the issue
  1506. func GetIssueAttachments(ctx *context.Context) {
  1507. issue := GetActionIssue(ctx)
  1508. var attachments = make([]*api.Attachment, len(issue.Attachments))
  1509. for i := 0; i < len(issue.Attachments); i++ {
  1510. attachments[i] = issue.Attachments[i].APIFormat()
  1511. }
  1512. ctx.JSON(200, attachments)
  1513. }
  1514. // GetCommentAttachments returns attachments for the comment
  1515. func GetCommentAttachments(ctx *context.Context) {
  1516. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  1517. if err != nil {
  1518. ctx.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
  1519. return
  1520. }
  1521. var attachments = make([]*api.Attachment, 0)
  1522. if comment.Type == models.CommentTypeComment {
  1523. if err := comment.LoadAttachments(); err != nil {
  1524. ctx.ServerError("LoadAttachments", err)
  1525. return
  1526. }
  1527. for i := 0; i < len(comment.Attachments); i++ {
  1528. attachments = append(attachments, comment.Attachments[i].APIFormat())
  1529. }
  1530. }
  1531. ctx.JSON(200, attachments)
  1532. }
  1533. func updateAttachments(item interface{}, files []string) error {
  1534. var attachments []*models.Attachment
  1535. switch content := item.(type) {
  1536. case *models.Issue:
  1537. attachments = content.Attachments
  1538. case *models.Comment:
  1539. attachments = content.Attachments
  1540. default:
  1541. return fmt.Errorf("Unknow Type")
  1542. }
  1543. for i := 0; i < len(attachments); i++ {
  1544. if util.IsStringInSlice(attachments[i].UUID, files) {
  1545. continue
  1546. }
  1547. if err := models.DeleteAttachment(attachments[i], true); err != nil {
  1548. return err
  1549. }
  1550. }
  1551. var err error
  1552. if len(files) > 0 {
  1553. switch content := item.(type) {
  1554. case *models.Issue:
  1555. err = content.UpdateAttachments(files)
  1556. case *models.Comment:
  1557. err = content.UpdateAttachments(files)
  1558. default:
  1559. return fmt.Errorf("Unknow Type")
  1560. }
  1561. if err != nil {
  1562. return err
  1563. }
  1564. }
  1565. switch content := item.(type) {
  1566. case *models.Issue:
  1567. content.Attachments, err = models.GetAttachmentsByIssueID(content.ID)
  1568. case *models.Comment:
  1569. content.Attachments, err = models.GetAttachmentsByCommentID(content.ID)
  1570. default:
  1571. return fmt.Errorf("Unknow Type")
  1572. }
  1573. return err
  1574. }
  1575. func attachmentsHTML(ctx *context.Context, attachments []*models.Attachment) string {
  1576. attachHTML, err := ctx.HTMLString(string(tplAttachment), map[string]interface{}{
  1577. "ctx": ctx.Data,
  1578. "Attachments": attachments,
  1579. })
  1580. if err != nil {
  1581. ctx.ServerError("attachmentsHTML.HTMLString", err)
  1582. return ""
  1583. }
  1584. return attachHTML
  1585. }