2017-09-12 14:48:13 +08:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-09-12 14:48:13 +08:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-12-05 21:07:53 +08:00
|
|
|
"strings"
|
2017-09-12 14:48:13 +08:00
|
|
|
"time"
|
|
|
|
|
2022-05-08 21:46:34 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 17:37:59 +08:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-02-16 00:50:10 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 23:36:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2024-02-27 15:12:22 +08:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2021-04-07 03:44:05 +08:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2024-12-05 21:07:53 +08:00
|
|
|
issue_service "code.gitea.io/gitea/services/issue"
|
2017-09-12 14:48:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddTimeManually tracks time manually
|
2021-01-26 23:36:53 +08:00
|
|
|
func AddTimeManually(c *context.Context) {
|
2021-02-19 18:52:11 +08:00
|
|
|
form := web.GetForm(c).(*forms.AddTimeManuallyForm)
|
2017-10-16 15:55:43 +08:00
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
2023-10-14 16:37:24 +08:00
|
|
|
if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
|
2018-01-11 05:34:17 +08:00
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
2017-09-12 14:48:13 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HasError() {
|
2024-12-05 21:07:53 +08:00
|
|
|
c.JSONError(c.GetErrMsg())
|
2017-09-12 14:48:13 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
total := time.Duration(form.Hours)*time.Hour + time.Duration(form.Minutes)*time.Minute
|
|
|
|
|
|
|
|
if total <= 0 {
|
2024-12-05 21:07:53 +08:00
|
|
|
c.JSONError(c.Tr("repo.issues.add_time_sum_to_small"))
|
2017-09-12 14:48:13 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-22 22:14:27 +08:00
|
|
|
if _, err := issues_model.AddTime(c, c.Doer, issue, int64(total.Seconds()), time.Now()); err != nil {
|
2018-01-11 05:34:17 +08:00
|
|
|
c.ServerError("AddTime", err)
|
2017-09-12 14:48:13 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-12-05 21:07:53 +08:00
|
|
|
c.JSONRedirect("")
|
2017-09-12 14:48:13 +08:00
|
|
|
}
|
2021-02-19 18:52:11 +08:00
|
|
|
|
|
|
|
// DeleteTime deletes tracked time
|
|
|
|
func DeleteTime(c *context.Context) {
|
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
2023-10-14 16:37:24 +08:00
|
|
|
if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
|
2021-02-19 18:52:11 +08:00
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-19 06:32:45 +08:00
|
|
|
t, err := issues_model.GetTrackedTimeByID(c, c.PathParamInt64(":timeid"))
|
2021-02-19 18:52:11 +08:00
|
|
|
if err != nil {
|
2022-05-08 21:46:34 +08:00
|
|
|
if db.IsErrNotExist(err) {
|
2021-02-19 18:52:11 +08:00
|
|
|
c.NotFound("time not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Error(http.StatusInternalServerError, "GetTrackedTimeByID", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// only OP or admin may delete
|
2022-03-22 15:03:22 +08:00
|
|
|
if !c.IsSigned || (!c.IsUserSiteAdmin() && c.Doer.ID != t.UserID) {
|
2021-02-19 18:52:11 +08:00
|
|
|
c.Error(http.StatusForbidden, "not allowed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-03 18:30:41 +08:00
|
|
|
if err = issues_model.DeleteTime(c, t); err != nil {
|
2021-02-19 18:52:11 +08:00
|
|
|
c.ServerError("DeleteTime", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-16 00:50:10 +08:00
|
|
|
c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToTime(t.Time)))
|
2024-12-05 21:07:53 +08:00
|
|
|
c.JSONRedirect("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateIssueTimeEstimate(ctx *context.Context) {
|
|
|
|
issue := GetActionIssue(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.IsSigned || (!issue.IsPoster(ctx.Doer.ID) && !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)) {
|
|
|
|
ctx.Error(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
timeStr := strings.TrimSpace(ctx.FormString("time_estimate"))
|
|
|
|
|
|
|
|
total, err := util.TimeEstimateParse(timeStr)
|
|
|
|
if err != nil {
|
|
|
|
ctx.JSONError(ctx.Tr("repo.issues.time_estimate_invalid"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// No time changed
|
|
|
|
if issue.TimeEstimate == total {
|
|
|
|
ctx.JSONRedirect("")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := issue_service.ChangeTimeEstimate(ctx, issue, ctx.Doer, total); err != nil {
|
|
|
|
ctx.ServerError("ChangeTimeEstimate", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSONRedirect("")
|
2021-02-19 18:52:11 +08:00
|
|
|
}
|