-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
142 lines (113 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
137
138
139
140
141
142
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"github.com/reugn/go-quartz/job"
"github.com/reugn/go-quartz/quartz"
"github.com/xanzy/go-gitlab"
)
func main() {
// Load configuration
config, err := loadConfig(&OsEnv{})
if err != nil {
log.Printf("Error loading configuration: %v", err)
os.Exit(1)
}
if config.CronSchedule == "" {
log.Printf("Running in one-shot mode")
execute(config)
return
}
runScheduler(config)
}
func runScheduler(config *Config) {
log.Printf("Running in cron mode with schedule: %s\n", config.CronSchedule)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sched := quartz.NewStdScheduler()
sched.Start(ctx)
cronTrigger, err := quartz.NewCronTrigger(config.CronSchedule)
if err != nil {
log.Printf("Error creating cron trigger: %v\n", err)
os.Exit(1)
}
executeJob := job.NewFunctionJob(func(_ context.Context) (int, error) {
execute(config)
return 0, nil
})
err = sched.ScheduleJob(quartz.NewJobDetail(executeJob, quartz.NewJobKey("executeJob")), cronTrigger)
if err != nil {
log.Printf("Error scheduling job: %v\n", err)
os.Exit(1)
}
<-ctx.Done()
}
func execute(config *Config) {
glClient, err := gitlab.NewClient(config.GitLab.Token,
gitlab.WithBaseURL(config.GitLab.URL))
if err != nil {
log.Printf("Error creating GitLab client: %v\n", err)
os.Exit(1)
}
gitlabClient := &gitLabClient{client: glClient}
mrs, err := fetchOpenedMergeRequests(config, gitlabClient)
if err != nil {
log.Printf("Error fetching opened merge requests: %v\n", err)
os.Exit(1)
}
mrs = filterMergeRequestsByAuthor(mrs, config.Authors)
if len(mrs) == 0 {
log.Println("No opened merge requests found.")
os.Exit(0)
}
summary := formatMergeRequestsSummary(mrs)
slackClient := &slackClient{webhookURL: config.Slack.WebhookURL}
err = sendSlackMessage(slackClient, summary)
if err != nil {
log.Printf("Error sending Slack message: %v\n", err)
os.Exit(1)
}
log.Println("Successfully sent merge request summary to Slack.")
}
func formatMergeRequestsSummary(mrs []*MergeRequestWithApprovals) string {
var summary string
for _, mr := range mrs {
approvedBy := strings.Join(mr.ApprovedBy, ", ")
if approvedBy == "" {
approvedBy = "None"
}
createdAtStr := mr.MergeRequest.CreatedAt.Format("2 January 2006, 15:04 MST")
var extra string
if !mr.MergeRequest.BlockingDiscussionsResolved {
extra = ":warning: Has unresolved blocking discussions"
}
summary += fmt.Sprintf(
":arrow_forward: <%s|%s>\n*Author:* %s\n*Created at:* %s\n*Approved by:* %s\n",
mr.MergeRequest.WebURL, mr.MergeRequest.Title, mr.MergeRequest.Author.Name, createdAtStr, approvedBy,
)
if extra != "" {
summary += fmt.Sprintf("*Extra:* %s\n", extra)
}
summary += "\n"
}
return summary
}
func filterMergeRequestsByAuthor(mrs []*MergeRequestWithApprovals, authors []ConfigAuthor) []*MergeRequestWithApprovals {
if len(authors) == 0 {
return mrs
}
var filteredMRs []*MergeRequestWithApprovals
for _, mr := range mrs {
for _, user := range authors {
if (user.ID != 0 && user.ID == mr.MergeRequest.Author.ID) ||
(user.Username != "" && user.Username == mr.MergeRequest.Author.Username) {
filteredMRs = append(filteredMRs, mr)
break
}
}
}
return filteredMRs
}