forked from leg100/otf
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathservice.go
More file actions
196 lines (169 loc) · 4.35 KB
/
service.go
File metadata and controls
196 lines (169 loc) · 4.35 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package github
import (
"context"
"errors"
"fmt"
"log/slog"
"github.com/gorilla/mux"
"github.com/tofutf/tofutf/internal"
"github.com/tofutf/tofutf/internal/http/html"
"github.com/tofutf/tofutf/internal/organization"
"github.com/tofutf/tofutf/internal/rbac"
"github.com/tofutf/tofutf/internal/sql"
"github.com/tofutf/tofutf/internal/vcs"
)
type (
// Service is the service for github app management
Service struct {
logger *slog.Logger
GithubHostname string
site internal.Authorizer
organization internal.Authorizer
db *pgdb
web *webHandlers
}
Options struct {
*sql.Pool
html.Renderer
vcs.Publisher
*internal.HostnameService
Logger *slog.Logger
GithubHostname string
SkipTLSVerification bool
}
)
func NewService(opts Options) *Service {
svc := Service{
logger: opts.Logger,
GithubHostname: opts.GithubHostname,
site: &internal.SiteAuthorizer{Logger: opts.Logger},
organization: &organization.Authorizer{Logger: opts.Logger},
db: &pgdb{opts.Pool},
}
svc.web = &webHandlers{
Renderer: opts.Renderer,
HostnameService: opts.HostnameService,
GithubHostname: opts.GithubHostname,
GithubSkipTLS: opts.SkipTLSVerification,
svc: &svc,
}
return &svc
}
func (a *Service) AddHandlers(r *mux.Router) {
a.web.addHandlers(r)
}
func (a *Service) CreateApp(ctx context.Context, opts CreateAppOptions) (*App, error) {
subject, err := a.site.CanAccess(ctx, rbac.CreateGithubAppAction, "")
if err != nil {
return nil, err
}
app := newApp(opts)
if err := a.db.create(ctx, app); err != nil {
a.logger.Error("creating github app", "app", app, "subject", subject, "err", err)
return nil, err
}
a.logger.Info("created github app", "app", app, "subject", subject)
return app, nil
}
func (a *Service) GetApp(ctx context.Context) (*App, error) {
subject, err := a.site.CanAccess(ctx, rbac.GetGithubAppAction, "")
if err != nil {
return nil, err
}
app, err := a.db.get(ctx)
if errors.Is(err, internal.ErrResourceNotFound) {
return nil, nil
} else if err != nil {
return nil, err
}
a.logger.Debug("retrieved github app", "app", app, "subject", subject)
return app, nil
}
func (a *Service) DeleteApp(ctx context.Context) error {
subject, err := a.site.CanAccess(ctx, rbac.DeleteGithubAppAction, "")
if err != nil {
return err
}
err = a.db.delete(ctx)
if err != nil {
a.logger.Error("deleting github app", "subject", subject, "err", err)
return err
}
a.logger.Info("deleted github app", "subject", subject)
return nil
}
func (a *Service) ListInstallations(ctx context.Context) ([]*Installation, error) {
app, err := a.db.get(ctx)
if errors.Is(err, internal.ErrResourceNotFound) {
return nil, nil
} else if err != nil {
return nil, err
}
client, err := a.newClient(app)
if err != nil {
return nil, err
}
from, err := client.ListInstallations(ctx)
if err != nil {
return nil, err
}
to := make([]*Installation, len(from))
for i, f := range from {
to[i] = &Installation{Installation: f}
}
return to, nil
}
func (a *Service) GetInstallCredentials(ctx context.Context, installID int64) (*InstallCredentials, error) {
app, err := a.db.get(ctx)
if err != nil {
return nil, err
}
client, err := a.newClient(app)
if err != nil {
return nil, err
}
install, err := client.GetInstallation(ctx, installID)
if err != nil {
return nil, err
}
creds := InstallCredentials{
ID: installID,
AppCredentials: AppCredentials{
ID: app.ID,
PrivateKey: app.PrivateKey,
},
}
switch install.GetTargetType() {
case "Organization":
creds.Organization = install.GetAccount().Login
case "User":
creds.User = install.GetAccount().Login
default:
return nil, fmt.Errorf("unexpected target type: %s", install.GetTargetType())
}
return &creds, nil
}
func (a *Service) DeleteInstallation(ctx context.Context, installID int64) error {
app, err := a.db.get(ctx)
if err != nil {
return err
}
client, err := a.newClient(app)
if err != nil {
return err
}
if err := client.DeleteInstallation(ctx, installID); err != nil {
return err
}
return nil
}
func (a *Service) newClient(app *App) (*Client, error) {
return NewClient(ClientOptions{
Hostname: a.GithubHostname,
SkipTLSVerification: true,
AppCredentials: &AppCredentials{
ID: app.ID,
PrivateKey: app.PrivateKey,
},
})
}