2021-02-01 16:57:12 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-02-01 16:57:12 -05:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2022-03-29 01:29:02 -05:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2024-02-27 02:12:22 -05:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2022-12-28 21:57:15 -05:00
|
|
|
"code.gitea.io/gitea/services/convert"
|
2022-08-24 21:31:57 -05:00
|
|
|
org_service "code.gitea.io/gitea/services/org"
|
2023-09-07 23:51:15 -05:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2021-02-01 16:57:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListTeams list a repository's teams
|
|
|
|
func ListTeams(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/teams repository repoListTeams
|
|
|
|
// ---
|
|
|
|
// summary: List a repository's teams
|
|
|
|
// 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
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/TeamList"
|
2023-09-12 21:37:54 -05:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2021-02-01 16:57:12 -05:00
|
|
|
|
|
|
|
if !ctx.Repo.Owner.IsOrganization() {
|
|
|
|
ctx.Error(http.StatusMethodNotAllowed, "noOrg", "repo is not owned by an organization")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 09:08:52 -05:00
|
|
|
teams, err := organization.GetRepoTeams(ctx, ctx.Repo.Repository)
|
2021-02-01 16:57:12 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 08:37:34 -05:00
|
|
|
apiTeams, err := convert.ToTeams(ctx, teams, false)
|
2022-05-13 12:27:58 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
2021-02-01 16:57:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, apiTeams)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTeam check if a team is assigned to a repository
|
|
|
|
func IsTeam(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/teams/{team} repository repoCheckTeam
|
|
|
|
// ---
|
|
|
|
// summary: Check if a team is assigned to a repository
|
|
|
|
// 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: team
|
|
|
|
// in: path
|
|
|
|
// description: team name
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/Team"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
// "405":
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
|
|
|
if !ctx.Repo.Owner.IsOrganization() {
|
|
|
|
ctx.Error(http.StatusMethodNotAllowed, "noOrg", "repo is not owned by an organization")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
team := getTeamByParam(ctx)
|
|
|
|
if team == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-15 01:13:19 -05:00
|
|
|
if repo_service.HasRepository(ctx, team, ctx.Repo.Repository.ID) {
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 08:37:34 -05:00
|
|
|
apiTeam, err := convert.ToTeam(ctx, team)
|
2022-05-13 12:27:58 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
2021-02-01 16:57:12 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, apiTeam)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.NotFound()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTeam add a team to a repository
|
|
|
|
func AddTeam(ctx *context.APIContext) {
|
|
|
|
// swagger:operation PUT /repos/{owner}/{repo}/teams/{team} repository repoAddTeam
|
|
|
|
// ---
|
|
|
|
// summary: Add a team to a repository
|
|
|
|
// 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: team
|
|
|
|
// in: path
|
|
|
|
// description: team name
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/validationError"
|
|
|
|
// "405":
|
|
|
|
// "$ref": "#/responses/error"
|
2023-09-12 21:37:54 -05:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2021-02-01 16:57:12 -05:00
|
|
|
|
|
|
|
changeRepoTeam(ctx, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTeam delete a team from a repository
|
|
|
|
func DeleteTeam(ctx *context.APIContext) {
|
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/teams/{team} repository repoDeleteTeam
|
|
|
|
// ---
|
|
|
|
// summary: Delete a team from a repository
|
|
|
|
// 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: team
|
|
|
|
// in: path
|
|
|
|
// description: team name
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/validationError"
|
|
|
|
// "405":
|
|
|
|
// "$ref": "#/responses/error"
|
2023-09-12 21:37:54 -05:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2021-02-01 16:57:12 -05:00
|
|
|
|
|
|
|
changeRepoTeam(ctx, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func changeRepoTeam(ctx *context.APIContext, add bool) {
|
|
|
|
if !ctx.Repo.Owner.IsOrganization() {
|
|
|
|
ctx.Error(http.StatusMethodNotAllowed, "noOrg", "repo is not owned by an organization")
|
|
|
|
}
|
|
|
|
if !ctx.Repo.Owner.RepoAdminChangeTeamAccess && !ctx.Repo.IsOwner() {
|
|
|
|
ctx.Error(http.StatusForbidden, "noAdmin", "user is nor repo admin nor owner")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
team := getTeamByParam(ctx)
|
|
|
|
if team == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-15 01:13:19 -05:00
|
|
|
repoHasTeam := repo_service.HasRepository(ctx, team, ctx.Repo.Repository.ID)
|
2021-02-01 16:57:12 -05:00
|
|
|
var err error
|
|
|
|
if add {
|
|
|
|
if repoHasTeam {
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "alreadyAdded", fmt.Errorf("team '%s' is already added to repo", team.Name))
|
|
|
|
return
|
|
|
|
}
|
2023-09-15 01:13:19 -05:00
|
|
|
err = org_service.TeamAddRepository(ctx, team, ctx.Repo.Repository)
|
2021-02-01 16:57:12 -05:00
|
|
|
} else {
|
|
|
|
if !repoHasTeam {
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "notAdded", fmt.Errorf("team '%s' was not added to repo", team.Name))
|
|
|
|
return
|
|
|
|
}
|
2023-09-07 23:51:15 -05:00
|
|
|
err = repo_service.RemoveRepositoryFromTeam(ctx, team, ctx.Repo.Repository.ID)
|
2021-02-01 16:57:12 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2022-03-29 01:29:02 -05:00
|
|
|
func getTeamByParam(ctx *context.APIContext) *organization.Team {
|
2022-05-20 09:08:52 -05:00
|
|
|
team, err := organization.GetTeam(ctx, ctx.Repo.Owner.ID, ctx.Params(":team"))
|
2021-02-01 16:57:12 -05:00
|
|
|
if err != nil {
|
2022-03-29 01:29:02 -05:00
|
|
|
if organization.IsErrTeamNotExist(err) {
|
2021-02-01 16:57:12 -05:00
|
|
|
ctx.Error(http.StatusNotFound, "TeamNotExit", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return team
|
|
|
|
}
|