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