diff options
author | Lanre Adelowo <adelowomailbox@gmail.com> | 2019-02-07 03:57:25 +0100 |
---|---|---|
committer | techknowlogick <matti@mdranta.net> | 2019-02-06 21:57:25 -0500 |
commit | 9dfdf80af08cee3b2f0297843084fe14e3af31e7 (patch) | |
tree | 82de57b6085e8dfa4c9697edbf6b13c1daf0e26d /routers | |
parent | cc48c12d8f16dcb10b21972f7bfd631c5d15be48 (diff) | |
download | gitea-9dfdf80af08cee3b2f0297843084fe14e3af31e7.tar.gz gitea-9dfdf80af08cee3b2f0297843084fe14e3af31e7.zip |
Expose issue stopwatch toggling via API (#5970)
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/api.go | 4 | ||||
-rw-r--r-- | routers/api/v1/repo/issue.go | 138 |
2 files changed, 142 insertions, 0 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 55f5c66290..c5a5488a81 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -557,6 +557,10 @@ func RegisterRoutes(m *macaron.Macaron) { }) m.Combo("/deadline").Post(reqToken(), bind(api.EditDeadlineOption{}), repo.UpdateIssueDeadline) + m.Group("/stopwatch", func() { + m.Post("/start", reqToken(), repo.StartIssueStopwatch) + m.Post("/stop", reqToken(), repo.StopIssueStopwatch) + }) }) }, mustEnableIssuesOrPulls) m.Group("/labels", func() { diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 1cb9c2f819..d339d8f0b7 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -439,3 +439,141 @@ func UpdateIssueDeadline(ctx *context.APIContext, form api.EditDeadlineOption) { ctx.JSON(201, api.IssueDeadline{Deadline: &deadline}) } + +// StartIssueStopwatch creates a stopwatch for the given issue. +func StartIssueStopwatch(ctx *context.APIContext) { + // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/start issue issueStartStopWatch + // --- + // summary: Start stopwatch on an issue. + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: owner + // in: path + // description: owner of the repo + // type: string + // required: true + // - name: repo + // in: path + // description: name of the repo + // type: string + // required: true + // - name: index + // in: path + // description: index of the issue to create the stopwatch on + // type: integer + // format: int64 + // required: true + // responses: + // "201": + // "$ref": "#/responses/empty" + // "403": + // description: Not repo writer, user does not have rights to toggle stopwatch + // "404": + // description: Issue not found + // "409": + // description: Cannot start a stopwatch again if it already exists + issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + if err != nil { + if models.IsErrIssueNotExist(err) { + ctx.Status(404) + } else { + ctx.Error(500, "GetIssueByIndex", err) + } + + return + } + + if !ctx.Repo.CanWrite(models.UnitTypeIssues) { + ctx.Status(403) + return + } + + if !ctx.Repo.CanUseTimetracker(issue, ctx.User) { + ctx.Status(403) + return + } + + if models.StopwatchExists(ctx.User.ID, issue.ID) { + ctx.Error(409, "StopwatchExists", "a stopwatch has already been started for this issue") + return + } + + if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil { + ctx.Error(500, "CreateOrStopIssueStopwatch", err) + return + } + + ctx.Status(201) +} + +// StopIssueStopwatch stops a stopwatch for the given issue. +func StopIssueStopwatch(ctx *context.APIContext) { + // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/stop issue issueStopWatch + // --- + // summary: Stop an issue's existing stopwatch. + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: owner + // in: path + // description: owner of the repo + // type: string + // required: true + // - name: repo + // in: path + // description: name of the repo + // type: string + // required: true + // - name: index + // in: path + // description: index of the issue to stop the stopwatch on + // type: integer + // format: int64 + // required: true + // responses: + // "201": + // "$ref": "#/responses/empty" + // "403": + // description: Not repo writer, user does not have rights to toggle stopwatch + // "404": + // description: Issue not found + // "409": + // description: Cannot stop a non existent stopwatch + issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + if err != nil { + if models.IsErrIssueNotExist(err) { + ctx.Status(404) + } else { + ctx.Error(500, "GetIssueByIndex", err) + } + + return + } + + if !ctx.Repo.CanWrite(models.UnitTypeIssues) { + ctx.Status(403) + return + } + + if !ctx.Repo.CanUseTimetracker(issue, ctx.User) { + ctx.Status(403) + return + } + + if !models.StopwatchExists(ctx.User.ID, issue.ID) { + ctx.Error(409, "StopwatchExists", "cannot stop a non existent stopwatch") + return + } + + if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil { + ctx.Error(500, "CreateOrStopIssueStopwatch", err) + return + } + + ctx.Status(201) +} |