Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shows total tracked time in issue and milestone list #3341

Merged
merged 39 commits into from
Apr 29, 2018
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
72c4296
Show total tracked time in issue and milestone list
jonasfranz Jan 9, 2018
1c0201f
Merge branch 'master' of https://github.com/go-gitea/gitea into 3003-…
jonasfranz Jan 9, 2018
1875038
Optimizing TotalTimes by using SumInt
jonasfranz Jan 10, 2018
b849f54
Merge branch 'master' into 3003-timetracking
jonasfranz Feb 3, 2018
fda4dd4
Fixing wrong total times for milestones caused by a missing JOIN
jonasfranz Feb 3, 2018
983c1e1
Logging error instead of ignoring it
jonasfranz Feb 3, 2018
707f3db
Merge branch 'master' into 3003-timetracking
jonasfranz Feb 3, 2018
26a1e02
Correcting spelling mistakes
jonasfranz Feb 3, 2018
67be8bd
Merge remote-tracking branch 'jonas/3003-timetracking' into 3003-time…
jonasfranz Feb 3, 2018
f1cfe62
Change error message to a short version
jonasfranz Feb 3, 2018
770fea4
Merge branch 'master' into 3003-timetracking
jonasfranz Feb 9, 2018
3c8507a
Merge branch 'master' into 3003-timetracking
jonasfranz Feb 26, 2018
c5c812c
Add error handling to TotalTimes
jonasfranz Feb 26, 2018
e92cc12
Merge branch 'master' into 3003-timetracking
jonasfranz Mar 10, 2018
8481f2d
Introduce TotalTrackedTimes as variable of issue
jonasfranz Mar 11, 2018
4cac53e
Fixed test + gofmt
jonasfranz Mar 11, 2018
79dc9d8
Merge branch 'master' into 3003-timetracking
jonasfranz Mar 15, 2018
421cbfb
Merge branch 'master' into 3003-timetracking
jonasfranz Mar 16, 2018
1077882
Merge branch 'master' into 3003-timetracking
jonasfranz Mar 28, 2018
9bb7714
Merge branch 'master' into 3003-timetracking
jonasfranz Mar 30, 2018
0143b50
Merge branch 'master' into 3003-timetracking
jonasfranz Apr 3, 2018
2f6a6fd
Merge branch 'master' into 3003-timetracking
jonasfranz Apr 4, 2018
2d13376
Merge branch 'master' into 3003-timetracking
jonasfranz Apr 10, 2018
a87698b
Merge branch 'master' into 3003-timetracking
jonasfranz Apr 13, 2018
ec0d41c
Load TotalTrackedTimes via MilestoneList instead of single requests
jonasfranz Apr 16, 2018
856087e
Merge branch 'master' into 3003-timetracking
jonasfranz Apr 16, 2018
02dc1cf
Add documentation for MilestoneList
jonasfranz Apr 16, 2018
9508985
Merge remote-tracking branch 'onion/3003-timetracking' into 3003-time…
jonasfranz Apr 16, 2018
0ba269b
Add documentation for MilestoneList
jonasfranz Apr 16, 2018
70e9bbd
Fix test
jonasfranz Apr 16, 2018
221a327
Change comment from SQL query to description
jonasfranz Apr 19, 2018
90658cc
Merge branch 'master' into 3003-timetracking
jonasfranz Apr 19, 2018
ca5243c
Fix unit test by using int64 instead of int
jonasfranz Apr 19, 2018
d623780
Fix unit test by using int64 instead of int
jonasfranz Apr 19, 2018
6a97bf7
Check if timetracker is enabled
jonasfranz Apr 20, 2018
62f9962
Fix test by enabling timetracking
jonasfranz Apr 21, 2018
2835f08
Merge branch 'master' into 3003-timetracking
jonasfranz Apr 21, 2018
526f7db
Merge branch 'master' into 3003-timetracking
jonasfranz Apr 23, 2018
e9562ad
Merge branch 'master' into 3003-timetracking
lafriks Apr 29, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ func init() {
issueTasksDonePat = regexp.MustCompile(issueTasksDoneRegexpStr)
}

