-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
github.go
96 lines (75 loc) · 3.76 KB
/
github.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
//+build !faker
package github
import (
"time"
"github.com/monitoror/monitoror/api/config/versions"
pkgMonitorable "github.com/monitoror/monitoror/internal/pkg/monitorable"
coreModels "github.com/monitoror/monitoror/models"
"github.com/monitoror/monitoror/monitorables/github/api"
githubDelivery "github.com/monitoror/monitoror/monitorables/github/api/delivery/http"
githubModels "github.com/monitoror/monitoror/monitorables/github/api/models"
githubRepository "github.com/monitoror/monitoror/monitorables/github/api/repository"
githubUsecase "github.com/monitoror/monitoror/monitorables/github/api/usecase"
githubConfig "github.com/monitoror/monitoror/monitorables/github/config"
"github.com/monitoror/monitoror/registry"
"github.com/monitoror/monitoror/service/options"
"github.com/monitoror/monitoror/store"
)
type Monitorable struct {
store *store.Store
config map[coreModels.VariantName]*githubConfig.Github
// Config tile settings
countTileEnabler registry.TileEnabler
checksTileEnabler registry.TileEnabler
pullRequestTileEnabler registry.TileEnabler
pullRequestGeneratorEnabler registry.GeneratorEnabler
}
func NewMonitorable(store *store.Store) *Monitorable {
m := &Monitorable{}
m.store = store
m.config = make(map[coreModels.VariantName]*githubConfig.Github)
// Load core config from env
pkgMonitorable.LoadConfig(&m.config, githubConfig.Default)
// Register Monitorable Tile in config manager
m.countTileEnabler = store.Registry.RegisterTile(api.GithubCountTileType, versions.MinimalVersion, m.GetVariantsNames())
m.checksTileEnabler = store.Registry.RegisterTile(api.GithubChecksTileType, versions.MinimalVersion, m.GetVariantsNames())
m.pullRequestTileEnabler = store.Registry.RegisterTile(api.GithubPullRequestTileType, versions.MinimalVersion, m.GetVariantsNames())
m.pullRequestGeneratorEnabler = store.Registry.RegisterGenerator(api.GithubPullRequestTileType, versions.MinimalVersion, m.GetVariantsNames())
return m
}
func (m *Monitorable) GetDisplayName() string {
return "GitHub"
}
func (m *Monitorable) GetVariantsNames() []coreModels.VariantName {
return pkgMonitorable.GetVariantsNames(m.config)
}
func (m *Monitorable) Validate(variantName coreModels.VariantName) (bool, []error) {
conf := m.config[variantName]
// No configuration set
if conf.URL == githubConfig.Default.URL && conf.Token == "" {
return false, nil
}
// Validate Config
if errors := pkgMonitorable.ValidateConfig(conf, variantName); errors != nil {
return false, errors
}
return true, nil
}
func (m *Monitorable) Enable(variantName coreModels.VariantName) {
conf := m.config[variantName]
// Custom UpstreamCacheExpiration only for count because github has no-cache for this query and the rate limit is 30req/Hour
countCacheExpiration := time.Millisecond * time.Duration(conf.CountCacheExpiration)
repository := githubRepository.NewGithubRepository(conf)
usecase := githubUsecase.NewGithubUsecase(repository)
delivery := githubDelivery.NewGithubDelivery(usecase)
// EnableTile route to echo
routeGroup := m.store.MonitorableRouter.Group("/github", variantName)
routeCount := routeGroup.GET("/count", delivery.GetCount, options.WithCustomCacheExpiration(countCacheExpiration))
routeChecks := routeGroup.GET("/checks", delivery.GetChecks)
routePullRequest := routeGroup.GET("/pullrequest", delivery.GetPullRequest)
// EnableTile data for config hydration
m.countTileEnabler.Enable(variantName, &githubModels.CountParams{}, routeCount.Path)
m.checksTileEnabler.Enable(variantName, &githubModels.ChecksParams{}, routeChecks.Path)
m.pullRequestTileEnabler.Enable(variantName, &githubModels.PullRequestParams{}, routePullRequest.Path)
m.pullRequestGeneratorEnabler.Enable(variantName, &githubModels.PullRequestGeneratorParams{}, usecase.PullRequestsGenerator)
}