Skip to content

Commit cac1f1d

Browse files
committed
forge--update-{issue,pullreq}: Always try to save people and labels
When a topic is fetched individually, i.e., outside a request that also fetches data about the repository itself, then it can happen that the topic data contains references to new users and labels, which do not exist in the local database yet. Because we only record the object id for users and labels in topic tables as foreign keys, that can lead to foreign key errors. In the past we did not even attempt to update these values when fetching individual topics. That was an okay approach, because the expectation was that users would always fetch everything. Support for only fetching individual topics was only added later, and was only intended to be used in special cases. This is changing (but still work in progress). More importantly though, when we modify a topic, we now follow that up by only fetching that topic from the forge again. Previously we would have fetched the complete repository, which was slow and unnecessary, because at that time all we care about is making sure that one topic is in sync. In that case we must update all slots. If the user modified, e.g., the labels, then they expect that to take effect locally, without having to explicitly fetch the complete repository. We can even be sure that the chosen labels are known locally already, else the user would not have been able to select them. When fetching an individual topic explicitly, it can still happen that it references an unknown label or person, but that is the exception. We now silently ignore these rare cases, which is better than to not even attempting to store this data for the vast majority of cases for which it would have worked. Closes #654.
1 parent 14a4155 commit cac1f1d

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

lisp/forge-github.el

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,16 @@
251251

252252
(cl-defmethod forge--update-issue ((repo forge-github-repository) data
253253
&optional bump initial-pull)
254-
(closql-with-transaction (forge-db)
254+
(let (issue-id issue)
255255
(let-alist data
256-
(let* ((issue-id (forge--object-id 'forge-issue repo .number))
257-
(issue (or (forge-get-issue repo .number)
256+
(closql-with-transaction (forge-db)
257+
(setq issue-id (forge--object-id 'forge-issue repo .number))
258+
(setq issue (or (forge-get-issue repo .number)
258259
(closql-insert
259260
(forge-db)
260261
(forge-issue :id issue-id
261262
:repository (oref repo id)
262-
:number .number)))))
263+
:number .number))))
263264
(oset issue their-id .id)
264265
(oset issue slug (format "#%s" .number))
265266
(oset issue state
@@ -291,12 +292,13 @@
291292
:updated .updatedAt
292293
:body (forge--sanitize-string .body))
293294
t)))
294-
(forge--update-status repo issue data bump initial-pull)
295-
(when bump
296-
(forge--set-id-slot repo issue 'assignees .assignees)
297-
(unless (magit-get-boolean "forge.kludge-for-issue-294")
298-
(forge--set-id-slot repo issue 'labels .labels)))
299-
issue))))
295+
(forge--update-status repo issue data bump initial-pull))
296+
(ignore-errors
297+
(forge--set-id-slot repo issue 'assignees .assignees))
298+
(ignore-errors
299+
(unless (magit-get-boolean "forge.kludge-for-issue-294")
300+
(forge--set-id-slot repo issue 'labels .labels))))
301+
issue))
300302

301303
;;;; Pullreqs
302304

@@ -308,15 +310,16 @@
308310

309311
(cl-defmethod forge--update-pullreq ((repo forge-github-repository) data
310312
&optional bump initial-pull)
311-
(closql-with-transaction (forge-db)
313+
(let (pullreq-id pullreq)
312314
(let-alist data
313-
(let* ((pullreq-id (forge--object-id 'forge-pullreq repo .number))
314-
(pullreq (or (forge-get-pullreq repo .number)
315+
(closql-with-transaction (forge-db)
316+
(setq pullreq-id (forge--object-id 'forge-pullreq repo .number))
317+
(setq pullreq (or (forge-get-pullreq repo .number)
315318
(closql-insert
316319
(forge-db)
317320
(forge-pullreq :id pullreq-id
318321
:repository (oref repo id)
319-
:number .number)))))
322+
:number .number))))
320323
(oset pullreq their-id .id)
321324
(oset pullreq slug (format "#%s" .number))
322325
(oset pullreq state (pcase-exhaustive .state
@@ -357,15 +360,17 @@
357360
:updated .updatedAt
358361
:body (forge--sanitize-string .body))
359362
t)))
360-
(forge--update-status repo pullreq data bump initial-pull)
361-
(when bump
362-
(forge--set-id-slot repo pullreq 'assignees .assignees)
363-
(forge--set-id-slot repo pullreq 'review-requests
364-
(--map (cdr (cadr (car it)))
365-
.reviewRequests))
366-
(unless (magit-get-boolean "forge.kludge-for-issue-294")
367-
(forge--set-id-slot repo pullreq 'labels .labels)))
368-
pullreq))))
363+
(forge--update-status repo pullreq data bump initial-pull))
364+
(ignore-errors
365+
(forge--set-id-slot repo pullreq 'assignees .assignees))
366+
(ignore-errors
367+
(forge--set-id-slot repo pullreq 'review-requests
368+
(--map (cdr (cadr (car it)))
369+
.reviewRequests)))
370+
(ignore-errors
371+
(unless (magit-get-boolean "forge.kludge-for-issue-294")
372+
(forge--set-id-slot repo pullreq 'labels .labels))))
373+
pullreq))
369374

370375
;;;; Notifications
371376

0 commit comments

Comments
 (0)