refactor(coderd/x/gitsync): remove unused Worker.RefreshChat (#30944)

Removes `Worker.RefreshChat` and the four `TestRefreshChat_*` tests that
only exercised it. `RefreshChat` has had no production caller since
#23901 stopped resolving diff statuses inline on `GetChat`. The worker's
periodic tick is the only refresh path.

`TestWorker_RefreshDoesNotReorderPrimary` used `RefreshChat` to refresh
one of two refs. It now keeps the newer ref fresh (`StaleAt` one hour
ahead) and runs one worker tick, so it checks the same ordering rule
through the production path.

**Why removal is safe:** `git grep '\.RefreshChat('` finds no caller.
Each removed assertion either checked `RefreshChat` itself or is still
caught by a remaining test. I mutated the production line behind each
shared assertion with `go test -overlay`:

| Removed test and assertion | Mutant | Remaining tests that fail |
|---|---|---|
| `TestRefreshChat_Success`: the upsert uses the chat's ID |
`Refresher.refreshOne` upserts a random chat ID |
`TestRefresher_WithPRURL`, `TestWorker`,
`TestWorker_UpsertError_ContinuesNextRow`,
`TestWorker_RefreshDoesNotReorderPrimary` |
| `TestRefreshChat_NoPR`: a branch without a PR is not an error |
`refreshOne` returns an error when there is no PR |
`TestRefresher_BranchNoPRYet` and the three `TestWorker_NoPR_*` tests |
| `TestRefreshChat_RefreshError`: the "no provider" error |
`Refresher.Refresh` changes the "no provider" message |
`TestRefresher_NoProviderForOrigin` |

The other assertions (the returned row, the `upsert chat diff status`
error wrap, and the publish call inside `RefreshChat`) checked only the
deleted method. For the rewritten test, a mutant that sets `updated_at`
in `UpsertChatDiffStatus` fails both the old and the new version. A
mutant that sets `updated_at` in `AcquireStaleChatDiffStatuses` fails
only the new version, because the old version never ran a tick.

**Review focus:** the fixture change in
`TestWorker_RefreshDoesNotReorderPrimary`. #29997 changes one line in
`TestRefreshChat_RefreshError`, so whichever PR lands second drops that
test.

Validation:

- `go test ./coderd/x/gitsync/ -count=1` passes, and `-race -count=5`
passes.
- `golangci-lint run ./coderd/x/gitsync/...` reports 0 issues, and the
pre-commit hook passed.

> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
This commit is contained in:
Michael Suchacz authored and GitHub committed 2026-10-27 16:15:02 +01:00
1 parent bcaf21b812
commit c9204a5cff
2 files changed
+8 -221

No files matched your search

-52
View File
@@ -439,55 +439,3 @@ func (w *Worker) clearStalePR(ctx context.Context, row database.ChatDiffStatus)
}
return nil
}
// RefreshChat synchronously refreshes a single chat's diff
// status using the same Refresher pipeline as the background
// worker. Returns nil, nil when no PR exists yet for the
// branch. Called from HTTP handlers for instant feedback.
func (w *Worker) RefreshChat(
ctx context.Context,
row database.ChatDiffStatus,
ownerID uuid.UUID,
) (*database.ChatDiffStatus, error) {
requests := []RefreshRequest{{
Row: row,
OwnerID: ownerID,
}}
results, err := w.refresher.Refresh(ctx, requests)
if err != nil {
return nil, xerrors.Errorf("refresh chat diff status: %w", err)
}
if len(results) == 0 {
return nil, nil //nolint:nilnil // Documented: nil, nil means no PR exists yet.
}
res := results[0]
if errors.Is(res.Error, ErrStalePullRequest) {
if err := w.clearStalePR(ctx, row); err != nil {
return nil, err
}
return nil, nil //nolint:nilnil // Documented: nil, nil means no PR exists yet.
}
if res.Error != nil {
return nil, xerrors.Errorf("refresh chat diff status: %w", res.Error)
}
if res.Params == nil {
return nil, nil //nolint:nilnil // Documented: nil, nil means no PR exists yet.
}
upserted, err := w.store.UpsertChatDiffStatus(ctx, *res.Params)
if err != nil {
return nil, xerrors.Errorf("upsert chat diff status: %w", err)
}
if w.publishDiffStatusChangeFn != nil {
if err := w.publishDiffStatusChangeFn(ctx, row.ChatID, codersdk.DiffStatusRef{RemoteOrigin: row.GitRemoteOrigin, GitBranch: row.GitBranch}); err != nil {
w.logger.Debug(ctx, "publish diff status change",
slog.F("chat_id", row.ChatID),
slog.Error(err))
}
}
return &upserted, nil
}
+8 -169
View File
@@ -1190,7 +1190,7 @@ func TestWorker_RefreshDoesNotReorderPrimary(t *testing.T) {
ChatID: chat.ID,
GitBranch: "new",
GitRemoteOrigin: "https://github.com/o/r",
StaleAt: time.Now().Add(-time.Minute),
StaleAt: time.Now().Add(time.Hour),
Url: sql.NullString{},
})
require.NoError(t, err)
@@ -1201,15 +1201,15 @@ func TestWorker_RefreshDoesNotReorderPrimary(t *testing.T) {
require.Equal(t, []string{"old", "new"}, gitBranches(before), "list follows first report")
require.Equal(t, "new", db2sdk.PrimaryChatDiffStatus(before).GitBranch)
// Refresh the old ref only. Both rows are stale, so acquire the
// old one explicitly through the sync path.
oldRow := before[0]
mClock := quartz.NewMock(t)
refresher := newTestRefresher(t, mClock)
tickDone := make(chan struct{})
pub := func(context.Context, uuid.UUID, codersdk.DiffStatusRef) error {
close(tickDone)
return nil
}
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
worker := gitsync.NewWorker(db, refresher, nil, mClock, logger)
_, err = worker.RefreshChat(ctx, oldRow, user.ID)
require.NoError(t, err)
worker := gitsync.NewWorker(db, newTestRefresher(t, mClock), pub, mClock, logger)
tickOnce(ctx, t, mClock, worker, tickDone)
// The refreshed old ref must not steal the primary from the
// newer report.
@@ -1244,167 +1244,6 @@ func gitBranches(statuses []database.ChatDiffStatus) []string {
return branches
}
func TestRefreshChat_Success(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
chatID := uuid.New()
ownerID := uuid.New()
row := database.ChatDiffStatus{
ChatID: chatID,
GitBranch: "feature",
GitRemoteOrigin: "https://github.com/owner/repo",
}
ctrl := gomock.NewController(t)
store := dbmock.NewMockStore(ctrl)
upsertedStatus := database.ChatDiffStatus{
ChatID: chatID,
Url: sql.NullString{String: "https://github.com/o/r/pull/1", Valid: true},
Additions: 10,
Deletions: 3,
ChangedFiles: 2,
}
store.EXPECT().UpsertChatDiffStatus(gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, arg database.UpsertChatDiffStatusParams) (database.ChatDiffStatus, error) {
assert.Equal(t, chatID, arg.ChatID)
return upsertedStatus, nil
})
var publishCalled atomic.Bool
pub := func(_ context.Context, id uuid.UUID, _ codersdk.DiffStatusRef) error {
assert.Equal(t, chatID, id)
publishCalled.Store(true)
return nil
}
mClock := quartz.NewMock(t)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
refresher := newTestRefresher(t, mClock)
worker := gitsync.NewWorker(store, refresher, pub, mClock, logger)
result, err := worker.RefreshChat(ctx, row, ownerID)
require.NoError(t, err)
require.NotNil(t, result)
assert.Equal(t, chatID, result.ChatID)
assert.Equal(t, upsertedStatus.Url, result.Url)
assert.True(t, publishCalled.Load(), "publish should have been called")
}
func TestRefreshChat_NoPR(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
chatID := uuid.New()
ownerID := uuid.New()
row := database.ChatDiffStatus{
ChatID: chatID,
GitBranch: "feature",
GitRemoteOrigin: "https://github.com/owner/repo",
}
ctrl := gomock.NewController(t)
store := dbmock.NewMockStore(ctrl)
// UpsertChatDiffStatus should NOT be called.
var publishCalled atomic.Bool
pub := func(_ context.Context, _ uuid.UUID, _ codersdk.DiffStatusRef) error {
publishCalled.Store(true)
return nil
}
mClock := quartz.NewMock(t)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
// ResolveBranchPullRequest returns nil → no PR exists yet.
refresher := newTestRefresher(t, mClock, withResolveBranchPR(
func(context.Context, string, gitprovider.BranchRef) (*gitprovider.PRRef, error) {
return nil, nil //nolint:nilnil // Provider contract: nil, nil means no open PR.
},
))
worker := gitsync.NewWorker(store, refresher, pub, mClock, logger)
result, err := worker.RefreshChat(ctx, row, ownerID)
require.NoError(t, err)
assert.Nil(t, result, "result should be nil when no PR exists")
assert.False(t, publishCalled.Load(), "publish should not be called when no PR exists")
}
func TestRefreshChat_RefreshError(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
chatID := uuid.New()
ownerID := uuid.New()
row := database.ChatDiffStatus{
ChatID: chatID,
Url: sql.NullString{String: "https://github.com/org/repo/pull/1", Valid: true},
GitBranch: "feature",
GitRemoteOrigin: "https://github.com/owner/repo",
}
ctrl := gomock.NewController(t)
store := dbmock.NewMockStore(ctrl)
// UpsertChatDiffStatus should NOT be called.
// Provider resolver returns nil → "no provider" error.
providers := func(context.Context, string) gitprovider.Provider { return nil }
tokens := func(context.Context, uuid.UUID, string) (*string, error) {
return new("tok"), nil
}
mClock := quartz.NewMock(t)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
refresher := gitsync.NewRefresher(providers, tokens, logger, mClock)
worker := gitsync.NewWorker(store, refresher, nil, mClock, logger)
result, err := worker.RefreshChat(ctx, row, ownerID)
require.Error(t, err)
assert.Contains(t, err.Error(), "no provider")
assert.Nil(t, result)
}
func TestRefreshChat_UpsertError(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
chatID := uuid.New()
ownerID := uuid.New()
row := database.ChatDiffStatus{
ChatID: chatID,
GitBranch: "feature",
GitRemoteOrigin: "https://github.com/owner/repo",
}
ctrl := gomock.NewController(t)
store := dbmock.NewMockStore(ctrl)
store.EXPECT().UpsertChatDiffStatus(gomock.Any(), gomock.Any()).
Return(database.ChatDiffStatus{}, xerrors.New("db write error"))
var publishCalled atomic.Bool
pub := func(_ context.Context, _ uuid.UUID, _ codersdk.DiffStatusRef) error {
publishCalled.Store(true)
return nil
}
mClock := quartz.NewMock(t)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
refresher := newTestRefresher(t, mClock)
worker := gitsync.NewWorker(store, refresher, pub, mClock, logger)
result, err := worker.RefreshChat(ctx, row, ownerID)
require.Error(t, err)
assert.Contains(t, err.Error(), "upsert chat diff status")
assert.Nil(t, result)
assert.False(t, publishCalled.Load(), "publish should not be called when upsert fails")
}
func TestWorker_NoTokenBackoff(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)