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