-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
156 lines (128 loc) · 3.82 KB
/
main.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
package main
import (
"errors"
"fmt"
"os"
"time"
client "github.com/sharovik/toggl-jira/src/clients"
"github.com/sharovik/toggl-jira/src/config"
"github.com/sharovik/toggl-jira/src/database"
"github.com/sharovik/toggl-jira/src/dto"
"github.com/sharovik/toggl-jira/src/log"
"github.com/sharovik/toggl-jira/src/services/arguments"
"github.com/sharovik/toggl-jira/src/services/jira"
"github.com/sharovik/toggl-jira/src/services/toggl"
)
var Cfg config.Config
func init() {
Cfg = config.Init()
if err := log.Init(Cfg); err != nil {
panic(err)
}
if err := database.InitDB(); err != nil {
panic(err)
}
httpClient := client.GetHttpClient()
jira.JS = jira.JiraService{
Client: &client.HTTPClient{
Client: &httpClient,
BaseURL: config.Cfg.JiraBaseURL,
},
}
httpClient = client.GetHttpClient()
toggl.TS = toggl.TogglService{
Client: &client.HTTPClient{
Client: &httpClient,
BaseURL: config.Cfg.TogglApiURL,
},
}
}
func main() {
args := arguments.ParseArgs()
if err := validateConfiguration(); err != nil {
log.Logger().AddError(err).Msg("It looks like there are problems with config. Stop.")
os.Exit(1)
}
if args.WorkspaceID == "" {
args.WorkspaceID = config.Get().TogglWorkspaceID
}
report, err := toggl.TS.GetReport(args)
if err != nil {
log.Logger().AddError(err).Msg("Failed to retrieve the export.")
os.Exit(1)
}
if len(report.Data) == 0 {
log.Logger().Info().Msg("No data in report.")
return
}
for _, item := range report.Data {
taskKey, err := jira.JS.ParseTaskKey(item.Description)
if err != nil {
log.Logger().Warn().Err(err).Msg("Failed to parse the task key.")
continue
}
if taskKey == "" {
log.Logger().Info().
Str("description", item.Description).
Int64("duration", item.Dur).
Str("added", item.Start.Format("2006-01-02")).
Msg("There is no task key specified in description.")
continue
}
timeEntry, err := InsertHistoryScenario(taskKey, item)
if err != nil {
log.Logger().AddError(err).Msg("Failed to insert time entry")
continue
}
spentMinutes := int64(timeEntry.Minutes())
if spentMinutes == 0 {
log.Logger().Info().
Str("task_key", taskKey).
Int64("spent_in_minutes", spentMinutes).
Str("spent_total_time", timeEntry.String()).
Msg("Nothing to track.")
continue
}
if err = jira.JS.SendTheTime(taskKey, fmt.Sprintf("%dm", spentMinutes), item.Start); err != nil {
log.Logger().AddError(err).Msg("Failed to send the worklog")
}
time.Sleep(time.Duration(1) * time.Second)
}
}
func InsertHistoryScenario(taskKey string, item dto.DataItem) (timeEntry time.Duration, err error) {
timeEntry = time.Duration(item.Dur) * time.Millisecond
historyItem, err := database.FindTask(taskKey, item.Dur, item.Start.Format("2006-01-02"))
if err != nil {
log.Logger().AddError(err).Msg("Failed to retrieve information from history table")
return 0, err
}
if historyItem != nil {
log.Logger().Info().
Str("task_key", taskKey).
Int64("duration", item.Dur).
Str("added", item.Start.Format("2006-01-02")).
Msg("This item was already processed. Ignoring.")
return 0, nil
}
log.Logger().Info().
Str("task_key", taskKey).
Int64("duration", item.Dur).
Str("added", item.Start.Format("2006-01-02")).
Msg("Inserting new history row")
if err := database.InsertTask(taskKey, item.Dur, item.Start.Format("2006-01-02")); err != nil {
log.Logger().AddError(err).Msg("Failed to insert the history item!")
return timeEntry, err
}
return timeEntry, nil
}
func validateConfiguration() error {
if config.Get().JiraBaseURL == "" ||
config.Get().JiraAppToken == "" ||
config.Get().JiraEmail == "" ||
config.Get().TogglApiToken == "" ||
config.Get().TogglApiURL == "" ||
config.Get().TogglWorkspaceID == "" {
return errors.New("One of the required items of configuration is missing. ")
}
return nil
}