From 1a905b2b8cfd16de792ffb50cf255988f712bafc Mon Sep 17 00:00:00 2001 From: kolaente Date: Tue, 1 May 2018 22:45:56 +0200 Subject: [PATCH] Implemented basic api endpoint to manage deadlines --- public/swagger.v1.json | 66 +++++++++++++++++++++++++ routers/api/v1/api.go | 2 + routers/api/v1/repo/issue.go | 63 +++++++++++++++++++++++ routers/api/v1/swagger/options.go | 1 + vendor/code.gitea.io/sdk/gitea/issue.go | 14 ++++++ 5 files changed, 146 insertions(+) diff --git a/public/swagger.v1.json b/public/swagger.v1.json index 6fea729c0..0ab9e8f93 100644 --- a/public/swagger.v1.json +++ b/public/swagger.v1.json @@ -2289,6 +2289,56 @@ } } }, + "/repos/{owner}/{repo}/issues/{index}/deadline": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "issue" + ], + "summary": "Set an issue deadline", + "operationId": "issueCreateIssueDeadline", + "parameters": [ + { + "type": "string", + "description": "owner of the repo", + "name": "owner", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "name of the repo", + "name": "repo", + "in": "path", + "required": true + }, + { + "type": "integer", + "description": "index of the issue to create or update a deadline on", + "name": "index", + "in": "path", + "required": true + }, + { + "name": "body", + "in": "body", + "schema": { + "$ref": "#/definitions/CreateDeadlineOption" + } + } + ], + "responses": { + "201": { + "$ref": "#/responses/IssueDeadline" + } + } + } + }, "/repos/{owner}/{repo}/issues/{index}/labels": { "get": { "produces": [ @@ -5470,6 +5520,21 @@ }, "x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea" }, + "CreateDeadlineOption": { + "description": "CreateDeadlineOption options for creating a deadline", + "type": "object", + "required": [ + "due_date" + ], + "properties": { + "due_date": { + "type": "string", + "format": "date-time", + "x-go-name": "Deadline" + } + }, + "x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea" + }, "CreateEmailOption": { "description": "CreateEmailOption options when creating email addresses", "type": "object", @@ -7675,6 +7740,7 @@ "headers": { "AddCollaboratorOption": {}, "AddTimeOption": {}, + "CreateDeadlineOption": {}, "CreateEmailOption": {}, "CreateForkOption": {}, "CreateHookOption": {}, diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index eec55cac6..421afc0da 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -446,6 +446,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Combo("").Get(repo.ListTrackedTimes). Post(reqToken(), bind(api.AddTimeOption{}), repo.AddTime) }) + + m.Combo("/deadline").Post(reqToken(), bind(api.CreateDeadlineOption{}), repo.UpdateIssueDeadline) }) }, mustEnableIssues) m.Group("/labels", func() { diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index cc033554f..4621e3df8 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -333,3 +333,66 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) { } ctx.JSON(201, issue.APIFormat()) } + +// UpdateIssueDeadline updates an issue deadline +func UpdateIssueDeadline(ctx *context.APIContext, form api.CreateDeadlineOption) { + // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/deadline issue issueCreateIssueDeadline + // --- + // summary: Set an issue deadline + // 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 or update a deadline on + // type: integer + // required: true + // - name: body + // in: body + // schema: + // "$ref": "#/definitions/CreateDeadlineOption" + // responses: + // "201": + // "$ref": "#/responses/IssueDeadline" + + if !ctx.Repo.IsWriter() { + ctx.JSON(401, map[string]string{ + "message": "Only users with write access to this repository can manage issue deadlines.", + }) + return + } + + 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 + } + + var deadlineUnix util.TimeStamp + if form.Deadline != nil && !form.Deadline.IsZero() { + deadlineUnix = util.TimeStamp(form.Deadline.Unix()) + } + + if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil { + ctx.Error(500, "UpdateIssueDeadline", err) + return + } + + ctx.JSON(201, api.IssueDeadline{Deadline:form.Deadline}) +} diff --git a/routers/api/v1/swagger/options.go b/routers/api/v1/swagger/options.go index 3ea324186..1c740a16d 100644 --- a/routers/api/v1/swagger/options.go +++ b/routers/api/v1/swagger/options.go @@ -24,6 +24,7 @@ type swaggerParameterBodies struct { CreateIssueOption api.CreateIssueOption EditIssueOption api.EditIssueOption + CreateDeadlineOption api.CreateDeadlineOption CreateIssueCommentOption api.CreateIssueCommentOption EditIssueCommentOption api.EditIssueCommentOption diff --git a/vendor/code.gitea.io/sdk/gitea/issue.go b/vendor/code.gitea.io/sdk/gitea/issue.go index 27809ca3b..0c3c6379c 100644 --- a/vendor/code.gitea.io/sdk/gitea/issue.go +++ b/vendor/code.gitea.io/sdk/gitea/issue.go @@ -138,3 +138,17 @@ func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) return issue, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), jsonHeader, bytes.NewReader(body), issue) } + +// CreateDeadlineOption options for creating a deadline +type CreateDeadlineOption struct { + // required:true + // swagger:strfmt date-time + Deadline *time.Time `json:"due_date"` +} + +// IssueDeadline represents an issue deadline +// swagger:model +type IssueDeadline struct { + // swagger:strfmt date-time + Deadline *time.Time `json:"due_date"` +}