Files

302 lines
7.8 KiB
Go
Raw Permalink Normal View History

// Copyright 2016 The Gogs Authors. All rights reserved.
2020-01-24 19:00:29 +00:00
// Copyright 2020 The Gitea Authors.
// SPDX-License-Identifier: MIT
package repo
import (
2019-12-20 18:07:12 +01:00
"net/http"
"strconv"
2016-08-24 16:05:56 -07:00
2023-12-11 16:56:48 +08:00
"code.gitea.io/gitea/models/db"
2022-04-08 17:11:15 +08:00
issues_model "code.gitea.io/gitea/models/issues"
2019-05-11 18:21:34 +08:00
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
2021-01-26 23:36:53 +08:00
"code.gitea.io/gitea/modules/web"
2020-01-24 19:00:29 +00:00
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/routers/common"
"code.gitea.io/gitea/services/context"
2022-12-29 03:57:15 +01:00
"code.gitea.io/gitea/services/convert"
)
// ListMilestones list milestones for a repository
func ListMilestones(ctx *context.APIContext) {
2018-06-12 16:59:22 +02:00
// swagger:operation GET /repos/{owner}/{repo}/milestones issue issueGetMilestonesList
2017-11-12 23:02:25 -08:00
// ---
// summary: Get all of a repository's opened milestones
2017-11-12 23:02:25 -08:00
// produces:
// - application/json
2018-06-12 16:59:22 +02:00
// 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: state
// in: query
2022-06-13 16:34:46 +09:00
// description: Milestone state, Recognized values are open, closed and all. Defaults to "open"
// type: string
// - name: name
// in: query
// description: filter by milestone name
// type: string
2020-01-24 19:00:29 +00:00
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
2020-01-24 19:00:29 +00:00
// type: integer
2017-11-12 23:02:25 -08:00
// responses:
// "200":
2018-06-12 16:59:22 +02:00
// "$ref": "#/responses/MilestoneList"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 18:07:12 +01:00
isClosed := common.ParseIssueFilterStateIsClosed(ctx.FormString("state"))
2023-12-11 16:56:48 +08:00
milestones, total, err := db.FindAndCount[issues_model.Milestone](ctx, issues_model.FindMilestoneOptions{
ListOptions: utils.GetListOptions(ctx),
RepoID: ctx.Repo.Repository.ID,
2023-12-11 16:56:48 +08:00
IsClosed: isClosed,
Name: ctx.FormString("name"),
})
if err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
return
}
apiMilestones := make([]*api.Milestone, len(milestones))
for i := range milestones {
2020-05-12 23:54:35 +02:00
apiMilestones[i] = convert.ToAPIMilestone(milestones[i])
}
2021-08-12 14:43:08 +02:00
ctx.SetTotalCountHeader(total)
2019-12-20 18:07:12 +01:00
ctx.JSON(http.StatusOK, &apiMilestones)
}
// GetMilestone get a milestone for a repository by ID and if not available by name
func GetMilestone(ctx *context.APIContext) {
2018-06-12 16:59:22 +02:00
// swagger:operation GET /repos/{owner}/{repo}/milestones/{id} issue issueGetMilestone
2017-11-12 23:02:25 -08:00
// ---
2018-06-12 16:59:22 +02:00
// summary: Get a milestone
2017-11-12 23:02:25 -08:00
// 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: id
// in: path
// description: the milestone to get, identified by ID and if not available by name
// type: string
2017-11-12 23:02:25 -08:00
// required: true
// responses:
// "200":
2018-06-12 16:59:22 +02:00
// "$ref": "#/responses/Milestone"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 18:07:12 +01:00
milestone := getMilestoneByIDOrName(ctx)
if ctx.Written() {
return
}
2020-05-12 23:54:35 +02:00
ctx.JSON(http.StatusOK, convert.ToAPIMilestone(milestone))
}
2016-11-24 15:04:31 +08:00
// CreateMilestone create a milestone for a repository
2021-01-26 23:36:53 +08:00
func CreateMilestone(ctx *context.APIContext) {
2017-11-12 23:02:25 -08:00
// swagger:operation POST /repos/{owner}/{repo}/milestones issue issueCreateMilestone
// ---
// summary: Create a milestone
// 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: body
// in: body
// schema:
// "$ref": "#/definitions/CreateMilestoneOption"
// responses:
// "201":
// "$ref": "#/responses/Milestone"
// "404":
// "$ref": "#/responses/notFound"
2021-01-26 23:36:53 +08:00
form := web.GetForm(ctx).(*api.CreateMilestoneOption)
2019-12-20 18:07:12 +01:00
var deadlineUnix int64
if form.Deadline != nil {
deadlineUnix = form.Deadline.Unix()
}
2022-04-08 17:11:15 +08:00
milestone := &issues_model.Milestone{
RepoID: ctx.Repo.Repository.ID,
Name: form.Title,
Content: form.Description,
DeadlineUnix: timeutil.TimeStamp(deadlineUnix),
}
if form.State == "closed" {
milestone.IsClosed = true
milestone.ClosedDateUnix = timeutil.TimeStampNow()
}
if err := issues_model.NewMilestone(ctx, milestone); err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
return
}
2020-05-12 23:54:35 +02:00
ctx.JSON(http.StatusCreated, convert.ToAPIMilestone(milestone))
}
// EditMilestone modify a milestone for a repository by ID and if not available by name
2021-01-26 23:36:53 +08:00
func EditMilestone(ctx *context.APIContext) {
2017-11-12 23:02:25 -08:00
// swagger:operation PATCH /repos/{owner}/{repo}/milestones/{id} issue issueEditMilestone
// ---
// summary: Update a milestone
// 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
2018-06-12 16:59:22 +02:00
// - name: id
// in: path
// description: the milestone to edit, identified by ID and if not available by name
// type: string
2018-06-12 16:59:22 +02:00
// required: true
2017-11-12 23:02:25 -08:00
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/EditMilestoneOption"
// responses:
// "200":
// "$ref": "#/responses/Milestone"
// "404":
// "$ref": "#/responses/notFound"
2021-01-26 23:36:53 +08:00
form := web.GetForm(ctx).(*api.EditMilestoneOption)
milestone := getMilestoneByIDOrName(ctx)
if ctx.Written() {
return
}
if len(form.Title) > 0 {
milestone.Name = form.Title
}
2016-08-24 16:05:56 -07:00
if form.Description != nil {
milestone.Content = *form.Description
}
milestone.DeadlineUnix, _ = common.ParseAPIDeadlineToEndOfDay(form.Deadline)
2016-08-24 16:05:56 -07:00
2022-01-20 18:46:10 +01:00
oldIsClosed := milestone.IsClosed
if form.State != nil {
milestone.IsClosed = *form.State == string(api.StateClosed)
}
if err := issues_model.UpdateMilestone(ctx, milestone, oldIsClosed); err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
return
}
2020-05-12 23:54:35 +02:00
ctx.JSON(http.StatusOK, convert.ToAPIMilestone(milestone))
}
// DeleteMilestone delete a milestone for a repository by ID and if not available by name
func DeleteMilestone(ctx *context.APIContext) {
2017-11-12 23:02:25 -08:00
// swagger:operation DELETE /repos/{owner}/{repo}/milestones/{id} issue issueDeleteMilestone
// ---
// summary: Delete a milestone
// 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
2018-06-12 16:59:22 +02:00
// - name: id
2017-11-12 23:02:25 -08:00
// in: path
// description: the milestone to delete, identified by ID and if not available by name
// type: string
2017-11-12 23:02:25 -08:00
// required: true
// responses:
// "204":
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 18:07:12 +01:00
m := getMilestoneByIDOrName(ctx)
if ctx.Written() {
return
}
if err := issues_model.DeleteMilestoneByRepoID(ctx, ctx.Repo.Repository.ID, m.ID); err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
return
}
2019-12-20 18:07:12 +01:00
ctx.Status(http.StatusNoContent)
}
// getMilestoneByIDOrName get milestone by ID and if not available by name
2022-04-08 17:11:15 +08:00
func getMilestoneByIDOrName(ctx *context.APIContext) *issues_model.Milestone {
2024-12-24 21:47:45 +08:00
mile := ctx.PathParam("id")
mileID, _ := strconv.ParseInt(mile, 0, 64)
if mileID != 0 {
2022-04-08 17:11:15 +08:00
milestone, err := issues_model.GetMilestoneByRepoID(ctx, ctx.Repo.Repository.ID, mileID)
if err == nil {
return milestone
2022-04-08 17:11:15 +08:00
} else if !issues_model.IsErrMilestoneNotExist(err) {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
return nil
}
}
milestone, err := issues_model.GetMilestoneByRepoIDANDName(ctx, ctx.Repo.Repository.ID, mile)
if err != nil {
2022-04-08 17:11:15 +08:00
if issues_model.IsErrMilestoneNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
return nil
}
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
return nil
}
return milestone
}