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
|
.Dd September 7, 2026
.Dt CODEBERG-SQLITE 1
.Os
.Sh NAME
.Nm codeberg-sqlite
.Nd mirror a Forgejo repository's issues and pull requests into SQLite
.Sh SYNOPSIS
.Nm
.Cm ingest
.Fl -db Ar path
.Fl -repo Ar owner/name
.Op Fl -host Ar host
.Op Fl -token Ar token
.Op Fl -full
.Op Fl -no-reviews
.Op Fl -timeout Ar duration
.Sh DESCRIPTION
.Nm
copies the issues, pull requests, comments and reviews of a Forgejo
repository into a local SQLite database, so they can be queried with SQL
instead of a web interface.
It is written for
.Lk https://codeberg.org
but talks to any Forgejo or Gitea instance.
.Pp
There is no server and no search subcommand: the output is a database with a
documented schema, and the query tool is
.Xr sqlite3 1 .
See
.Sx EXAMPLES .
.Pp
The mirror is read-only and incremental.
The first run of a repository the size of
.Ql flohmarkt/flohmarkt
\(em 493 issues, 538 pull requests, 1793 comments \(em costs about seventy
requests and ninety seconds, plus the review backfill described under
.Sx RATE LIMITS .
Subsequent runs cost six requests and about seven seconds, because each phase
remembers the newest item it saw and asks only for what changed.
.Ss Why the API and not RSS or mail
Forgejo serves no issue feed:
.Pa /issues.rss
is a 404, and the repository feed
.Pq Pa /flohmarkt/flohmarkt.rss
is a truncated activity stream carrying neither bodies, nor labels, nor
state.
Notification mail is lossy in the same way and only covers what arrived after
one subscribed.
The API is the only source with the whole history in it, and it needs no
authentication.
.Ss Subcommands
.Bl -tag -width Ds
.It Cm ingest Fl -db Ar path Fl -repo Ar owner/name
Fetch and store.
The database is created if it does not exist, and the schema is applied on
every run, so a new version of
.Nm
adds its tables to an old file without a migration step.
.El
.Ss Options
.Bl -tag -width "--no-reviews"
.It Fl -db Ar path
The SQLite database to write.
Required.
.It Fl -repo Ar owner/name
The repository, exactly as it appears in its URL.
Required, and validated before any request is made.
.It Fl -host Ar host
The Forgejo instance.
Defaults to
.Ar codeberg.org .
.It Fl -token Ar token
An API token.
Optional for public repositories.
Prefer the
.Ev CODEBERG_TOKEN
environment variable: an argument vector is world-readable through
.Pa /proc
for the lifetime of the process.
.It Fl -full
Ignore the stored watermarks and re-fetch everything.
Rarely needed \(em the incremental path is not a cache, it is driven by
upstream's own
.Ql updated_at
\(em but it is the way to repair a database that was edited by hand or
interrupted in some novel way.
.It Fl -no-reviews
Skip the reviews phase, which is the only expensive one.
Useful for a first look at a large repository:
everything except approvals and inline diff comments is fetched in about a
minute.
.It Fl -timeout Ar duration
Per-request HTTP timeout.
Defaults to
.Ar 2m .
.El
.Sh DATA MODEL
Eight tables.
The shape follows Forgejo's own model rather than tidying it up, because
every deviation would be a place where a query gives a different answer than
the web interface does.
.Ss A pull request is an issue
This is the fact the schema is built around.
In Forgejo a pull request
.Em is
an issue: they share one number space per repository, one API endpoint
returns both, and a comment refers to
.Dq issue 925
whether or not 925 is a pull request.
.Pp
So
.Ar issue
holds both, distinguished by
.Ar is_pull ,
and
.Ar pull
is a side-table holding only what exists for a pull request \(em merge state,
diff stats, branches.
.Dq All open items
is a scan of one table;
.Dq pull requests by size
is one join.
Splitting them would have made every comment join a
.Dv UNION .
.Ss Two kinds of comment
The API keeps them strictly apart and so does this schema.
.Ar comment
is the conversation: what you write in the box at the bottom of the page.
.Ar review_comment
is a remark pinned to a line of a diff; it hangs off a
.Ar review
\(em an approval, rejection or bare comment \(em rather than off the issue,
and it carries
.Ar path ,
.Ar position
and
.Ar diff_hunk .
Merging the two would lose the code location, which is most of the reason a
review comment is worth having.
.Ss Timestamps
Stored as the RFC 3339 strings the API returns, in the API's own offset:
Codeberg answers
.Ql +01:00
in winter and
.Ql +02:00
in summer.
They are therefore
.Em not
in lexical order.
Compare them with SQLite's
.Fn datetime ,
which normalises the offset, and not as bare strings \(em
.Ql 2026-07-01T00:00:00+02:00
sorts after
.Ql 2026-06-30T23:30:00+01:00
but is the earlier instant.
.Ss Tables
.Bl -tag -width "review_comment"
.It Ar repo
The mirrored repository:
.Ar id , host , full_name .
Everything else carries
.Ar repo_id .
.It Ar issue
One row per issue
.Em and
pull request:
.Ar number , id , is_pull , title , body , state , author , milestone ,
.Ar created_at , updated_at , closed_at , comments .
.It Ar pull
Pull-request-only columns:
.Ar draft , merged , merged_at , merged_by , mergeable ,
.Ar additions , deletions , changed_files ,
and the head and base branch and SHA.
.Ar mergeable
is
.Dv NULL
until Forgejo computes it, which it does lazily; absence means
.Dq not known ,
not
.Dq conflicts .
.It Ar label , Ar issue_label
The repository's labels and their assignment, as a join table rather than a
JSON column, so that
.Dq everything tagged security
is an index lookup instead of a
.Dv LIKE
over every body in the repository.
.It Ar comment
Conversation comments on issues and pull requests alike.
.It Ar review
One submitted verdict on a pull request:
.Ar state
is
.Dv APPROVED , REQUEST_CHANGES , COMMENT
or
.Dv REQUEST_REVIEW .
Its
.Ar body
is the overall remark, usually empty.
.It Ar review_comment
A comment pinned to a line of the diff, with
.Ar path ,
.Ar position ,
.Ar diff_hunk
and the login of whoever resolved it.
.It Ar meta
Per-phase watermarks and the time of the last run.
.El
.Sh RATE LIMITS
Codeberg meters the API in several buckets, and they are far apart.
Ordinary endpoints are charged to a
.Dq baseline
bucket of
.Em 2000
requests per ten minutes, which a whole ingest barely touches.
But
.Pa /pulls/{n}/reviews
is charged to a
.Dq git_op
bucket of only
.Em 250 .
.Pp
A repository with more pull requests than that
.Em cannot
be back-filled inside one window, no matter how polite the client is:
flohmarkt has 538, so a cold review backfill takes three windows and around
twenty minutes, most of it spent waiting.
.Pp
.Nm
therefore waits out the reset and continues \(em but only during a cold
backfill, where hitting the wall is a certainty rather than a symptom.
An incremental run has a handful of pull requests to check, so a rate limit
there means something is wrong, and it is reported instead: a command that
silently blocks for ten minutes is worse than one that says why it stopped.
.Pp
Interrupting a backfill is safe.
Progress is recorded after every pull request, so the next run resumes at the
next number rather than starting over, and says so.
Passing
.Fl -no-reviews
skips the phase entirely.
.Sh ENVIRONMENT
.Bl -tag -width CODEBERG_TOKEN
.It Ev CODEBERG_TOKEN
API token, used when
.Fl -token
is not given.
Preferred, because it does not appear in the process table.
.El
.Sh EXIT STATUS
.Ex -std
A run that stops on a rate limit exits non-zero, having committed everything
it fetched up to that point.
.Sh EXAMPLES
Mirror a repository and ask it something:
.Bd -literal -offset indent
codeberg-sqlite ingest --db flohmarkt.db --repo flohmarkt/flohmarkt
sqlite3 flohmarkt.db "SELECT number, title FROM issue
WHERE state='open' AND is_pull=0"
.Ed
.Pp
Refresh it later.
This is the ordinary case and costs about six requests:
.Bd -literal -offset indent
codeberg-sqlite ingest --db flohmarkt.db --repo flohmarkt/flohmarkt
.Ed
.Pp
The open pull requests, oldest first, with their size:
.Bd -literal -offset indent
SELECT i.number, i.title, i.author,
CAST(julianday('now') - julianday(i.created_at) AS INT) AS age_days,
p.additions, p.deletions
FROM issue i JOIN pull p USING (repo_id, number)
WHERE i.state = 'open' AND p.merged = 0
ORDER BY i.created_at;
.Ed
.Pp
How many open issues carry each label:
.Bd -literal -offset indent
SELECT l.name, count(*) AS n
FROM issue_label il
JOIN label l ON l.id = il.label_id
JOIN issue i ON i.repo_id = il.repo_id AND i.number = il.number
WHERE i.state = 'open'
GROUP BY l.name ORDER BY n DESC;
.Ed
.Pp
Everything said about one issue, in order \(em the body and then the
conversation:
.Bd -literal -offset indent
SELECT author, created_at, body FROM (
SELECT author, created_at, body FROM issue WHERE number = 925
UNION ALL
SELECT author, created_at, body FROM comment WHERE number = 925
) ORDER BY datetime(created_at);
.Ed
.Pp
Review remarks left on a particular file, which is what the
.Ar review_comment
table exists for:
.Bd -literal -offset indent
SELECT number, author, path, position, body
FROM review_comment
WHERE path LIKE '%session.py'
ORDER BY datetime(created_at);
.Ed
.Pp
Full-text search, without an index.
At this size a scan is instant, and it keeps the schema honest:
.Bd -literal -offset indent
SELECT number, title FROM issue
WHERE body LIKE '%activitypub%' OR title LIKE '%activitypub%';
.Ed
.Pp
What changed in the last week:
.Bd -literal -offset indent
SELECT number, is_pull, state, title FROM issue
WHERE datetime(updated_at) > datetime('now', '-7 days')
ORDER BY datetime(updated_at) DESC;
.Ed
.Sh SEE ALSO
.Xr sqlite3 1 ,
.Lk https://codeberg.org/api/swagger "Forgejo API reference"
.Sh AUTHORS
.An Profpatsch
.Sh CAVEATS
.Ss Pagination must be asked for
Forgejo only paginates when a request carries an explicit
.Ql page
parameter.
Without one it returns the
.Em entire
collection and ignores
.Ql limit :
.Pa /issues/comments?limit=50
answers with all 1793 comments, while
.Pa /issues/comments?limit=50&page=1
answers with 50.
.Pp
Omitting the parameter therefore appears to work, and better \(em one request
instead of thirty-six \(em right up until a repository is large enough for
the server to truncate the dump, at which point comments go missing with no
error at all.
.Nm
always sends
.Ql page ,
and a test reproduces the quirk to keep it that way.
.Ss The list endpoints cap at fifty
.Ql limit=200
silently yields 50.
The paging loop terminates on a page shorter than it asked for, so a client
that requested 200 would treat every page as the last and fetch exactly one.
.Ss /pulls ignores ?since
Passing
.Ql since
to the pull request list has no effect: it returns all 538 rows regardless.
Only
.Pa /issues
honours it.
The incremental path therefore drives from
.Pa /issues?type=all ,
which covers pull requests too, and the
.Ar pull
phase walks
.Pa /pulls
newest-first and stops at the first page that predates its watermark.
.Ss Reviews cost one request per pull request
There is no repository-wide reviews endpoint.
This is the whole reason the phase is separate, resumable, and skippable; see
.Sx RATE LIMITS .
Asking for the reviews of a number that is an
.Em issue
is an error rather than an empty list, so only known pull requests are
visited.
.Ss Watermarks come from the data, not the clock
Each phase stores the greatest
.Ql updated_at
it actually saw.
Using the local clock would be wrong twice over: our clock and Codeberg's
need not agree, and an item modified while we page through a list sorted by
recency moves to the front of a list we have already walked past.
With a data-derived mark such an item is safe \(em it did not raise the mark,
and its new timestamp exceeds everything we did see, so the next run collects
it.
With a clock-derived one it would be lost for good.
.Pp
The filter is inclusive, so every run re-fetches the handful of items sitting
exactly on the mark.
That is deliberate: the alternative is adding a second to the watermark,
which would skip anything modified within that second.
Every write is an idempotent
.Dv UPSERT ,
so the cost is a few rewritten rows.
.Ss Reviews are replaced, not accumulated
A review or an inline comment can be deleted upstream, and re-fetching the
pull request is the only moment that could be noticed.
The phase therefore deletes and re-inserts a pull request's reviews rather
than upserting them.
A consequence is that
.Ar review
ids are stable but the rows are rewritten on every visit.
.Ss Comments may name an issue that is not there
The comment feed carries its own watermark and can legitimately mention an
issue created after the issue phase ran.
Such comments are stored anyway, with no foreign key onto
.Ar issue ,
because the next run's
.Ql since
has already moved past them and dropping them would lose them permanently.
.Nm
counts them at the end of a run and the next one reconciles.
Join with a
.Dv LEFT JOIN
if this matters to a query.
.Ss What is not fetched
Diffs and patches, commit lists, reactions, wiki pages, releases and the
issue timeline
.Pq who assigned what to whom, and when .
The timeline in particular is available at
.Pa /issues/{n}/timeline
and would cost one request per issue, in the same expensive bucket as
reviews.
|