You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue.go 12 kB

Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // Copyright 2016 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. "fmt"
  8. "net/http"
  9. "strings"
  10. "time"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/indexer"
  14. "code.gitea.io/gitea/modules/notification"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/util"
  17. api "code.gitea.io/sdk/gitea"
  18. )
  19. // ListIssues list the issues of a repository
  20. func ListIssues(ctx *context.APIContext) {
  21. // swagger:operation GET /repos/{owner}/{repo}/issues issue issueListIssues
  22. // ---
  23. // summary: List a repository's issues
  24. // produces:
  25. // - application/json
  26. // parameters:
  27. // - name: owner
  28. // in: path
  29. // description: owner of the repo
  30. // type: string
  31. // required: true
  32. // - name: repo
  33. // in: path
  34. // description: name of the repo
  35. // type: string
  36. // required: true
  37. // - name: state
  38. // in: query
  39. // description: whether issue is open or closed
  40. // type: string
  41. // - name: page
  42. // in: query
  43. // description: page number of requested issues
  44. // type: integer
  45. // - name: q
  46. // in: query
  47. // description: search string
  48. // type: string
  49. // responses:
  50. // "200":
  51. // "$ref": "#/responses/IssueList"
  52. var isClosed util.OptionalBool
  53. switch ctx.Query("state") {
  54. case "closed":
  55. isClosed = util.OptionalBoolTrue
  56. case "all":
  57. isClosed = util.OptionalBoolNone
  58. default:
  59. isClosed = util.OptionalBoolFalse
  60. }
  61. var issues []*models.Issue
  62. keyword := strings.Trim(ctx.Query("q"), " ")
  63. if strings.IndexByte(keyword, 0) >= 0 {
  64. keyword = ""
  65. }
  66. var issueIDs []int64
  67. var err error
  68. if len(keyword) > 0 {
  69. issueIDs, err = indexer.SearchIssuesByKeyword(ctx.Repo.Repository.ID, keyword)
  70. }
  71. // Only fetch the issues if we either don't have a keyword or the search returned issues
  72. // This would otherwise return all issues if no issues were found by the search.
  73. if len(keyword) == 0 || len(issueIDs) > 0 {
  74. issues, err = models.Issues(&models.IssuesOptions{
  75. RepoIDs: []int64{ctx.Repo.Repository.ID},
  76. Page: ctx.QueryInt("page"),
  77. PageSize: setting.UI.IssuePagingNum,
  78. IsClosed: isClosed,
  79. IssueIDs: issueIDs,
  80. })
  81. }
  82. if err != nil {
  83. ctx.Error(500, "Issues", err)
  84. return
  85. }
  86. apiIssues := make([]*api.Issue, len(issues))
  87. for i := range issues {
  88. apiIssues[i] = issues[i].APIFormat()
  89. }
  90. ctx.SetLinkHeader(ctx.Repo.Repository.NumIssues, setting.UI.IssuePagingNum)
  91. ctx.JSON(200, &apiIssues)
  92. }
  93. // GetIssue get an issue of a repository
  94. func GetIssue(ctx *context.APIContext) {
  95. // swagger:operation GET /repos/{owner}/{repo}/issues/{index} issue issueGetIssue
  96. // ---
  97. // summary: Get an issue
  98. // produces:
  99. // - application/json
  100. // parameters:
  101. // - name: owner
  102. // in: path
  103. // description: owner of the repo
  104. // type: string
  105. // required: true
  106. // - name: repo
  107. // in: path
  108. // description: name of the repo
  109. // type: string
  110. // required: true
  111. // - name: index
  112. // in: path
  113. // description: index of the issue to get
  114. // type: integer
  115. // format: int64
  116. // required: true
  117. // responses:
  118. // "200":
  119. // "$ref": "#/responses/Issue"
  120. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  121. if err != nil {
  122. if models.IsErrIssueNotExist(err) {
  123. ctx.Status(404)
  124. } else {
  125. ctx.Error(500, "GetIssueByIndex", err)
  126. }
  127. return
  128. }
  129. ctx.JSON(200, issue.APIFormat())
  130. }
  131. // CreateIssue create an issue of a repository
  132. func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
  133. // swagger:operation POST /repos/{owner}/{repo}/issues issue issueCreateIssue
  134. // ---
  135. // summary: Create an issue. If using deadline only the date will be taken into account, and time of day ignored.
  136. // consumes:
  137. // - application/json
  138. // produces:
  139. // - application/json
  140. // parameters:
  141. // - name: owner
  142. // in: path
  143. // description: owner of the repo
  144. // type: string
  145. // required: true
  146. // - name: repo
  147. // in: path
  148. // description: name of the repo
  149. // type: string
  150. // required: true
  151. // - name: body
  152. // in: body
  153. // schema:
  154. // "$ref": "#/definitions/CreateIssueOption"
  155. // responses:
  156. // "201":
  157. // "$ref": "#/responses/Issue"
  158. var deadlineUnix util.TimeStamp
  159. if form.Deadline != nil && ctx.Repo.CanWrite(models.UnitTypeIssues) {
  160. deadlineUnix = util.TimeStamp(form.Deadline.Unix())
  161. }
  162. issue := &models.Issue{
  163. RepoID: ctx.Repo.Repository.ID,
  164. Repo: ctx.Repo.Repository,
  165. Title: form.Title,
  166. PosterID: ctx.User.ID,
  167. Poster: ctx.User,
  168. Content: form.Body,
  169. DeadlineUnix: deadlineUnix,
  170. }
  171. var assigneeIDs = make([]int64, 0)
  172. var err error
  173. if ctx.Repo.CanWrite(models.UnitTypeIssues) {
  174. issue.MilestoneID = form.Milestone
  175. assigneeIDs, err = models.MakeIDsFromAPIAssigneesToAdd(form.Assignee, form.Assignees)
  176. if err != nil {
  177. if models.IsErrUserNotExist(err) {
  178. ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
  179. } else {
  180. ctx.Error(500, "AddAssigneeByName", err)
  181. }
  182. return
  183. }
  184. } else {
  185. // setting labels is not allowed if user is not a writer
  186. form.Labels = make([]int64, 0)
  187. }
  188. if err := models.NewIssue(ctx.Repo.Repository, issue, form.Labels, assigneeIDs, nil); err != nil {
  189. if models.IsErrUserDoesNotHaveAccessToRepo(err) {
  190. ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
  191. return
  192. }
  193. ctx.Error(500, "NewIssue", err)
  194. return
  195. }
  196. notification.NotifyNewIssue(issue)
  197. if form.Closed {
  198. if err := issue.ChangeStatus(ctx.User, true); err != nil {
  199. if models.IsErrDependenciesLeft(err) {
  200. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
  201. return
  202. }
  203. ctx.Error(500, "ChangeStatus", err)
  204. return
  205. }
  206. }
  207. // Refetch from database to assign some automatic values
  208. issue, err = models.GetIssueByID(issue.ID)
  209. if err != nil {
  210. ctx.Error(500, "GetIssueByID", err)
  211. return
  212. }
  213. ctx.JSON(201, issue.APIFormat())
  214. }
  215. // EditIssue modify an issue of a repository
  216. func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
  217. // swagger:operation PATCH /repos/{owner}/{repo}/issues/{index} issue issueEditIssue
  218. // ---
  219. // summary: Edit an issue. If using deadline only the date will be taken into account, and time of day ignored.
  220. // consumes:
  221. // - application/json
  222. // produces:
  223. // - application/json
  224. // parameters:
  225. // - name: owner
  226. // in: path
  227. // description: owner of the repo
  228. // type: string
  229. // required: true
  230. // - name: repo
  231. // in: path
  232. // description: name of the repo
  233. // type: string
  234. // required: true
  235. // - name: index
  236. // in: path
  237. // description: index of the issue to edit
  238. // type: integer
  239. // format: int64
  240. // required: true
  241. // - name: body
  242. // in: body
  243. // schema:
  244. // "$ref": "#/definitions/EditIssueOption"
  245. // responses:
  246. // "201":
  247. // "$ref": "#/responses/Issue"
  248. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  249. if err != nil {
  250. if models.IsErrIssueNotExist(err) {
  251. ctx.Status(404)
  252. } else {
  253. ctx.Error(500, "GetIssueByIndex", err)
  254. }
  255. return
  256. }
  257. issue.Repo = ctx.Repo.Repository
  258. if !issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  259. ctx.Status(403)
  260. return
  261. }
  262. if len(form.Title) > 0 {
  263. issue.Title = form.Title
  264. }
  265. if form.Body != nil {
  266. issue.Content = *form.Body
  267. }
  268. // Update the deadline
  269. var deadlineUnix util.TimeStamp
  270. if form.Deadline != nil && !form.Deadline.IsZero() && ctx.Repo.CanWrite(models.UnitTypeIssues) {
  271. deadlineUnix = util.TimeStamp(form.Deadline.Unix())
  272. }
  273. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  274. ctx.Error(500, "UpdateIssueDeadline", err)
  275. return
  276. }
  277. // Add/delete assignees
  278. // Deleting is done the Github way (quote from their api documentation):
  279. // https://developer.github.com/v3/issues/#edit-an-issue
  280. // "assignees" (array): Logins for Users to assign to this issue.
  281. // Pass one or more user logins to replace the set of assignees on this Issue.
  282. // Send an empty array ([]) to clear all assignees from the Issue.
  283. if ctx.Repo.CanWrite(models.UnitTypeIssues) && (form.Assignees != nil || form.Assignee != nil) {
  284. oneAssignee := ""
  285. if form.Assignee != nil {
  286. oneAssignee = *form.Assignee
  287. }
  288. err = models.UpdateAPIAssignee(issue, oneAssignee, form.Assignees, ctx.User)
  289. if err != nil {
  290. ctx.Error(500, "UpdateAPIAssignee", err)
  291. return
  292. }
  293. }
  294. if ctx.Repo.CanWrite(models.UnitTypeIssues) && form.Milestone != nil &&
  295. issue.MilestoneID != *form.Milestone {
  296. oldMilestoneID := issue.MilestoneID
  297. issue.MilestoneID = *form.Milestone
  298. if err = models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
  299. ctx.Error(500, "ChangeMilestoneAssign", err)
  300. return
  301. }
  302. }
  303. if err = models.UpdateIssue(issue); err != nil {
  304. ctx.Error(500, "UpdateIssue", err)
  305. return
  306. }
  307. if form.State != nil {
  308. if err = issue.ChangeStatus(ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
  309. if models.IsErrDependenciesLeft(err) {
  310. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
  311. return
  312. }
  313. ctx.Error(500, "ChangeStatus", err)
  314. return
  315. }
  316. notification.NotifyIssueChangeStatus(ctx.User, issue, api.StateClosed == api.StateType(*form.State))
  317. }
  318. // Refetch from database to assign some automatic values
  319. issue, err = models.GetIssueByID(issue.ID)
  320. if err != nil {
  321. ctx.Error(500, "GetIssueByID", err)
  322. return
  323. }
  324. ctx.JSON(201, issue.APIFormat())
  325. }
  326. // UpdateIssueDeadline updates an issue deadline
  327. func UpdateIssueDeadline(ctx *context.APIContext, form api.EditDeadlineOption) {
  328. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/deadline issue issueEditIssueDeadline
  329. // ---
  330. // summary: Set an issue deadline. If set to null, the deadline is deleted. If using deadline only the date will be taken into account, and time of day ignored.
  331. // consumes:
  332. // - application/json
  333. // produces:
  334. // - application/json
  335. // parameters:
  336. // - name: owner
  337. // in: path
  338. // description: owner of the repo
  339. // type: string
  340. // required: true
  341. // - name: repo
  342. // in: path
  343. // description: name of the repo
  344. // type: string
  345. // required: true
  346. // - name: index
  347. // in: path
  348. // description: index of the issue to create or update a deadline on
  349. // type: integer
  350. // format: int64
  351. // required: true
  352. // - name: body
  353. // in: body
  354. // schema:
  355. // "$ref": "#/definitions/EditDeadlineOption"
  356. // responses:
  357. // "201":
  358. // "$ref": "#/responses/IssueDeadline"
  359. // "403":
  360. // description: Not repo writer
  361. // "404":
  362. // description: Issue not found
  363. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  364. if err != nil {
  365. if models.IsErrIssueNotExist(err) {
  366. ctx.Status(404)
  367. } else {
  368. ctx.Error(500, "GetIssueByIndex", err)
  369. }
  370. return
  371. }
  372. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  373. ctx.Status(403)
  374. return
  375. }
  376. var deadlineUnix util.TimeStamp
  377. var deadline time.Time
  378. if form.Deadline != nil && !form.Deadline.IsZero() {
  379. deadline = time.Date(form.Deadline.Year(), form.Deadline.Month(), form.Deadline.Day(),
  380. 23, 59, 59, 0, form.Deadline.Location())
  381. deadlineUnix = util.TimeStamp(deadline.Unix())
  382. }
  383. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  384. ctx.Error(500, "UpdateIssueDeadline", err)
  385. return
  386. }
  387. ctx.JSON(201, api.IssueDeadline{Deadline: &deadline})
  388. }