time_stats.go

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