-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoggl.go
147 lines (113 loc) · 2.72 KB
/
toggl.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
package main
import (
"fmt"
"github.com/google/logger"
"github.com/jason0x43/go-toggl"
"regexp"
"time"
)
type TogglProject struct {
Id int
Name string
}
type TogglTask struct {
Id int
JiraId string
Name string
}
type TogglEntry struct {
Id int
Start time.Time
Stop time.Time
Duration int64
Description string
Project TogglProject
Task TogglTask
Imported bool
}
type Session struct {
toggl.Session
}
type Account struct {
toggl.Account
}
func getTogglSession(token string) toggl.Session {
return toggl.OpenSession(token)
}
// Return entries with Project and Task only
func (s *Session) getTogglEntries(days int) []TogglEntry {
if days == 0 {
days = 7
}
a, _ := s.GetAccount()
account := Account{a}
var entries []TogglEntry
e, _ := s.GetTimeEntries(time.Now().AddDate(0, 0, -days), time.Now())
for _, entry := range e {
project, error := account.getTogglProjectById(entry.Pid)
if error != nil {
logger.Infof("[toggl] Entry '%s' with id %d dosen't have project", entry.Description, entry.ID)
continue
}
task, error := account.getTogglTaskById(entry.Tid)
if error != nil {
logger.Infof("[toggl] Entry '%s' with id %d dosen't have task", entry.Description, entry.ID)
continue
}
entry := TogglEntry{
Id: entry.ID,
Start: entry.Start.Add(time.Hour).Add(time.Millisecond),
Duration: entry.Duration,
Description: entry.Description,
Project: project,
Task: task,
}
entries = append(entries, entry)
}
return entries
}
func (a *Account) getTogglProjectById(id int) (TogglProject, error) {
for _, project := range a.getTogglProjects() {
if project.Id == id {
return project, nil
}
}
return TogglProject{}, fmt.Errorf("Project with id %d not exists", id)
}
func (a *Account) getTogglProjects() []TogglProject {
var projects []TogglProject
for _, project := range a.Data.Projects {
project := TogglProject{
Id: project.ID,
Name: project.Name,
}
projects = append(projects, project)
}
return projects
}
func (a *Account) getTogglTaskById(id int) (TogglTask, error) {
for _, task := range a.getTogglTasks() {
if task.Id == id {
return task, nil
}
}
return TogglTask{}, fmt.Errorf("Task with id %d not exists", id)
}
func (a *Account) getTogglTasks() []TogglTask {
var tasks []TogglTask
for _, task := range a.Data.Tasks {
jiraId := ""
regex := regexp.MustCompile(`\w+\-\d*`)
regexResults := regex.FindAllSubmatch([]byte(task.Name), 1)
if len(task.Name) > 0 && len(regexResults) > 0 {
jiraId = string(regexResults[0][0])
}
task := TogglTask{
Id: task.ID,
JiraId: jiraId,
Name: task.Name,
}
tasks = append(tasks, task)
}
return tasks
}