1
1
mirror of https://github.com/go-gitea/gitea synced 2025-01-10 09:44:43 +00:00

api: create endpoint for changing project status

This commit is contained in:
eyad-hussein 2024-07-10 10:32:46 +03:00
parent 5814b6d590
commit 773bd91232
2 changed files with 53 additions and 1 deletions

View File

@ -811,6 +811,28 @@ func checkDeprecatedAuthMethods(ctx *context.APIContext) {
} }
} }
func reqUnitAccess(unitType unit.Type, accessMode perm.AccessMode, ignoreGlobal bool) func(ctx *context.APIContext) {
return func(ctx *context.APIContext) {
// only check global disabled units when ignoreGlobal is false
if !ignoreGlobal && unitType.UnitGlobalDisabled() {
ctx.NotFound("Repo unit is is disabled: "+unitType.LogString(), nil)
return
}
if ctx.ContextUser == nil {
ctx.NotFound("ContextUser is nil", nil)
return
}
if ctx.ContextUser.IsOrganization() {
if ctx.Org.Organization.UnitPermission(ctx, ctx.Doer, unitType) < accessMode {
ctx.NotFound("ContextUser is org but doer has no access to unit", nil)
return
}
}
}
}
// Routes registers all v1 APIs routes to web application. // Routes registers all v1 APIs routes to web application.
func Routes() *web.Router { func Routes() *web.Router {
m := web.NewRouter() m := web.NewRouter()
@ -960,8 +982,16 @@ func Routes() *web.Router {
m.Group("/projects", func() { m.Group("/projects", func() {
m.Post("", bind(api.CreateProjectOption{}), org.CreateProject) m.Post("", bind(api.CreateProjectOption{}), org.CreateProject)
m.Group("/{id}", func() {
m.Post("/{action:open|close}", org.ChangeProjectStatus)
})
})
}, context.UserAssignmentAPI(), reqUnitAccess(unit.TypeProjects, perm.AccessModeWrite, true), func(ctx *context.APIContext) {
if ctx.ContextUser.IsIndividual() && ctx.ContextUser.ID != ctx.Doer.ID {
ctx.NotFound("NewProject", nil)
return
}
}) })
}, context.UserAssignmentAPI())
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser), reqToken()) }, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser), reqToken())
// Users (requires user scope) // Users (requires user scope)

View File

@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/services/context" "code.gitea.io/gitea/services/context"
) )
// CreateProject creates a new project
func CreateProject(ctx *context.APIContext) { func CreateProject(ctx *context.APIContext) {
form := web.GetForm(ctx).(*api.CreateProjectOption) form := web.GetForm(ctx).(*api.CreateProjectOption)
@ -37,3 +38,24 @@ func CreateProject(ctx *context.APIContext) {
ctx.JSON(http.StatusCreated, map[string]int64{"id": project.ID}) ctx.JSON(http.StatusCreated, map[string]int64{"id": project.ID})
} }
// ChangeProjectStatus updates the status of a project between "open" and "close"
func ChangeProjectStatus(ctx *context.APIContext) {
var toClose bool
switch ctx.PathParam(":action") {
case "open":
toClose = false
case "close":
toClose = true
default:
ctx.NotFound("ChangeProjectStatus", nil)
return
}
id := ctx.PathParamInt64(":id")
if err := project_model.ChangeProjectStatusByRepoIDAndID(ctx, 0, id, toClose); err != nil {
ctx.NotFoundOrServerError("ChangeProjectStatusByRepoIDAndID", project_model.IsErrProjectNotExist, err)
return
}
ctx.JSON(http.StatusOK, map[string]any{"message": "project status updated successfully"})
}