-
Notifications
You must be signed in to change notification settings - Fork 1
/
clockify.go
206 lines (174 loc) · 5.69 KB
/
clockify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package clockify
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"regexp"
"time"
"strconv"
"github.com/gabor-boros/minutes/internal/pkg/client"
"github.com/gabor-boros/minutes/internal/pkg/worklog"
)
const (
// DateFormat is the specific format used by Clockify to parse time.
DateFormat string = "2006-01-02T15:04:05Z"
// MaxPageLength is the maximum page length defined by Clockify.
MaxPageLength int = 5000
// PathWorklog is the API endpoint used to search and create worklogs.
PathWorklog string = "/api/v1/workspaces/%s/user/%s/time-entries"
)
// Project represents the project assigned to an entry.
type Project struct {
worklog.IDNameField
ClientID string `json:"clientId"`
ClientName string `json:"clientName"`
}
// Interval represents the Start and End date of an entry.
type Interval struct {
Start time.Time `json:"start"`
End time.Time `json:"end"`
}
// FetchEntry represents the entry fetched from Clockify.
type FetchEntry struct {
Description string `json:"description"`
Billable bool `json:"billable"`
Project Project `json:"project"`
TimeInterval Interval `json:"timeInterval"`
Task worklog.IDNameField `json:"task"`
Tags []worklog.IDNameField `json:"tags"`
}
// WorklogSearchParams represents the parameters used to filter search results.
// Hydrated indicates to return the "expanded" search result. Expanded result
// contains the project, task, and tag details, not just their ID.
type WorklogSearchParams struct {
Start string
End string
Page int
PageSize int
Hydrated bool
InProgress bool
}
// ClientOpts is the client specific options, extending client.BaseClientOpts.
type ClientOpts struct {
client.BaseClientOpts
Workspace string
}
type clockifyClient struct {
opts *ClientOpts
}
func (c *clockifyClient) getSearchURL(user string, params *WorklogSearchParams) (string, error) {
searchPath := fmt.Sprintf(PathWorklog, c.opts.Workspace, user)
worklogURL, err := url.Parse(c.opts.BaseURL + searchPath)
if err != nil {
return "", err
}
queryParams := worklogURL.Query()
queryParams.Add("start", params.Start)
queryParams.Add("end", params.End)
queryParams.Add("page", strconv.Itoa(params.Page))
queryParams.Add("page-size", strconv.Itoa(params.PageSize))
queryParams.Add("hydrated", strconv.FormatBool(params.Hydrated))
queryParams.Add("in-progress", strconv.FormatBool(params.InProgress))
worklogURL.RawQuery = queryParams.Encode()
return fmt.Sprintf("%s?%s", worklogURL.Path, worklogURL.Query().Encode()), nil
}
func (c *clockifyClient) fetchEntries(ctx context.Context, path string) ([]FetchEntry, error) {
resp, err := client.SendRequest(ctx, &client.SendRequestOpts{
Method: http.MethodGet,
Path: path,
ClientOpts: &c.opts.HTTPClientOpts,
Data: nil,
})
if err != nil {
return nil, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
var fetchedEntries []FetchEntry
if err = json.NewDecoder(resp.Body).Decode(&fetchedEntries); err != nil {
return nil, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
return fetchedEntries, err
}
func (c *clockifyClient) parseEntries(fetchedEntries []FetchEntry, tagsAsTasksRegex *regexp.Regexp) []worklog.Entry {
var entries []worklog.Entry
for _, entry := range fetchedEntries {
billableDuration := entry.TimeInterval.End.Sub(entry.TimeInterval.Start)
unbillableDuration := time.Duration(0)
if !entry.Billable {
unbillableDuration = billableDuration
billableDuration = 0
}
worklogEntry := worklog.Entry{
Client: worklog.IDNameField{
ID: entry.Project.ClientID,
Name: entry.Project.ClientName,
},
Project: worklog.IDNameField{
ID: entry.Project.ID,
Name: entry.Project.Name,
},
Task: worklog.IDNameField{
ID: entry.Task.ID,
Name: entry.Task.Name,
},
Summary: entry.Task.Name,
Notes: entry.Description,
Start: entry.TimeInterval.Start,
BillableDuration: billableDuration,
UnbillableDuration: unbillableDuration,
}
if c.opts.TagsAsTasks && len(entry.Tags) > 0 {
pageEntries := worklogEntry.SplitByTagsAsTasks(entry.Description, tagsAsTasksRegex, entry.Tags)
entries = append(entries, pageEntries...)
} else {
entries = append(entries, worklogEntry)
}
}
return entries
}
func (c *clockifyClient) FetchEntries(ctx context.Context, opts *client.FetchOpts) ([]worklog.Entry, error) {
var err error
var entries []worklog.Entry
currentPage := 1
pageSize := 100
var tagsAsTasksRegex *regexp.Regexp
if c.opts.TagsAsTasks {
tagsAsTasksRegex, err = regexp.Compile(c.opts.TagsAsTasksRegex)
if err != nil {
return nil, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
}
// Naive pagination as the API does not return the number of total entries
for currentPage*pageSize < MaxPageLength {
searchParams := &WorklogSearchParams{
Start: opts.Start.Format(DateFormat),
End: opts.End.Format(DateFormat),
Page: currentPage,
PageSize: pageSize,
Hydrated: true,
InProgress: false,
}
searchURL, err := c.getSearchURL(opts.User, searchParams)
if err != nil {
return nil, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
fetchedEntries, err := c.fetchEntries(ctx, searchURL)
if err != nil {
return nil, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
// The API returned no entries, meaning no entries left
if len(fetchedEntries) == 0 {
break
}
entries = append(entries, c.parseEntries(fetchedEntries, tagsAsTasksRegex)...)
currentPage++
}
return entries, nil
}
// NewClient returns a new Clockify client.
func NewClient(opts *ClientOpts) client.Fetcher {
return &clockifyClient{
opts: opts,
}
}