|
|
|
@ -9,6 +9,8 @@ import ( |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/timeutil" |
|
|
|
|
|
|
|
|
|
"xorm.io/xorm" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Stopwatch represents a stopwatch for time tracking.
|
|
|
|
@ -61,8 +63,12 @@ func StopwatchExists(userID, issueID int64) bool { |
|
|
|
|
|
|
|
|
|
// HasUserStopwatch returns true if the user has a stopwatch
|
|
|
|
|
func HasUserStopwatch(userID int64) (exists bool, sw *Stopwatch, err error) { |
|
|
|
|
return hasUserStopwatch(x, userID) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func hasUserStopwatch(e Engine, userID int64) (exists bool, sw *Stopwatch, err error) { |
|
|
|
|
sw = new(Stopwatch) |
|
|
|
|
exists, err = x. |
|
|
|
|
exists, err = e. |
|
|
|
|
Where("user_id = ?", userID). |
|
|
|
|
Get(sw) |
|
|
|
|
return |
|
|
|
@ -70,11 +76,23 @@ func HasUserStopwatch(userID int64) (exists bool, sw *Stopwatch, err error) { |
|
|
|
|
|
|
|
|
|
// CreateOrStopIssueStopwatch will create or remove a stopwatch and will log it into issue's timeline.
|
|
|
|
|
func CreateOrStopIssueStopwatch(user *User, issue *Issue) error { |
|
|
|
|
sw, exists, err := getStopwatch(x, user.ID, issue.ID) |
|
|
|
|
sess := x.NewSession() |
|
|
|
|
defer sess.Close() |
|
|
|
|
if err := sess.Begin(); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
if err := createOrStopIssueStopwatch(sess, user, issue); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
return sess.Commit() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func createOrStopIssueStopwatch(e *xorm.Session, user *User, issue *Issue) error { |
|
|
|
|
sw, exists, err := getStopwatch(e, user.ID, issue.ID) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
if err := issue.loadRepo(x); err != nil { |
|
|
|
|
if err := issue.loadRepo(e); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -90,11 +108,11 @@ func CreateOrStopIssueStopwatch(user *User, issue *Issue) error { |
|
|
|
|
Time: timediff, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if _, err := x.Insert(tt); err != nil { |
|
|
|
|
if _, err := e.Insert(tt); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if _, err := CreateComment(&CreateCommentOptions{ |
|
|
|
|
if _, err := createComment(e, &CreateCommentOptions{ |
|
|
|
|
Doer: user, |
|
|
|
|
Issue: issue, |
|
|
|
|
Repo: issue.Repo, |
|
|
|
@ -104,21 +122,21 @@ func CreateOrStopIssueStopwatch(user *User, issue *Issue) error { |
|
|
|
|
}); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
if _, err := x.Delete(sw); err != nil { |
|
|
|
|
if _, err := e.Delete(sw); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// if another stopwatch is running: stop it
|
|
|
|
|
exists, sw, err := HasUserStopwatch(user.ID) |
|
|
|
|
exists, sw, err := hasUserStopwatch(e, user.ID) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
if exists { |
|
|
|
|
issue, err := getIssueByID(x, sw.IssueID) |
|
|
|
|
issue, err := getIssueByID(e, sw.IssueID) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
if err := CreateOrStopIssueStopwatch(user, issue); err != nil { |
|
|
|
|
if err := createOrStopIssueStopwatch(e, user, issue); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -129,11 +147,11 @@ func CreateOrStopIssueStopwatch(user *User, issue *Issue) error { |
|
|
|
|
IssueID: issue.ID, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if _, err := x.Insert(sw); err != nil { |
|
|
|
|
if _, err := e.Insert(sw); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if _, err := CreateComment(&CreateCommentOptions{ |
|
|
|
|
if _, err := createComment(e, &CreateCommentOptions{ |
|
|
|
|
Doer: user, |
|
|
|
|
Issue: issue, |
|
|
|
|
Repo: issue.Repo, |
|
|
|
@ -147,21 +165,33 @@ func CreateOrStopIssueStopwatch(user *User, issue *Issue) error { |
|
|
|
|
|
|
|
|
|
// CancelStopwatch removes the given stopwatch and logs it into issue's timeline.
|
|
|
|
|
func CancelStopwatch(user *User, issue *Issue) error { |
|
|
|
|
sw, exists, err := getStopwatch(x, user.ID, issue.ID) |
|
|
|
|
sess := x.NewSession() |
|
|
|
|
defer sess.Close() |
|
|
|
|
if err := sess.Begin(); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
if err := cancelStopwatch(sess, user, issue); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
return sess.Commit() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func cancelStopwatch(e *xorm.Session, user *User, issue *Issue) error { |
|
|
|
|
sw, exists, err := getStopwatch(e, user.ID, issue.ID) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if exists { |
|
|
|
|
if _, err := x.Delete(sw); err != nil { |
|
|
|
|
if _, err := e.Delete(sw); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err := issue.loadRepo(x); err != nil { |
|
|
|
|
if err := issue.loadRepo(e); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if _, err := CreateComment(&CreateCommentOptions{ |
|
|
|
|
if _, err := createComment(e, &CreateCommentOptions{ |
|
|
|
|
Doer: user, |
|
|
|
|
Issue: issue, |
|
|
|
|
Repo: issue.Repo, |
|
|
|
|