Skip to content

Commit 114c8f4

Browse files
committed
refactor: remove uuid dependency and simplify database operations in GitHub integration
1 parent 8e8f942 commit 114c8f4

6 files changed

Lines changed: 73 additions & 102 deletions

File tree

backend/branches.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77

88
plugin "github.com/Paca-AI/plugin-sdk-go"
9-
"github.com/google/uuid"
109
)
1110

1211
// ─── DTOs ────────────────────────────────────────────────────────────────────
@@ -86,12 +85,11 @@ func (p *githubPlugin) createBranch(req *plugin.Request, res *plugin.Response) {
8685

8786
// Link the branch to the task.
8887
now := nowStr()
89-
branchID := uuid.New().String()
9088
rowsAffected, dbErr := p.db.Exec(`
91-
INSERT INTO github_task_branches (id, task_id, repo_id, branch_name, created_at)
92-
VALUES ($1,$2,$3,$4,$5)
89+
INSERT INTO github_task_branches (task_id, repo_id, branch_name, created_at)
90+
VALUES ($1,$2,$3,$4)
9391
ON CONFLICT (task_id, repo_id, branch_name) DO NOTHING
94-
`, branchID, taskID, b.RepoID, b.BranchName, now)
92+
`, taskID, b.RepoID, b.BranchName, now)
9593
if dbErr != nil {
9694
p.log.Error("failed to link branch to task: " + dbErr.Error())
9795
} else if rowsAffected == 0 {

backend/go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@ module github.com/Paca-AI/first-party/github
22

33
go 1.24
44

5-
require (
6-
github.com/Paca-AI/plugin-sdk-go v0.2.0
7-
github.com/google/uuid v1.6.0
8-
)
5+
require github.com/Paca-AI/plugin-sdk-go v0.2.0

backend/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
github.com/Paca-AI/plugin-sdk-go v0.2.0 h1:Fur6p+OQoC5imq7qmvaQtJnZ3SRVRskx8KcT/rqFHj4=
22
github.com/Paca-AI/plugin-sdk-go v0.2.0/go.mod h1:5WeC6cSEf2wM1ovICZbDaVky9oi5id/Qpdfc5LDAQnw=
3-
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4-
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

backend/integration.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010

1111
plugin "github.com/Paca-AI/plugin-sdk-go"
12-
"github.com/google/uuid"
1312
)
1413

1514
// ─── DTOs ────────────────────────────────────────────────────────────────────
@@ -358,17 +357,22 @@ func (p *githubPlugin) linkRepository(req *plugin.Request, res *plugin.Response)
358357
}
359358

360359
now := nowStr()
361-
repoID := uuid.New().String()
362-
_, dbErr := p.db.Exec(`
360+
inserted, dbErr := p.db.Query(`
363361
INSERT INTO github_repositories
364-
(id, project_id, integration_id, owner, repo_name, full_name, webhook_id, webhook_secret_enc, default_branch, created_at, updated_at)
365-
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$10)
366-
`, repoID, projectID, integrationID, ghRepo.Owner.Login, ghRepo.Name, ghRepo.FullName, webhookID, encSecret, ghRepo.DefaultBranch, now)
367-
if dbErr != nil {
362+
(project_id, integration_id, owner, repo_name, full_name, webhook_id, webhook_secret_enc, default_branch, created_at, updated_at)
363+
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$9)
364+
RETURNING id
365+
`, projectID, integrationID, ghRepo.Owner.Login, ghRepo.Name, ghRepo.FullName, webhookID, encSecret, ghRepo.DefaultBranch, now)
366+
if dbErr != nil || len(inserted.Rows) == 0 {
368367
_ = ghc.deleteWebhook(context.Background(), ghRepo.Owner.Login, ghRepo.Name, webhookID)
369-
apiError(res, 500, "INTERNAL_ERROR", dbErr.Error())
368+
if dbErr != nil {
369+
apiError(res, 500, "INTERNAL_ERROR", dbErr.Error())
370+
} else {
371+
apiError(res, 500, "INTERNAL_ERROR", "insert returned no rows")
372+
}
370373
return
371374
}
375+
repoID := newRowScanner(inserted.Columns, inserted.Rows[0]).str("id")
372376

373377
created(res, repositoryResponse{
374378
ID: repoID,

backend/pull_requests.go

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77

88
plugin "github.com/Paca-AI/plugin-sdk-go"
9-
"github.com/google/uuid"
109
)
1110

1211
// ─── DTOs ────────────────────────────────────────────────────────────────────
@@ -135,50 +134,41 @@ func (p *githubPlugin) linkPRToTask(req *plugin.Request, res *plugin.Response) {
135134

136135
now := nowStr()
137136

138-
// Check if PR already cached.
139-
existResult, _ := p.db.Query(
140-
`SELECT id, created_at FROM github_pull_requests WHERE repo_id = $1 AND pr_number = $2`,
141-
b.RepoID, b.PRNumber,
142-
)
143-
var prID string
144-
var prCreatedAt string
145-
if existResult != nil && len(existResult.Rows) > 0 {
146-
eSc := newRowScanner(existResult.Columns, existResult.Rows[0])
147-
prID = eSc.str("id")
148-
prCreatedAt = eSc.str("created_at")
149-
} else {
150-
prID = uuid.New().String()
151-
prCreatedAt = now
152-
}
153-
154137
var mergedAtStr *string
155138
if ghPR.MergedAt != nil {
156139
s := ghPR.MergedAt.UTC().Format("2006-01-02T15:04:05.999999999Z")
157140
mergedAtStr = &s
158141
}
159142

160-
// Upsert the PR cache.
161-
_, err = p.db.Exec(`
143+
// Upsert the PR cache; let PostgreSQL generate id on insert, RETURNING gives us id+created_at.
144+
upserted, err := p.db.Query(`
162145
INSERT INTO github_pull_requests
163-
(id, project_id, repo_id, pr_number, github_pr_id, title, state, html_url, head_branch, base_branch, author, merged_at, created_at, updated_at)
164-
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14)
146+
(project_id, repo_id, pr_number, github_pr_id, title, state, html_url, head_branch, base_branch, author, merged_at, created_at, updated_at)
147+
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)
165148
ON CONFLICT (repo_id, pr_number) DO UPDATE SET
166-
title=$6, state=$7, html_url=$8, head_branch=$9, base_branch=$10,
167-
author=$11, merged_at=$12, updated_at=$14
168-
`, prID, projectID, b.RepoID, b.PRNumber, ghPR.ID, ghPR.Title, state,
169-
ghPR.HTMLURL, ghPR.Head.Ref, ghPR.Base.Ref, ghPR.User.Login, mergedAtStr, prCreatedAt, now)
170-
if err != nil {
171-
apiError(res, 500, "INTERNAL_ERROR", err.Error())
149+
title=$5, state=$6, html_url=$7, head_branch=$8, base_branch=$9,
150+
author=$10, merged_at=$11, updated_at=$13
151+
RETURNING id, created_at
152+
`, projectID, b.RepoID, b.PRNumber, ghPR.ID, ghPR.Title, state,
153+
ghPR.HTMLURL, ghPR.Head.Ref, ghPR.Base.Ref, ghPR.User.Login, mergedAtStr, now, now)
154+
if err != nil || len(upserted.Rows) == 0 {
155+
if err != nil {
156+
apiError(res, 500, "INTERNAL_ERROR", err.Error())
157+
} else {
158+
apiError(res, 500, "INTERNAL_ERROR", "upsert returned no rows")
159+
}
172160
return
173161
}
162+
prSc := newRowScanner(upserted.Columns, upserted.Rows[0])
163+
prID := prSc.str("id")
164+
prCreatedAt := prSc.str("created_at")
174165

175166
// Link the PR to the task.
176-
linkID := uuid.New().String()
177167
rowsAffected, lErr := p.db.Exec(`
178-
INSERT INTO github_task_pr_links (id, task_id, pull_request_id, created_at)
179-
VALUES ($1,$2,$3,$4)
168+
INSERT INTO github_task_pr_links (task_id, pull_request_id, created_at)
169+
VALUES ($1,$2,$3)
180170
ON CONFLICT (task_id, pull_request_id) DO NOTHING
181-
`, linkID, taskID, prID, now)
171+
`, taskID, prID, now)
182172
if lErr != nil {
183173
apiError(res, 500, "INTERNAL_ERROR", lErr.Error())
184174
return
@@ -279,34 +269,38 @@ func (p *githubPlugin) createPullRequest(req *plugin.Request, res *plugin.Respon
279269
}
280270

281271
now := nowStr()
282-
prID := uuid.New().String()
283272

284273
var mergedAtStr *string
285274
if ghPR.MergedAt != nil {
286275
s := ghPR.MergedAt.UTC().Format("2006-01-02T15:04:05.999999999Z")
287276
mergedAtStr = &s
288277
}
289278

290-
_, err = p.db.Exec(`
279+
upserted, err := p.db.Query(`
291280
INSERT INTO github_pull_requests
292-
(id, project_id, repo_id, pr_number, github_pr_id, title, state, html_url, head_branch, base_branch, author, merged_at, created_at, updated_at)
293-
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$13)
281+
(project_id, repo_id, pr_number, github_pr_id, title, state, html_url, head_branch, base_branch, author, merged_at, created_at, updated_at)
282+
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$12)
294283
ON CONFLICT (repo_id, pr_number) DO UPDATE SET
295-
title=$6, state=$7, html_url=$8, head_branch=$9, base_branch=$10,
296-
author=$11, merged_at=$12, updated_at=$13
297-
`, prID, projectID, b.RepoID, ghPR.Number, ghPR.ID, ghPR.Title, state,
284+
title=$5, state=$6, html_url=$7, head_branch=$8, base_branch=$9,
285+
author=$10, merged_at=$11, updated_at=$12
286+
RETURNING id
287+
`, projectID, b.RepoID, ghPR.Number, ghPR.ID, ghPR.Title, state,
298288
ghPR.HTMLURL, ghPR.Head.Ref, ghPR.Base.Ref, ghPR.User.Login, mergedAtStr, now)
299-
if err != nil {
300-
apiError(res, 500, "INTERNAL_ERROR", err.Error())
289+
if err != nil || len(upserted.Rows) == 0 {
290+
if err != nil {
291+
apiError(res, 500, "INTERNAL_ERROR", err.Error())
292+
} else {
293+
apiError(res, 500, "INTERNAL_ERROR", "upsert returned no rows")
294+
}
301295
return
302296
}
297+
prID := newRowScanner(upserted.Columns, upserted.Rows[0]).str("id")
303298

304-
linkID := uuid.New().String()
305299
_, lErr := p.db.Exec(`
306-
INSERT INTO github_task_pr_links (id, task_id, pull_request_id, created_at)
307-
VALUES ($1,$2,$3,$4)
300+
INSERT INTO github_task_pr_links (task_id, pull_request_id, created_at)
301+
VALUES ($1,$2,$3)
308302
ON CONFLICT (task_id, pull_request_id) DO NOTHING
309-
`, linkID, taskID, prID, now)
303+
`, taskID, prID, now)
310304
if lErr != nil {
311305
p.log.Error("failed to link PR to task: " + lErr.Error())
312306
}

backend/webhook.go

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
plugin "github.com/Paca-AI/plugin-sdk-go"
14-
"github.com/google/uuid"
1514
)
1615

1716
// branchTaskRefRe matches a task-ID prefix in a branch name (e.g. "PROJ-42").
@@ -114,42 +113,25 @@ func (p *githubPlugin) handlePREvent(repoID, projectID string, payload []byte) e
114113
mergedAtStr = &s
115114
}
116115

117-
// Check if PR already cached.
118-
existResult, err := p.db.Query(
119-
`SELECT id, created_at FROM github_pull_requests WHERE repo_id = $1 AND pr_number = $2`,
120-
repoID, gh.Number,
121-
)
122-
if err != nil {
123-
p.log.Error("github: failed to check existing PR: " + err.Error() + ", repo_id=" + repoID + ", pr_number=" + strconv.Itoa(gh.Number))
124-
return err
125-
}
126-
var prID string
127-
var prCreatedAt string
128-
if existResult != nil && len(existResult.Rows) > 0 {
129-
eSc := newRowScanner(existResult.Columns, existResult.Rows[0])
130-
prID = eSc.str("id")
131-
prCreatedAt = eSc.str("created_at")
132-
p.log.Info("github: PR already cached, updating, pr_id=" + prID + ", pr_number=" + strconv.Itoa(gh.Number))
133-
} else {
134-
prID = uuid.New().String()
135-
prCreatedAt = now
136-
p.log.Info("github: PR not cached, inserting, pr_id=" + prID + ", pr_number=" + strconv.Itoa(gh.Number))
137-
}
138-
139-
// Upsert the PR cache.
140-
_, err = p.db.Exec(`
116+
// Upsert the PR cache; let PostgreSQL generate id on insert, RETURNING gives us the id.
117+
upserted, err := p.db.Query(`
141118
INSERT INTO github_pull_requests
142-
(id, project_id, repo_id, pr_number, github_pr_id, title, state, html_url, head_branch, base_branch, author, merged_at, created_at, updated_at)
143-
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14)
119+
(project_id, repo_id, pr_number, github_pr_id, title, state, html_url, head_branch, base_branch, author, merged_at, created_at, updated_at)
120+
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)
144121
ON CONFLICT (repo_id, pr_number) DO UPDATE SET
145-
title=$6, state=$7, html_url=$8, head_branch=$9, base_branch=$10,
146-
author=$11, merged_at=$12, updated_at=$14
147-
`, prID, projectID, repoID, gh.Number, gh.ID, gh.Title, state,
148-
gh.HTMLURL, gh.Head.Ref, gh.Base.Ref, gh.User.Login, mergedAtStr, prCreatedAt, now)
122+
title=$5, state=$6, html_url=$7, head_branch=$8, base_branch=$9,
123+
author=$10, merged_at=$11, updated_at=$13
124+
RETURNING id
125+
`, projectID, repoID, gh.Number, gh.ID, gh.Title, state,
126+
gh.HTMLURL, gh.Head.Ref, gh.Base.Ref, gh.User.Login, mergedAtStr, now, now)
149127
if err != nil {
150128
p.log.Error("github: failed to upsert PR: " + err.Error() + ", repo_id=" + repoID + ", pr_number=" + strconv.Itoa(gh.Number))
151129
return err
152130
}
131+
var prID string
132+
if len(upserted.Rows) > 0 {
133+
prID = newRowScanner(upserted.Columns, upserted.Rows[0]).str("id")
134+
}
153135

154136
p.log.Info("github: PR saved successfully, pr_id=" + prID + ", pr_number=" + strconv.Itoa(gh.Number) + ", action=" + event.Action)
155137

@@ -161,12 +143,11 @@ func (p *githubPlugin) handlePREvent(repoID, projectID string, payload []byte) e
161143
)
162144
if brResult != nil && len(brResult.Rows) > 0 {
163145
taskID := newRowScanner(brResult.Columns, brResult.Rows[0]).str("task_id")
164-
linkID := uuid.New().String()
165146
_, _ = p.db.Exec(`
166-
INSERT INTO github_task_pr_links (id, task_id, pull_request_id, created_at)
167-
VALUES ($1,$2,$3,$4)
147+
INSERT INTO github_task_pr_links (task_id, pull_request_id, created_at)
148+
VALUES ($1,$2,$3)
168149
ON CONFLICT (task_id, pull_request_id) DO NOTHING
169-
`, linkID, taskID, prID, now)
150+
`, taskID, prID, now)
170151

171152
plugin.EmitEvent("github.pr_linked", map[string]any{
172153
"project_id": projectID,
@@ -229,12 +210,11 @@ func (p *githubPlugin) handlePushEvent(repoID, projectID string, payload []byte)
229210
taskID := newRowScanner(taskResult.Columns, taskResult.Rows[0]).str("id")
230211

231212
now := time.Now().UTC().Format(time.RFC3339Nano)
232-
branchID := uuid.New().String()
233213
_, _ = p.db.Exec(`
234-
INSERT INTO github_task_branches (id, task_id, repo_id, branch_name, created_at)
235-
VALUES ($1,$2,$3,$4,$5)
214+
INSERT INTO github_task_branches (task_id, repo_id, branch_name, created_at)
215+
VALUES ($1,$2,$3,$4)
236216
ON CONFLICT (task_id, repo_id, branch_name) DO NOTHING
237-
`, branchID, taskID, repoID, branchName, now)
217+
`, taskID, repoID, branchName, now)
238218

239219
plugin.EmitEvent("github.branch_linked", map[string]any{
240220
"project_id": projectID,

0 commit comments

Comments
 (0)