Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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