Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue_tracked_time.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 models
  5. import (
  6. "time"
  7. api "code.gitea.io/sdk/gitea"
  8. "github.com/go-xorm/builder"
  9. )
  10. // TrackedTime represents a time that was spent for a specific issue.
  11. type TrackedTime struct {
  12. ID int64 `xorm:"pk autoincr" json:"id"`
  13. IssueID int64 `xorm:"INDEX" json:"issue_id"`
  14. UserID int64 `xorm:"INDEX" json:"user_id"`
  15. Created time.Time `xorm:"-" json:"created"`
  16. CreatedUnix int64 `xorm:"created" json:"-"`
  17. Time int64 `json:"time"`
  18. }
  19. // AfterLoad is invoked from XORM after setting the values of all fields of this object.
  20. func (t *TrackedTime) AfterLoad() {
  21. t.Created = time.Unix(t.CreatedUnix, 0).Local()
  22. }
  23. // APIFormat converts TrackedTime to API format
  24. func (t *TrackedTime) APIFormat() *api.TrackedTime {
  25. return &api.TrackedTime{
  26. ID: t.ID,
  27. IssueID: t.IssueID,
  28. UserID: t.UserID,
  29. Time: t.Time,
  30. Created: t.Created,
  31. }
  32. }
  33. // FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.
  34. type FindTrackedTimesOptions struct {
  35. IssueID int64
  36. UserID int64
  37. RepositoryID int64
  38. }
  39. // ToCond will convert each condition into a xorm-Cond
  40. func (opts *FindTrackedTimesOptions) ToCond() builder.Cond {
  41. cond := builder.NewCond()
  42. if opts.IssueID != 0 {
  43. cond = cond.And(builder.Eq{"issue_id": opts.IssueID})
  44. }
  45. if opts.UserID != 0 {
  46. cond = cond.And(builder.Eq{"user_id": opts.UserID})
  47. }
  48. if opts.RepositoryID != 0 {
  49. cond = cond.And(builder.Eq{"issue.repo_id": opts.RepositoryID})
  50. }
  51. return cond
  52. }
  53. // GetTrackedTimes returns all tracked times that fit to the given options.
  54. func GetTrackedTimes(options FindTrackedTimesOptions) (trackedTimes []*TrackedTime, err error) {
  55. if options.RepositoryID > 0 {
  56. err = x.Join("INNER", "issue", "issue.id = tracked_time.issue_id").Where(options.ToCond()).Find(&trackedTimes)
  57. return
  58. }
  59. err = x.Where(options.ToCond()).Find(&trackedTimes)
  60. return
  61. }
  62. // AddTime will add the given time (in seconds) to the issue
  63. func AddTime(user *User, issue *Issue, time int64) (*TrackedTime, error) {
  64. tt := &TrackedTime{
  65. IssueID: issue.ID,
  66. UserID: user.ID,
  67. Time: time,
  68. }
  69. if _, err := x.Insert(tt); err != nil {
  70. return nil, err
  71. }
  72. if _, err := CreateComment(&CreateCommentOptions{
  73. Issue: issue,
  74. Repo: issue.Repo,
  75. Doer: user,
  76. Content: secToTime(time),
  77. Type: CommentTypeAddTimeManual,
  78. }); err != nil {
  79. return nil, err
  80. }
  81. return tt, nil
  82. }
  83. // TotalTimes returns the spent time for each user by an issue
  84. func TotalTimes(options FindTrackedTimesOptions) (map[*User]string, error) {
  85. trackedTimes, err := GetTrackedTimes(options)
  86. if err != nil {
  87. return nil, err
  88. }
  89. //Adding total time per user ID
  90. totalTimesByUser := make(map[int64]int64)
  91. for _, t := range trackedTimes {
  92. totalTimesByUser[t.UserID] += t.Time
  93. }
  94. totalTimes := make(map[*User]string)
  95. //Fetching User and making time human readable
  96. for userID, total := range totalTimesByUser {
  97. user, err := GetUserByID(userID)
  98. if err != nil {
  99. if IsErrUserNotExist(err) {
  100. continue
  101. }
  102. return nil, err
  103. }
  104. totalTimes[user] = secToTime(total)
  105. }
  106. return totalTimes, nil
  107. }