|
|
@ -13,6 +13,26 @@ import ( |
|
|
|
"code.gitea.io/gitea/modules/timeutil" |
|
|
|
"code.gitea.io/gitea/modules/timeutil" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ErrIssueStopwatchNotExist represents an error that stopwatch is not exist
|
|
|
|
|
|
|
|
type ErrIssueStopwatchNotExist struct { |
|
|
|
|
|
|
|
UserID int64 |
|
|
|
|
|
|
|
IssueID int64 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (err ErrIssueStopwatchNotExist) Error() string { |
|
|
|
|
|
|
|
return fmt.Sprintf("issue stopwatch doesn't exist[uid: %d, issue_id: %d", err.UserID, err.IssueID) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ErrIssueStopwatchAlreadyExist represents an error that stopwatch is already exist
|
|
|
|
|
|
|
|
type ErrIssueStopwatchAlreadyExist struct { |
|
|
|
|
|
|
|
UserID int64 |
|
|
|
|
|
|
|
IssueID int64 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (err ErrIssueStopwatchAlreadyExist) Error() string { |
|
|
|
|
|
|
|
return fmt.Sprintf("issue stopwatch already exists[uid: %d, issue_id: %d", err.UserID, err.IssueID) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Stopwatch represents a stopwatch for time tracking.
|
|
|
|
// Stopwatch represents a stopwatch for time tracking.
|
|
|
|
type Stopwatch struct { |
|
|
|
type Stopwatch struct { |
|
|
|
ID int64 `xorm:"pk autoincr"` |
|
|
|
ID int64 `xorm:"pk autoincr"` |
|
|
@ -35,9 +55,9 @@ func (s Stopwatch) Duration() string { |
|
|
|
return SecToTime(s.Seconds()) |
|
|
|
return SecToTime(s.Seconds()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func getStopwatch(e db.Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) { |
|
|
|
func getStopwatch(ctx context.Context, userID, issueID int64) (sw *Stopwatch, exists bool, err error) { |
|
|
|
sw = new(Stopwatch) |
|
|
|
sw = new(Stopwatch) |
|
|
|
exists, err = e. |
|
|
|
exists, err = db.GetEngine(ctx). |
|
|
|
Where("user_id = ?", userID). |
|
|
|
Where("user_id = ?", userID). |
|
|
|
And("issue_id = ?", issueID). |
|
|
|
And("issue_id = ?", issueID). |
|
|
|
Get(sw) |
|
|
|
Get(sw) |
|
|
@ -66,7 +86,7 @@ func CountUserStopwatches(userID int64) (int64, error) { |
|
|
|
|
|
|
|
|
|
|
|
// StopwatchExists returns true if the stopwatch exists
|
|
|
|
// StopwatchExists returns true if the stopwatch exists
|
|
|
|
func StopwatchExists(userID, issueID int64) bool { |
|
|
|
func StopwatchExists(userID, issueID int64) bool { |
|
|
|
_, exists, _ := getStopwatch(db.GetEngine(db.DefaultContext), userID, issueID) |
|
|
|
_, exists, _ := getStopwatch(db.DefaultContext, userID, issueID) |
|
|
|
return exists |
|
|
|
return exists |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -83,93 +103,122 @@ func hasUserStopwatch(e db.Engine, userID int64) (exists bool, sw *Stopwatch, er |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// CreateOrStopIssueStopwatch will create or remove a stopwatch and will log it into issue's timeline.
|
|
|
|
// FinishIssueStopwatchIfPossible if stopwatch exist then finish it otherwise ignore
|
|
|
|
func CreateOrStopIssueStopwatch(user *User, issue *Issue) error { |
|
|
|
func FinishIssueStopwatchIfPossible(ctx context.Context, user *User, issue *Issue) error { |
|
|
|
ctx, committer, err := db.TxContext() |
|
|
|
_, exists, err := getStopwatch(ctx, user.ID, issue.ID) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
defer committer.Close() |
|
|
|
if !exists { |
|
|
|
if err := createOrStopIssueStopwatch(ctx, user, issue); err != nil { |
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return FinishIssueStopwatch(ctx, user, issue) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CreateOrStopIssueStopwatch create an issue stopwatch if it's not exist, otherwise finish it
|
|
|
|
|
|
|
|
func CreateOrStopIssueStopwatch(user *User, issue *Issue) error { |
|
|
|
|
|
|
|
_, exists, err := getStopwatch(db.DefaultContext, user.ID, issue.ID) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
return committer.Commit() |
|
|
|
if exists { |
|
|
|
|
|
|
|
return FinishIssueStopwatch(db.DefaultContext, user, issue) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return CreateIssueStopwatch(db.DefaultContext, user, issue) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func createOrStopIssueStopwatch(ctx context.Context, user *User, issue *Issue) error { |
|
|
|
// FinishIssueStopwatch if stopwatch exist then finish it otherwise return an error
|
|
|
|
e := db.GetEngine(ctx) |
|
|
|
func FinishIssueStopwatch(ctx context.Context, user *User, issue *Issue) error { |
|
|
|
sw, exists, err := getStopwatch(e, user.ID, issue.ID) |
|
|
|
sw, exists, err := getStopwatch(ctx, user.ID, issue.ID) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
if err := issue.loadRepo(e); err != nil { |
|
|
|
if !exists { |
|
|
|
|
|
|
|
return ErrIssueStopwatchNotExist{ |
|
|
|
|
|
|
|
UserID: user.ID, |
|
|
|
|
|
|
|
IssueID: issue.ID, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create tracked time out of the time difference between start date and actual date
|
|
|
|
|
|
|
|
timediff := time.Now().Unix() - int64(sw.CreatedUnix) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create TrackedTime
|
|
|
|
|
|
|
|
tt := &TrackedTime{ |
|
|
|
|
|
|
|
Created: time.Now(), |
|
|
|
|
|
|
|
IssueID: issue.ID, |
|
|
|
|
|
|
|
UserID: user.ID, |
|
|
|
|
|
|
|
Time: timediff, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := db.Insert(ctx, tt); err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if exists { |
|
|
|
if err := issue.loadRepo(db.GetEngine(ctx)); err != nil { |
|
|
|
// Create tracked time out of the time difference between start date and actual date
|
|
|
|
return err |
|
|
|
timediff := time.Now().Unix() - int64(sw.CreatedUnix) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Create TrackedTime
|
|
|
|
if _, err := createComment(ctx, &CreateCommentOptions{ |
|
|
|
tt := &TrackedTime{ |
|
|
|
Doer: user, |
|
|
|
Created: time.Now(), |
|
|
|
Issue: issue, |
|
|
|
IssueID: issue.ID, |
|
|
|
Repo: issue.Repo, |
|
|
|
UserID: user.ID, |
|
|
|
Content: SecToTime(timediff), |
|
|
|
Time: timediff, |
|
|
|
Type: CommentTypeStopTracking, |
|
|
|
} |
|
|
|
TimeID: tt.ID, |
|
|
|
|
|
|
|
}); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_, err = db.GetEngine(ctx).Delete(sw) |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if _, err := e.Insert(tt); err != nil { |
|
|
|
// CreateIssueStopwatch creates a stopwatch if not exist, otherwise return an error
|
|
|
|
return err |
|
|
|
func CreateIssueStopwatch(ctx context.Context, user *User, issue *Issue) error { |
|
|
|
} |
|
|
|
e := db.GetEngine(ctx) |
|
|
|
|
|
|
|
if err := issue.loadRepo(e); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if _, err := createComment(ctx, &CreateCommentOptions{ |
|
|
|
// if another stopwatch is running: stop it
|
|
|
|
Doer: user, |
|
|
|
exists, sw, err := hasUserStopwatch(e, user.ID) |
|
|
|
Issue: issue, |
|
|
|
if err != nil { |
|
|
|
Repo: issue.Repo, |
|
|
|
return err |
|
|
|
Content: SecToTime(timediff), |
|
|
|
} |
|
|
|
Type: CommentTypeStopTracking, |
|
|
|
if exists { |
|
|
|
TimeID: tt.ID, |
|
|
|
issue, err := getIssueByID(e, sw.IssueID) |
|
|
|
}); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if _, err := e.Delete(sw); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// if another stopwatch is running: stop it
|
|
|
|
|
|
|
|
exists, sw, err := hasUserStopwatch(e, user.ID) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
if exists { |
|
|
|
|
|
|
|
issue, err := getIssueByID(e, sw.IssueID) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if err := createOrStopIssueStopwatch(ctx, user, issue); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create stopwatch
|
|
|
|
if err := FinishIssueStopwatch(ctx, user, issue); err != nil { |
|
|
|
sw = &Stopwatch{ |
|
|
|
|
|
|
|
UserID: user.ID, |
|
|
|
|
|
|
|
IssueID: issue.ID, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := db.Insert(ctx, sw); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if _, err := createComment(ctx, &CreateCommentOptions{ |
|
|
|
// Create stopwatch
|
|
|
|
Doer: user, |
|
|
|
sw = &Stopwatch{ |
|
|
|
Issue: issue, |
|
|
|
UserID: user.ID, |
|
|
|
Repo: issue.Repo, |
|
|
|
IssueID: issue.ID, |
|
|
|
Type: CommentTypeStartTracking, |
|
|
|
} |
|
|
|
}); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
if err := db.Insert(ctx, sw); err != nil { |
|
|
|
} |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := issue.loadRepo(db.GetEngine(ctx)); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if _, err := createComment(ctx, &CreateCommentOptions{ |
|
|
|
|
|
|
|
Doer: user, |
|
|
|
|
|
|
|
Issue: issue, |
|
|
|
|
|
|
|
Repo: issue.Repo, |
|
|
|
|
|
|
|
Type: CommentTypeStartTracking, |
|
|
|
|
|
|
|
}); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -188,7 +237,7 @@ func CancelStopwatch(user *User, issue *Issue) error { |
|
|
|
|
|
|
|
|
|
|
|
func cancelStopwatch(ctx context.Context, user *User, issue *Issue) error { |
|
|
|
func cancelStopwatch(ctx context.Context, user *User, issue *Issue) error { |
|
|
|
e := db.GetEngine(ctx) |
|
|
|
e := db.GetEngine(ctx) |
|
|
|
sw, exists, err := getStopwatch(e, user.ID, issue.ID) |
|
|
|
sw, exists, err := getStopwatch(ctx, user.ID, issue.ID) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -202,6 +251,10 @@ func cancelStopwatch(ctx context.Context, user *User, issue *Issue) error { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := issue.loadRepo(db.GetEngine(ctx)); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if _, err := createComment(ctx, &CreateCommentOptions{ |
|
|
|
if _, err := createComment(ctx, &CreateCommentOptions{ |
|
|
|
Doer: user, |
|
|
|
Doer: user, |
|
|
|
Issue: issue, |
|
|
|
Issue: issue, |
|
|
|