-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
113 lines (94 loc) · 2.52 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
package main
import (
"context"
"fmt"
"log/slog"
"os"
"strconv"
"github.com/navikt/ghep/internal/api"
"github.com/navikt/ghep/internal/events"
"github.com/navikt/ghep/internal/github"
"github.com/navikt/ghep/internal/redis"
"github.com/navikt/ghep/internal/slack"
)
func main() {
log := slog.New(slog.NewJSONHandler(os.Stdout, nil))
log.Info(fmt.Sprintf("Starting Ghep for %s", os.Getenv("GITHUB_ORG")))
githubClient := github.New(
log.With("component", "github"),
os.Getenv("GITHUB_APP_INSTALLATION_ID"),
os.Getenv("GITHUB_APP_ID"),
os.Getenv("GITHUB_APP_PRIVATE_KEY"),
os.Getenv("GITHUB_ORG"),
)
subscribeToOrg, _ := strconv.ParseBool(os.Getenv("GHEP_SUBSCRIBE_TO_ORG"))
log.Info("Gettings repositories from Github")
teams, err := githubClient.FetchTeams(
os.Getenv("REPOS_CONFIG_FILE_PATH"),
os.Getenv("GITHUB_BLOCKLIST_REPOS"),
subscribeToOrg,
)
if err != nil {
log.Error("fetching teams from Github", "err", err.Error())
os.Exit(1)
}
logTeams(log, teams)
log.Info("Creating Slack client")
slackApi, err := slack.New(
log.With("component", "slack"),
os.Getenv("SLACK_TOKEN"),
)
if err != nil {
log.Error("creating Slack client", "err", err.Error())
os.Exit(1)
}
log.Info("Ensuring Slack channels")
if err := slackApi.EnsureChannels(teams); err != nil {
log.Error("ensuring Slack channels", "err", err.Error())
os.Exit(1)
}
ctx := context.Background()
log.Info("Creating Redis client")
rdb, err := redis.New(
ctx,
log.With("component", "redis"),
os.Getenv("REDIS_URI_EVENTS"),
os.Getenv("REDIS_USERNAME_EVENTS"),
os.Getenv("REDIS_PASSWORD_EVENTS"),
)
if err != nil {
log.Error("creating Redis client", "err", err.Error())
os.Exit(1)
}
log.Info("Creating event handler")
eventHandler := events.NewHandler(githubClient, rdb, slackApi, teams)
orgMembers, err := githubClient.FetchOrgMembers()
if err != nil {
log.Error("fetching org members", "err", err.Error())
}
apiClient := api.New(
log.With("component", "api"),
eventHandler,
rdb,
teams,
orgMembers,
os.Getenv("EXTERNAL_CONTRIBUTORS_CHANNEL"),
subscribeToOrg,
)
addr := os.Getenv("SERVER_ADDR")
if addr == "" {
addr = "0.0.0.0:8080"
}
log.Info("Starting API server")
if err := apiClient.Run(os.Getenv("API_BASE_PATH"), addr); err != nil {
log.Error(err.Error())
os.Exit(1)
}
}
func logTeams(log *slog.Logger, teams []github.Team) {
var teamNames []string
for _, team := range teams {
teamNames = append(teamNames, team.Name)
}
log.Info(fmt.Sprintf("Teams using Ghep: %v", teamNames))
}