Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,7 @@ issues.remove_time_estimate_at = removed time estimate %s
issues.time_estimate_invalid = Time estimate format is invalid
issues.start_tracking_history = started working %s
issues.tracker_auto_close = Timer will be stopped automatically when this issue gets closed
issues.tracker_already_stopped = The timer for this issue is already stopped
issues.tracking_already_started = `You have already started time tracking on <a href="%s">another issue</a>!`
issues.stop_tracking = Stop Timer
issues.stop_tracking_history = worked for <b>%[1]s</b> %[2]s
Expand Down
40 changes: 29 additions & 11 deletions routers/web/repo/issue_stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,49 @@ import (
"code.gitea.io/gitea/services/context"
)

// IssueStopwatch creates or stops a stopwatch for the given issue.
func IssueStopwatch(c *context.Context) {
// IssueStartStopwatch creates a stopwatch for the given issue.
func IssueStartStopwatch(c *context.Context) {
issue := GetActionIssue(c)
if c.Written() {
return
}

var showSuccessMessage bool
if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
c.NotFound(nil)
return
}

if !issues_model.StopwatchExists(c, c.Doer.ID, issue.ID) {
showSuccessMessage = true
if err := issues_model.CreateIssueStopwatch(c, c.Doer, issue); err != nil {
c.ServerError("CreateIssueStopwatch", err)
return
}

if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
c.NotFound(nil)
c.Flash.Success(c.Tr("repo.issues.tracker_auto_close"))

c.JSONRedirect("")
}

// IssueStopStopwatch stops a stopwatch for the given issue.
func IssueStopStopwatch(c *context.Context) {
issue := GetActionIssue(c)
if c.Written() {
return
}

if err := issues_model.CreateOrStopIssueStopwatch(c, c.Doer, issue); err != nil {
c.ServerError("CreateOrStopIssueStopwatch", err)
if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
c.NotFound(nil)
return
}

if showSuccessMessage {
c.Flash.Success(c.Tr("repo.issues.tracker_auto_close"))
wasRunning := issues_model.StopwatchExists(c, c.Doer.ID, issue.ID)

if wasRunning {
if err := issues_model.FinishIssueStopwatch(c, c.Doer, issue); err != nil {
c.ServerError("FinishIssueStopwatch", err)
return
}
} else {
c.Flash.Warning(c.Tr("repo.issues.tracker_already_stopped"))
}

c.JSONRedirect("")
Expand Down
3 changes: 2 additions & 1 deletion routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,8 @@ func registerWebRoutes(m *web.Router) {
m.Post("/add", web.Bind(forms.AddTimeManuallyForm{}), repo.AddTimeManually)
m.Post("/{timeid}/delete", repo.DeleteTime)
m.Group("/stopwatch", func() {
m.Post("/toggle", repo.IssueStopwatch)
m.Post("/start", repo.IssueStartStopwatch)
m.Post("/stop", repo.IssueStopStopwatch)
m.Post("/cancel", repo.CancelStopwatch)
})
})
Expand Down
2 changes: 1 addition & 1 deletion templates/base/head_navbar.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
<span class="stopwatch-issue">{{$activeStopwatch.RepoSlug}}#{{$activeStopwatch.IssueIndex}}</span>
</a>
<div class="tw-flex tw-gap-1">
<form class="stopwatch-commit form-fetch-action" method="post" action="{{$activeStopwatch.IssueLink}}/times/stopwatch/toggle">
<form class="stopwatch-commit form-fetch-action" method="post" action="{{$activeStopwatch.IssueLink}}/times/stopwatch/stop">
{{.CsrfTokenHtml}}
<button
type="submit"
Expand Down
4 changes: 2 additions & 2 deletions templates/repo/issue/sidebar/stopwatch_timetracker.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
</a>
<div class="divider"></div>
{{if $.IsStopwatchRunning}}
<a class="item issue-stop-time link-action" data-url="{{.Issue.Link}}/times/stopwatch/toggle">
<a class="item issue-stop-time link-action" data-url="{{.Issue.Link}}/times/stopwatch/stop">
{{svg "octicon-stopwatch"}} {{ctx.Locale.Tr "repo.issues.timetracker_timer_stop"}}
</a>
<a class="item issue-cancel-time link-action" data-url="{{.Issue.Link}}/times/stopwatch/cancel">
{{svg "octicon-trash"}} {{ctx.Locale.Tr "repo.issues.timetracker_timer_discard"}}
</a>
{{else}}
<a class="item issue-start-time link-action" data-url="{{.Issue.Link}}/times/stopwatch/toggle">
<a class="item issue-start-time link-action" data-url="{{.Issue.Link}}/times/stopwatch/start">
{{svg "octicon-stopwatch"}} {{ctx.Locale.Tr "repo.issues.timetracker_timer_start"}}
</a>
<a class="item issue-add-time show-modal" data-modal="#issue-time-manually-add-modal">
Expand Down
18 changes: 11 additions & 7 deletions tests/integration/timetracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ func testViewTimetrackingControls(t *testing.T, session *TestSession, user, repo
AssertHTMLElement(t, htmlDoc, ".issue-add-time", canTrackTime)

issueLink := path.Join(user, repo, "issues", issue)
req = NewRequestWithValues(t, "POST", path.Join(issueLink, "times", "stopwatch", "toggle"), map[string]string{
"_csrf": htmlDoc.GetCSRF(),
})

if canTrackTime {
session.MakeRequest(t, req, http.StatusOK)
reqStart := NewRequestWithValues(t, "POST", path.Join(issueLink, "times", "stopwatch", "start"), map[string]string{
"_csrf": htmlDoc.GetCSRF(),
})
session.MakeRequest(t, reqStart, http.StatusOK)

req = NewRequest(t, "GET", issueLink)
resp = session.MakeRequest(t, req, http.StatusOK)
Expand All @@ -65,10 +66,10 @@ func testViewTimetrackingControls(t *testing.T, session *TestSession, user, repo
// Sleep for 1 second to not get wrong order for stopping timer
time.Sleep(time.Second)

req = NewRequestWithValues(t, "POST", path.Join(issueLink, "times", "stopwatch", "toggle"), map[string]string{
reqStop := NewRequestWithValues(t, "POST", path.Join(issueLink, "times", "stopwatch", "stop"), map[string]string{
"_csrf": htmlDoc.GetCSRF(),
})
session.MakeRequest(t, req, http.StatusOK)
session.MakeRequest(t, reqStop, http.StatusOK)

req = NewRequest(t, "GET", issueLink)
resp = session.MakeRequest(t, req, http.StatusOK)
Expand All @@ -77,6 +78,9 @@ func testViewTimetrackingControls(t *testing.T, session *TestSession, user, repo
events = htmlDoc.doc.Find(".event > span.text")
assert.Contains(t, events.Last().Text(), "worked for ")
} else {
session.MakeRequest(t, req, http.StatusNotFound)
reqStart := NewRequestWithValues(t, "POST", path.Join(issueLink, "times", "stopwatch", "start"), map[string]string{
"_csrf": htmlDoc.GetCSRF(),
})
session.MakeRequest(t, reqStart, http.StatusNotFound)
}
}
2 changes: 1 addition & 1 deletion web_src/js/features/stopwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function updateStopwatchData(data: any) {
const {repo_owner_name, repo_name, issue_index, seconds} = watch;
const issueUrl = `${appSubUrl}/${repo_owner_name}/${repo_name}/issues/${issue_index}`;
document.querySelector('.stopwatch-link')?.setAttribute('href', issueUrl);
document.querySelector('.stopwatch-commit')?.setAttribute('action', `${issueUrl}/times/stopwatch/toggle`);
document.querySelector('.stopwatch-commit')?.setAttribute('action', `${issueUrl}/times/stopwatch/stop`);
document.querySelector('.stopwatch-cancel')?.setAttribute('action', `${issueUrl}/times/stopwatch/cancel`);
const stopwatchIssue = document.querySelector('.stopwatch-issue');
if (stopwatchIssue) stopwatchIssue.textContent = `${repo_owner_name}/${repo_name}#${issue_index}`;
Expand Down