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

Fix close issue but time watcher still running #17643

Merged
merged 6 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
187 changes: 118 additions & 69 deletions models/issue_stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ import (
"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 is not exist[uid: %d, issue_id: %d", err.UserID, err.IssueID)
lunny marked this conversation as resolved.
Show resolved Hide resolved
}

// 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 is already exist[uid: %d, issue_id: %d", err.UserID, err.IssueID)
lunny marked this conversation as resolved.
Show resolved Hide resolved
}

// Stopwatch represents a stopwatch for time tracking.
type Stopwatch struct {
ID int64 `xorm:"pk autoincr"`
Expand All @@ -35,9 +55,9 @@ func (s Stopwatch) Duration() string {
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)
exists, err = e.
exists, err = db.GetEngine(ctx).
Where("user_id = ?", userID).
And("issue_id = ?", issueID).
Get(sw)
Expand Down Expand Up @@ -66,7 +86,7 @@ func CountUserStopwatches(userID int64) (int64, error) {

// StopwatchExists returns true if the stopwatch exists
func StopwatchExists(userID, issueID int64) bool {
_, exists, _ := getStopwatch(db.GetEngine(db.DefaultContext), userID, issueID)
_, exists, _ := getStopwatch(db.DefaultContext, userID, issueID)
return exists
}

Expand All @@ -83,93 +103,118 @@ func hasUserStopwatch(e db.Engine, userID int64) (exists bool, sw *Stopwatch, er
return
}

// CreateOrStopIssueStopwatch will create or remove a stopwatch and will log it into issue's timeline.
func CreateOrStopIssueStopwatch(user *User, issue *Issue) error {
ctx, committer, err := db.TxContext()
// FinishIssueStopwatchIfPossible if stopwatch exist then finish it otherwise ignore
func FinishIssueStopwatchIfPossible(ctx context.Context, user *User, issue *Issue) error {
_, exists, err := getStopwatch(ctx, user.ID, issue.ID)
if err != nil {
return err
}
defer committer.Close()
if err := createOrStopIssueStopwatch(ctx, user, issue); err != nil {
return err
if !exists {
return nil
}
return committer.Commit()
return FinishIssueStopwatch(ctx, user, issue)
}

func createOrStopIssueStopwatch(ctx context.Context, user *User, issue *Issue) error {
e := db.GetEngine(ctx)
sw, exists, err := getStopwatch(e, user.ID, issue.ID)
// 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
}
if err := issue.loadRepo(e); 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)
return FinishIssueStopwatch(db.DefaultContext, user, issue)
}
return CreateIssueStopwatch(db.DefaultContext, user, issue)
}

// Create TrackedTime
tt := &TrackedTime{
Created: time.Now(),
IssueID: issue.ID,
// FinishIssueStopwatch if stopwatch exist then finish it otherwise return an error
func FinishIssueStopwatch(ctx context.Context, user *User, issue *Issue) error {
sw, exists, err := getStopwatch(ctx, user.ID, issue.ID)
if err != nil {
return err
}
if !exists {
return ErrIssueStopwatchNotExist{
UserID: user.ID,
Time: timediff,
IssueID: issue.ID,
}
}

if _, err := e.Insert(tt); err != nil {
return err
}
// Create tracked time out of the time difference between start date and actual date
timediff := time.Now().Unix() - int64(sw.CreatedUnix)

if _, err := createComment(ctx, &CreateCommentOptions{
Doer: user,
Issue: issue,
Repo: issue.Repo,
Content: SecToTime(timediff),
Type: CommentTypeStopTracking,
TimeID: tt.ID,
}); 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 {
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 TrackedTime
tt := &TrackedTime{
Created: time.Now(),
IssueID: issue.ID,
UserID: user.ID,
Time: timediff,
}

// Create stopwatch
sw = &Stopwatch{
if err := db.Insert(ctx, tt); 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,
Content: SecToTime(timediff),
Type: CommentTypeStopTracking,
TimeID: tt.ID,
}); err != nil {
return err
}
_, err = db.GetEngine(ctx).Delete(sw)
return err
}

// CreateIssueStopwatch creates a stopwatch if not exist, otherwise return an error
func CreateIssueStopwatch(ctx context.Context, user *User, issue *Issue) error {
e := db.GetEngine(ctx)
if err := issue.loadRepo(e); err != nil {
return err
}

// if another stopwatch is running: stop it
exists, _, err := hasUserStopwatch(e, user.ID)
if err != nil {
return err
}
if exists {
return ErrIssueStopwatchAlreadyExist{
UserID: user.ID,
IssueID: issue.ID,
}
}

if err := db.Insert(ctx, sw); err != nil {
return err
}
// Create stopwatch
var sw = &Stopwatch{
UserID: user.ID,
IssueID: issue.ID,
}

if _, err := createComment(ctx, &CreateCommentOptions{
Doer: user,
Issue: issue,
Repo: issue.Repo,
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
}

Expand All @@ -188,7 +233,7 @@ func CancelStopwatch(user *User, issue *Issue) error {

func cancelStopwatch(ctx context.Context, user *User, issue *Issue) error {
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 {
return err
}
Expand All @@ -202,6 +247,10 @@ func cancelStopwatch(ctx context.Context, user *User, issue *Issue) error {
return err
}

if err := issue.loadRepo(db.GetEngine(ctx)); err != nil {
return err
}

if _, err := createComment(ctx, &CreateCommentOptions{
Doer: user,
Issue: issue,
Expand Down
51 changes: 12 additions & 39 deletions modules/repofiles/action.go → services/issue/commit.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Copyright 2021 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 repofiles
package issue

import (
"fmt"
Expand All @@ -14,7 +14,6 @@ import (
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/references"
"code.gitea.io/gitea/modules/repository"
)
Expand All @@ -29,19 +28,6 @@ const (

var reDuration = regexp.MustCompile(`(?i)^(?:(\d+([\.,]\d+)?)(?:mo))?(?:(\d+([\.,]\d+)?)(?:w))?(?:(\d+([\.,]\d+)?)(?:d))?(?:(\d+([\.,]\d+)?)(?:h))?(?:(\d+([\.,]\d+)?)(?:m))?$`)

// getIssueFromRef returns the issue referenced by a ref. Returns a nil *Issue
// if the provided ref references a non-existent issue.
func getIssueFromRef(repo *models.Repository, index int64) (*models.Issue, error) {
issue, err := models.GetIssueByIndex(repo.ID, index)
if err != nil {
if models.IsErrIssueNotExist(err) {
return nil, nil
}
return nil, err
}
return issue, nil
}

// timeLogToAmount parses time log string and returns amount in seconds
func timeLogToAmount(str string) int64 {
matches := reDuration.FindAllStringSubmatch(str, -1)
Expand Down Expand Up @@ -96,31 +82,17 @@ func issueAddTime(issue *models.Issue, doer *models.User, time time.Time, timeLo
return err
}

func changeIssueStatus(repo *models.Repository, issue *models.Issue, doer *models.User, closed bool) error {
stopTimerIfAvailable := func(doer *models.User, issue *models.Issue) error {

if models.StopwatchExists(doer.ID, issue.ID) {
if err := models.CreateOrStopIssueStopwatch(doer, issue); err != nil {
return err
}
}

return nil
}

issue.Repo = repo
comment, err := issue.ChangeStatus(doer, closed)
// getIssueFromRef returns the issue referenced by a ref. Returns a nil *Issue
// if the provided ref references a non-existent issue.
func getIssueFromRef(repo *models.Repository, index int64) (*models.Issue, error) {
issue, err := models.GetIssueByIndex(repo.ID, index)
if err != nil {
// Don't return an error when dependencies are open as this would let the push fail
if models.IsErrDependenciesLeft(err) {
return stopTimerIfAvailable(doer, issue)
if models.IsErrIssueNotExist(err) {
return nil, nil
}
return err
return nil, err
}

notification.NotifyIssueChangeStatus(doer, issue, comment, closed)

return stopTimerIfAvailable(doer, issue)
return issue, nil
}

// UpdateIssuesCommit checks if issues are manipulated by commit message.
Expand Down Expand Up @@ -209,7 +181,8 @@ func UpdateIssuesCommit(doer *models.User, repo *models.Repository, commits []*r
}
}
if close != refIssue.IsClosed {
if err := changeIssueStatus(refRepo, refIssue, doer, close); err != nil {
refIssue.Repo = refRepo
if err := ChangeStatus(refIssue, doer, close); err != nil {
return err
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package repofiles
package issue

import (
"testing"
Expand Down
22 changes: 18 additions & 4 deletions services/issue/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@ package issue

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/notification"
)

// ChangeStatus changes issue status to open or closed.
func ChangeStatus(issue *models.Issue, doer *models.User, isClosed bool) (err error) {
comment, err := issue.ChangeStatus(doer, isClosed)
func ChangeStatus(issue *models.Issue, doer *models.User, closed bool) error {
comment, err := issue.ChangeStatus(doer, closed)
if err != nil {
return
// Don't return an error when dependencies are open as this would let the push fail
if models.IsErrDependenciesLeft(err) {
if closed {
return models.FinishIssueStopwatchIfPossible(db.DefaultContext, doer, issue)
}
}
return err
}

notification.NotifyIssueChangeStatus(doer, issue, comment, isClosed)
if closed {
if err := models.FinishIssueStopwatchIfPossible(db.DefaultContext, doer, issue); err != nil {
return err
}
}

notification.NotifyIssueChangeStatus(doer, issue, comment, closed)

return nil
}
Loading