-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
105 lines (83 loc) · 2.49 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
package main
import (
"os"
"time"
"github.com/GEPROG/lassie-bot-dog/config"
"github.com/GEPROG/lassie-bot-dog/plugins"
"github.com/go-co-op/gocron"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
"github.com/xanzy/go-gitlab"
)
var loadedPlugins []plugins.Plugin
func loop(client *gitlab.Client) {
log.Debug("Starting plugin loop ...")
listProjectsOptions := &gitlab.ListProjectsOptions{
// Search: gitlab.String("lassie-test"),
Membership: gitlab.Bool(true), // only list projects that lassie is a member of
Sort: gitlab.String("asc"),
Archived: gitlab.Bool(false), // only list unarchived projects
}
for {
projects, resp, err := client.Projects.ListProjects(listProjectsOptions)
if err != nil {
log.Debugf("Failed to receive project list: %v", err)
return
}
for _, project := range projects {
config := config.LoadConfig(client, project)
if config == nil {
log.Debugf("No project config found for %s", project.PathWithNamespace)
continue
}
log.Debugf("Found Lassie config for %s", project.PathWithNamespace)
for _, plugin := range loadedPlugins {
log.Debugf("Running plugin '%s' on project '%s'", plugin.Name(), project.PathWithNamespace)
plugin.Execute(project, *config)
}
}
// Exit the loop when we've seen all pages
if resp.CurrentPage >= resp.TotalPages {
break
}
// Update the page number to get the next page
listProjectsOptions.Page = resp.NextPage
}
}
func main() {
err := godotenv.Load()
if err != nil {
log.Debug("No .env file found")
}
if os.Getenv("LOG") == "debug" {
log.SetLevel(log.DebugLevel)
}
gitlabURL := os.Getenv("GITLAB_URL")
if gitlabURL == "" {
log.Fatal("Please provide a gitlab url with GITLAB_URL='https://gitlab.example.com'")
}
gitlabToken := os.Getenv("GITLAB_TOKEN")
if gitlabToken == "" {
log.Fatal("Please provide a gitlab token with GITLAB_TOKEN")
}
log.Info("Lassie is waking up '" + gitlabURL + "' ...")
client, err := gitlab.NewClient(gitlabToken, gitlab.WithBaseURL(gitlabURL))
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
loadedPlugins = plugins.GetPlugins(client)
log.Info("Lassie is now waiting for some work!")
updateInterval := 30
if os.Getenv("LOG") == "debug" {
updateInterval = 5
}
s := gocron.NewScheduler(time.UTC)
s.SetMaxConcurrentJobs(1, gocron.RescheduleMode)
_, err = s.Every(updateInterval).Seconds().Do(func() {
loop(client)
})
if err != nil {
log.Fatal("Failed to start gocron")
}
s.StartBlocking()
}