Skip to content

Commit 5390791

Browse files
adelowolafriks
authored andcommitted
Automatically clear stopwatch on merging a PR (#4327)
* Don't display buttons if there are no notices * clear stopwatch on merging a PR * remove redundant gt check * use ctx.Flash as per @bkcsoft comment * stop timer on closing issues/PRs too * updated translation as per review * redirect to login page after successfully activating account * remove unrelated changes * stop timer for issues that are closed via commits too..Not just the 'close' UI button
1 parent 5bd594c commit 5390791

File tree

6 files changed

+49
-2
lines changed

6 files changed

+49
-2
lines changed

models/action.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,27 @@ func changeIssueStatus(repo *Repository, doer *User, ref string, refMarked map[i
491491
return nil
492492
}
493493

494+
stopTimerIfAvailable := func(doer *User, issue *Issue) error {
495+
496+
if StopwatchExists(doer.ID, issue.ID) {
497+
if err := CreateOrStopIssueStopwatch(doer, issue); err != nil {
498+
return err
499+
}
500+
}
501+
502+
return nil
503+
}
504+
494505
issue.Repo = repo
495506
if err = issue.ChangeStatus(doer, status); err != nil {
496507
// Don't return an error when dependencies are open as this would let the push fail
497508
if IsErrDependenciesLeft(err) {
498-
return nil
509+
return stopTimerIfAvailable(doer, issue)
499510
}
500511
return err
501512
}
502-
return nil
513+
514+
return stopTimerIfAvailable(doer, issue)
503515
}
504516

505517
// UpdateIssuesCommit checks if issues are manipulated by commit message.

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ issues.tracker = Time Tracker
781781
issues.start_tracking_short = Start
782782
issues.start_tracking = Start Time Tracking
783783
issues.start_tracking_history = `started working %s`
784+
issues.tracker_auto_close = Timer will be stopped automatically when this issue gets closed
784785
issues.tracking_already_started = `You have already started time tracking on this <a href="%s">issue</a>!`
785786
issues.stop_tracking = Stop
786787
issues.stop_tracking_history = `stopped working %s`

routers/repo/issue.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,12 @@ func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
11811181
return
11821182
}
11831183
} else {
1184+
1185+
if err := stopTimerIfAvailable(ctx.User, issue); err != nil {
1186+
ctx.ServerError("CreateOrStopIssueStopwatch", err)
1187+
return
1188+
}
1189+
11841190
log.Trace("Issue [%d] status changed to closed: %v", issue.ID, issue.IsClosed)
11851191

11861192
notification.NotifyIssueChangeStatus(ctx.User, issue, isClosed)

routers/repo/issue_stopwatch.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ func IssueStopwatch(c *context.Context) {
1717
if c.Written() {
1818
return
1919
}
20+
21+
var showSuccessMessage bool
22+
23+
if !models.StopwatchExists(c.User.ID, issue.ID) {
24+
showSuccessMessage = true
25+
}
26+
2027
if !c.Repo.CanUseTimetracker(issue, c.User) {
2128
c.NotFound("CanUseTimetracker", nil)
2229
return
@@ -27,6 +34,10 @@ func IssueStopwatch(c *context.Context) {
2734
return
2835
}
2936

37+
if showSuccessMessage {
38+
c.Flash.Success(c.Tr("repo.issues.tracker_auto_close"))
39+
}
40+
3041
url := issue.HTMLURL()
3142
c.Redirect(url, http.StatusSeeOther)
3243
}

routers/repo/pull.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,28 @@ func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
590590
return
591591
}
592592

593+
if err := stopTimerIfAvailable(ctx.User, issue); err != nil {
594+
ctx.ServerError("CreateOrStopIssueStopwatch", err)
595+
return
596+
}
597+
593598
notification.NotifyMergePullRequest(pr, ctx.User, ctx.Repo.GitRepo)
594599

595600
log.Trace("Pull request merged: %d", pr.ID)
596601
ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
597602
}
598603

604+
func stopTimerIfAvailable(user *models.User, issue *models.Issue) error {
605+
606+
if models.StopwatchExists(user.ID, issue.ID) {
607+
if err := models.CreateOrStopIssueStopwatch(user, issue); err != nil {
608+
return err
609+
}
610+
}
611+
612+
return nil
613+
}
614+
599615
// ParseCompareInfo parse compare info between two commit for preparing pull request
600616
func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
601617
baseRepo := ctx.Repo.Repository

templates/repo/issue/view_content/comments.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{ template "base/alert" }}
12
{{range .Issue.Comments}}
23
{{ $createdStr:= TimeSinceUnix .CreatedUnix $.Lang }}
34

0 commit comments

Comments
 (0)