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 16 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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/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: labels
  41. // in: query
  42. // description: comma separated list of labels. Fetch only issues that have any of this labels. Non existent labels are discarded
  43. // type: string
  44. // - name: page
  45. // in: query
  46. // description: page number of requested issues
  47. // type: integer
  48. // - name: q
  49. // in: query
  50. // description: search string
  51. // type: string
  52. // responses:
  53. // "200":
  54. // "$ref": "#/responses/IssueList"
  55. var isClosed util.OptionalBool
  56. switch ctx.Query("state") {
  57. case "closed":
  58. isClosed = util.OptionalBoolTrue
  59. case "all":
  60. isClosed = util.OptionalBoolNone
  61. default:
  62. isClosed = util.OptionalBoolFalse
  63. }
  64. var issues []*models.Issue
  65. keyword := strings.Trim(ctx.Query("q"), " ")
  66. if strings.IndexByte(keyword, 0) >= 0 {
  67. keyword = ""
  68. }
  69. var issueIDs []int64
  70. var labelIDs []int64
  71. var err error
  72. if len(keyword) > 0 {
  73. issueIDs, err = models.SearchIssuesByKeyword(ctx.Repo.Repository.ID, keyword)
  74. }
  75. if splitted := strings.Split(ctx.Query("labels"), ","); len(splitted) > 0 {
  76. labelIDs, err = models.GetLabelIDsInRepoByNames(ctx.Repo.Repository.ID, splitted)
  77. if err != nil {
  78. ctx.Error(500, "GetLabelIDsInRepoByNames", err)
  79. return
  80. }
  81. }
  82. // Only fetch the issues if we either don't have a keyword or the search returned issues
  83. // This would otherwise return all issues if no issues were found by the search.
  84. if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
  85. issues, err = models.Issues(&models.IssuesOptions{
  86. RepoIDs: []int64{ctx.Repo.Repository.ID},
  87. Page: ctx.QueryInt("page"),
  88. PageSize: setting.UI.IssuePagingNum,
  89. IsClosed: isClosed,
  90. IssueIDs: issueIDs,
  91. LabelIDs: labelIDs,
  92. })
  93. }
  94. if err != nil {
  95. ctx.Error(500, "Issues", err)
  96. return
  97. }
  98. apiIssues := make([]*api.Issue, len(issues))
  99. for i := range issues {
  100. apiIssues[i] = issues[i].APIFormat()
  101. }
  102. ctx.SetLinkHeader(ctx.Repo.Repository.NumIssues, setting.UI.IssuePagingNum)
  103. ctx.JSON(200, &apiIssues)
  104. }
  105. // GetIssue get an issue of a repository
  106. func GetIssue(ctx *context.APIContext) {
  107. // swagger:operation GET /repos/{owner}/{repo}/issues/{index} issue issueGetIssue
  108. // ---
  109. // summary: Get an issue
  110. // produces:
  111. // - application/json
  112. // parameters:
  113. // - name: owner
  114. // in: path
  115. // description: owner of the repo
  116. // type: string
  117. // required: true
  118. // - name: repo
  119. // in: path
  120. // description: name of the repo
  121. // type: string
  122. // required: true
  123. // - name: index
  124. // in: path
  125. // description: index of the issue to get
  126. // type: integer
  127. // format: int64
  128. // required: true
  129. // responses:
  130. // "200":
  131. // "$ref": "#/responses/Issue"
  132. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  133. if err != nil {
  134. if models.IsErrIssueNotExist(err) {
  135. ctx.Status(404)
  136. } else {
  137. ctx.Error(500, "GetIssueByIndex", err)
  138. }
  139. return
  140. }
  141. ctx.JSON(200, issue.APIFormat())
  142. }
  143. // CreateIssue create an issue of a repository
  144. func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
  145. // swagger:operation POST /repos/{owner}/{repo}/issues issue issueCreateIssue
  146. // ---
  147. // summary: Create an issue. If using deadline only the date will be taken into account, and time of day ignored.
  148. // consumes:
  149. // - application/json
  150. // produces:
  151. // - application/json
  152. // parameters:
  153. // - name: owner
  154. // in: path
  155. // description: owner of the repo
  156. // type: string
  157. // required: true
  158. // - name: repo
  159. // in: path
  160. // description: name of the repo
  161. // type: string
  162. // required: true
  163. // - name: body
  164. // in: body
  165. // schema:
  166. // "$ref": "#/definitions/CreateIssueOption"
  167. // responses:
  168. // "201":
  169. // "$ref": "#/responses/Issue"
  170. var deadlineUnix util.TimeStamp
  171. if form.Deadline != nil && ctx.Repo.CanWrite(models.UnitTypeIssues) {
  172. deadlineUnix = util.TimeStamp(form.Deadline.Unix())
  173. }
  174. issue := &models.Issue{
  175. RepoID: ctx.Repo.Repository.ID,
  176. Repo: ctx.Repo.Repository,
  177. Title: form.Title,
  178. PosterID: ctx.User.ID,
  179. Poster: ctx.User,
  180. Content: form.Body,
  181. DeadlineUnix: deadlineUnix,
  182. }
  183. var assigneeIDs = make([]int64, 0)
  184. var err error
  185. if ctx.Repo.CanWrite(models.UnitTypeIssues) {
  186. issue.MilestoneID = form.Milestone
  187. assigneeIDs, err = models.MakeIDsFromAPIAssigneesToAdd(form.Assignee, form.Assignees)
  188. if err != nil {
  189. if models.IsErrUserNotExist(err) {
  190. ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
  191. } else {
  192. ctx.Error(500, "AddAssigneeByName", err)
  193. }
  194. return
  195. }
  196. } else {
  197. // setting labels is not allowed if user is not a writer
  198. form.Labels = make([]int64, 0)
  199. }
  200. if err := models.NewIssue(ctx.Repo.Repository, issue, form.Labels, assigneeIDs, nil); err != nil {
  201. if models.IsErrUserDoesNotHaveAccessToRepo(err) {
  202. ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
  203. return
  204. }
  205. ctx.Error(500, "NewIssue", err)
  206. return
  207. }
  208. notification.NotifyNewIssue(issue)
  209. if form.Closed {
  210. if err := issue.ChangeStatus(ctx.User, true); err != nil {
  211. if models.IsErrDependenciesLeft(err) {
  212. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
  213. return
  214. }
  215. ctx.Error(500, "ChangeStatus", err)
  216. return
  217. }
  218. }
  219. // Refetch from database to assign some automatic values
  220. issue, err = models.GetIssueByID(issue.ID)
  221. if err != nil {
  222. ctx.Error(500, "GetIssueByID", err)
  223. return
  224. }
  225. ctx.JSON(201, issue.APIFormat())
  226. }
  227. // EditIssue modify an issue of a repository
  228. func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
  229. // swagger:operation PATCH /repos/{owner}/{repo}/issues/{index} issue issueEditIssue
  230. // ---
  231. // summary: Edit an issue. If using deadline only the date will be taken into account, and time of day ignored.
  232. // consumes:
  233. // - application/json
  234. // produces:
  235. // - application/json
  236. // parameters:
  237. // - name: owner
  238. // in: path
  239. // description: owner of the repo
  240. // type: string
  241. // required: true
  242. // - name: repo
  243. // in: path
  244. // description: name of the repo
  245. // type: string
  246. // required: true
  247. // - name: index
  248. // in: path
  249. // description: index of the issue to edit
  250. // type: integer
  251. // format: int64
  252. // required: true
  253. // - name: body
  254. // in: body
  255. // schema:
  256. // "$ref": "#/definitions/EditIssueOption"
  257. // responses:
  258. // "201":
  259. // "$ref": "#/responses/Issue"
  260. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  261. if err != nil {
  262. if models.IsErrIssueNotExist(err) {
  263. ctx.Status(404)
  264. } else {
  265. ctx.Error(500, "GetIssueByIndex", err)
  266. }
  267. return
  268. }
  269. issue.Repo = ctx.Repo.Repository
  270. if !issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  271. ctx.Status(403)
  272. return
  273. }
  274. if len(form.Title) > 0 {
  275. issue.Title = form.Title
  276. }
  277. if form.Body != nil {
  278. issue.Content = *form.Body
  279. }
  280. // Update the deadline
  281. var deadlineUnix util.TimeStamp
  282. if form.Deadline != nil && !form.Deadline.IsZero() && ctx.Repo.CanWrite(models.UnitTypeIssues) {
  283. deadlineUnix = util.TimeStamp(form.Deadline.Unix())
  284. }
  285. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  286. ctx.Error(500, "UpdateIssueDeadline", err)
  287. return
  288. }
  289. // Add/delete assignees
  290. // Deleting is done the Github way (quote from their api documentation):
  291. // https://developer.github.com/v3/issues/#edit-an-issue
  292. // "assignees" (array): Logins for Users to assign to this issue.
  293. // Pass one or more user logins to replace the set of assignees on this Issue.
  294. // Send an empty array ([]) to clear all assignees from the Issue.
  295. if ctx.Repo.CanWrite(models.UnitTypeIssues) && (form.Assignees != nil || form.Assignee != nil) {
  296. oneAssignee := ""
  297. if form.Assignee != nil {
  298. oneAssignee = *form.Assignee
  299. }
  300. err = models.UpdateAPIAssignee(issue, oneAssignee, form.Assignees, ctx.User)
  301. if err != nil {
  302. ctx.Error(500, "UpdateAPIAssignee", err)
  303. return
  304. }
  305. }
  306. if ctx.Repo.CanWrite(models.UnitTypeIssues) && form.Milestone != nil &&
  307. issue.MilestoneID != *form.Milestone {
  308. oldMilestoneID := issue.MilestoneID
  309. issue.MilestoneID = *form.Milestone
  310. if err = models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
  311. ctx.Error(500, "ChangeMilestoneAssign", err)
  312. return
  313. }
  314. }
  315. if err = models.UpdateIssue(issue); err != nil {
  316. ctx.Error(500, "UpdateIssue", err)
  317. return
  318. }
  319. if form.State != nil {
  320. if err = issue.ChangeStatus(ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
  321. if models.IsErrDependenciesLeft(err) {
  322. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
  323. return
  324. }
  325. ctx.Error(500, "ChangeStatus", err)
  326. return
  327. }
  328. notification.NotifyIssueChangeStatus(ctx.User, issue, api.StateClosed == api.StateType(*form.State))
  329. }
  330. // Refetch from database to assign some automatic values
  331. issue, err = models.GetIssueByID(issue.ID)
  332. if err != nil {
  333. ctx.Error(500, "GetIssueByID", err)
  334. return
  335. }
  336. ctx.JSON(201, issue.APIFormat())
  337. }
  338. // UpdateIssueDeadline updates an issue deadline
  339. func UpdateIssueDeadline(ctx *context.APIContext, form api.EditDeadlineOption) {
  340. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/deadline issue issueEditIssueDeadline
  341. // ---
  342. // 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.
  343. // consumes:
  344. // - application/json
  345. // produces:
  346. // - application/json
  347. // parameters:
  348. // - name: owner
  349. // in: path
  350. // description: owner of the repo
  351. // type: string
  352. // required: true
  353. // - name: repo
  354. // in: path
  355. // description: name of the repo
  356. // type: string
  357. // required: true
  358. // - name: index
  359. // in: path
  360. // description: index of the issue to create or update a deadline on
  361. // type: integer
  362. // format: int64
  363. // required: true
  364. // - name: body
  365. // in: body
  366. // schema:
  367. // "$ref": "#/definitions/EditDeadlineOption"
  368. // responses:
  369. // "201":
  370. // "$ref": "#/responses/IssueDeadline"
  371. // "403":
  372. // description: Not repo writer
  373. // "404":
  374. // description: Issue not found
  375. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  376. if err != nil {
  377. if models.IsErrIssueNotExist(err) {
  378. ctx.Status(404)
  379. } else {
  380. ctx.Error(500, "GetIssueByIndex", err)
  381. }
  382. return
  383. }
  384. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  385. ctx.Status(403)
  386. return
  387. }
  388. var deadlineUnix util.TimeStamp
  389. var deadline time.Time
  390. if form.Deadline != nil && !form.Deadline.IsZero() {
  391. deadline = time.Date(form.Deadline.Year(), form.Deadline.Month(), form.Deadline.Day(),
  392. 23, 59, 59, 0, form.Deadline.Location())
  393. deadlineUnix = util.TimeStamp(deadline.Unix())
  394. }
  395. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  396. ctx.Error(500, "UpdateIssueDeadline", err)
  397. return
  398. }
  399. ctx.JSON(201, api.IssueDeadline{Deadline: &deadline})
  400. }
  401. // StartIssueStopwatch creates a stopwatch for the given issue.
  402. func StartIssueStopwatch(ctx *context.APIContext) {
  403. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/start issue issueStartStopWatch
  404. // ---
  405. // summary: Start stopwatch on an issue.
  406. // consumes:
  407. // - application/json
  408. // produces:
  409. // - application/json
  410. // parameters:
  411. // - name: owner
  412. // in: path
  413. // description: owner of the repo
  414. // type: string
  415. // required: true
  416. // - name: repo
  417. // in: path
  418. // description: name of the repo
  419. // type: string
  420. // required: true
  421. // - name: index
  422. // in: path
  423. // description: index of the issue to create the stopwatch on
  424. // type: integer
  425. // format: int64
  426. // required: true
  427. // responses:
  428. // "201":
  429. // "$ref": "#/responses/empty"
  430. // "403":
  431. // description: Not repo writer, user does not have rights to toggle stopwatch
  432. // "404":
  433. // description: Issue not found
  434. // "409":
  435. // description: Cannot start a stopwatch again if it already exists
  436. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  437. if err != nil {
  438. if models.IsErrIssueNotExist(err) {
  439. ctx.Status(404)
  440. } else {
  441. ctx.Error(500, "GetIssueByIndex", err)
  442. }
  443. return
  444. }
  445. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  446. ctx.Status(403)
  447. return
  448. }
  449. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  450. ctx.Status(403)
  451. return
  452. }
  453. if models.StopwatchExists(ctx.User.ID, issue.ID) {
  454. ctx.Error(409, "StopwatchExists", "a stopwatch has already been started for this issue")
  455. return
  456. }
  457. if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
  458. ctx.Error(500, "CreateOrStopIssueStopwatch", err)
  459. return
  460. }
  461. ctx.Status(201)
  462. }
  463. // StopIssueStopwatch stops a stopwatch for the given issue.
  464. func StopIssueStopwatch(ctx *context.APIContext) {
  465. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/stop issue issueStopWatch
  466. // ---
  467. // summary: Stop an issue's existing stopwatch.
  468. // consumes:
  469. // - application/json
  470. // produces:
  471. // - application/json
  472. // parameters:
  473. // - name: owner
  474. // in: path
  475. // description: owner of the repo
  476. // type: string
  477. // required: true
  478. // - name: repo
  479. // in: path
  480. // description: name of the repo
  481. // type: string
  482. // required: true
  483. // - name: index
  484. // in: path
  485. // description: index of the issue to stop the stopwatch on
  486. // type: integer
  487. // format: int64
  488. // required: true
  489. // responses:
  490. // "201":
  491. // "$ref": "#/responses/empty"
  492. // "403":
  493. // description: Not repo writer, user does not have rights to toggle stopwatch
  494. // "404":
  495. // description: Issue not found
  496. // "409":
  497. // description: Cannot stop a non existent stopwatch
  498. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  499. if err != nil {
  500. if models.IsErrIssueNotExist(err) {
  501. ctx.Status(404)
  502. } else {
  503. ctx.Error(500, "GetIssueByIndex", err)
  504. }
  505. return
  506. }
  507. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  508. ctx.Status(403)
  509. return
  510. }
  511. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  512. ctx.Status(403)
  513. return
  514. }
  515. if !models.StopwatchExists(ctx.User.ID, issue.ID) {
  516. ctx.Error(409, "StopwatchExists", "cannot stop a non existent stopwatch")
  517. return
  518. }
  519. if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
  520. ctx.Error(500, "CreateOrStopIssueStopwatch", err)
  521. return
  522. }
  523. ctx.Status(201)
  524. }