time_stats.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package gitlab
  2. import (
  3. "fmt"
  4. "net/url"
  5. )
  6. // timeStatsService handles communication with the time tracking related
  7. // methods of the GitLab API.
  8. //
  9. // GitLab docs: https://docs.gitlab.com/ce/workflow/time_tracking.html
  10. type timeStatsService struct {
  11. client *Client
  12. }
  13. // TimeStats represents the time estimates and time spent for an issue.
  14. //
  15. // GitLab docs: https://docs.gitlab.com/ce/workflow/time_tracking.html
  16. type TimeStats struct {
  17. HumanTimeEstimate string `json:"human_time_estimate"`
  18. HumanTotalTimeSpent string `json:"human_total_time_spent"`
  19. TimeEstimate int `json:"time_estimate"`
  20. TotalTimeSpent int `json:"total_time_spent"`
  21. }
  22. func (t TimeStats) String() string {
  23. return Stringify(t)
  24. }
  25. // SetTimeEstimateOptions represents the available SetTimeEstimate()
  26. // options.
  27. //
  28. // GitLab docs: https://docs.gitlab.com/ce/workflow/time_tracking.html
  29. type SetTimeEstimateOptions struct {
  30. Duration *string `url:"duration,omitempty" json:"duration,omitempty"`
  31. }
  32. // setTimeEstimate sets the time estimate for a single project issue.
  33. //
  34. // GitLab docs: https://docs.gitlab.com/ce/workflow/time_tracking.html
  35. func (s *timeStatsService) setTimeEstimate(pid interface{}, entity string, issue int, opt *SetTimeEstimateOptions, options ...OptionFunc) (*TimeStats, *Response, error) {
  36. project, err := parseID(pid)
  37. if err != nil {
  38. return nil, nil, err
  39. }
  40. u := fmt.Sprintf("projects/%s/%s/%d/time_estimate", url.QueryEscape(project), entity, issue)
  41. req, err := s.client.NewRequest("POST", u, opt, options)
  42. if err != nil {
  43. return nil, nil, err
  44. }
  45. t := new(TimeStats)
  46. resp, err := s.client.Do(req, t)
  47. if err != nil {
  48. return nil, resp, err
  49. }
  50. return t, resp, err
  51. }
  52. // resetTimeEstimate resets the time estimate for a single project issue.
  53. //
  54. // GitLab docs: https://docs.gitlab.com/ce/workflow/time_tracking.html
  55. func (s *timeStatsService) resetTimeEstimate(pid interface{}, entity string, issue int, options ...OptionFunc) (*TimeStats, *Response, error) {
  56. project, err := parseID(pid)
  57. if err != nil {
  58. return nil, nil, err
  59. }
  60. u := fmt.Sprintf("projects/%s/%s/%d/reset_time_estimate", url.QueryEscape(project), entity, issue)
  61. req, err := s.client.NewRequest("POST", u, nil, options)
  62. if err != nil {
  63. return nil, nil, err
  64. }
  65. t := new(TimeStats)
  66. resp, err := s.client.Do(req, t)
  67. if err != nil {
  68. return nil, resp, err
  69. }
  70. return t, resp, err
  71. }
  72. // AddSpentTimeOptions represents the available AddSpentTime() options.
  73. //
  74. // GitLab docs: https://docs.gitlab.com/ce/workflow/time_tracking.html
  75. type AddSpentTimeOptions struct {
  76. Duration *string `url:"duration,omitempty" json:"duration,omitempty"`
  77. }
  78. // addSpentTime adds spent time for a single project issue.
  79. //
  80. // GitLab docs: https://docs.gitlab.com/ce/workflow/time_tracking.html
  81. func (s *timeStatsService) addSpentTime(pid interface{}, entity string, issue int, opt *AddSpentTimeOptions, options ...OptionFunc) (*TimeStats, *Response, error) {
  82. project, err := parseID(pid)
  83. if err != nil {
  84. return nil, nil, err
  85. }
  86. u := fmt.Sprintf("projects/%s/%s/%d/add_spent_time", url.QueryEscape(project), entity, issue)
  87. req, err := s.client.NewRequest("POST", u, opt, options)
  88. if err != nil {
  89. return nil, nil, err
  90. }
  91. t := new(TimeStats)
  92. resp, err := s.client.Do(req, t)
  93. if err != nil {
  94. return nil, resp, err
  95. }
  96. return t, resp, err
  97. }
  98. // resetSpentTime resets the spent time for a single project issue.
  99. //
  100. // GitLab docs: https://docs.gitlab.com/ce/workflow/time_tracking.html
  101. func (s *timeStatsService) resetSpentTime(pid interface{}, entity string, issue int, options ...OptionFunc) (*TimeStats, *Response, error) {
  102. project, err := parseID(pid)
  103. if err != nil {
  104. return nil, nil, err
  105. }
  106. u := fmt.Sprintf("projects/%s/%s/%d/reset_spent_time", url.QueryEscape(project), entity, issue)
  107. req, err := s.client.NewRequest("POST", u, nil, options)
  108. if err != nil {
  109. return nil, nil, err
  110. }
  111. t := new(TimeStats)
  112. resp, err := s.client.Do(req, t)
  113. if err != nil {
  114. return nil, resp, err
  115. }
  116. return t, resp, err
  117. }
  118. // getTimeSpent gets the spent time for a single project issue.
  119. //
  120. // GitLab docs: https://docs.gitlab.com/ce/workflow/time_tracking.html
  121. func (s *timeStatsService) getTimeSpent(pid interface{}, entity string, issue int, options ...OptionFunc) (*TimeStats, *Response, error) {
  122. project, err := parseID(pid)
  123. if err != nil {
  124. return nil, nil, err
  125. }
  126. u := fmt.Sprintf("projects/%s/%s/%d/time_stats", url.QueryEscape(project), entity, issue)
  127. req, err := s.client.NewRequest("GET", u, nil, options)
  128. if err != nil {
  129. return nil, nil, err
  130. }
  131. t := new(TimeStats)
  132. resp, err := s.client.Do(req, t)
  133. if err != nil {
  134. return nil, resp, err
  135. }
  136. return t, resp, err
  137. }