chore: remove dead code in WatchIfAuto and general model documentation (#10880)

I've been reading through the watch model code and found that the `isWrite` flag is dead code — it isn't used anywhere. To be honest I don't see why we should need that anyways. Therefore, I deleted it.

Also I've added some comments for the `WatchMode`. It took me quite some time to figure out why there are four options.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10880
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: christopher-besch <mail@chris-besch.com>
Co-committed-by: christopher-besch <mail@chris-besch.com>
This commit is contained in:
christopher-besch 2026-01-26 12:03:08 +01:00 committed by Gusted
commit 7408bf9b1e
3 changed files with 18 additions and 9 deletions

View file

@ -17,12 +17,20 @@ type WatchMode int8
const (
// WatchModeNone don't watch
// This means there is no Watch record in the db.
// We never store this mode in the db and instead remove the record from the db.
// Furthermore, this means there is a WatchMode for all combinations of user and repo.
WatchModeNone WatchMode = iota // 0
// WatchModeNormal watch repository (from other sources)
// This means the user explicitly chose to watch the repo.
WatchModeNormal // 1
// WatchModeDont explicit don't auto-watch
// This means the user explicitly removed themselves as a watcher.
// Then the AutoWatchOnChanges feature doesn't make the user a watcher when they push to the repo.
WatchModeDont // 2
// WatchModeAuto watch repository (from AutoWatchOnChanges)
// This is used when the user pushed to the repo and setting.Service.AutoWatchOnChanges is true.
// That way we can differentiate people explicitly watching the repo and people only watching it because of the AutoWatchOnChanges feature.
WatchModeAuto // 3
)
@ -74,6 +82,7 @@ func watchRepoMode(ctx context.Context, watch Watch, mode WatchMode) (err error)
}
hadrec := watch.Mode != WatchModeNone
// WatchModeNone means there is no record in the db.
needsrec := mode != WatchModeNone
repodiff := 0
@ -169,8 +178,8 @@ func GetRepoWatchers(ctx context.Context, repoID int64, opts db.ListOptions) ([]
}
// WatchIfAuto subscribes to repo if AutoWatchOnChanges is set
func WatchIfAuto(ctx context.Context, userID, repoID int64, isWrite bool) error {
if !isWrite || !setting.Service.AutoWatchOnChanges {
func WatchIfAuto(ctx context.Context, userID, repoID int64) error {
if !setting.Service.AutoWatchOnChanges {
return nil
}
watch, err := GetWatch(ctx, userID, repoID)

View file

@ -74,13 +74,13 @@ func TestWatchIfAuto(t *testing.T) {
prevCount := repo.NumWatches
// Must not add watch
require.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 8, 1, true))
require.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 8, 1))
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
require.NoError(t, err)
assert.Len(t, watchers, prevCount)
// Should not add watch
require.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 10, 1, true))
require.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 10, 1))
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
require.NoError(t, err)
assert.Len(t, watchers, prevCount)
@ -88,19 +88,19 @@ func TestWatchIfAuto(t *testing.T) {
setting.Service.AutoWatchOnChanges = true
// Must not add watch
require.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 8, 1, true))
require.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 8, 1))
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
require.NoError(t, err)
assert.Len(t, watchers, prevCount)
// Should not add watch
require.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1, false))
// We simply don't WatchIfAuto
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
require.NoError(t, err)
assert.Len(t, watchers, prevCount)
// Should add watch
require.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1, true))
require.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1))
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
require.NoError(t, err)
assert.Len(t, watchers, prevCount+1)
@ -112,7 +112,7 @@ func TestWatchIfAuto(t *testing.T) {
assert.Len(t, watchers, prevCount)
// Must not add watch
require.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1, true))
require.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1))
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
require.NoError(t, err)
assert.Len(t, watchers, prevCount)

View file

@ -271,7 +271,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
}
// Even if user delete a branch on a repository which he didn't watch, he will be watch that.
if err = repo_model.WatchIfAuto(ctx, opts.PusherID, repo.ID, true); err != nil {
if err = repo_model.WatchIfAuto(ctx, opts.PusherID, repo.ID); err != nil {
log.Warn("Fail to perform auto watch on user %v for repo %v: %v", opts.PusherID, repo.ID, err)
}
} else {