Skip to content

Commit e10cd3d

Browse files
6543zeripath
andauthored
Fix session bugs (#16552) (#16553)
* Fix session bugs (#16552) * fix deadlog bug * Fix models/issue_stopwatch.go * Update models/issue_stopwatch.go Co-authored-by: zeripath <[email protected]> * fix getLatestCommitStatus Co-authored-by: zeripath <[email protected]>
1 parent 6932754 commit e10cd3d

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

models/commit_status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func getLatestCommitStatus(e Engine, repoID int64, sha string, listOptions ListO
160160
if len(ids) == 0 {
161161
return statuses, nil
162162
}
163-
return statuses, x.In("id", ids).Find(&statuses)
163+
return statuses, e.In("id", ids).Find(&statuses)
164164
}
165165

166166
// FindRepoRecentCommitStatusContexts returns repository's recent commit status contexts

models/issue_stopwatch.go

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"time"
1010

1111
"code.gitea.io/gitea/modules/timeutil"
12+
13+
"xorm.io/xorm"
1214
)
1315

1416
// Stopwatch represents a stopwatch for time tracking.
@@ -61,20 +63,36 @@ func StopwatchExists(userID, issueID int64) bool {
6163

6264
// HasUserStopwatch returns true if the user has a stopwatch
6365
func HasUserStopwatch(userID int64) (exists bool, sw *Stopwatch, err error) {
66+
return hasUserStopwatch(x, userID)
67+
}
68+
69+
func hasUserStopwatch(e Engine, userID int64) (exists bool, sw *Stopwatch, err error) {
6470
sw = new(Stopwatch)
65-
exists, err = x.
71+
exists, err = e.
6672
Where("user_id = ?", userID).
6773
Get(sw)
6874
return
6975
}
7076

7177
// CreateOrStopIssueStopwatch will create or remove a stopwatch and will log it into issue's timeline.
7278
func CreateOrStopIssueStopwatch(user *User, issue *Issue) error {
73-
sw, exists, err := getStopwatch(x, user.ID, issue.ID)
79+
sess := x.NewSession()
80+
defer sess.Close()
81+
if err := sess.Begin(); err != nil {
82+
return err
83+
}
84+
if err := createOrStopIssueStopwatch(sess, user, issue); err != nil {
85+
return err
86+
}
87+
return sess.Commit()
88+
}
89+
90+
func createOrStopIssueStopwatch(e *xorm.Session, user *User, issue *Issue) error {
91+
sw, exists, err := getStopwatch(e, user.ID, issue.ID)
7492
if err != nil {
7593
return err
7694
}
77-
if err := issue.loadRepo(x); err != nil {
95+
if err := issue.loadRepo(e); err != nil {
7896
return err
7997
}
8098

@@ -90,11 +108,11 @@ func CreateOrStopIssueStopwatch(user *User, issue *Issue) error {
90108
Time: timediff,
91109
}
92110

93-
if _, err := x.Insert(tt); err != nil {
111+
if _, err := e.Insert(tt); err != nil {
94112
return err
95113
}
96114

97-
if _, err := CreateComment(&CreateCommentOptions{
115+
if _, err := createComment(e, &CreateCommentOptions{
98116
Doer: user,
99117
Issue: issue,
100118
Repo: issue.Repo,
@@ -104,21 +122,21 @@ func CreateOrStopIssueStopwatch(user *User, issue *Issue) error {
104122
}); err != nil {
105123
return err
106124
}
107-
if _, err := x.Delete(sw); err != nil {
125+
if _, err := e.Delete(sw); err != nil {
108126
return err
109127
}
110128
} else {
111129
// if another stopwatch is running: stop it
112-
exists, sw, err := HasUserStopwatch(user.ID)
130+
exists, sw, err := hasUserStopwatch(e, user.ID)
113131
if err != nil {
114132
return err
115133
}
116134
if exists {
117-
issue, err := getIssueByID(x, sw.IssueID)
135+
issue, err := getIssueByID(e, sw.IssueID)
118136
if err != nil {
119137
return err
120138
}
121-
if err := CreateOrStopIssueStopwatch(user, issue); err != nil {
139+
if err := createOrStopIssueStopwatch(e, user, issue); err != nil {
122140
return err
123141
}
124142
}
@@ -129,11 +147,11 @@ func CreateOrStopIssueStopwatch(user *User, issue *Issue) error {
129147
IssueID: issue.ID,
130148
}
131149

132-
if _, err := x.Insert(sw); err != nil {
150+
if _, err := e.Insert(sw); err != nil {
133151
return err
134152
}
135153

136-
if _, err := CreateComment(&CreateCommentOptions{
154+
if _, err := createComment(e, &CreateCommentOptions{
137155
Doer: user,
138156
Issue: issue,
139157
Repo: issue.Repo,
@@ -147,21 +165,33 @@ func CreateOrStopIssueStopwatch(user *User, issue *Issue) error {
147165

148166
// CancelStopwatch removes the given stopwatch and logs it into issue's timeline.
149167
func CancelStopwatch(user *User, issue *Issue) error {
150-
sw, exists, err := getStopwatch(x, user.ID, issue.ID)
168+
sess := x.NewSession()
169+
defer sess.Close()
170+
if err := sess.Begin(); err != nil {
171+
return err
172+
}
173+
if err := cancelStopwatch(sess, user, issue); err != nil {
174+
return err
175+
}
176+
return sess.Commit()
177+
}
178+
179+
func cancelStopwatch(e *xorm.Session, user *User, issue *Issue) error {
180+
sw, exists, err := getStopwatch(e, user.ID, issue.ID)
151181
if err != nil {
152182
return err
153183
}
154184

155185
if exists {
156-
if _, err := x.Delete(sw); err != nil {
186+
if _, err := e.Delete(sw); err != nil {
157187
return err
158188
}
159189

160-
if err := issue.loadRepo(x); err != nil {
190+
if err := issue.loadRepo(e); err != nil {
161191
return err
162192
}
163193

164-
if _, err := CreateComment(&CreateCommentOptions{
194+
if _, err := createComment(e, &CreateCommentOptions{
165195
Doer: user,
166196
Issue: issue,
167197
Repo: issue.Repo,

models/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ func CreateRepository(ctx DBContext, doer, u *User, repo *Repository, overwriteO
11251125

11261126
// Give access to all members in teams with access to all repositories.
11271127
if u.IsOrganization() {
1128-
if err := u.GetTeams(&SearchTeamOptions{}); err != nil {
1128+
if err := u.getTeams(ctx.e); err != nil {
11291129
return fmt.Errorf("GetTeams: %v", err)
11301130
}
11311131
for _, t := range u.Teams {

0 commit comments

Comments
 (0)