From bac57ab5900672357fb0a36f9433e516088bd21b Mon Sep 17 00:00:00 2001
From: bobemoe <inbox.github@jhodges.co.uk>
Date: Sun, 12 Jul 2020 11:01:20 +0100
Subject: [PATCH] When starting new stopwatch stop previous if it is still
 running (#10533)

Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: Lauris BH <lauris@nix.lv>
---
 models/issue_stopwatch.go | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/models/issue_stopwatch.go b/models/issue_stopwatch.go
index ca92141157..79ce48c4cd 100644
--- a/models/issue_stopwatch.go
+++ b/models/issue_stopwatch.go
@@ -101,6 +101,21 @@ func CreateOrStopIssueStopwatch(user *User, issue *Issue) error {
 			return err
 		}
 	} else {
+		//if another stopwatch is running: stop it
+		exists, sw, err := HasUserStopwatch(user.ID)
+		if err != nil {
+			return err
+		}
+		if exists {
+			issue, err := getIssueByID(x, sw.IssueID)
+			if err != nil {
+				return err
+			}
+			if err := CreateOrStopIssueStopwatch(user, issue); err != nil {
+				return err
+			}
+		}
+
 		// Create stopwatch
 		sw = &Stopwatch{
 			UserID:  user.ID,