Skip to content

Commit 2f9fb24

Browse files
committed
split toggle endpoint into 'start' and 'stop' to make it more explicit
1 parent 36f2306 commit 2f9fb24

File tree

3 files changed

+136
-11
lines changed

3 files changed

+136
-11
lines changed

routers/api/v1/api.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,10 @@ func RegisterRoutes(m *macaron.Macaron) {
557557
})
558558

559559
m.Combo("/deadline").Post(reqToken(), bind(api.EditDeadlineOption{}), repo.UpdateIssueDeadline)
560-
m.Post("/stopwatch/toggle", reqToken(), repo.ToggleIssueStopwatch)
560+
m.Group("/stopwatch", func() {
561+
m.Post("/start", reqToken(), repo.StartIssueStopwatch)
562+
m.Post("/stop", reqToken(), repo.StopIssueStopwatch)
563+
})
561564
})
562565
}, mustEnableIssuesOrPulls)
563566
m.Group("/labels", func() {

routers/api/v1/repo/issue.go

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,11 @@ func UpdateIssueDeadline(ctx *context.APIContext, form api.EditDeadlineOption) {
440440
ctx.JSON(201, api.IssueDeadline{Deadline: &deadline})
441441
}
442442

443-
// ToggleIssueStopwatch creates or stops a stopwatch for the given issue.
444-
func ToggleIssueStopwatch(ctx *context.APIContext) {
445-
// swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/toggle issue issueToggleStopWatch
443+
// StartIssueStopwatch creates or stops a stopwatch for the given issue.
444+
func StartIssueStopwatch(ctx *context.APIContext) {
445+
// swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/start issue issueStartStopWatch
446446
// ---
447-
// summary: Toggle stopwatch on an issue.
447+
// summary: Start stopwatch on an issue.
448448
// consumes:
449449
// - application/json
450450
// produces:
@@ -470,7 +470,7 @@ func ToggleIssueStopwatch(ctx *context.APIContext) {
470470
// "201":
471471
// "$ref": "#/responses/empty"
472472
// "403":
473-
// description: Not repo writer or user does not have rights to toggle stopwatch
473+
// description: Not repo writer, user does not have rights to toggle stopwatch or trying to start a stopwatch again if it already exists
474474
// "404":
475475
// description: Issue not found
476476
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
@@ -494,8 +494,80 @@ func ToggleIssueStopwatch(ctx *context.APIContext) {
494494
return
495495
}
496496

497+
if models.StopwatchExists(ctx.User.ID, issue.ID) {
498+
ctx.Error(403, "StopwatchExists", "a stopwatch has already been started for this issue")
499+
return
500+
}
501+
502+
if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
503+
ctx.Error(500, "CreateOrStopIssueStopwatch", err)
504+
return
505+
}
506+
507+
ctx.Status(201)
508+
}
509+
510+
// StopIssueStopwatch creates or stops a stopwatch for the given issue.
511+
func StopIssueStopwatch(ctx *context.APIContext) {
512+
// swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/stop issue issueStopWatch
513+
// ---
514+
// summary: Stop an issue's existing stopwatch.
515+
// consumes:
516+
// - application/json
517+
// produces:
518+
// - application/json
519+
// parameters:
520+
// - name: owner
521+
// in: path
522+
// description: owner of the repo
523+
// type: string
524+
// required: true
525+
// - name: repo
526+
// in: path
527+
// description: name of the repo
528+
// type: string
529+
// required: true
530+
// - name: index
531+
// in: path
532+
// description: index of the issue to create or stop the stopwatch on
533+
// type: integer
534+
// format: int64
535+
// required: true
536+
// responses:
537+
// "201":
538+
// "$ref": "#/responses/empty"
539+
// "403":
540+
// description: Not repo writer, user does not have rights to toggle stopwatch or trying to stop a non existent stopwatch
541+
// "404":
542+
// description: Issue not found
543+
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
544+
if err != nil {
545+
if models.IsErrIssueNotExist(err) {
546+
ctx.Status(404)
547+
} else {
548+
ctx.Error(500, "GetIssueByIndex", err)
549+
}
550+
551+
return
552+
}
553+
554+
if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
555+
ctx.Status(403)
556+
return
557+
}
558+
559+
if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
560+
ctx.Status(403)
561+
return
562+
}
563+
564+
if !models.StopwatchExists(ctx.User.ID, issue.ID) {
565+
ctx.Error(403, "StopwatchExists", "cannot stop a non existent stopwatch")
566+
return
567+
}
568+
497569
if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
498-
ctx.ServerError("CreateOrStopIssueStopwatch", err)
570+
ctx.Error(500, "CreateOrStopIssueStopwatch", err)
499571
return
500572
}
501573

templates/swagger/v1_json.tmpl

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2924,7 +2924,7 @@
29242924
}
29252925
}
29262926
},
2927-
"/repos/{owner}/{repo}/issues/{index}/stopwatch/toggle": {
2927+
"/repos/{owner}/{repo}/issues/{index}/stopwatch/start": {
29282928
"post": {
29292929
"consumes": [
29302930
"application/json"
@@ -2935,8 +2935,8 @@
29352935
"tags": [
29362936
"issue"
29372937
],
2938-
"summary": "Toggle stopwatch on an issue.",
2939-
"operationId": "issueToggleStopWatch",
2938+
"summary": "Start stopwatch on an issue.",
2939+
"operationId": "issueStartStopWatch",
29402940
"parameters": [
29412941
{
29422942
"type": "string",
@@ -2966,7 +2966,57 @@
29662966
"$ref": "#/responses/empty"
29672967
},
29682968
"403": {
2969-
"description": "Not repo writer or user does not have rights to toggle stopwatch"
2969+
"description": "Not repo writer, user does not have rights to toggle stopwatch or trying to start a stopwatch again if it already exists"
2970+
},
2971+
"404": {
2972+
"description": "Issue not found"
2973+
}
2974+
}
2975+
}
2976+
},
2977+
"/repos/{owner}/{repo}/issues/{index}/stopwatch/stop": {
2978+
"post": {
2979+
"consumes": [
2980+
"application/json"
2981+
],
2982+
"produces": [
2983+
"application/json"
2984+
],
2985+
"tags": [
2986+
"issue"
2987+
],
2988+
"summary": "Stop an issue's existing stopwatch.",
2989+
"operationId": "issueStopWatch",
2990+
"parameters": [
2991+
{
2992+
"type": "string",
2993+
"description": "owner of the repo",
2994+
"name": "owner",
2995+
"in": "path",
2996+
"required": true
2997+
},
2998+
{
2999+
"type": "string",
3000+
"description": "name of the repo",
3001+
"name": "repo",
3002+
"in": "path",
3003+
"required": true
3004+
},
3005+
{
3006+
"type": "integer",
3007+
"format": "int64",
3008+
"description": "index of the issue to create or stop the stopwatch on",
3009+
"name": "index",
3010+
"in": "path",
3011+
"required": true
3012+
}
3013+
],
3014+
"responses": {
3015+
"201": {
3016+
"$ref": "#/responses/empty"
3017+
},
3018+
"403": {
3019+
"description": "Not repo writer, user does not have rights to toggle stopwatch or trying to stop a non existent stopwatch"
29703020
},
29713021
"404": {
29723022
"description": "Issue not found"

0 commit comments

Comments
 (0)