-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (76 loc) · 2.27 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
package main
import (
"fmt"
"log"
"os"
"github.com/robfig/cron/v3"
"github.com/spf13/cobra"
"github.com/vshn/gitlab-scheduled-merge/client"
"github.com/vshn/gitlab-scheduled-merge/task"
)
var (
version = "snapshot"
commit = "unknown"
date = "unknown"
)
type RepositoryConfig struct {
MergeWindows []MergeWindow `yaml:"mergeWindows"`
}
type MergeWindow struct {
Cron string `yaml:"cron"`
MaxDelay string `yaml:"maxDelay"`
}
func main() {
cmd := &cobra.Command{
Use: "gitlab-schedule-merge",
}
gitlabToken := cmd.Flags().StringP("gitlab-token", "t", "", "Token with which to authenticate with GitLab")
gitlabBaseUrl := cmd.Flags().String("gitlab-base-url", "https://gitlab.com/api/v4", "Base URL of GitLab API to use")
scheduledLabel := cmd.Flags().String("scheduled-label", "scheduled", "Name of the label which indicates a MR should be scheduled")
configFilePath := cmd.Flags().String("config-file-path", ".merge-schedule.yml", "Path of the config file in the repo which is used to configure merge windows")
taskSchedule := cmd.Flags().String("task-schedule", "@every 15m", "Cron schedule for how frequently to process merge requests")
cmd.Run = func(*cobra.Command, []string) {
gitlabConfig := client.GitlabConfig{
AccessToken: *gitlabToken,
BaseURL: *gitlabBaseUrl,
}
gitlabClient, err := client.NewGitlabClient(gitlabConfig)
if err != nil {
log.Fatalf("GitLab client error: %s", err.Error())
}
task, err := setupCronTask(gitlabClient, *taskSchedule, *scheduledLabel, *configFilePath)
if err != nil {
log.Fatalf("Error setting up cron task: %s", err.Error())
}
log.Println("Starting task...")
task.Run()
}
if err := cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func setupCronTask(
client client.GitlabClient,
crontab string,
scheduledLabel string,
configFilePath string,
) (*cron.Cron, error) {
config := task.TaskConfig{
MergeRequestScheduledLabel: scheduledLabel,
ConfigFilePath: configFilePath,
}
periodicTask := task.NewTask(client, config)
c := cron.New()
_, err := c.AddFunc(crontab, func() {
err := periodicTask.Run()
if err == nil {
return
}
log.Printf("error during periodic job: %s\n", err.Error())
})
if err != nil {
return nil, err
}
return c, nil
}