8
8
"bytes"
9
9
"encoding/json"
10
10
"fmt"
11
+ "net/url"
11
12
"time"
12
13
)
13
14
@@ -25,24 +26,47 @@ type TrackedTime struct {
25
26
Issue * Issue `json:"issue"`
26
27
}
27
28
28
- // GetUserTrackedTimes list tracked times of a user
29
- func (c * Client ) GetUserTrackedTimes (owner , repo , user string ) ([]* TrackedTime , * Response , error ) {
30
- times := make ([]* TrackedTime , 0 , 10 )
31
- resp , err := c .getParsedResponse ("GET" , fmt .Sprintf ("/repos/%s/%s/times/%s" , owner , repo , user ), nil , nil , & times )
32
- return times , resp , err
29
+ // ListTrackedTimesOptions options for listing repository's tracked times
30
+ type ListTrackedTimesOptions struct {
31
+ ListOptions
32
+ Since time.Time
33
+ Before time.Time
34
+ // User filter is only used by ListRepoTrackedTimes !!!
35
+ User string
33
36
}
34
37
35
- // GetRepoTrackedTimes list tracked times of a repository
36
- func (c * Client ) GetRepoTrackedTimes (owner , repo string ) ([]* TrackedTime , * Response , error ) {
37
- times := make ([]* TrackedTime , 0 , 10 )
38
- resp , err := c .getParsedResponse ("GET" , fmt .Sprintf ("/repos/%s/%s/times" , owner , repo ), nil , nil , & times )
38
+ // QueryEncode turns options into querystring argument
39
+ func (opt * ListTrackedTimesOptions ) QueryEncode () string {
40
+ query := opt .getURLQuery ()
41
+
42
+ if ! opt .Since .IsZero () {
43
+ query .Add ("since" , opt .Since .Format (time .RFC3339 ))
44
+ }
45
+ if ! opt .Before .IsZero () {
46
+ query .Add ("before" , opt .Before .Format (time .RFC3339 ))
47
+ }
48
+
49
+ if len (opt .User ) != 0 {
50
+ query .Add ("user" , opt .User )
51
+ }
52
+
53
+ return query .Encode ()
54
+ }
55
+
56
+ // ListRepoTrackedTimes list tracked times of a repository
57
+ func (c * Client ) ListRepoTrackedTimes (owner , repo string , opt ListTrackedTimesOptions ) ([]* TrackedTime , * Response , error ) {
58
+ link , _ := url .Parse (fmt .Sprintf ("/repos/%s/%s/times" , owner , repo ))
59
+ opt .setDefaults ()
60
+ link .RawQuery = opt .QueryEncode ()
61
+ times := make ([]* TrackedTime , 0 , opt .PageSize )
62
+ resp , err := c .getParsedResponse ("GET" , link .String (), jsonHeader , nil , & times )
39
63
return times , resp , err
40
64
}
41
65
42
66
// GetMyTrackedTimes list tracked times of the current user
43
67
func (c * Client ) GetMyTrackedTimes () ([]* TrackedTime , * Response , error ) {
44
68
times := make ([]* TrackedTime , 0 , 10 )
45
- resp , err := c .getParsedResponse ("GET" , "/user/times" , nil , nil , & times )
69
+ resp , err := c .getParsedResponse ("GET" , "/user/times" , jsonHeader , nil , & times )
46
70
return times , resp , err
47
71
}
48
72
@@ -80,27 +104,24 @@ func (c *Client) AddTime(owner, repo string, index int64, opt AddTimeOption) (*T
80
104
return t , resp , err
81
105
}
82
106
83
- // ListTrackedTimesOptions options for listing repository's tracked times
84
- type ListTrackedTimesOptions struct {
85
- ListOptions
86
- }
87
-
88
- // ListTrackedTimes list tracked times of a single issue for a given repository
89
- func (c * Client ) ListTrackedTimes (owner , repo string , index int64 , opt ListTrackedTimesOptions ) ([]* TrackedTime , * Response , error ) {
107
+ // ListIssueTrackedTimes list tracked times of a single issue for a given repository
108
+ func (c * Client ) ListIssueTrackedTimes (owner , repo string , index int64 , opt ListTrackedTimesOptions ) ([]* TrackedTime , * Response , error ) {
109
+ link , _ := url .Parse (fmt .Sprintf ("/repos/%s/%s/issues/%d/times" , owner , repo , index ))
90
110
opt .setDefaults ()
111
+ link .RawQuery = opt .QueryEncode ()
91
112
times := make ([]* TrackedTime , 0 , opt .PageSize )
92
- resp , err := c .getParsedResponse ("GET" , fmt . Sprintf ( "/repos/%s/%s/issues/%d/times?%s" , owner , repo , index , opt . getURLQuery (). Encode ()), nil , nil , & times )
113
+ resp , err := c .getParsedResponse ("GET" , link . String (), jsonHeader , nil , & times )
93
114
return times , resp , err
94
115
}
95
116
96
117
// ResetIssueTime reset tracked time of a single issue for a given repository
97
118
func (c * Client ) ResetIssueTime (owner , repo string , index int64 ) (* Response , error ) {
98
- _ , resp , err := c .getResponse ("DELETE" , fmt .Sprintf ("/repos/%s/%s/issues/%d/times" , owner , repo , index ), nil , nil )
119
+ _ , resp , err := c .getResponse ("DELETE" , fmt .Sprintf ("/repos/%s/%s/issues/%d/times" , owner , repo , index ), jsonHeader , nil )
99
120
return resp , err
100
121
}
101
122
102
123
// DeleteTime delete a specific tracked time by id of a single issue for a given repository
103
124
func (c * Client ) DeleteTime (owner , repo string , index , timeID int64 ) (* Response , error ) {
104
- _ , resp , err := c .getResponse ("DELETE" , fmt .Sprintf ("/repos/%s/%s/issues/%d/times/%d" , owner , repo , index , timeID ), nil , nil )
125
+ _ , resp , err := c .getResponse ("DELETE" , fmt .Sprintf ("/repos/%s/%s/issues/%d/times/%d" , owner , repo , index , timeID ), jsonHeader , nil )
105
126
return resp , err
106
127
}
0 commit comments