Profpatsch/users/Profpatsch/codeberg-sqlite

codeberg-sqlite(1)

codeberg-sqlite - mirror a Forgejo repository's issues and pull requests into SQLite

codeberg-sqlite ingest --db path --repo owner/name [--host host] [--token token] [--full] [--no-reviews] [--timeout duration]

codeberg-sqlite 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 https://codeberg.org but talks to any Forgejo or Gitea instance.

There is no server and no search subcommand: the output is a database with a documented schema, and the query tool is sqlite3(1). See EXAMPLES.

The mirror is read-only and incremental. The first run of a repository the size of 'flohmarkt/flohmarkt' -- 493 issues, 538 pull requests, 1793 comments -- costs about seventy requests and ninety seconds, plus the review backfill described under 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.

Forgejo serves no issue feed: /issues.rss is a 404, and the repository feed (/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.

ingest --db path --repo 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
**codeberg-sqlite**
adds its tables to an old file without a migration step.

--db path

The SQLite database to write. Required.

--repo owner/name

The repository, exactly as it appears in its URL. Required, and validated before any request is made.

--host host

The Forgejo instance. Defaults to codeberg.org.

--token token

An API token. Optional for public repositories. Prefer the CODEBERG_TOKEN environment variable: an argument vector is world-readable through /proc for the lifetime of the process.

--full

Ignore the stored watermarks and re-fetch everything. Rarely needed — the incremental path is not a cache, it is driven by upstream's own 'updated\_at' -- but it is the way to repair a database that was edited by hand or interrupted in some novel way.

--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.

--timeout duration

Per-request HTTP timeout.
Defaults to
*2m*.

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.

This is the fact the schema is built around. In Forgejo a pull request is an issue: they share one number space per repository, one API endpoint returns both, and a comment refers to "issue 925" whether or not 925 is a pull request.

So issue holds both, distinguished by is_pull, and pull is a side-table holding only what exists for a pull request -- merge state, diff stats, branches. "All open items" is a scan of one table; "pull requests by size" is one join. Splitting them would have made every comment join a UNION.

The API keeps them strictly apart and so does this schema. comment is the conversation: what you write in the box at the bottom of the page. review_comment is a remark pinned to a line of a diff; it hangs off a review -- an approval, rejection or bare comment -- rather than off the issue, and it carries path, position and diff_hunk. Merging the two would lose the code location, which is most of the reason a review comment is worth having.

Stored as the RFC 3339 strings the API returns, in the API's own offset: Codeberg answers '+01:00' in winter and '+02:00' in summer. They are therefore not in lexical order. Compare them with SQLite's datetime(), which normalises the offset, and not as bare strings -- '2026-07-01T00:00:00+02:00' sorts after '2026-06-30T23:30:00+01:00' but is the earlier instant.

repo

The mirrored repository:
*id*, *host*, *full_name*.
Everything else carries
*repo_id*.

issue

One row per issue
*and*
pull request:
*number*, *id*, *is_pull*, *title*, *body*, *state*, *author*, *milestone*,
*created_at*, *updated_at*, *closed_at*, *comments*.

pull

Pull-request-only columns:
*draft*, *merged*, *merged_at*, *merged_by*, *mergeable*,
*additions*, *deletions*, *changed_files*,
and the head and base branch and SHA.
*mergeable*
is
`NULL`
until Forgejo computes it, which it does lazily; absence means
"not known",
not
"conflicts".

label, issue_label

The repository's labels and their assignment, as a join table rather than a
JSON column, so that
"everything tagged security"
is an index lookup instead of a
`LIKE`
over every body in the repository.

comment

Conversation comments on issues and pull requests alike.

review

One submitted verdict on a pull request:
*state*
is
`APPROVED`, `REQUEST_CHANGES`, `COMMENT`
or
`REQUEST_REVIEW`.
Its
*body*
is the overall remark, usually empty.

review_comment

A comment pinned to a line of the diff, with
*path*,
*position*,
*diff_hunk*
and the login of whoever resolved it.

meta

Per-phase watermarks and the time of the last run.

Codeberg meters the API in several buckets, and they are far apart. Ordinary endpoints are charged to a "baseline" bucket of 2000 requests per ten minutes, which a whole ingest barely touches. But /pulls/{n}/reviews is charged to a "git_op" bucket of only 250.

A repository with more pull requests than that 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.

codeberg-sqlite therefore waits out the reset and continues -- 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.

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 --no-reviews skips the phase entirely.

CODEBERG_TOKEN

API token, used when
**--token**
is not given.
Preferred, because it does not appear in the process table.

The codeberg-sqlite utility exits0 on success, and>0 if an error occurs. A run that stops on a rate limit exits non-zero, having committed everything it fetched up to that point.

Mirror a repository and ask it something:

codeberg-sqlite ingest --db flohmarkt.db --repo flohmarkt/flohmarkt
sqlite3 flohmarkt.db "SELECT number, title FROM issue
                      WHERE state='open' AND is_pull=0"

Refresh it later. This is the ordinary case and costs about six requests:

codeberg-sqlite ingest --db flohmarkt.db --repo flohmarkt/flohmarkt

The open pull requests, oldest first, with their size:

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;

How many open issues carry each label:

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;

Everything said about one issue, in order -- the body and then the conversation:

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);

Review remarks left on a particular file, which is what the review_comment table exists for:

SELECT number, author, path, position, body
FROM review_comment
WHERE path LIKE '%session.py'
ORDER BY datetime(created_at);

Full-text search, without an index. At this size a scan is instant, and it keeps the schema honest:

SELECT number, title FROM issue
WHERE body LIKE '%activitypub%' OR title LIKE '%activitypub%';

What changed in the last week:

SELECT number, is_pull, state, title FROM issue
WHERE datetime(updated_at) > datetime('now', '-7 days')
ORDER BY datetime(updated_at) DESC;

sqlite3(1), Forgejo API reference

Profpatsch

Forgejo only paginates when a request carries an explicit 'page' parameter. Without one it returns the entire collection and ignores 'limit': /issues/comments?limit=50 answers with all 1793 comments, while /issues/comments?limit=50&page=1 answers with 50.

Omitting the parameter therefore appears to work, and better -- one request instead of thirty-six -- 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. codeberg-sqlite always sends 'page', and a test reproduces the quirk to keep it that way.

'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.

Passing 'since' to the pull request list has no effect: it returns all 538 rows regardless. Only /issues honours it. The incremental path therefore drives from /issues?type=all, which covers pull requests too, and the pull phase walks /pulls newest-first and stops at the first page that predates its watermark.

There is no repository-wide reviews endpoint. This is the whole reason the phase is separate, resumable, and skippable; see RATE LIMITS. Asking for the reviews of a number that is an issue is an error rather than an empty list, so only known pull requests are visited.

Each phase stores the greatest '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 -- 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.

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 UPSERT, so the cost is a few rewritten rows.

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 review ids are stable but the rows are rewritten on every visit.

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 issue, because the next run's 'since' has already moved past them and dropping them would lose them permanently. codeberg-sqlite counts them at the end of a run and the next one reconciles. Join with a LEFT JOIN if this matters to a query.

Diffs and patches, commit lists, reactions, wiki pages, releases and the issue timeline (who assigned what to whom, and when). The timeline in particular is available at /issues/{n}/timeline and would cost one request per issue, in the same expensive bucket as reviews.