|
9 | 9 | "time"
|
10 | 10 |
|
11 | 11 | "code.gitea.io/gitea/models/db"
|
| 12 | + "code.gitea.io/gitea/models/repo" |
12 | 13 | user_model "code.gitea.io/gitea/models/user"
|
13 | 14 | "code.gitea.io/gitea/modules/timeutil"
|
14 | 15 | "code.gitea.io/gitea/modules/util"
|
@@ -132,12 +133,26 @@ func StopwatchExists(userID, issueID int64) bool {
|
132 | 133 | }
|
133 | 134 |
|
134 | 135 | // HasUserStopwatch returns true if the user has a stopwatch
|
135 |
| -func HasUserStopwatch(ctx context.Context, userID int64) (exists bool, sw *Stopwatch, err error) { |
136 |
| - sw = new(Stopwatch) |
| 136 | +func HasUserStopwatch(ctx context.Context, userID int64) (exists bool, sw *Stopwatch, issue *Issue, err error) { |
| 137 | + type stopwatchIssueRepo struct { |
| 138 | + Stopwatch `xorm:"extends"` |
| 139 | + Issue `xorm:"extends"` |
| 140 | + repo.Repository `xorm:"extends"` |
| 141 | + } |
| 142 | + |
| 143 | + swIR := new(stopwatchIssueRepo) |
137 | 144 | exists, err = db.GetEngine(ctx).
|
| 145 | + Table("stopwatch"). |
138 | 146 | Where("user_id = ?", userID).
|
139 |
| - Get(sw) |
140 |
| - return exists, sw, err |
| 147 | + Join("INNER", "issue", "issue.id = stopwatch.issue_id"). |
| 148 | + Join("INNER", "repository", "repository.id = issue.repo_id"). |
| 149 | + Get(swIR) |
| 150 | + if exists { |
| 151 | + sw = &swIR.Stopwatch |
| 152 | + issue = &swIR.Issue |
| 153 | + issue.Repo = &swIR.Repository |
| 154 | + } |
| 155 | + return exists, sw, issue, err |
141 | 156 | }
|
142 | 157 |
|
143 | 158 | // FinishIssueStopwatchIfPossible if stopwatch exist then finish it otherwise ignore
|
@@ -217,23 +232,18 @@ func CreateIssueStopwatch(ctx context.Context, user *user_model.User, issue *Iss
|
217 | 232 | }
|
218 | 233 |
|
219 | 234 | // if another stopwatch is running: stop it
|
220 |
| - exists, sw, err := HasUserStopwatch(ctx, user.ID) |
| 235 | + exists, _, otherIssue, err := HasUserStopwatch(ctx, user.ID) |
221 | 236 | if err != nil {
|
222 | 237 | return err
|
223 | 238 | }
|
224 | 239 | if exists {
|
225 |
| - issue, err := GetIssueByID(ctx, sw.IssueID) |
226 |
| - if err != nil { |
227 |
| - return err |
228 |
| - } |
229 |
| - |
230 |
| - if err := FinishIssueStopwatch(ctx, user, issue); err != nil { |
| 240 | + if err := FinishIssueStopwatch(ctx, user, otherIssue); err != nil { |
231 | 241 | return err
|
232 | 242 | }
|
233 | 243 | }
|
234 | 244 |
|
235 | 245 | // Create stopwatch
|
236 |
| - sw = &Stopwatch{ |
| 246 | + sw := &Stopwatch{ |
237 | 247 | UserID: user.ID,
|
238 | 248 | IssueID: issue.ID,
|
239 | 249 | }
|
|
0 commit comments