func (issue *Issue) totalTimes(e Engine) (string, error) {
opts := FindTrackedTimesOptions{IssueID: issue.ID}
totalTime, err := opts.ToSession(e).SumInt(&TrackedTime{}, "time")
if err != nil {
return "", err
}
return secToTime(totalTime), nil
}

// TotalTimes returns the total amount of tracked time for the issue
func (issue *Issue) TotalTimes() string {
times, _ := issue.totalTimes(x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error should be at least logged

return times
}

func (issue *Issue) loadRepo(e Engine) (err error) {
if issue.Repo == nil {
issue.Repo, err = getRepositoryByID(e, issue.RepoID)
Expand All @@ -78,6 +93,15 @@ func (issue *Issue) loadRepo(e Engine) (err error) {
return nil
}

// IsTimetrackerEnabled returns true if the repo enables timetracking
func (issue *Issue) IsTimetrackerEnabled() bool {
if err := issue.loadRepo(x); err != nil {
log.Error(4, fmt.Sprintf("loadRepo: %v", err))
return false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If error is ignored than at least it should be logged

}
return issue.Repo.IsTimetrackerEnabled()
}

// GetPullRequest returns the issue pull request
func (issue *Issue) GetPullRequest() (pr *PullRequest, err error) {
if !issue.IsPull {
Expand Down
21 changes: 21 additions & 0 deletions models/issue_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ func (m *Milestone) APIFormat() *api.Milestone {
return apiMilestone
}

func (m *Milestone) totalTimes(e Engine) (string, error) {
opts := FindTrackedTimesOptions{MilestoneID: m.ID}
totalTime, err := opts.ToSession(e).SumInt(&TrackedTime{}, "time")
if err != nil {
return "", err
}
return secToTime(totalTime), nil
}

// TotalTimes returns the amount of tracked time of the issues inside the milestone
func (m *Milestone) TotalTimes() string {
times, _ := m.totalTimes(x)
return times
}

// MustGetRepo returns milestone's repository by ignoring errors
func (m *Milestone) MustGetRepo() *Repository {
repo, _ := GetRepositoryByID(m.RepoID)
return repo
}

// NewMilestone creates new milestone of repository.
func NewMilestone(m *Milestone) (err error) {
sess := x.NewSession()
Expand Down
8 changes: 8 additions & 0 deletions models/issue_milestone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,11 @@ func TestDeleteMilestoneByRepoID(t *testing.T) {

assert.NoError(t, DeleteMilestoneByRepoID(NonexistentID, NonexistentID))
}

func TestMilestone_TotalTimes(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
ms, err := GetMilestoneByRepoID(1, 1)
assert.NoError(t, err)
times := ms.TotalTimes()
assert.Equal(t, "1h 1min 2s", times)
}
8 changes: 8 additions & 0 deletions models/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,11 @@ func TestGetUserIssueStats(t *testing.T) {
assert.Equal(t, test.ExpectedIssueStats, *stats)
}
}

func TestIssue_TotalTimes(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
ms, err := GetIssueByID(2)
assert.NoError(t, err)
times := ms.TotalTimes()
assert.Equal(t, "1h 1min 2s", times)
}
19 changes: 14 additions & 5 deletions models/issue_tracked_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
api "code.gitea.io/sdk/gitea"

"github.com/go-xorm/builder"
"github.com/go-xorm/xorm"
)

// TrackedTime represents a time that was spent for a specific issue.
Expand Down Expand Up @@ -44,6 +45,7 @@ type FindTrackedTimesOptions struct {
IssueID int64
UserID int64
RepositoryID int64
MilestoneID int64
}

// ToCond will convert each condition into a xorm-Cond
Expand All @@ -58,16 +60,23 @@ func (opts *FindTrackedTimesOptions) ToCond() builder.Cond {
if opts.RepositoryID != 0 {
cond = cond.And(builder.Eq{"issue.repo_id": opts.RepositoryID})
}
if opts.MilestoneID != 0 {
cond = cond.And(builder.Eq{"issue.milestone_id": opts.MilestoneID})
}
return cond
}

// ToSession will convert the given options to a xorm Session by using the conditions from ToCond and joining with issue table if required
func (opts *FindTrackedTimesOptions) ToSession(e Engine) *xorm.Session {
if opts.RepositoryID > 0 || opts.MilestoneID > 0 {
return e.Join("INNER", "issue", "issue.id = tracked_time.issue_id").Where(opts.ToCond())
}
return x.Where(opts.ToCond())
}

// GetTrackedTimes returns all tracked times that fit to the given options.
func GetTrackedTimes(options FindTrackedTimesOptions) (trackedTimes []*TrackedTime, err error) {
if options.RepositoryID > 0 {
err = x.Join("INNER", "issue", "issue.id = tracked_time.issue_id").Where(options.ToCond()).Find(&trackedTimes)
return
}
err = x.Where(options.ToCond()).Find(&trackedTimes)
err = options.ToSession(x).Find(&trackedTimes)
return
}

Expand Down
2 changes: 1 addition & 1 deletion options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ issues.add_time_minutes = Minutes
issues.add_time_sum_to_small = No time was entered
issues.cancel_tracking = Cancel
issues.cancel_tracking_history = `cancelled time tracking %s`
issues.time_spent_total = Total time spent
issues.time_spent_from_all_authors = `Total time spent: %s`

pulls.desc = Pulls management your code review and merge requests
pulls.new = New Pull Request
Expand Down
4 changes: 4 additions & 0 deletions templates/repo/issue/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@
<span class="comment ui right"><i class="octicon octicon-comment"></i> {{.NumComments}}</span>
{{end}}

{{if and .IsTimetrackerEnabled .TotalTimes}}
<span class="comment ui right"><i class="octicon octicon-clock"></i> {{.TotalTimes}}</span>
{{end}}

<p class="desc">
{{$.i18n.Tr "repo.issues.opened_by" $timeStr .Poster.HomeLink .Poster.Name | Safe}}
{{$tasks := .GetTasks}}
Expand Down
1 change: 1 addition & 0 deletions templates/repo/issue/milestones.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<span class="issue-stats">
<i class="octicon octicon-issue-opened"></i> {{$.i18n.Tr "repo.issues.open_tab" .NumOpenIssues}}
<i class="octicon octicon-issue-closed"></i> {{$.i18n.Tr "repo.issues.close_tab" .NumClosedIssues}}
{{if and .TotalTimes .MustGetRepo.IsTimetrackerEnabled}}<i class="octicon octicon-clock"></i> {{.TotalTimes}}{{end}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem here as it was with issue list (each milestone queried even if time tracking disabled) and also each milestone queried separately

</span>
</div>
{{if $.IsRepositoryWriter}}
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/issue/view_content/sidebar.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
{{if gt (len .WorkingUsers) 0}}
<div class="ui divider"></div>
<div class="ui participants comments">
<span class="text"><strong>{{.i18n.Tr "repo.issues.time_spent_total"}}</strong></span>
<span class="text"><strong>{{.i18n.Tr "repo.issues.time_spent_from_all_authors" $.Issue.TotalTimes | Safe}}</strong></span>
<div>
{{range $user, $trackedtime := .WorkingUsers}}
<div class="comment">
Expand Down
4 changes: 4 additions & 0 deletions templates/user/dashboard/issues.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
<span class="comment ui right"><i class="octicon octicon-comment"></i> {{.NumComments}}</span>
{{end}}

{{if and .IsTimetrackerEnabled .TotalTimes}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Result of .TotalTimes be assigned probably to some variable otherwise currently if there is value this query will be executed twice

<span class="comment ui right"><i class="octicon octicon-clock"></i> {{.TotalTimes}}</span>
{{end}}

<p class="desc">
{{$.i18n.Tr "repo.issues.opened_by" $timeStr .Poster.HomeLink .Poster.Name | Safe}}
{{if .Assignee}}
Expand Down