chore: fix repo watch (#38921)

This commit is contained in:
wxiaoguang
2026-08-16 11:00:59 +08:00
committed by GitHub
parent 56ad4689ad
commit 5e4d21acd5
26 changed files with 156 additions and 137 deletions
+3 -3
View File
@@ -74,13 +74,13 @@ func notifyWatchers(ctx context.Context, act *activities_model.Action, watchers
case activities_model.ActionCommitRepo, activities_model.ActionPushTag, activities_model.ActionDeleteTag, activities_model.ActionDeleteBranch:
allowed = permCode[i] && watcher.IsWatchingAll()
case activities_model.ActionPublishRelease:
allowed = permCode[i] && watcher.Releases
allowed = permCode[i] && watcher.IncludeReleases
case activities_model.ActionCreateIssue, activities_model.ActionCommentIssue, activities_model.ActionCloseIssue, activities_model.ActionReopenIssue:
allowed = permIssue[i] && watcher.Issues
allowed = permIssue[i] && watcher.IncludeIssues
case activities_model.ActionCreatePullRequest, activities_model.ActionCommentPull, activities_model.ActionMergePullRequest, activities_model.ActionClosePullRequest,
activities_model.ActionReopenPullRequest, activities_model.ActionAutoMergePullRequest, activities_model.ActionApprovePullRequest,
activities_model.ActionRejectPullRequest, activities_model.ActionPullReviewDismissed, activities_model.ActionPullRequestReadyForReview:
allowed = permPR[i] && watcher.PullRequests
allowed = permPR[i] && watcher.IncludePullRequests
default:
allowed = watcher.IsWatchingAll() // repository events have no watch option of their own
}
+3 -1
View File
@@ -205,7 +205,9 @@ func TestNotifyWatchersRespectsWatchOptions(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
// user 1 watches repo 1 for issues only, user 4 keeps every event
assert.NoError(t, repo_model.SetWatchOptions(t.Context(), 1, 1, repo_model.WatchOptions{Issues: true}))
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
assert.NoError(t, repo_model.WatchRepoWithOptions(t.Context(), user1, repo1, repo_model.WatchOptions{Mode: repo_model.WatchModeNormal, WatchIssues: true}))
assert.NoError(t, NotifyWatchers(t.Context(),
&activities_model.Action{ActUserID: 8, RepoID: 1, OpType: activities_model.ActionCreateIssue},
+2 -2
View File
@@ -40,8 +40,8 @@ func TestMailNewReleaseFiltersUnauthorizedWatchers(t *testing.T) {
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
unauthorized := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
assert.NoError(t, repo_model.WatchRepo(t.Context(), admin, repo, true))
assert.NoError(t, repo_model.WatchRepo(t.Context(), unauthorized, repo, true))
assert.NoError(t, repo_model.WatchRepoAuto(t.Context(), admin, repo, true))
assert.NoError(t, repo_model.WatchRepoAuto(t.Context(), unauthorized, repo, true))
rel := unittest.AssertExistsAndLoadBean(t, &repo_model.Release{ID: 11})
rel.Repo = nil
+1 -1
View File
@@ -77,7 +77,7 @@ func TestOrg(t *testing.T) {
// an outside user watches and stars the repo while the org is still visible
watcher := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
require.NoError(t, repo_model.WatchRepo(t.Context(), watcher, repo, true))
require.NoError(t, repo_model.WatchRepoAuto(t.Context(), watcher, repo, true))
require.NoError(t, repo_model.StarRepo(t.Context(), watcher, repo, true))
unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{UserID: watcher.ID, RepoID: repo.ID})
+1 -1
View File
@@ -263,7 +263,7 @@ func AddTeamMember(ctx context.Context, team *organization.Team, user *user_mode
go func(repos []*repo_model.Repository) {
for _, repo := range repos {
if err = repo_model.WatchRepo(graceful.GetManager().ShutdownContext(), user, repo, true); err != nil {
if err = repo_model.WatchRepoAuto(graceful.GetManager().ShutdownContext(), user, repo, true); err != nil {
log.Error("watch repo failed: %v", err)
}
}
+2 -2
View File
@@ -72,7 +72,7 @@ func TestRemoveTeamMemberRemovesSubscriptionsAndStopwatches(t *testing.T) {
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
assert.NoError(t, repo_model.WatchRepo(ctx, user, repo, true))
assert.NoError(t, repo_model.WatchRepoAuto(ctx, user, repo, true))
assert.NoError(t, issues_model.CreateOrUpdateIssueWatch(ctx, user.ID, issue.ID, true))
ok, err := issues_model.CreateIssueStopwatch(ctx, user, issue)
assert.NoError(t, err)
@@ -82,7 +82,7 @@ func TestRemoveTeamMemberRemovesSubscriptionsAndStopwatches(t *testing.T) {
watch, err := repo_model.GetWatch(ctx, user.ID, repo.ID)
assert.NoError(t, err)
assert.False(t, repo_model.IsWatchMode(watch.Mode))
assert.False(t, repo_model.IsWatchModeWatching(watch.Mode))
_, exists, err := issues_model.GetIssueWatch(ctx, user.ID, issue.ID)
assert.NoError(t, err)
+1 -1
View File
@@ -69,7 +69,7 @@ func RemoveOrgUser(ctx context.Context, org *organization.Organization, user *us
if err != nil {
return err
}
if err = repo_model.WatchRepo(ctx, user, repo, false); err != nil {
if err = repo_model.WatchRepoAuto(ctx, user, repo, false); err != nil {
return err
}
}
+2 -2
View File
@@ -88,7 +88,7 @@ func DeleteCollaboration(ctx context.Context, repo *repo_model.Repository, colla
return err
}
if err = repo_model.WatchRepo(ctx, collaborator, repo, false); err != nil {
if err = repo_model.WatchRepoAuto(ctx, collaborator, repo, false); err != nil {
return err
}
@@ -118,7 +118,7 @@ func ReconsiderWatches(ctx context.Context, repo *repo_model.Repository, user *u
if has, err := access_model.HasAnyUnitAccess(ctx, user.ID, repo); err != nil || has {
return err
}
if err := repo_model.WatchRepo(ctx, user, repo, false); err != nil {
if err := repo_model.WatchRepoAuto(ctx, user, repo, false); err != nil {
return err
}
+2 -2
View File
@@ -59,7 +59,7 @@ func TestRepository_DeleteCollaborationRemovesSubscriptionsAndStopwatches(t *tes
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 15})
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 22})
assert.NoError(t, repo.LoadOwner(ctx))
assert.NoError(t, repo_model.WatchRepo(ctx, user, repo, true))
assert.NoError(t, repo_model.WatchRepoAuto(ctx, user, repo, true))
hasAccess, err := access_model.HasAnyUnitAccess(ctx, user.ID, repo)
assert.NoError(t, err)
@@ -88,7 +88,7 @@ func TestRepository_DeleteCollaborationRemovesSubscriptionsAndStopwatches(t *tes
watch, err := repo_model.GetWatch(ctx, user.ID, repo.ID)
assert.NoError(t, err)
assert.False(t, repo_model.IsWatchMode(watch.Mode))
assert.False(t, repo_model.IsWatchModeWatching(watch.Mode))
_, exists, err := issues_model.GetIssueWatch(ctx, user.ID, tempIssue.ID)
assert.NoError(t, err)
+1 -1
View File
@@ -438,7 +438,7 @@ func createRepositoryInDB(ctx context.Context, doer, u *user_model.User, repo *r
}
if setting.Service.AutoWatchNewRepos {
if err = repo_model.WatchRepo(ctx, doer, repo, true); err != nil {
if err = repo_model.WatchRepoAuto(ctx, doer, repo, true); err != nil {
return fmt.Errorf("WatchRepo: %w", err)
}
}
+3 -3
View File
@@ -50,7 +50,7 @@ func addRepositoryToTeam(ctx context.Context, t *organization.Team, repo *repo_m
return fmt.Errorf("getMembers: %w", err)
}
for _, u := range t.Members {
if err = repo_model.WatchRepo(ctx, u, repo, true); err != nil {
if err = repo_model.WatchRepoAuto(ctx, u, repo, true); err != nil {
return fmt.Errorf("watchRepo: %w", err)
}
}
@@ -117,7 +117,7 @@ func removeAllRepositoriesFromTeam(ctx context.Context, t *organization.Team) (e
continue
}
if err = repo_model.WatchRepo(ctx, user, repo, false); err != nil {
if err = repo_model.WatchRepoAuto(ctx, user, repo, false); err != nil {
return err
}
@@ -198,7 +198,7 @@ func removeRepositoryFromTeam(ctx context.Context, t *organization.Team, repo *r
continue
}
if err = repo_model.WatchRepo(ctx, member, repo, false); err != nil {
if err = repo_model.WatchRepoAuto(ctx, member, repo, false); err != nil {
return err
}
+2 -2
View File
@@ -267,13 +267,13 @@ func transferOwnership(ctx context.Context, doer *user_model.User, newOwnerName
return fmt.Errorf("decrease old owner repository count: %w", err)
}
if err := repo_model.WatchRepo(ctx, doer, repo, true); err != nil {
if err := repo_model.WatchRepoAuto(ctx, doer, repo, true); err != nil {
return fmt.Errorf("watchRepo: %w", err)
}
if oldOwner.IsOrganization() {
// Remove watch for organization.
if err := repo_model.WatchRepo(ctx, oldOwner, repo, false); err != nil {
if err := repo_model.WatchRepoAuto(ctx, oldOwner, repo, false); err != nil {
return fmt.Errorf("watchRepo [false]: %w", err)
}
+1 -1
View File
@@ -183,7 +183,7 @@ func unwatchRepos(ctx context.Context, watcher, repoOwner *user_model.User) erro
}
for _, repo := range repos {
if err := repo_model.WatchRepo(ctx, watcher, repo, false); err != nil {
if err := repo_model.WatchRepoAuto(ctx, watcher, repo, false); err != nil {
return err
}
}