-
Notifications
You must be signed in to change notification settings - Fork 1
/
timewarrior.go
211 lines (177 loc) · 5.63 KB
/
timewarrior.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
207
208
209
210
211
package timewarrior
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"regexp"
"time"
"github.com/gabor-boros/minutes/internal/pkg/client"
"github.com/gabor-boros/minutes/internal/pkg/worklog"
)
const (
dateFormat string = "2006-01-02T15:04:05"
ParseDateFormat string = "20060102T150405Z"
)
// FetchEntry represents the entry exported from Timewarrior.
type FetchEntry struct {
ID int `json:"id"`
Start string `json:"start"`
End string `json:"end"`
Tags []string `json:"tags"`
Annotation string `json:"annotation"`
}
// ClientOpts is the client specific options, extending client.BaseClientOpts.
// Since Timewarrior is a CLI tool, hence it has no API we could call on HTTP.
// Although client.HTTPClientOptions is part of client.BaseClientOpts, we are
// not using that as part of this integration, instead we are defining the path
// of the executable (Command) and the command arguments used for export
// (CommandArguments).
type ClientOpts struct {
client.BaseClientOpts
Command string
CommandArguments []string
CommandCtxExecutor func(ctx context.Context, name string, arg ...string) *exec.Cmd
UnbillableTag string
ClientTagRegex string
ProjectTagRegex string
}
type timewarriorClient struct {
opts *ClientOpts
}
func (c *timewarriorClient) assembleCommand(subcommand string, opts *client.FetchOpts) (string, []string) {
arguments := []string{subcommand}
arguments = append(
arguments,
[]string{
"from", opts.Start.Format(dateFormat),
"to", opts.End.Format(dateFormat),
}...,
)
arguments = append(arguments, c.opts.CommandArguments...)
return c.opts.Command, arguments
}
func (c *timewarriorClient) splitEntry(entry worklog.Entry, fetchEntry FetchEntry) ([]worklog.Entry, error) {
r, err := regexp.Compile(c.opts.TagsAsTasksRegex)
if err != nil {
return nil, err
}
tasks := map[string]string{}
for _, tag := range fetchEntry.Tags {
if task := r.FindString(tag); task != "" {
tasks[tag] = task
}
}
var entries []worklog.Entry
totalTasks := len(tasks)
for taskID, taskName := range tasks {
splitBillable, splitUnbillable := entry.SplitDuration(totalTasks)
entries = append(entries, worklog.Entry{
Client: entry.Client,
Project: entry.Project,
Task: worklog.IDNameField{
ID: taskID,
Name: taskName,
},
Summary: entry.Summary,
Notes: entry.Notes,
Start: entry.Start,
BillableDuration: splitBillable,
UnbillableDuration: splitUnbillable,
})
}
return entries, nil
}
func (c *timewarriorClient) FetchEntries(ctx context.Context, opts *client.FetchOpts) ([]worklog.Entry, error) {
var entries []worklog.Entry
var fetchedEntries []FetchEntry
command, arguments := c.assembleCommand("export", opts)
out, err := c.opts.CommandCtxExecutor(ctx, command, arguments...).Output() // #nosec G204
if err != nil {
return entries, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
if err = json.Unmarshal(out, &fetchedEntries); err != nil {
return entries, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
clientTagRegex, err := regexp.Compile(c.opts.ClientTagRegex)
if err != nil {
return entries, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
projectTagRegex, err := regexp.Compile(c.opts.ProjectTagRegex)
if err != nil {
return entries, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
tagsAsTasksRegex, err := regexp.Compile(c.opts.TagsAsTasksRegex)
if err != nil {
return entries, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
for _, entry := range fetchedEntries {
var clientName string
var projectName string
var task worklog.IDNameField
startDate, err := time.ParseInLocation(ParseDateFormat, entry.Start, time.Local)
if err != nil {
return entries, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
endDate, err := time.ParseInLocation(ParseDateFormat, entry.End, time.Local)
if err != nil {
return entries, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
billableDuration := endDate.Sub(startDate)
unbillableDuration := time.Duration(0)
for _, tag := range entry.Tags {
if tag == c.opts.UnbillableTag {
unbillableDuration = billableDuration
billableDuration = 0
} else if c.opts.ClientTagRegex != "" && clientTagRegex.MatchString(tag) {
clientName = tag
} else if c.opts.ProjectTagRegex != "" && projectTagRegex.MatchString(tag) {
projectName = tag
} else if c.opts.TagsAsTasksRegex != "" && tagsAsTasksRegex.MatchString(tag) {
task = worklog.IDNameField{
ID: tag,
Name: tag,
}
}
}
// If the task was not found in tags, make sure to set it to annotation
if !task.IsComplete() {
task = worklog.IDNameField{
ID: entry.Annotation,
Name: entry.Annotation,
}
}
worklogEntry := worklog.Entry{
Client: worklog.IDNameField{
ID: clientName,
Name: clientName,
},
Project: worklog.IDNameField{
ID: projectName,
Name: projectName,
},
Task: task,
Summary: entry.Annotation,
Notes: entry.Annotation,
Start: startDate,
BillableDuration: billableDuration,
UnbillableDuration: unbillableDuration,
}
if c.opts.TagsAsTasks && len(entry.Tags) > 0 {
splitEntries, err := c.splitEntry(worklogEntry, entry)
if err != nil {
return entries, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}
entries = append(entries, splitEntries...)
} else {
entries = append(entries, worklogEntry)
}
}
return entries, nil
}
// NewClient returns a new Timewarrior client.
func NewClient(opts *ClientOpts) client.Fetcher {
return &timewarriorClient{
opts: opts,
}
}