-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
136 lines (118 loc) · 3.25 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
package main
import (
"log"
"os"
"sync"
"time"
"github.com/go-co-op/gocron"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/xanzy/go-gitlab"
)
func runTask(bot *tgbotapi.BotAPI, user *User) {
// Authenticate to the GitLab API
git, err := gitlab.NewClient(user.GitLabToken, gitlab.WithBaseURL("https://b3.complang.tuwien.ac.at/"))
if err != nil {
log.Printf("[Error] Unable to authenticate for user %v: %v", user.TelegramID, err.Error())
// Send a error message to the user if not already sent
if user.HasError {
return
}
sendMessage(bot, user.TelegramID, tokenErrorMessage(user))
user.HasError = true
user.Save()
return
}
// Update the user
if user.HasError {
user.HasError = false
user.Save()
}
// Load the logged in user informations
gitUser, _, err := git.Users.CurrentUser()
if err != nil {
sendMessage(bot, user.TelegramID, tokenErrorMessage(user))
return
}
// Load the Projects
options := &gitlab.ListProjectsOptions{
Starred: gitlab.Bool(true),
}
projects, _, err := git.Projects.ListProjects(options)
if err != nil {
log.Printf("[Warning] Unable to load projects for user %v: %v", user.TelegramID, err)
return
}
// Get commit and issue updates on every project
tmpTime := time.Now()
var wg sync.WaitGroup
for _, project := range projects {
wg.Add(2)
go func(bot *tgbotapi.BotAPI, git *gitlab.Client, user *User, gitUser *gitlab.User, project *gitlab.Project) {
defer wg.Done()
sendCommitUpdate(bot, git, user, gitUser, project)
}(bot, git, user, gitUser, project)
go func(bot *tgbotapi.BotAPI, git *gitlab.Client, user *User, gitUser *gitlab.User, project *gitlab.Project) {
defer wg.Done()
sendIssueUpdate(bot, git, user, gitUser, project)
}(bot, git, user, gitUser, project)
}
wg.Wait()
user.LastChecked = tmpTime
user.Save()
}
func runTasks(bot *tgbotapi.BotAPI) {
// Load all users
users, err := LoadAllUsers()
if err != nil {
log.Println("[Error] Unable to load users from disk for background job.")
}
log.Print("[Info] Run background job...")
// Run the task for every user
var wg sync.WaitGroup
counter := 0
for _, user := range users {
// Ignore users that don't have a GitLab Token
if user.GitLabToken == "" {
continue
}
counter++
wg.Add(1)
go func(bot *tgbotapi.BotAPI, user User) {
defer wg.Done()
runTask(bot, &user)
}(bot, user)
}
wg.Wait()
log.Printf("[Info] Completed background job for %v users", counter)
}
func main() {
// Setup the Telegrambot
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_TOKEN"))
if err != nil {
log.Fatalf("Unable to authorize as telegram bot: %v", err.Error())
return
}
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
// Setup the user DB
err = InitUsers()
if err != nil {
log.Fatalf("Unable to initialize user db: %v", err.Error())
return
}
// Start the background jobs
s1 := gocron.NewScheduler(time.UTC)
s1.Every(15).Minutes().Do(runTasks, bot)
s1.StartAsync()
runTasks(bot)
// Listen for Telegramm events
updates, err := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil {
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
go handleUpdate(bot, &update)
}
}