-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration.go
More file actions
500 lines (441 loc) · 16.5 KB
/
Copy pathintegration.go
File metadata and controls
500 lines (441 loc) · 16.5 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package main
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"strings"
plugin "github.com/Paca-AI/plugin-sdk-go"
)
// ─── DTOs ────────────────────────────────────────────────────────────────────
type integrationResponse struct {
ProjectID string `json:"project_id"`
Connected bool `json:"connected"`
CreatedAt *string `json:"created_at,omitempty"`
UpdatedAt *string `json:"updated_at,omitempty"`
}
// accessibleRepoResponse is returned by GET /integration/accessible-repos.
// It reflects data fetched live from the GitHub API.
type accessibleRepoResponse struct {
FullName string `json:"full_name"`
Owner string `json:"owner"`
RepoName string `json:"repo_name"`
DefaultBranch string `json:"default_branch"`
Private bool `json:"private"`
}
// repositoryResponse is the canonical DTO for a project-linked repository.
// Returned by GET /repositories and POST /repositories.
type repositoryResponse struct {
ID string `json:"id"`
ProjectID string `json:"project_id"`
Owner string `json:"owner"`
RepoName string `json:"repo_name"`
FullName string `json:"full_name"`
DefaultBranch string `json:"default_branch"`
CloneURL string `json:"clone_url"`
WebhookActive bool `json:"webhook_active"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// repoCloneInfo is returned by GET /repositories/:repoId/clone-info.
// It includes a short-lived token for cloning.
type repoCloneInfo struct {
ID string `json:"id"`
FullName string `json:"full_name"`
Owner string `json:"owner"`
RepoName string `json:"repo_name"`
CloneURL string `json:"clone_url"`
Token string `json:"token"`
ExpiresAt float64 `json:"expires_at"`
}
const githubPluginID = "com.paca.github"
func webhookURLFromPublicURL(cfg *plugin.Config, projectID string) (string, error) {
publicURL, ok := cfg.Get("PUBLIC_URL")
if !ok || strings.TrimSpace(publicURL) == "" {
return "", errors.New("PUBLIC_URL is not configured")
}
base := strings.TrimRight(strings.TrimSpace(publicURL), "/")
return fmt.Sprintf("%s/api/v1/plugins/%s/projects/%s/webhook", base, githubPluginID, projectID), nil
}
// ─── Helper: decrypt PAT for a project ───────────────────────────────────────
func (p *githubPlugin) decryptToken(projectID string) (string, error) {
result, err := p.db.Query(
`SELECT access_token_enc FROM github_integrations WHERE project_id = $1`,
projectID,
)
if err != nil {
return "", err
}
if len(result.Rows) == 0 {
return "", &appError{code: "GITHUB_INTEGRATION_NOT_FOUND", status: 404, msg: "GitHub integration not found"}
}
enc := newRowScanner(result.Columns, result.Rows[0]).str("access_token_enc")
return p.decrypt(enc)
}
// ─── GET /integration ────────────────────────────────────────────────────────
func (p *githubPlugin) getIntegration(req *plugin.Request, res *plugin.Response) {
projectID := req.Caller.ProjectID
result, err := p.db.Query(
`SELECT project_id, created_at, updated_at FROM github_integrations WHERE project_id = $1`,
projectID,
)
if err != nil {
apiError(res, 500, "INTERNAL_ERROR", err.Error())
return
}
if len(result.Rows) == 0 {
ok(res, integrationResponse{ProjectID: projectID, Connected: false})
return
}
sc := newRowScanner(result.Columns, result.Rows[0])
ca := sc.str("created_at")
ua := sc.str("updated_at")
ok(res, integrationResponse{
ProjectID: sc.str("project_id"),
Connected: true,
CreatedAt: &ca,
UpdatedAt: &ua,
})
}
// ─── POST /integration/token ─────────────────────────────────────────────────
func (p *githubPlugin) setToken(req *plugin.Request, res *plugin.Response) {
projectID := req.Caller.ProjectID
type bodyT struct {
Token string `json:"token"`
}
b, err := plugin.JSONBody[bodyT](req)
if err != nil || b.Token == "" {
apiError(res, 400, "BAD_REQUEST", "token is required")
return
}
// Validate against GitHub API.
ghc := newGHClient(b.Token)
if err := ghc.validateToken(context.Background()); err != nil {
var apiErr *ghAPIError
if errors.As(err, &apiErr) && (apiErr.StatusCode == 401 || apiErr.StatusCode == 403) {
apiError(res, 422, "GITHUB_INVALID_TOKEN", "GitHub token is invalid or expired")
return
}
apiError(res, 502, "INTERNAL_ERROR", fmt.Sprintf("failed to validate token: %s", err))
return
}
enc, err := p.encrypt(b.Token)
if err != nil {
apiError(res, 500, "INTERNAL_ERROR", "failed to encrypt token")
return
}
now := nowStr()
_, err = p.db.Exec(`
INSERT INTO github_integrations (project_id, access_token_enc, created_at, updated_at)
VALUES ($1, $2, $3, $4)
ON CONFLICT (project_id) DO UPDATE SET access_token_enc = EXCLUDED.access_token_enc, updated_at = EXCLUDED.updated_at
`, projectID, enc, now, now)
if err != nil {
apiError(res, 500, "INTERNAL_ERROR", err.Error())
return
}
// Fetch back the record.
result, qErr := p.db.Query(
`SELECT project_id, created_at, updated_at FROM github_integrations WHERE project_id = $1`,
projectID,
)
if qErr != nil || len(result.Rows) == 0 {
ok(res, integrationResponse{ProjectID: projectID, Connected: true})
return
}
sc := newRowScanner(result.Columns, result.Rows[0])
ca := sc.str("created_at")
ua := sc.str("updated_at")
ok(res, integrationResponse{
ProjectID: sc.str("project_id"),
Connected: true,
CreatedAt: &ca,
UpdatedAt: &ua,
})
}
// ─── DELETE /integration/token ───────────────────────────────────────────────
func (p *githubPlugin) deleteToken(req *plugin.Request, res *plugin.Response) {
projectID := req.Caller.ProjectID
// Best-effort: delete webhooks for all linked repositories first.
token, tokenErr := p.decryptToken(projectID)
if tokenErr == nil {
ghc := newGHClient(token)
rows, qErr := p.db.Query(
`SELECT owner, repo_name, webhook_id FROM github_repositories WHERE project_id = $1`,
projectID,
)
if qErr == nil {
for _, row := range rows.Rows {
sc := newRowScanner(rows.Columns, row)
whID := sc.int64Val("webhook_id")
if whID > 0 {
_ = ghc.deleteWebhook(context.Background(), sc.str("owner"), sc.str("repo_name"), whID)
}
}
}
}
_, execErr := p.db.Exec(`DELETE FROM github_integrations WHERE project_id = $1`, projectID)
if execErr != nil {
apiError(res, 500, "INTERNAL_ERROR", execErr.Error())
return
}
noContent(res)
}
// ─── GET /integration/accessible-repos ──────────────────────────────────────
func (p *githubPlugin) listAccessibleRepos(req *plugin.Request, res *plugin.Response) {
projectID := req.Caller.ProjectID
token, err := p.decryptToken(projectID)
if err != nil {
writeAppError(res, err)
return
}
ghc := newGHClient(token)
repos, err := ghc.listRepositories(context.Background())
if err != nil {
apiError(res, 502, "INTERNAL_ERROR", fmt.Sprintf("failed to list repositories: %s", err))
return
}
items := make([]accessibleRepoResponse, len(repos))
for i, r := range repos {
items[i] = accessibleRepoResponse{
FullName: r.FullName,
Owner: r.Owner.Login,
RepoName: r.Name,
DefaultBranch: r.DefaultBranch,
Private: r.Private,
}
}
ok(res, items)
}
// ─── GET /repositories ───────────────────────────────────────────────────────
func (p *githubPlugin) listRepositories(req *plugin.Request, res *plugin.Response) {
projectID := req.Caller.ProjectID
result, err := p.db.Query(`
SELECT id, project_id, owner, repo_name, full_name, default_branch, webhook_id, created_at, updated_at
FROM github_repositories WHERE project_id = $1 ORDER BY created_at ASC
`, projectID)
if err != nil {
apiError(res, 500, "INTERNAL_ERROR", err.Error())
return
}
items := make([]repositoryResponse, 0, len(result.Rows))
for _, row := range result.Rows {
sc := newRowScanner(result.Columns, row)
fullName := sc.str("full_name")
items = append(items, repositoryResponse{
ID: sc.str("id"),
ProjectID: sc.str("project_id"),
Owner: sc.str("owner"),
RepoName: sc.str("repo_name"),
FullName: fullName,
DefaultBranch: sc.str("default_branch"),
CloneURL: "https://github.com/" + fullName + ".git",
WebhookActive: sc.int64Val("webhook_id") > 0,
CreatedAt: sc.str("created_at"),
UpdatedAt: sc.str("updated_at"),
})
}
ok(res, items)
}
// ─── POST /repositories ───────────────────────────────────────────────────────
func (p *githubPlugin) linkRepository(req *plugin.Request, res *plugin.Response) {
projectID := req.Caller.ProjectID
type bodyT struct {
Owner string `json:"owner"`
RepoName string `json:"repo_name"`
}
b, err := plugin.JSONBody[bodyT](req)
if err != nil || b.Owner == "" || b.RepoName == "" {
apiError(res, 400, "BAD_REQUEST", "owner and repo_name are required")
return
}
webhookURL, err := webhookURLFromPublicURL(p.cfg, projectID)
if err != nil {
apiError(res, 422, "GITHUB_WEBHOOK_URL_REQUIRED", "PUBLIC_URL is not configured")
return
}
token, err := p.decryptToken(projectID)
if err != nil {
writeAppError(res, err)
return
}
ghc := newGHClient(token)
ghRepo, err := ghc.getRepository(context.Background(), b.Owner, b.RepoName)
if err != nil {
var apiErr *ghAPIError
if errors.As(err, &apiErr) && (apiErr.StatusCode == 403 || apiErr.StatusCode == 404) {
apiError(res, 422, "GITHUB_REPO_NOT_ACCESSIBLE", "Repository not accessible with the provided token")
return
}
apiError(res, 502, "INTERNAL_ERROR", fmt.Sprintf("failed to get repository: %s", err))
return
}
// Check if already linked.
existResult, _ := p.db.Query(
`SELECT id FROM github_repositories WHERE project_id = $1 AND full_name = $2`,
projectID, ghRepo.FullName,
)
if existResult != nil && len(existResult.Rows) > 0 {
apiError(res, 409, "GITHUB_REPO_ALREADY_LINKED", "Repository is already linked to this project")
return
}
// Fetch integration ID.
integResult, iErr := p.db.Query(`SELECT id FROM github_integrations WHERE project_id = $1`, projectID)
if iErr != nil || len(integResult.Rows) == 0 {
apiError(res, 404, "GITHUB_INTEGRATION_NOT_FOUND", "GitHub integration not found")
return
}
integrationID := newRowScanner(integResult.Columns, integResult.Rows[0]).str("id")
// Generate webhook secret.
secretBytes := make([]byte, 32)
if _, randErr := rand.Read(secretBytes); randErr != nil {
apiError(res, 500, "INTERNAL_ERROR", "failed to generate webhook secret")
return
}
webhookSecret := hex.EncodeToString(secretBytes)
webhookID, err := ghc.createWebhook(context.Background(), ghRepo.Owner.Login, ghRepo.Name, webhookURL, webhookSecret,
[]string{"push", "pull_request", "check_run"})
if err != nil {
var apiErr *ghAPIError
if errors.As(err, &apiErr) && isWebhookURLNotPublic(apiErr) {
apiError(res, 422, "GITHUB_WEBHOOK_URL_NOT_PUBLIC", "Webhook URL is not publicly accessible")
return
}
apiError(res, 502, "GITHUB_WEBHOOK_CREATION_FAILED", fmt.Sprintf("failed to create webhook: %s", err))
return
}
encSecret, err := p.encrypt(webhookSecret)
if err != nil {
_ = ghc.deleteWebhook(context.Background(), ghRepo.Owner.Login, ghRepo.Name, webhookID)
apiError(res, 500, "INTERNAL_ERROR", "failed to encrypt webhook secret")
return
}
now := nowStr()
inserted, dbErr := p.db.Query(`
INSERT INTO github_repositories
(project_id, integration_id, owner, repo_name, full_name, webhook_id, webhook_secret_enc, default_branch, created_at, updated_at)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$9)
RETURNING id
`, projectID, integrationID, ghRepo.Owner.Login, ghRepo.Name, ghRepo.FullName, webhookID, encSecret, ghRepo.DefaultBranch, now)
if dbErr != nil || len(inserted.Rows) == 0 {
_ = ghc.deleteWebhook(context.Background(), ghRepo.Owner.Login, ghRepo.Name, webhookID)
if dbErr != nil {
apiError(res, 500, "INTERNAL_ERROR", dbErr.Error())
} else {
apiError(res, 500, "INTERNAL_ERROR", "insert returned no rows")
}
return
}
repoID := newRowScanner(inserted.Columns, inserted.Rows[0]).str("id")
created(res, repositoryResponse{
ID: repoID,
ProjectID: projectID,
Owner: ghRepo.Owner.Login,
RepoName: ghRepo.Name,
FullName: ghRepo.FullName,
DefaultBranch: ghRepo.DefaultBranch,
CloneURL: "https://github.com/" + ghRepo.FullName + ".git",
WebhookActive: webhookID > 0,
CreatedAt: now,
UpdatedAt: now,
})
}
// ─── DELETE /repositories/:repoId ────────────────────────────────────────────
func (p *githubPlugin) unlinkRepository(req *plugin.Request, res *plugin.Response) {
projectID := req.Caller.ProjectID
repoID := req.PathParam("repoId")
// Fetch repository details to delete webhook.
result, err := p.db.Query(
`SELECT owner, repo_name, webhook_id FROM github_repositories WHERE id = $1 AND project_id = $2`,
repoID, projectID,
)
if err != nil {
apiError(res, 500, "INTERNAL_ERROR", err.Error())
return
}
if len(result.Rows) == 0 {
apiError(res, 404, "GITHUB_REPOSITORY_NOT_FOUND", "Repository not found")
return
}
sc := newRowScanner(result.Columns, result.Rows[0])
owner := sc.str("owner")
repoName := sc.str("repo_name")
webhookID := sc.int64Val("webhook_id")
// Best-effort: delete the webhook.
if webhookID > 0 {
if token, tErr := p.decryptToken(projectID); tErr == nil {
ghc := newGHClient(token)
_ = ghc.deleteWebhook(context.Background(), owner, repoName, webhookID)
}
}
_, err = p.db.Exec(`DELETE FROM github_repositories WHERE id = $1`, repoID)
if err != nil {
apiError(res, 500, "INTERNAL_ERROR", err.Error())
return
}
noContent(res)
}
// ─── GET /repositories/:repoId/clone-info ────────────────────────────────────
func (p *githubPlugin) getRepoCloneInfo(req *plugin.Request, res *plugin.Response) {
projectID := req.Caller.ProjectID
repoID := req.PathParam("repoId")
if repoID == "" {
apiError(res, 400, "BAD_REQUEST", "repoId path parameter is required")
return
}
result, err := p.db.Query(
`SELECT id, full_name, owner, repo_name FROM github_repositories WHERE project_id = $1 AND id = $2`,
projectID, repoID,
)
if err != nil {
apiError(res, 500, "INTERNAL_ERROR", err.Error())
return
}
if len(result.Rows) == 0 {
apiError(res, 404, "GITHUB_REPOSITORY_NOT_FOUND", "repository not found")
return
}
token, err := p.decryptToken(projectID)
if err != nil {
writeAppError(res, err)
return
}
sc := newRowScanner(result.Columns, result.Rows[0])
fullName := sc.str("full_name")
ok(res, repoCloneInfo{
ID: sc.str("id"),
FullName: fullName,
Owner: sc.str("owner"),
RepoName: sc.str("repo_name"),
CloneURL: "https://github.com/" + fullName + ".git",
Token: token,
ExpiresAt: 0,
})
}
// ─── GET /github/repo-info (REMOVED) ─────────────────────────────────────────
// Replaced by GET /repositories (list all) and GET /repositories/:repoId/clone-info
// ─── Error helpers ────────────────────────────────────────────────────────────
type appError struct {
code string
status int
msg string
}
func (e *appError) Error() string { return e.msg }
func writeAppError(res *plugin.Response, err error) {
var ae *appError
if errors.As(err, &ae) {
apiError(res, ae.status, ae.code, ae.msg)
return
}
apiError(res, 500, "INTERNAL_ERROR", err.Error())
}
func isWebhookURLNotPublic(apiErr *ghAPIError) bool {
if apiErr == nil || apiErr.StatusCode != 422 {
return false
}
msg := strings.ToLower(apiErr.Message + " " + apiErr.Details)
return strings.Contains(msg, "isn't reachable over the public internet") ||
strings.Contains(msg, "not publicly reachable") ||
strings.Contains(msg, "localhost")
}