aboutsummaryrefslogtreecommitdiffstats
path: root/routers/web/repo/issue_timetrack.go
blob: 36e931a48fb054a159bba65fbe659f2e840a1dd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package repo

import (
	"net/http"
	"strings"
	"time"

	"code.gitea.io/gitea/models/db"
	issues_model "code.gitea.io/gitea/models/issues"
	"code.gitea.io/gitea/modules/util"
	"code.gitea.io/gitea/modules/web"
	"code.gitea.io/gitea/services/context"
	"code.gitea.io/gitea/services/forms"
	issue_service "code.gitea.io/gitea/services/issue"
)

// AddTimeManually tracks time manually
func AddTimeManually(c *context.Context) {
	form := web.GetForm(c).(*forms.AddTimeManuallyForm)
	issue := GetActionIssue(c)
	if c.Written() {
		return
	}
	if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
		c.NotFound("CanUseTimetracker", nil)
		return
	}

	if c.HasError() {
		c.JSONError(c.GetErrMsg())
		return
	}

	total := time.Duration(form.Hours)*time.Hour + time.Duration(form.Minutes)*time.Minute

	if total <= 0 {
		c.JSONError(c.Tr("repo.issues.add_time_sum_to_small"))
		return
	}

	if _, err := issues_model.AddTime(c, c.Doer, issue, int64(total.Seconds()), time.Now()); err != nil {
		c.ServerError("AddTime", err)
		return
	}

	c.JSONRedirect("")
}

// DeleteTime deletes tracked time
func DeleteTime(c *context.Context) {
	issue := GetActionIssue(c)
	if c.Written() {
		return
	}
	if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
		c.NotFound("CanUseTimetracker", nil)
		return
	}

	t, err := issues_model.GetTrackedTimeByID(c, c.PathParamInt64("timeid"))
	if err != nil {
		if db.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.Doer.ID != t.UserID) {
		c.Error(http.StatusForbidden, "not allowed")
		return
	}

	if err = issues_model.DeleteTime(c, t); err != nil {
		c.ServerError("DeleteTime", err)
		return
	}

	c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToTime(t.Time)))
	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("")
}