From de1f301d2ffe5f8a002a0d456e9f0c7015c8b2a5 Mon Sep 17 00:00:00 2001 From: Brendan Kellam <10233483+brendan-kellam@users.noreply.github.com> Date: Tue, 30 Jun 2026 19:29:19 +0000 Subject: [PATCH 1/2] fix(backend): handle Gitea ERR_STREAM_PREMATURE_CLOSE and null repo data Force identity encoding / connection close in the Gitea API fetch to avoid cross-fetch failing to read the response body, and guard against null repo data so a failed fetch no longer crashes on repo.full_name. Co-authored-by: linear-code[bot] <222613912+linear-code[bot]@users.noreply.github.com> --- packages/backend/src/gitea.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/gitea.ts b/packages/backend/src/gitea.ts index ac7566ee8..4ebdc2033 100644 --- a/packages/backend/src/gitea.ts +++ b/packages/backend/src/gitea.ts @@ -12,6 +12,22 @@ import { measure } from './utils.js'; const logger = createLogger('gitea'); const GITEA_CLOUD_HOSTNAME = "gitea.com"; +// Some Gitea instances (particularly when behind certain reverse proxies or with +// response compression enabled) cause `cross-fetch` to fail while reading the +// response body with ERR_STREAM_PREMATURE_CLOSE. Forcing identity encoding and +// closing the connection avoids the premature close. +// @see https://github.com/sourcebot-dev/sourcebot/issues/1404 +const customFetch: typeof fetch = (url, options = {}) => { + return fetch(url, { + ...options, + headers: { + ...(options.headers ?? {}), + 'Accept-Encoding': 'identity', + 'Connection': 'close', + }, + }); +}; + export const getGiteaReposFromConfig = async (config: GiteaConnectionConfig) => { const hostname = config.url ? new URL(config.url).hostname : @@ -25,7 +41,7 @@ export const getGiteaReposFromConfig = async (config: GiteaConnectionConfig) => const api = giteaApi(config.url ?? 'https://gitea.com', { token: token, - customFetch: fetch, + customFetch, }); let allRepos: GiteaRepository[] = []; @@ -49,8 +65,11 @@ export const getGiteaReposFromConfig = async (config: GiteaConnectionConfig) => allWarnings = allWarnings.concat(warnings); } - allRepos = allRepos.filter(repo => repo.full_name !== undefined); allRepos = allRepos.filter(repo => { + if (repo === null || repo === undefined) { + logger.warn(`Skipping null/undefined repository returned by the Gitea API`); + return false; + } if (repo.full_name === undefined) { logger.warn(`Repository with undefined full_name found: repoId=${repo.id}`); return false; @@ -208,6 +227,10 @@ const getRepos = async (repoList: string[], api: Api) => { api.repos.repoGet(owner, repoName), ); + if (response.error || !response.data) { + throw response.error ?? new Error(`Received empty response body while fetching repository ${repo}`); + } + logger.debug(`Found repo ${repo} in ${durationMs}ms.`); return { type: 'valid' as const, From 474b7be2a2ce28c20521e36d7bcce02467726926 Mon Sep 17 00:00:00 2001 From: Brendan Kellam <10233483+brendan-kellam@users.noreply.github.com> Date: Tue, 30 Jun 2026 19:29:46 +0000 Subject: [PATCH 2/2] docs: add CHANGELOG entry for #1405 Co-authored-by: linear-code[bot] <222613912+linear-code[bot]@users.noreply.github.com> --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32a565fc3..333546b6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [EE] Fixed Ask Sourcebot mermaid diagrams overflowing their container by contain-fitting them to both width and height, and made revealing a diagram from the answer jump it into view instantly to avoid over/undershooting. [#1373](https://github.com/sourcebot-dev/sourcebot/pull/1373) - Verified GitHub review webhook deliveries before processing them. [#1378](https://github.com/sourcebot-dev/sourcebot/pull/1378) - Passed Zoekt index parameters via argv to preserve revision names with punctuation. [#1376](https://github.com/sourcebot-dev/sourcebot/pull/1376) +- Fixed Gitea sync failing with `ERR_STREAM_PREMATURE_CLOSE` by forcing identity encoding on the Gitea API fetch and guarding against null repository responses. [#1405](https://github.com/sourcebot-dev/sourcebot/pull/1405) ## [5.0.4] - 2026-06-18