enhance: refine repo watching (#38835)

Follow-up to https://github.com/go-gitea/gitea/pull/37571.

"Participating and mentions" deleted the watch row, so choosing it
dropped you out of the watcher count. It is a watch like the others, so
it now keeps a row and simply subscribes to no events.

The dashboard feed ignored the per-event options, so a "Custom: issues"
watcher still got pull request activity there. It now gates on the same
options as mail and notifications. That also closes a gap where pull
request reviews bypassed the permission check.

Also, address
https://github.com/go-gitea/gitea/pull/37571#discussion_r3740487363 and
reword a UI text for clarity.

---------

Signed-off-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
silverwind
2026-08-09 10:54:31 +00:00
committed by GitHub
co-authored by wxiaoguang
parent 76a81b24f9
commit ad6107ab88
14 changed files with 137 additions and 92 deletions
+1 -2
View File
@@ -644,8 +644,7 @@ func repoAssignmentPrepareTemplateData(ctx *Context, data *repoAssignmentPrepare
ctx.ServerError("GetWatch", err)
return
}
ctx.Data["Watch"] = watch
ctx.Data["IsWatchingRepo"] = repo_model.IsWatchMode(watch.Mode)
ctx.Data["RepoWatch"] = watch
ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, repo.ID)
}
+14 -12
View File
@@ -69,20 +69,23 @@ func notifyWatchers(ctx context.Context, act *activities_model.Action, watchers
act.UserID = watcher.UserID
act.Repo.Units = nil
var allowed bool
switch act.OpType {
case activities_model.ActionCommitRepo, activities_model.ActionPushTag, activities_model.ActionDeleteTag, activities_model.ActionPublishRelease, activities_model.ActionDeleteBranch:
if !permCode[i] {
continue
}
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
case activities_model.ActionCreateIssue, activities_model.ActionCommentIssue, activities_model.ActionCloseIssue, activities_model.ActionReopenIssue:
if !permIssue[i] {
continue
}
case activities_model.ActionCreatePullRequest, activities_model.ActionCommentPull, activities_model.ActionMergePullRequest, activities_model.ActionClosePullRequest, activities_model.ActionReopenPullRequest, activities_model.ActionAutoMergePullRequest:
if !permPR[i] {
continue
}
allowed = permIssue[i] && watcher.Issues
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
default:
allowed = watcher.IsWatchingAll() // repository events have no watch option of their own
}
if !allowed {
continue
}
if err := db.Insert(ctx, act); err != nil {
@@ -120,7 +123,6 @@ func NotifyWatchers(ctx context.Context, acts ...*activities_model.Action) error
if err != nil {
return fmt.Errorf("get watchers: %w", err)
}
permCode := make([]bool, len(watchers))
permIssue := make([]bool, len(watchers))
permPR := make([]bool, len(watchers))
+16
View File
@@ -200,3 +200,19 @@ func TestNotifyWatchers(t *testing.T) {
OpType: action.OpType,
})
}
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}))
assert.NoError(t, NotifyWatchers(t.Context(),
&activities_model.Action{ActUserID: 8, RepoID: 1, OpType: activities_model.ActionCreateIssue},
&activities_model.Action{ActUserID: 8, RepoID: 1, OpType: activities_model.ActionApprovePullRequest},
))
unittest.AssertExistsAndLoadBean(t, &activities_model.Action{UserID: 1, RepoID: 1, OpType: activities_model.ActionCreateIssue})
unittest.AssertNotExistsBean(t, &activities_model.Action{UserID: 1, RepoID: 1, OpType: activities_model.ActionApprovePullRequest})
unittest.AssertExistsAndLoadBean(t, &activities_model.Action{UserID: 4, RepoID: 1, OpType: activities_model.ActionApprovePullRequest})
}