-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
76 lines (61 loc) · 1.89 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
package main
import (
"flag"
"github.com/andygrunwald/go-jira"
"github.com/google/logger"
"github.com/joho/godotenv"
"github.com/recoilme/slowpoke"
"os"
"strconv"
"strings"
)
const logPath = "./app.log"
func init() {
godotenv.Load(".env")
}
func main() {
flag.Parse()
lf, error := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
if error != nil {
logger.Fatalf("Failed to open log file: %v", error)
}
defer slowpoke.CloseAll()
defer lf.Close()
defer logger.Init("Logger", false, true, lf).Close()
for _, user := range getUsers() {
togglSession := getTogglSession(user.TogglToken)
session := &Session{togglSession}
jiraClient, _ := getJiraClient(user.JiraLogin, user.JiraToken)
days, _ := strconv.Atoi(os.Getenv("DAYS"))
for _, entry := range session.getTogglEntries(days) {
value := getEntryFromDB(entry.Id)
duration := strings.Split(value, " ")[0]
jiraWorklogId := strings.Split(value, " ")[1]
description := ""
if entry.Task.Name != entry.Description {
description = entry.Description
}
if duration != strconv.FormatInt(entry.Duration, 10) && entry.Task.JiraId != "" {
if jiraWorklogId != "0" {
//update
start := jira.Time(entry.Start)
worklog, error := jiraClient.updateWorkLog(entry.Task.JiraId, description, jiraWorklogId, entry.Duration, start)
if error == nil {
setEntryInDB(entry.Id, strconv.FormatInt(entry.Duration, 10)+" "+worklog.ID)
} else {
logger.Fatalf("[jira] Task %s error: %v", entry.Task.JiraId, error)
}
} else {
//new
start := jira.Time(entry.Start)
worklog, error := jiraClient.addWorkLog(entry.Task.JiraId, description, entry.Duration, start)
if error == nil {
setEntryInDB(entry.Id, strconv.FormatInt(entry.Duration, 10)+" "+worklog.ID)
} else {
logger.Errorf("[jira] Task %s error: %v", entry.Task.JiraId, error)
}
}
}
}
}
}