Skip to content

fix(backend): don't crash Gitea sync when a repo fetch returns a null body#1416

Open
rachit367 wants to merge 2 commits into
sourcebot-dev:mainfrom
rachit367:rachit367/fix-gitea-null-crash
Open

fix(backend): don't crash Gitea sync when a repo fetch returns a null body#1416
rachit367 wants to merge 2 commits into
sourcebot-dev:mainfrom
rachit367:rachit367/fix-gitea-null-crash

Conversation

@rachit367

@rachit367 rachit367 commented Jul 2, 2026

Copy link
Copy Markdown

Fixes #1404

Problem

Syncing repositories from a (self-hosted) Gitea instance can crash the entire connection sync with:

Cannot read properties of null (reading 'full_name')

When api.repos.repoGet() fails while reading the response body (e.g. ERR_STREAM_PREMATURE_CLOSE), gitea-js does not throw — it resolves with { data: null, error: {...} }. In getRepos (packages/backend/src/gitea.ts) that response is wrapped as data: [response.data], so null is pushed into allRepos. The top-level allRepos.filter(repo => repo.full_name !== undefined) then dereferences null.full_name and fails the whole sync — one transient per-repo error takes down every repo in the connection.

Fix

  • In getRepos, if response.data is null, emit a per-repo warning (mirroring the existing 404 handling) instead of returning a null "valid" repo, so a single failed fetch is skipped and the rest of the connection still syncs.
  • Harden the top-level filter to also drop null/undefined entries defensively, and collapse the redundant duplicate full_name filter into one.

This doesn't attempt the underlying stream workaround (the reporter's Accept-Encoding: identity patch) — that's a separate networking concern; this PR makes the sync resilient to the failure rather than papering over the transport.

Tests

Added gitea.test.ts:

  • a repo whose fetch returns a null body is skipped with a warning (previously crashed);
  • a repo with a valid body is returned normally.

Both pass. tsc --noEmit is clean for the backend package.

Summary by CodeRabbit

  • Bug Fixes
    • Prevented Gitea sync crashes when a repository fetch returns no data.
    • Repositories with missing details are now skipped with a warning instead of causing failures.
    • Improved handling of invalid repository responses so only valid repositories are included.

rachit367 added 2 commits July 2, 2026 14:01
… body

When repoGet fails mid-body (e.g. ERR_STREAM_PREMATURE_CLOSE) gitea-js
resolves with { data: null, error } instead of throwing. getRepos wrapped
that as [null], which flowed into allRepos and crashed the whole sync at
allRepos.filter(repo => repo.full_name !== undefined) on null.full_name.

Treat a null response body as a per-repo warning (like the 404 path) so a
single failed fetch is skipped instead of failing the connection, and
harden the top-level filter against null. Also drops a redundant
duplicate full_name filter.

Fixes sourcebot-dev#1404
@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3524be98-9c60-46e5-afba-6214cf87a7ca

📥 Commits

Reviewing files that changed from the base of the PR and between fd6720f and 5edbe54.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • packages/backend/src/gitea.test.ts
  • packages/backend/src/gitea.ts

Walkthrough

This PR fixes a Gitea connection sync crash that occurred when a repository fetch returned an empty response body. It adds null/undefined guards in gitea.ts filtering and getRepos logic, logs warnings for skipped repos, adds corresponding tests, and updates the changelog.

Changes

Gitea sync crash fix

Layer / File(s) Summary
Null/empty response guards
packages/backend/src/gitea.ts, CHANGELOG.md
Filtering now skips repos when the repo object or full_name is missing (logging repo?.id); getRepos returns a warning result and logs a warning when response.data is falsy instead of proceeding with null data. Changelog updated with the fix.
Tests for repo fetch guards
packages/backend/src/gitea.test.ts
New Vitest tests mock gitea-js, cross-fetch, and getTokenFromConfig to verify that a null repoGet response is skipped with a warning and that a valid repo payload is correctly included.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant getGiteaReposFromConfig
  participant getRepos
  participant giteaApi
  participant logger

  getGiteaReposFromConfig->>getRepos: request repo list
  getRepos->>giteaApi: repos.repoGet(org, repo)
  giteaApi-->>getRepos: response (data: null, error)
  getRepos->>logger: warn(repo context, error)
  getRepos-->>getGiteaReposFromConfig: warning result (skip repo)
  getGiteaReposFromConfig->>getGiteaReposFromConfig: filter allRepos (skip null/missing full_name)
  getGiteaReposFromConfig->>logger: warn(repo?.id)
Loading

Suggested reviewers: msukkari

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main fix: preventing Gitea sync crashes from null repo responses.
Linked Issues check ✅ Passed The changes skip null repo responses with warnings and harden filtering, matching issue #1404's required behavior.
Out of Scope Changes check ✅ Passed All code changes are directly related to the Gitea null-body crash fix and its test coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Gitea sync fails with ERR_STREAM_PREMATURE_CLOSE, then crashes on null full_name

1 participant