|
6 | 6 | "fmt" |
7 | 7 |
|
8 | 8 | plugin "github.com/Paca-AI/plugin-sdk-go" |
9 | | - "github.com/google/uuid" |
10 | 9 | ) |
11 | 10 |
|
12 | 11 | // ─── DTOs ──────────────────────────────────────────────────────────────────── |
@@ -135,50 +134,41 @@ func (p *githubPlugin) linkPRToTask(req *plugin.Request, res *plugin.Response) { |
135 | 134 |
|
136 | 135 | now := nowStr() |
137 | 136 |
|
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 | | - |
154 | 137 | var mergedAtStr *string |
155 | 138 | if ghPR.MergedAt != nil { |
156 | 139 | s := ghPR.MergedAt.UTC().Format("2006-01-02T15:04:05.999999999Z") |
157 | 140 | mergedAtStr = &s |
158 | 141 | } |
159 | 142 |
|
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(` |
162 | 145 | 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) |
165 | 148 | 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 | + } |
172 | 160 | return |
173 | 161 | } |
| 162 | + prSc := newRowScanner(upserted.Columns, upserted.Rows[0]) |
| 163 | + prID := prSc.str("id") |
| 164 | + prCreatedAt := prSc.str("created_at") |
174 | 165 |
|
175 | 166 | // Link the PR to the task. |
176 | | - linkID := uuid.New().String() |
177 | 167 | 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) |
180 | 170 | ON CONFLICT (task_id, pull_request_id) DO NOTHING |
181 | | - `, linkID, taskID, prID, now) |
| 171 | + `, taskID, prID, now) |
182 | 172 | if lErr != nil { |
183 | 173 | apiError(res, 500, "INTERNAL_ERROR", lErr.Error()) |
184 | 174 | return |
@@ -279,34 +269,38 @@ func (p *githubPlugin) createPullRequest(req *plugin.Request, res *plugin.Respon |
279 | 269 | } |
280 | 270 |
|
281 | 271 | now := nowStr() |
282 | | - prID := uuid.New().String() |
283 | 272 |
|
284 | 273 | var mergedAtStr *string |
285 | 274 | if ghPR.MergedAt != nil { |
286 | 275 | s := ghPR.MergedAt.UTC().Format("2006-01-02T15:04:05.999999999Z") |
287 | 276 | mergedAtStr = &s |
288 | 277 | } |
289 | 278 |
|
290 | | - _, err = p.db.Exec(` |
| 279 | + upserted, err := p.db.Query(` |
291 | 280 | 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) |
294 | 283 | 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, |
298 | 288 | 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 | + } |
301 | 295 | return |
302 | 296 | } |
| 297 | + prID := newRowScanner(upserted.Columns, upserted.Rows[0]).str("id") |
303 | 298 |
|
304 | | - linkID := uuid.New().String() |
305 | 299 | _, 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) |
308 | 302 | ON CONFLICT (task_id, pull_request_id) DO NOTHING |
309 | | - `, linkID, taskID, prID, now) |
| 303 | + `, taskID, prID, now) |
310 | 304 | if lErr != nil { |
311 | 305 | p.log.Error("failed to link PR to task: " + lErr.Error()) |
312 | 306 | } |
|
0 commit comments