diff --git a/models/issue_comment.go b/models/issue_comment.go index b15b5169ff..6cc03ba0e8 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -136,6 +136,8 @@ type Comment struct { MilestoneID int64 OldMilestone *Milestone `xorm:"-"` Milestone *Milestone `xorm:"-"` + TimeID int64 + Time *TrackedTime `xorm:"-"` AssigneeID int64 RemovedAssignee bool Assignee *User `xorm:"-"` @@ -541,6 +543,16 @@ func (c *Comment) LoadDepIssueDetails() (err error) { return err } +// LoadTime loads the associated time for a CommentTypeAddTimeManual +func (c *Comment) LoadTime() error { + if c.Time != nil || c.TimeID == 0 { + return nil + } + var err error + c.Time, err = GetTrackedTimeByID(c.TimeID) + return err +} + func (c *Comment) loadReactions(e Engine, repo *Repository) (err error) { if c.Reactions != nil { return nil @@ -692,6 +704,7 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err MilestoneID: opts.MilestoneID, OldProjectID: opts.OldProjectID, ProjectID: opts.ProjectID, + TimeID: opts.TimeID, RemovedAssignee: opts.RemovedAssignee, AssigneeID: opts.AssigneeID, AssigneeTeamID: opts.AssigneeTeamID, @@ -859,6 +872,7 @@ type CreateCommentOptions struct { MilestoneID int64 OldProjectID int64 ProjectID int64 + TimeID int64 AssigneeID int64 AssigneeTeamID int64 RemovedAssignee bool diff --git a/models/issue_stopwatch.go b/models/issue_stopwatch.go index a1c88503d8..19bd4ab2c5 100644 --- a/models/issue_stopwatch.go +++ b/models/issue_stopwatch.go @@ -100,6 +100,7 @@ func CreateOrStopIssueStopwatch(user *User, issue *Issue) error { Repo: issue.Repo, Content: SecToTime(timediff), Type: CommentTypeStopTracking, + TimeID: tt.ID, }); err != nil { return err } diff --git a/models/issue_tracked_time.go b/models/issue_tracked_time.go index 195f3e7850..9717944cbb 100644 --- a/models/issue_tracked_time.go +++ b/models/issue_tracked_time.go @@ -162,6 +162,7 @@ func AddTime(user *User, issue *Issue, amount int64, created time.Time) (*Tracke Doer: user, Content: SecToTime(amount), Type: CommentTypeAddTimeManual, + TimeID: t.ID, }); err != nil { return nil, err } diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 740e8cb3ee..4fc737e1bf 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -292,6 +292,8 @@ var migrations = []Migration{ NewMigration("Add Sorting to ProjectBoard table", addSortingColToProjectBoard), // v172 -> v173 NewMigration("Add sessions table for go-chi/session", addSessionTable), + // v173 -> v174 + NewMigration("Add time_id column to Comment", addTimeIDCommentColumn), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v173.go b/models/migrations/v173.go new file mode 100644 index 0000000000..dd4589066d --- /dev/null +++ b/models/migrations/v173.go @@ -0,0 +1,22 @@ +// 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 migrations + +import ( + "fmt" + + "xorm.io/xorm" +) + +func addTimeIDCommentColumn(x *xorm.Engine) error { + type Comment struct { + TimeID int64 + } + + if err := x.Sync2(new(Comment)); err != nil { + return fmt.Errorf("Sync2: %v", err) + } + return nil +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index ec9f82ef03..2690303253 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1163,6 +1163,7 @@ issues.stop_tracking_history = `stopped working %s` issues.cancel_tracking = Discard issues.cancel_tracking_history = `cancelled time tracking %s` issues.add_time = Manually Add Time +issues.del_time = Delete this time log issues.add_time_short = Add Time issues.add_time_cancel = Cancel issues.add_time_history = `added spent time %s` diff --git a/routers/repo/issue.go b/routers/repo/issue.go index fa1ee99771..a9459a10ed 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -1416,6 +1416,10 @@ func ViewIssue(ctx *context.Context) { ctx.ServerError("LoadPushCommits", err) return } + } else if comment.Type == models.CommentTypeAddTimeManual || + comment.Type == models.CommentTypeStopTracking { + // drop error since times could be pruned from DB.. + _ = comment.LoadTime() } } diff --git a/routers/repo/issue_timetrack.go b/routers/repo/issue_timetrack.go index 425f215110..3b13770d61 100644 --- a/routers/repo/issue_timetrack.go +++ b/routers/repo/issue_timetrack.go @@ -10,13 +10,13 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" - auth "code.gitea.io/gitea/modules/forms" + "code.gitea.io/gitea/modules/forms" "code.gitea.io/gitea/modules/web" ) // AddTimeManually tracks time manually func AddTimeManually(c *context.Context) { - form := web.GetForm(c).(*auth.AddTimeManuallyForm) + form := web.GetForm(c).(*forms.AddTimeManuallyForm) issue := GetActionIssue(c) if c.Written() { return @@ -48,3 +48,39 @@ func AddTimeManually(c *context.Context) { c.Redirect(url, http.StatusSeeOther) } + +// DeleteTime deletes tracked time +func DeleteTime(c *context.Context) { + issue := GetActionIssue(c) + if c.Written() { + return + } + if !c.Repo.CanUseTimetracker(issue, c.User) { + c.NotFound("CanUseTimetracker", nil) + return + } + + t, err := models.GetTrackedTimeByID(c.ParamsInt64(":timeid")) + if err != nil { + if models.IsErrNotExist(err) { + c.NotFound("time not found", err) + return + } + c.Error(http.StatusInternalServerError, "GetTrackedTimeByID", err.Error()) + return + } + + // only OP or admin may delete + if !c.IsSigned || (!c.IsUserSiteAdmin() && c.User.ID != t.UserID) { + c.Error(http.StatusForbidden, "not allowed") + return + } + + if err = models.DeleteTime(t); err != nil { + c.ServerError("DeleteTime", err) + return + } + + c.Flash.Success(c.Tr("repo.issues.del_time_history", models.SecToTime(t.Time))) + c.Redirect(issue.HTMLURL()) +} diff --git a/routers/routes/web.go b/routers/routes/web.go index b3edd8f713..dd43663e35 100644 --- a/routers/routes/web.go +++ b/routers/routes/web.go @@ -723,6 +723,7 @@ func RegisterRoutes(m *web.Route) { m.Combo("/comments").Post(repo.MustAllowUserComment, bindIgnErr(auth.CreateCommentForm{}), repo.NewComment) m.Group("/times", func() { m.Post("/add", bindIgnErr(auth.AddTimeManuallyForm{}), repo.AddTimeManually) + m.Post("/{timeid}/delete", repo.DeleteTime) m.Group("/stopwatch", func() { m.Post("/toggle", repo.IssueStopwatch) m.Post("/cancel", repo.CancelStopwatch) diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index b971c6b1ae..cfacde9648 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -276,6 +276,7 @@ {{.Poster.GetDisplayName}} {{$.i18n.Tr "repo.issues.stop_tracking_history" $createdStr | Safe}} + {{ template "repo/issue/view_content/comments_delete_time" Dict "ctx" $ "comment" . }}
{{svg "octicon-clock"}} {{.Content}} @@ -291,6 +292,7 @@ {{.Poster.GetDisplayName}} {{$.i18n.Tr "repo.issues.add_time_history" $createdStr | Safe}} + {{ template "repo/issue/view_content/comments_delete_time" Dict "ctx" $ "comment" . }}
{{svg "octicon-clock"}} {{.Content}} diff --git a/templates/repo/issue/view_content/comments_delete_time.tmpl b/templates/repo/issue/view_content/comments_delete_time.tmpl new file mode 100644 index 0000000000..a28d222f86 --- /dev/null +++ b/templates/repo/issue/view_content/comments_delete_time.tmpl @@ -0,0 +1,21 @@ +{{ if .comment.Time }} {{/* compatibility with time comments made before v1.14 */}} + {{ if (not .comment.Time.Deleted) }} + {{ if (or .ctx.IsAdmin (and .ctx.IsSigned (eq .ctx.SignedUserID .comment.PosterID))) }} + + + + + {{end}} + {{end}} +{{end}} diff --git a/templates/repo/issue/view_content/sidebar.tmpl b/templates/repo/issue/view_content/sidebar.tmpl index 3a21a9271b..9df7d7ed44 100644 --- a/templates/repo/issue/view_content/sidebar.tmpl +++ b/templates/repo/issue/view_content/sidebar.tmpl @@ -348,7 +348,7 @@ {{end}}
-