mirror of
https://github.com/go-gitea/gitea
synced 2024-11-05 01:34:25 +00:00
8d5f58d834
* Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
155 lines
3.4 KiB
Go
155 lines
3.4 KiB
Go
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
)
|
|
|
|
// Stopwatch represents a stopwatch for time tracking.
|
|
type Stopwatch struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
IssueID int64 `xorm:"INDEX"`
|
|
UserID int64 `xorm:"INDEX"`
|
|
CreatedUnix util.TimeStamp `xorm:"created"`
|
|
}
|
|
|
|
func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
|
|
sw = new(Stopwatch)
|
|
exists, err = e.
|
|
Where("user_id = ?", userID).
|
|
And("issue_id = ?", issueID).
|
|
Get(sw)
|
|
return
|
|
}
|
|
|
|
// StopwatchExists returns true if the stopwatch exists
|
|
func StopwatchExists(userID int64, issueID int64) bool {
|
|
_, exists, _ := getStopwatch(x, userID, issueID)
|
|
return exists
|
|
}
|
|
|
|
// HasUserStopwatch returns true if the user has a stopwatch
|
|
func HasUserStopwatch(userID int64) (exists bool, sw *Stopwatch, err error) {
|
|
sw = new(Stopwatch)
|
|
exists, err = x.
|
|
Where("user_id = ?", userID).
|
|
Get(sw)
|
|
return
|
|
}
|
|
|
|
// 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)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if exists {
|
|
// 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 := x.Insert(tt); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := CreateComment(&CreateCommentOptions{
|
|
Doer: user,
|
|
Issue: issue,
|
|
Repo: issue.Repo,
|
|
Content: SecToTime(timediff),
|
|
Type: CommentTypeStopTracking,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
if _, err := x.Delete(sw); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
// Create stopwatch
|
|
sw = &Stopwatch{
|
|
UserID: user.ID,
|
|
IssueID: issue.ID,
|
|
}
|
|
|
|
if _, err := x.Insert(sw); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := CreateComment(&CreateCommentOptions{
|
|
Doer: user,
|
|
Issue: issue,
|
|
Repo: issue.Repo,
|
|
Type: CommentTypeStartTracking,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if exists {
|
|
if _, err := x.Delete(sw); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := CreateComment(&CreateCommentOptions{
|
|
Doer: user,
|
|
Issue: issue,
|
|
Repo: issue.Repo,
|
|
Type: CommentTypeCancelTracking,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s)
|
|
func SecToTime(duration int64) string {
|
|
seconds := duration % 60
|
|
minutes := (duration / (60)) % 60
|
|
hours := duration / (60 * 60)
|
|
|
|
var hrs string
|
|
|
|
if hours > 0 {
|
|
hrs = fmt.Sprintf("%dh", hours)
|
|
}
|
|
if minutes > 0 {
|
|
if hours == 0 {
|
|
hrs = fmt.Sprintf("%dmin", minutes)
|
|
} else {
|
|
hrs = fmt.Sprintf("%s %dmin", hrs, minutes)
|
|
}
|
|
}
|
|
if seconds > 0 {
|
|
if hours == 0 && minutes == 0 {
|
|
hrs = fmt.Sprintf("%ds", seconds)
|
|
} else {
|
|
hrs = fmt.Sprintf("%s %ds", hrs, seconds)
|
|
}
|
|
}
|
|
|
|
return hrs
|
|
}
|