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