diff --git a/models/repo/watch.go b/models/repo/watch.go index 3fd915e1e7..ceec20fdd6 100644 --- a/models/repo/watch.go +++ b/models/repo/watch.go @@ -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) diff --git a/models/repo/watch_test.go b/models/repo/watch_test.go index 698f6a5f49..ccc56ad168 100644 --- a/models/repo/watch_test.go +++ b/models/repo/watch_test.go @@ -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) diff --git a/services/repository/push.go b/services/repository/push.go index eaedd80e1f..f42b231f8b 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -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 {