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

issue_tracked_time.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "fmt"
  7. "net/http"
  8. "time"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/convert"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/web"
  14. "code.gitea.io/gitea/routers/api/v1/utils"
  15. )
  16. // ListTrackedTimes list all the tracked times of an issue
  17. func ListTrackedTimes(ctx *context.APIContext) {
  18. // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/times issue issueTrackedTimes
  19. // ---
  20. // summary: List an issue's tracked times
  21. // produces:
  22. // - application/json
  23. // parameters:
  24. // - name: owner
  25. // in: path
  26. // description: owner of the repo
  27. // type: string
  28. // required: true
  29. // - name: repo
  30. // in: path
  31. // description: name of the repo
  32. // type: string
  33. // required: true
  34. // - name: index
  35. // in: path
  36. // description: index of the issue
  37. // type: integer
  38. // format: int64
  39. // required: true
  40. // - name: user
  41. // in: query
  42. // description: optional filter by user (available for issue managers)
  43. // type: string
  44. // - name: since
  45. // in: query
  46. // description: Only show times updated after the given time. This is a timestamp in RFC 3339 format
  47. // type: string
  48. // format: date-time
  49. // - name: before
  50. // in: query
  51. // description: Only show times updated before the given time. This is a timestamp in RFC 3339 format
  52. // type: string
  53. // format: date-time
  54. // - name: page
  55. // in: query
  56. // description: page number of results to return (1-based)
  57. // type: integer
  58. // - name: limit
  59. // in: query
  60. // description: page size of results
  61. // type: integer
  62. // responses:
  63. // "200":
  64. // "$ref": "#/responses/TrackedTimeList"
  65. // "404":
  66. // "$ref": "#/responses/notFound"
  67. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  68. ctx.NotFound("Timetracker is disabled")
  69. return
  70. }
  71. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  72. if err != nil {
  73. if models.IsErrIssueNotExist(err) {
  74. ctx.NotFound(err)
  75. } else {
  76. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  77. }
  78. return
  79. }
  80. opts := &models.FindTrackedTimesOptions{
  81. ListOptions: utils.GetListOptions(ctx),
  82. RepositoryID: ctx.Repo.Repository.ID,
  83. IssueID: issue.ID,
  84. }
  85. qUser := ctx.FormTrim("user")
  86. if qUser != "" {
  87. user, err := models.GetUserByName(qUser)
  88. if models.IsErrUserNotExist(err) {
  89. ctx.Error(http.StatusNotFound, "User does not exist", err)
  90. } else if err != nil {
  91. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  92. return
  93. }
  94. opts.UserID = user.ID
  95. }
  96. if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil {
  97. ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
  98. return
  99. }
  100. cantSetUser := !ctx.User.IsAdmin &&
  101. opts.UserID != ctx.User.ID &&
  102. !ctx.IsUserRepoWriter([]models.UnitType{models.UnitTypeIssues})
  103. if cantSetUser {
  104. if opts.UserID == 0 {
  105. opts.UserID = ctx.User.ID
  106. } else {
  107. ctx.Error(http.StatusForbidden, "", fmt.Errorf("query by user not allowed; not enough rights"))
  108. return
  109. }
  110. }
  111. count, err := models.CountTrackedTimes(opts)
  112. if err != nil {
  113. ctx.InternalServerError(err)
  114. return
  115. }
  116. trackedTimes, err := models.GetTrackedTimes(opts)
  117. if err != nil {
  118. ctx.Error(http.StatusInternalServerError, "GetTrackedTimes", err)
  119. return
  120. }
  121. if err = trackedTimes.LoadAttributes(); err != nil {
  122. ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
  123. return
  124. }
  125. ctx.SetTotalCountHeader(count)
  126. ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes))
  127. }
  128. // AddTime add time manual to the given issue
  129. func AddTime(ctx *context.APIContext) {
  130. // swagger:operation Post /repos/{owner}/{repo}/issues/{index}/times issue issueAddTime
  131. // ---
  132. // summary: Add tracked time to a 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: index
  149. // in: path
  150. // description: index of the issue
  151. // type: integer
  152. // format: int64
  153. // required: true
  154. // - name: body
  155. // in: body
  156. // schema:
  157. // "$ref": "#/definitions/AddTimeOption"
  158. // responses:
  159. // "200":
  160. // "$ref": "#/responses/TrackedTime"
  161. // "400":
  162. // "$ref": "#/responses/error"
  163. // "403":
  164. // "$ref": "#/responses/forbidden"
  165. form := web.GetForm(ctx).(*api.AddTimeOption)
  166. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  167. if err != nil {
  168. if models.IsErrIssueNotExist(err) {
  169. ctx.NotFound(err)
  170. } else {
  171. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  172. }
  173. return
  174. }
  175. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  176. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  177. ctx.Error(http.StatusBadRequest, "", "time tracking disabled")
  178. return
  179. }
  180. ctx.Status(http.StatusForbidden)
  181. return
  182. }
  183. user := ctx.User
  184. if form.User != "" {
  185. if (ctx.IsUserRepoAdmin() && ctx.User.Name != form.User) || ctx.User.IsAdmin {
  186. //allow only RepoAdmin, Admin and User to add time
  187. user, err = models.GetUserByName(form.User)
  188. if err != nil {
  189. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  190. }
  191. }
  192. }
  193. created := time.Time{}
  194. if !form.Created.IsZero() {
  195. created = form.Created
  196. }
  197. trackedTime, err := models.AddTime(user, issue, form.Time, created)
  198. if err != nil {
  199. ctx.Error(http.StatusInternalServerError, "AddTime", err)
  200. return
  201. }
  202. if err = trackedTime.LoadAttributes(); err != nil {
  203. ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
  204. return
  205. }
  206. ctx.JSON(http.StatusOK, convert.ToTrackedTime(trackedTime))
  207. }
  208. // ResetIssueTime reset time manual to the given issue
  209. func ResetIssueTime(ctx *context.APIContext) {
  210. // swagger:operation Delete /repos/{owner}/{repo}/issues/{index}/times issue issueResetTime
  211. // ---
  212. // summary: Reset a tracked time of an issue
  213. // consumes:
  214. // - application/json
  215. // produces:
  216. // - application/json
  217. // parameters:
  218. // - name: owner
  219. // in: path
  220. // description: owner of the repo
  221. // type: string
  222. // required: true
  223. // - name: repo
  224. // in: path
  225. // description: name of the repo
  226. // type: string
  227. // required: true
  228. // - name: index
  229. // in: path
  230. // description: index of the issue to add tracked time to
  231. // type: integer
  232. // format: int64
  233. // required: true
  234. // responses:
  235. // "204":
  236. // "$ref": "#/responses/empty"
  237. // "400":
  238. // "$ref": "#/responses/error"
  239. // "403":
  240. // "$ref": "#/responses/forbidden"
  241. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  242. if err != nil {
  243. if models.IsErrIssueNotExist(err) {
  244. ctx.NotFound(err)
  245. } else {
  246. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  247. }
  248. return
  249. }
  250. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  251. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  252. ctx.JSON(http.StatusBadRequest, struct{ Message string }{Message: "time tracking disabled"})
  253. return
  254. }
  255. ctx.Status(http.StatusForbidden)
  256. return
  257. }
  258. err = models.DeleteIssueUserTimes(issue, ctx.User)
  259. if err != nil {
  260. if models.IsErrNotExist(err) {
  261. ctx.Error(http.StatusNotFound, "DeleteIssueUserTimes", err)
  262. } else {
  263. ctx.Error(http.StatusInternalServerError, "DeleteIssueUserTimes", err)
  264. }
  265. return
  266. }
  267. ctx.Status(204)
  268. }
  269. // DeleteTime delete a specific time by id
  270. func DeleteTime(ctx *context.APIContext) {
  271. // swagger:operation Delete /repos/{owner}/{repo}/issues/{index}/times/{id} issue issueDeleteTime
  272. // ---
  273. // summary: Delete specific tracked time
  274. // consumes:
  275. // - application/json
  276. // produces:
  277. // - application/json
  278. // parameters:
  279. // - name: owner
  280. // in: path
  281. // description: owner of the repo
  282. // type: string
  283. // required: true
  284. // - name: repo
  285. // in: path
  286. // description: name of the repo
  287. // type: string
  288. // required: true
  289. // - name: index
  290. // in: path
  291. // description: index of the issue
  292. // type: integer
  293. // format: int64
  294. // required: true
  295. // - name: id
  296. // in: path
  297. // description: id of time to delete
  298. // type: integer
  299. // format: int64
  300. // required: true
  301. // responses:
  302. // "204":
  303. // "$ref": "#/responses/empty"
  304. // "400":
  305. // "$ref": "#/responses/error"
  306. // "403":
  307. // "$ref": "#/responses/forbidden"
  308. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  309. if err != nil {
  310. if models.IsErrIssueNotExist(err) {
  311. ctx.NotFound(err)
  312. } else {
  313. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  314. }
  315. return
  316. }
  317. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  318. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  319. ctx.JSON(http.StatusBadRequest, struct{ Message string }{Message: "time tracking disabled"})
  320. return
  321. }
  322. ctx.Status(http.StatusForbidden)
  323. return
  324. }
  325. time, err := models.GetTrackedTimeByID(ctx.ParamsInt64(":id"))
  326. if err != nil {
  327. if models.IsErrNotExist(err) {
  328. ctx.NotFound(err)
  329. return
  330. }
  331. ctx.Error(http.StatusInternalServerError, "GetTrackedTimeByID", err)
  332. return
  333. }
  334. if time.Deleted {
  335. ctx.NotFound(fmt.Errorf("tracked time [%d] already deleted", time.ID))
  336. return
  337. }
  338. if !ctx.User.IsAdmin && time.UserID != ctx.User.ID {
  339. //Only Admin and User itself can delete their time
  340. ctx.Status(http.StatusForbidden)
  341. return
  342. }
  343. err = models.DeleteTime(time)
  344. if err != nil {
  345. ctx.Error(http.StatusInternalServerError, "DeleteTime", err)
  346. return
  347. }
  348. ctx.Status(http.StatusNoContent)
  349. }
  350. // ListTrackedTimesByUser lists all tracked times of the user
  351. func ListTrackedTimesByUser(ctx *context.APIContext) {
  352. // swagger:operation GET /repos/{owner}/{repo}/times/{user} repository userTrackedTimes
  353. // ---
  354. // summary: List a user's tracked times in a repo
  355. // deprecated: true
  356. // produces:
  357. // - application/json
  358. // parameters:
  359. // - name: owner
  360. // in: path
  361. // description: owner of the repo
  362. // type: string
  363. // required: true
  364. // - name: repo
  365. // in: path
  366. // description: name of the repo
  367. // type: string
  368. // required: true
  369. // - name: user
  370. // in: path
  371. // description: username of user
  372. // type: string
  373. // required: true
  374. // responses:
  375. // "200":
  376. // "$ref": "#/responses/TrackedTimeList"
  377. // "400":
  378. // "$ref": "#/responses/error"
  379. // "403":
  380. // "$ref": "#/responses/forbidden"
  381. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  382. ctx.Error(http.StatusBadRequest, "", "time tracking disabled")
  383. return
  384. }
  385. user, err := models.GetUserByName(ctx.Params(":timetrackingusername"))
  386. if err != nil {
  387. if models.IsErrUserNotExist(err) {
  388. ctx.NotFound(err)
  389. } else {
  390. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  391. }
  392. return
  393. }
  394. if user == nil {
  395. ctx.NotFound()
  396. return
  397. }
  398. if !ctx.IsUserRepoAdmin() && !ctx.User.IsAdmin && ctx.User.ID != user.ID {
  399. ctx.Error(http.StatusForbidden, "", fmt.Errorf("query by user not allowed; not enough rights"))
  400. return
  401. }
  402. opts := &models.FindTrackedTimesOptions{
  403. UserID: user.ID,
  404. RepositoryID: ctx.Repo.Repository.ID,
  405. }
  406. trackedTimes, err := models.GetTrackedTimes(opts)
  407. if err != nil {
  408. ctx.Error(http.StatusInternalServerError, "GetTrackedTimes", err)
  409. return
  410. }
  411. if err = trackedTimes.LoadAttributes(); err != nil {
  412. ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
  413. return
  414. }
  415. ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes))
  416. }
  417. // ListTrackedTimesByRepository lists all tracked times of the repository
  418. func ListTrackedTimesByRepository(ctx *context.APIContext) {
  419. // swagger:operation GET /repos/{owner}/{repo}/times repository repoTrackedTimes
  420. // ---
  421. // summary: List a repo's tracked times
  422. // produces:
  423. // - application/json
  424. // parameters:
  425. // - name: owner
  426. // in: path
  427. // description: owner of the repo
  428. // type: string
  429. // required: true
  430. // - name: repo
  431. // in: path
  432. // description: name of the repo
  433. // type: string
  434. // required: true
  435. // - name: user
  436. // in: query
  437. // description: optional filter by user (available for issue managers)
  438. // type: string
  439. // - name: since
  440. // in: query
  441. // description: Only show times updated after the given time. This is a timestamp in RFC 3339 format
  442. // type: string
  443. // format: date-time
  444. // - name: before
  445. // in: query
  446. // description: Only show times updated before the given time. This is a timestamp in RFC 3339 format
  447. // type: string
  448. // format: date-time
  449. // - name: page
  450. // in: query
  451. // description: page number of results to return (1-based)
  452. // type: integer
  453. // - name: limit
  454. // in: query
  455. // description: page size of results
  456. // type: integer
  457. // responses:
  458. // "200":
  459. // "$ref": "#/responses/TrackedTimeList"
  460. // "400":
  461. // "$ref": "#/responses/error"
  462. // "403":
  463. // "$ref": "#/responses/forbidden"
  464. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  465. ctx.Error(http.StatusBadRequest, "", "time tracking disabled")
  466. return
  467. }
  468. opts := &models.FindTrackedTimesOptions{
  469. ListOptions: utils.GetListOptions(ctx),
  470. RepositoryID: ctx.Repo.Repository.ID,
  471. }
  472. // Filters
  473. qUser := ctx.FormTrim("user")
  474. if qUser != "" {
  475. user, err := models.GetUserByName(qUser)
  476. if models.IsErrUserNotExist(err) {
  477. ctx.Error(http.StatusNotFound, "User does not exist", err)
  478. } else if err != nil {
  479. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  480. return
  481. }
  482. opts.UserID = user.ID
  483. }
  484. var err error
  485. if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil {
  486. ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
  487. return
  488. }
  489. cantSetUser := !ctx.User.IsAdmin &&
  490. opts.UserID != ctx.User.ID &&
  491. !ctx.IsUserRepoWriter([]models.UnitType{models.UnitTypeIssues})
  492. if cantSetUser {
  493. if opts.UserID == 0 {
  494. opts.UserID = ctx.User.ID
  495. } else {
  496. ctx.Error(http.StatusForbidden, "", fmt.Errorf("query by user not allowed; not enough rights"))
  497. return
  498. }
  499. }
  500. count, err := models.CountTrackedTimes(opts)
  501. if err != nil {
  502. ctx.InternalServerError(err)
  503. return
  504. }
  505. trackedTimes, err := models.GetTrackedTimes(opts)
  506. if err != nil {
  507. ctx.Error(http.StatusInternalServerError, "GetTrackedTimes", err)
  508. return
  509. }
  510. if err = trackedTimes.LoadAttributes(); err != nil {
  511. ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
  512. return
  513. }
  514. ctx.SetTotalCountHeader(count)
  515. ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes))
  516. }
  517. // ListMyTrackedTimes lists all tracked times of the current user
  518. func ListMyTrackedTimes(ctx *context.APIContext) {
  519. // swagger:operation GET /user/times user userCurrentTrackedTimes
  520. // ---
  521. // summary: List the current user's tracked times
  522. // parameters:
  523. // - name: page
  524. // in: query
  525. // description: page number of results to return (1-based)
  526. // type: integer
  527. // - name: limit
  528. // in: query
  529. // description: page size of results
  530. // type: integer
  531. // produces:
  532. // - application/json
  533. // parameters:
  534. // - name: since
  535. // in: query
  536. // description: Only show times updated after the given time. This is a timestamp in RFC 3339 format
  537. // type: string
  538. // format: date-time
  539. // - name: before
  540. // in: query
  541. // description: Only show times updated before the given time. This is a timestamp in RFC 3339 format
  542. // type: string
  543. // format: date-time
  544. // responses:
  545. // "200":
  546. // "$ref": "#/responses/TrackedTimeList"
  547. opts := &models.FindTrackedTimesOptions{
  548. ListOptions: utils.GetListOptions(ctx),
  549. UserID: ctx.User.ID,
  550. }
  551. var err error
  552. if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil {
  553. ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
  554. return
  555. }
  556. count, err := models.CountTrackedTimes(opts)
  557. if err != nil {
  558. ctx.InternalServerError(err)
  559. return
  560. }
  561. trackedTimes, err := models.GetTrackedTimes(opts)
  562. if err != nil {
  563. ctx.Error(http.StatusInternalServerError, "GetTrackedTimesByUser", err)
  564. return
  565. }
  566. if err = trackedTimes.LoadAttributes(); err != nil {
  567. ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
  568. return
  569. }
  570. ctx.SetTotalCountHeader(count)
  571. ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes))
  572. }