/
integration.go
127 lines (109 loc) · 2.87 KB
/
integration.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
package giteaintegration
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"time"
"code.gitea.io/sdk/gitea"
"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pb"
"github.com/traPtitech/neoshowcase/pkg/util/loop"
"github.com/traPtitech/neoshowcase/pkg/util/retry"
"github.com/traPtitech/neoshowcase/pkg/util/scutil"
)
type Config struct {
URL string
Token string
IntervalSeconds int
Concurrency int
}
func (c *Config) Validate() error {
if c.Token == "" {
return fmt.Errorf("provide admin gitea token (got empty string)")
}
if c.IntervalSeconds <= 0 {
return fmt.Errorf("provide positive interval seconds (got %v)", c.IntervalSeconds)
}
if c.Concurrency <= 0 {
return fmt.Errorf("provide positive concurrency (got %v)", c.Concurrency)
}
return nil
}
type Integration struct {
c *gitea.Client
interval time.Duration
concurrency int
controller domain.ControllerGiteaIntegrationServiceClient
gitRepo domain.GitRepositoryRepository
appRepo domain.ApplicationRepository
userRepo domain.UserRepository
cancel func()
syncer *scutil.Coalescer
}
func NewIntegration(
c Config,
controller domain.ControllerGiteaIntegrationServiceClient,
gitRepo domain.GitRepositoryRepository,
appRepo domain.ApplicationRepository,
userRepo domain.UserRepository,
) (*Integration, error) {
// Validate config
if err := c.Validate(); err != nil {
return nil, err
}
// Generate gitea client
client, err := gitea.NewClient(
c.URL,
gitea.SetToken(c.Token),
// Skip version check (traP fork versioned such as 'traP-1.19.3-2' regarded "Malformed version")
gitea.SetGiteaVersion(""),
)
if err != nil {
return nil, err
}
i := &Integration{
c: client,
interval: time.Duration(c.IntervalSeconds) * time.Second,
concurrency: c.Concurrency,
controller: controller,
gitRepo: gitRepo,
appRepo: appRepo,
userRepo: userRepo,
}
i.syncer = scutil.NewCoalescer(i.syncAndLog)
return i, nil
}
func (i *Integration) Start() error {
ctx, cancel := context.WithCancel(context.Background())
i.cancel = cancel
go retry.Do(ctx, func(ctx context.Context) error {
return i.controller.Connect(ctx, i.onRequest(ctx))
}, "connect to controller")
go loop.Loop(ctx, func(ctx context.Context) {
_ = i.syncer.Do(ctx)
}, i.interval, true)
return nil
}
func (i *Integration) onRequest(ctx context.Context) func(req *pb.GiteaIntegrationRequest) {
return func(req *pb.GiteaIntegrationRequest) {
switch req.Type {
case pb.GiteaIntegrationRequest_RESYNC:
go func() {
_ = i.syncer.Do(ctx)
}()
}
}
}
func (i *Integration) syncAndLog(ctx context.Context) error {
err := i.sync(ctx)
if err != nil {
log.Errorf("failed to sync: %+v", err)
}
return nil
}
func (i *Integration) Shutdown() error {
if i.cancel != nil {
i.cancel()
}
return nil
}