fix(datasource/gitlab): emptied files no longer leave their old body searchable (#4008)

A blob that a commit emptied is still a blob, and ingestion reads an item with
neither content nor URL as nothing to sync: applyFetchedItem counts it as
skipped and the sync reports success. The body indexed from an earlier commit
stays fully searchable, and no later sync revisits it — a full sync still sees
the path in the tree, and an incremental sync only walks the diffs of commits
newer than the recorded head, which the emptying commit already moved past.

Emit the file's path as the content in that case so ingestion replaces the stale
body, matching what the Notion (b9be7d0d), Confluence and IMA (b71b31c8)
connectors already do. The check is on length alone: a blob holding only
whitespace still carries content the repository has.
main
ordinary being 2026-10-12 13:06:19 +08:00 committed by GitHub
parent 24b5ca3f23
commit 7925f774d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 137 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import (
"time"
"github.com/Tencent/WeKnora/internal/datasource"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
)
@ -400,11 +401,30 @@ func (c *Connector) item(ctx context.Context, p *project, ref, file string) (typ
return types.FetchedItem{}, err
}
id := fmt.Sprintf("gitlab:%s:%d:%s:%s", c.canonicalBase, p.ID, ref, file)
title := p.PathWithNamespace + "/" + file
// A file emptied by a commit still exists at this path, and ingestion reads
// an item with no content and no URL as "nothing to sync" (see
// datasource_service applyFetchedItem): the sync is acknowledged as
// successful while the body indexed from an earlier commit stays fully
// searchable. Nothing revisits it afterwards either — a full sync sees the
// file present in the tree, and an incremental sync only walks the diffs of
// commits newer than the recorded head, which the emptying commit already
// moved past.
//
// Emitting the file's path as its content replaces the stale body instead,
// which is what the Notion (b9be7d0d), Confluence and IMA (b71b31c8)
// connectors already do for the same case. The check is on length alone, not
// on TrimSpace: a blob that holds only whitespace still carries content the
// repository has, and only a zero-byte blob has none.
if len(body) == 0 {
logger.Infof(ctx, "[GitLab] %s is empty, syncing its path only", title)
body = []byte("# " + title + "\n")
}
// UpdatedAt is intentionally left unset: the file's last commit time is not
// fetched here, and a fetch timestamp would be a fabricated source time.
return types.FetchedItem{
ExternalID: id,
Title: p.PathWithNamespace + "/" + file,
Title: title,
FileName: knowledgeRelativePath(p.Name, ref, file),
Content: body,
ContentType: "text/plain",

View File

@ -0,0 +1,116 @@
package gitlab
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/Tencent/WeKnora/internal/types"
"github.com/stretchr/testify/require"
)
// An emptied file is still a file. Ingestion treats an item with neither content
// nor URL as nothing to ingest and acknowledges the sync, so emitting the
// emptied blob verbatim leaves the body indexed from an earlier commit
// searchable forever — and no later sync revisits it, because a full sync still
// sees the path in the tree and an incremental sync has already moved its head
// past the commit that emptied it.
//
// The Notion (b9be7d0d), Confluence and IMA (b71b31c8) connectors emit the
// document's title in this case so ingestion replaces the stale body. GitLab
// does the same with the file's path, on both sync paths that can observe the
// change.
//
// Only a zero-byte blob is rewritten. A blob holding whitespace carries content
// the repository still has, so its bytes must reach ingestion untouched.
func TestConnectorEmitsPathWhenAFileIsEmptied(t *testing.T) {
for _, mode := range []string{"full", "incremental", "stream"} {
t.Run(mode, func(t *testing.T) {
allowLocalGitLabServer(t)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/api/v4/projects/1":
_, _ = w.Write([]byte(`{
"id":1,"name":"docs","path_with_namespace":"group/docs",
"web_url":"https://gitlab.test/group/docs","default_branch":"main"
}`))
case "/api/v4/projects/1/repository/commits/main":
_, _ = w.Write([]byte(`{"id":"head"}`))
case "/api/v4/projects/1/repository/compare":
_, _ = w.Write([]byte(`{"diffs":[
{"old_path":"runbook.md","new_path":"runbook.md"},
{"old_path":"indented.md","new_path":"indented.md"},
{"old_path":"kept.md","new_path":"kept.md"}
]}`))
case "/api/v4/projects/1/repository/tree":
_, _ = w.Write([]byte(`[
{"name":"runbook.md","path":"runbook.md","type":"blob"},
{"name":"indented.md","path":"indented.md","type":"blob"},
{"name":"kept.md","path":"kept.md","type":"blob"}
]`))
default:
if strings.Contains(r.URL.Path, "/repository/files/") {
switch {
case strings.Contains(r.URL.Path, "runbook.md"):
// 200 with no bytes at all: the emptied commit.
case strings.Contains(r.URL.Path, "indented.md"):
_, _ = w.Write([]byte(" \n\t\n"))
default:
_, _ = w.Write([]byte("# still here"))
}
return
}
http.NotFound(w, r)
}
}))
defer server.Close()
cfg := &types.DataSourceConfig{
Credentials: map[string]interface{}{"base_url": server.URL, "access_token": "token"},
Settings: map[string]interface{}{"projects": []interface{}{
map[string]interface{}{"project_id": "1", "paths": []interface{}{}},
}},
}
connector := NewConnector()
var items []types.FetchedItem
var err error
switch mode {
case "full":
items, err = connector.FetchAll(context.Background(), cfg, nil)
case "incremental":
items, _, err = connector.FetchIncremental(
context.Background(), cfg, gitLabCursor(cursor{Projects: map[string]string{"1": "previous"}}))
default:
handler := &gitLabStreamRecorder{}
_, err = connector.FetchStream(
context.Background(), cfg,
gitLabCursor(cursor{Projects: map[string]string{"1": "previous"}}), handler)
items = handler.items
}
require.NoError(t, err)
require.Len(t, items, 3)
byPath := make(map[string]types.FetchedItem, len(items))
for _, item := range items {
byPath[item.Metadata["gitlab_path"]] = item
}
emptied := byPath["runbook.md"]
require.False(t, emptied.IsDeleted)
require.Equal(t,
"# group/docs/runbook.md\n",
string(emptied.Content),
"an emptied file must still reach ingestion, or its stale body stays searchable")
require.Equal(t, "group/docs/runbook.md", emptied.Title)
// Whitespace is content: it must not be mistaken for an emptied file.
require.Equal(t, " \n\t\n", string(byPath["indented.md"].Content))
require.Equal(t, "# still here", string(byPath["kept.md"].Content))
})
}
}