mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
235 lines
5.5 KiB
Go
235 lines
5.5 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/hanzoai/git/models/organization"
|
|
"github.com/hanzoai/git/services/context"
|
|
"github.com/hanzoai/git/services/convert"
|
|
repo_service "github.com/hanzoai/git/services/repository"
|
|
)
|
|
|
|
// 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"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
if !ctx.Repo.Owner.IsOrganization() {
|
|
ctx.APIError(http.StatusMethodNotAllowed, "repo is not owned by an organization")
|
|
return
|
|
}
|
|
|
|
teams, err := organization.GetRepoTeams(ctx, ctx.Repo.Repository.OwnerID, ctx.Repo.Repository.ID)
|
|
if err != nil {
|
|
ctx.APIErrorInternal(err)
|
|
return
|
|
}
|
|
|
|
apiTeams, err := convert.ToTeams(ctx, teams, false)
|
|
if err != nil {
|
|
ctx.APIErrorInternal(err)
|
|
return
|
|
}
|
|
|
|
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.APIError(http.StatusMethodNotAllowed, "repo is not owned by an organization")
|
|
return
|
|
}
|
|
|
|
team := getTeamByParam(ctx)
|
|
if team == nil {
|
|
return
|
|
}
|
|
|
|
if repo_service.HasRepository(ctx, team, ctx.Repo.Repository.ID) {
|
|
apiTeam, err := convert.ToTeam(ctx, team)
|
|
if err != nil {
|
|
ctx.APIErrorInternal(err)
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusOK, apiTeam)
|
|
return
|
|
}
|
|
|
|
ctx.APIErrorNotFound()
|
|
}
|
|
|
|
// 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"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
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"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
changeRepoTeam(ctx, false)
|
|
}
|
|
|
|
func changeRepoTeam(ctx *context.APIContext, add bool) {
|
|
if !ctx.Repo.Owner.IsOrganization() {
|
|
ctx.APIError(http.StatusMethodNotAllowed, "repo is not owned by an organization")
|
|
}
|
|
if !ctx.Repo.Owner.RepoAdminChangeTeamAccess && !ctx.Repo.Permission.IsOwner() {
|
|
ctx.APIError(http.StatusForbidden, "user is nor repo admin nor owner")
|
|
return
|
|
}
|
|
|
|
team := getTeamByParam(ctx)
|
|
if team == nil {
|
|
return
|
|
}
|
|
|
|
repoHasTeam := repo_service.HasRepository(ctx, team, ctx.Repo.Repository.ID)
|
|
var err error
|
|
if add {
|
|
if repoHasTeam {
|
|
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("team '%s' is already added to repo", team.Name))
|
|
return
|
|
}
|
|
err = repo_service.TeamAddRepository(ctx, team, ctx.Repo.Repository)
|
|
} else {
|
|
if !repoHasTeam {
|
|
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("team '%s' was not added to repo", team.Name))
|
|
return
|
|
}
|
|
err = repo_service.RemoveRepositoryFromTeam(ctx, team, ctx.Repo.Repository.ID)
|
|
}
|
|
if err != nil {
|
|
ctx.APIErrorInternal(err)
|
|
return
|
|
}
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func getTeamByParam(ctx *context.APIContext) *organization.Team {
|
|
team, err := organization.GetTeam(ctx, ctx.Repo.Owner.ID, ctx.PathParam("team"))
|
|
if err != nil {
|
|
if organization.IsErrTeamNotExist(err) {
|
|
ctx.APIError(http.StatusNotFound, err.Error())
|
|
return nil
|
|
}
|
|
ctx.APIErrorInternal(err)
|
|
return nil
|
|
}
|
|
return team
|
|
}
|