Skip to content

Commit ce99d45

Browse files
author
Guillermo Prandi
committed
Updated deleteUser to take RepoWatchModeDont into account, corrected inverted DefaultWatchOnClone and DefaultWatchOnChanges behaviour, updated and added tests.
1 parent 6e4a28d commit ce99d45

File tree

7 files changed

+133
-9
lines changed

7 files changed

+133
-9
lines changed

models/consistency.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,17 @@ func (user *User) checkForConsistency(t *testing.T) {
8484
func (repo *Repository) checkForConsistency(t *testing.T) {
8585
assert.Equal(t, repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo)
8686
assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars)
87-
assertCount(t, &Watch{RepoID: repo.ID}, repo.NumWatches)
8887
assertCount(t, &Milestone{RepoID: repo.ID}, repo.NumMilestones)
8988
assertCount(t, &Repository{ForkID: repo.ID}, repo.NumForks)
9089
if repo.IsFork {
9190
AssertExistsAndLoadBean(t, &Repository{ID: repo.ForkID})
9291
}
9392

94-
actual := getCount(t, x.Where("is_pull=?", false), &Issue{RepoID: repo.ID})
93+
actual := getCount(t, x.Where("Mode<>?", RepoWatchModeDont), &Watch{RepoID: repo.ID})
94+
assert.EqualValues(t, repo.NumWatches, actual,
95+
"Unexpected number of watches for repo %+v", repo)
96+
97+
actual = getCount(t, x.Where("is_pull=?", false), &Issue{RepoID: repo.ID})
9598
assert.EqualValues(t, repo.NumIssues, actual,
9699
"Unexpected number of issues for repo %+v", repo)
97100

models/fixtures/repository.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
num_closed_pulls: 0
1111
num_milestones: 3
1212
num_closed_milestones: 1
13-
num_watches: 3
13+
num_watches: 4
1414

1515
-
1616
id: 2

models/fixtures/watch.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,28 @@
22
id: 1
33
user_id: 1
44
repo_id: 1
5+
mode: 1 # normal
56

67
-
78
id: 2
89
user_id: 4
910
repo_id: 1
11+
mode: 1 # normal
1012

1113
-
1214
id: 3
1315
user_id: 9
1416
repo_id: 1
17+
mode: 1 # normal
18+
19+
-
20+
id: 4
21+
user_id: 8
22+
repo_id: 1
23+
mode: 2 # don't watch
24+
25+
-
26+
id: 5
27+
user_id: 11
28+
repo_id: 1
29+
mode: 3 # auto

models/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,8 +2261,8 @@ func CheckRepoStats() {
22612261
checkers := []*repoChecker{
22622262
// Repository.NumWatches
22632263
{
2264-
"SELECT repo.id FROM `repository` repo WHERE repo.num_watches!=(SELECT COUNT(*) FROM `watch` WHERE repo_id=repo.id)",
2265-
"UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=?) WHERE id=?",
2264+
"SELECT repo.id FROM `repository` repo WHERE repo.num_watches!=(SELECT COUNT(*) FROM `watch` WHERE repo_id=repo.id AND mode<>2)",
2265+
"UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?",
22662266
"repository count 'num_watches'",
22672267
},
22682268
// Repository.NumStars

models/repo_watch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,11 @@ func NotifyWatchers(act *Action) error {
218218

219219
func watchIfAuto(e Engine, userID, repoID int64, isWrite bool) error {
220220
if isWrite {
221-
if !setting.Service.AutoWatchOnClone {
221+
if !setting.Service.AutoWatchOnChanges {
222222
return nil
223223
}
224224
} else {
225-
if !setting.Service.AutoWatchOnChanges {
225+
if !setting.Service.AutoWatchOnClone {
226226
return nil
227227
}
228228
}

models/repo_watch_test.go

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package models
77
import (
88
"testing"
99

10+
"code.gitea.io/gitea/modules/setting"
11+
1012
"github.com/stretchr/testify/assert"
1113
)
1214

@@ -15,8 +17,10 @@ func TestIsWatching(t *testing.T) {
1517

1618
assert.True(t, IsWatching(1, 1))
1719
assert.True(t, IsWatching(4, 1))
20+
assert.True(t, IsWatching(11, 1))
1821

1922
assert.False(t, IsWatching(1, 5))
23+
assert.False(t, IsWatching(8, 1))
2024
assert.False(t, IsWatching(NonexistentID, NonexistentID))
2125
}
2226

@@ -78,7 +82,7 @@ func TestNotifyWatchers(t *testing.T) {
7882
}
7983
assert.NoError(t, NotifyWatchers(action))
8084

81-
// One watchers are inactive, thus action is only created for user 8, 1, 4
85+
// One watchers are inactive, thus action is only created for user 8, 1, 4, 11
8286
AssertExistsAndLoadBean(t, &Action{
8387
ActUserID: action.ActUserID,
8488
UserID: 8,
@@ -97,4 +101,106 @@ func TestNotifyWatchers(t *testing.T) {
97101
RepoID: action.RepoID,
98102
OpType: action.OpType,
99103
})
104+
AssertExistsAndLoadBean(t, &Action{
105+
ActUserID: action.ActUserID,
106+
UserID: 11,
107+
RepoID: action.RepoID,
108+
OpType: action.OpType,
109+
})
110+
}
111+
112+
func TestWatchIfAuto(t *testing.T) {
113+
assert.NoError(t, PrepareTestDatabase())
114+
115+
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
116+
watchers, err := repo.GetWatchers(1)
117+
assert.NoError(t, err)
118+
assert.Len(t, watchers, repo.NumWatches)
119+
120+
setting.Service.AutoWatchOnClone = false
121+
setting.Service.AutoWatchOnChanges = false
122+
123+
prevCount := repo.NumWatches
124+
125+
// Must not add watch
126+
assert.NoError(t, WatchIfAuto(8, 1, false))
127+
watchers, err = repo.GetWatchers(1)
128+
assert.NoError(t, err)
129+
assert.Len(t, watchers, prevCount)
130+
131+
// Should not add watch
132+
assert.NoError(t, WatchIfAuto(10, 1, false))
133+
watchers, err = repo.GetWatchers(1)
134+
assert.NoError(t, err)
135+
assert.Len(t, watchers, prevCount)
136+
137+
setting.Service.AutoWatchOnClone = true
138+
139+
// Should not add watch
140+
assert.NoError(t, WatchIfAuto(10, 1, true))
141+
watchers, err = repo.GetWatchers(1)
142+
assert.NoError(t, err)
143+
assert.Len(t, watchers, prevCount)
144+
145+
// Should add watch
146+
assert.NoError(t, WatchIfAuto(10, 1, false))
147+
watchers, err = repo.GetWatchers(1)
148+
assert.NoError(t, err)
149+
assert.Len(t, watchers, prevCount+1)
150+
151+
prevCount++
152+
153+
setting.Service.AutoWatchOnClone = false
154+
setting.Service.AutoWatchOnChanges = true
155+
156+
// Must not add watch
157+
assert.NoError(t, WatchIfAuto(8, 1, false))
158+
watchers, err = repo.GetWatchers(1)
159+
assert.NoError(t, err)
160+
assert.Len(t, watchers, prevCount)
161+
162+
// Should not add watch
163+
assert.NoError(t, WatchIfAuto(12, 1, false))
164+
watchers, err = repo.GetWatchers(1)
165+
assert.NoError(t, err)
166+
assert.Len(t, watchers, prevCount)
167+
168+
// Should add watch
169+
assert.NoError(t, WatchIfAuto(12, 1, true))
170+
watchers, err = repo.GetWatchers(1)
171+
assert.NoError(t, err)
172+
assert.Len(t, watchers, prevCount+1)
173+
174+
// Should remove watch, inhibit from adding auto
175+
assert.NoError(t, WatchRepo(12, 1, false))
176+
watchers, err = repo.GetWatchers(1)
177+
assert.NoError(t, err)
178+
assert.Len(t, watchers, prevCount)
179+
180+
// Must not add watch
181+
assert.NoError(t, WatchIfAuto(12, 1, false))
182+
watchers, err = repo.GetWatchers(1)
183+
assert.NoError(t, err)
184+
assert.Len(t, watchers, prevCount)
185+
}
186+
187+
func TestWatchRepoMode(t *testing.T) {
188+
assert.NoError(t, PrepareTestDatabase())
189+
190+
AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0)
191+
192+
assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeAuto))
193+
AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
194+
AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeAuto}, 1)
195+
196+
assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeNormal))
197+
AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
198+
AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeNormal}, 1)
199+
200+
assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeDont))
201+
AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
202+
AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeDont}, 1)
203+
204+
assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeNone))
205+
AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0)
100206
}

models/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ func deleteUser(e *xorm.Session, u *User) error {
10471047
// ***** START: Watch *****
10481048
watchedRepoIDs := make([]int64, 0, 10)
10491049
if err = e.Table("watch").Cols("watch.repo_id").
1050-
Where("watch.user_id = ?", u.ID).Find(&watchedRepoIDs); err != nil {
1050+
Where("watch.user_id = ?", u.ID).And("watch.mode <>?", RepoWatchModeDont).Find(&watchedRepoIDs); err != nil {
10511051
return fmt.Errorf("get all watches: %v", err)
10521052
}
10531053
if _, err = e.Decr("num_watches").In("id", watchedRepoIDs).NoAutoTime().Update(new(Repository)); err != nil {

0 commit comments

Comments
 (0)