|
1 | 1 | package interactor |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
| 6 | + "time" |
5 | 7 |
|
6 | 8 | evbus "github.com/asaskevich/EventBus" |
7 | 9 | "go.uber.org/zap" |
8 | 10 | ) |
9 | 11 |
|
10 | | -type ( |
11 | | - Interactor struct { |
12 | | - Store |
13 | | - SCM |
14 | | - |
15 | | - // The channel to stop background workers. |
16 | | - stopCh chan struct{} |
17 | | - |
18 | | - common *service |
19 | | - |
20 | | - // services used for talking to different parts of the entities. |
21 | | - *ConfigInteractor |
22 | | - *DeploymentInteractor |
23 | | - *DeploymentStatisticsInteractor |
24 | | - *EventInteractor |
25 | | - *LicenseInteractor |
26 | | - *LockInteractor |
27 | | - *RepoInteractor |
28 | | - *ReviewInteractor |
29 | | - *UserInteractor |
30 | | - } |
| 12 | +type InteractorConfig struct { |
| 13 | + ServerHost string |
| 14 | + ServerProto string |
| 15 | + ServerProxyHost string |
| 16 | + ServerProxyProto string |
31 | 17 |
|
32 | | - InteractorConfig struct { |
33 | | - ServerHost string |
34 | | - ServerProto string |
35 | | - ServerProxyHost string |
36 | | - ServerProxyProto string |
| 18 | + OrgEntries []string |
| 19 | + MemberEntries []string |
| 20 | + AdminUsers []string |
37 | 21 |
|
38 | | - OrgEntries []string |
39 | | - MemberEntries []string |
40 | | - AdminUsers []string |
| 22 | + WebhookSecret string |
41 | 23 |
|
42 | | - WebhookSecret string |
| 24 | + LicenseKey string |
43 | 25 |
|
44 | | - LicenseKey string |
| 26 | + Store |
| 27 | + SCM |
| 28 | +} |
45 | 29 |
|
46 | | - Store |
47 | | - SCM |
| 30 | +func (c *InteractorConfig) BuildWebhookURL() string { |
| 31 | + if c.ServerProxyProto != "" && c.ServerProxyHost != "" { |
| 32 | + return fmt.Sprintf("%s://%s/hooks", c.ServerProxyProto, c.ServerProxyHost) |
48 | 33 | } |
49 | 34 |
|
50 | | - service struct { |
51 | | - store Store |
52 | | - scm SCM |
53 | | - log *zap.Logger |
| 35 | + return fmt.Sprintf("%s://%s/hooks", c.ServerProto, c.ServerHost) |
| 36 | +} |
| 37 | + |
| 38 | +func (c *InteractorConfig) CheckWebhookSSL() bool { |
| 39 | + if c.ServerProxyProto != "" && c.ServerProxyHost != "" { |
| 40 | + return c.ServerProxyProto == "https" |
54 | 41 | } |
55 | | -) |
| 42 | + |
| 43 | + return c.ServerProto == "https" |
| 44 | +} |
| 45 | + |
| 46 | +type service struct { |
| 47 | + store Store |
| 48 | + scm SCM |
| 49 | + log *zap.Logger |
| 50 | +} |
| 51 | + |
| 52 | +type Interactor struct { |
| 53 | + Store |
| 54 | + SCM |
| 55 | + |
| 56 | + // The channel to stop background workers. |
| 57 | + stopCh chan struct{} |
| 58 | + |
| 59 | + common *service |
| 60 | + |
| 61 | + // services used for talking to different parts of the entities. |
| 62 | + *ConfigInteractor |
| 63 | + *DeploymentInteractor |
| 64 | + *DeploymentStatisticsInteractor |
| 65 | + *EventInteractor |
| 66 | + *LicenseInteractor |
| 67 | + *LockInteractor |
| 68 | + *RepoInteractor |
| 69 | + *ReviewInteractor |
| 70 | + *UserInteractor |
| 71 | + *PermInteractor |
| 72 | +} |
56 | 73 |
|
57 | 74 | func NewInteractor(c *InteractorConfig) *Interactor { |
| 75 | + log := zap.L().Named("interactor") |
| 76 | + defer log.Sync() |
| 77 | + |
58 | 78 | i := &Interactor{ |
59 | 79 | Store: c.Store, |
60 | 80 | SCM: c.SCM, |
61 | 81 | stopCh: make(chan struct{}), |
62 | 82 | } |
63 | 83 |
|
64 | | - log := zap.L().Named("interactor") |
65 | | - |
66 | 84 | i.common = &service{ |
67 | 85 | store: c.Store, |
68 | 86 | scm: c.SCM, |
@@ -94,37 +112,38 @@ func NewInteractor(c *InteractorConfig) *Interactor { |
94 | 112 | orgEntries: c.OrgEntries, |
95 | 113 | memberEntries: c.MemberEntries, |
96 | 114 | } |
| 115 | + i.PermInteractor = &PermInteractor{ |
| 116 | + service: i.common, |
| 117 | + orgEntries: c.OrgEntries, |
| 118 | + } |
| 119 | + |
| 120 | + return i |
| 121 | +} |
| 122 | + |
| 123 | +func (i *Interactor) Init() { |
| 124 | + log := zap.L().Named("interactor") |
| 125 | + defer log.Sync() |
| 126 | + |
| 127 | + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) |
| 128 | + defer cancel() |
| 129 | + |
| 130 | + log.Debug("Resync organization entries.") |
| 131 | + if err := i.ResyncPerms(ctx); err != nil { |
| 132 | + log.Fatal("Failed to resynchronize with the perms.", zap.Error(err)) |
| 133 | + } |
97 | 134 |
|
98 | 135 | go func() { |
99 | | - log.Info("Start the working publishing events.") |
| 136 | + log.Debug("Start the working publishing events.") |
100 | 137 | i.runPublishingEvents(i.stopCh) |
101 | 138 | }() |
102 | 139 |
|
103 | 140 | go func() { |
104 | | - log.Info("Start the worker canceling inactive deployments.") |
| 141 | + log.Debug("Start the worker canceling inactive deployments.") |
105 | 142 | i.runClosingInactiveDeployment(i.stopCh) |
106 | 143 | }() |
107 | 144 |
|
108 | 145 | go func() { |
109 | | - log.Info("Start the worker for the auto unlock.") |
| 146 | + log.Debug("Start the worker for the auto unlock.") |
110 | 147 | i.runAutoUnlock(i.stopCh) |
111 | 148 | }() |
112 | | - |
113 | | - return i |
114 | | -} |
115 | | - |
116 | | -func (c *InteractorConfig) BuildWebhookURL() string { |
117 | | - if c.ServerProxyProto != "" && c.ServerProxyHost != "" { |
118 | | - return fmt.Sprintf("%s://%s/hooks", c.ServerProxyProto, c.ServerProxyHost) |
119 | | - } |
120 | | - |
121 | | - return fmt.Sprintf("%s://%s/hooks", c.ServerProto, c.ServerHost) |
122 | | -} |
123 | | - |
124 | | -func (c *InteractorConfig) CheckWebhookSSL() bool { |
125 | | - if c.ServerProxyProto != "" && c.ServerProxyHost != "" { |
126 | | - return c.ServerProxyProto == "https" |
127 | | - } |
128 | | - |
129 | | - return c.ServerProto == "https" |
130 | 149 | } |
0 commit comments