2020-01-31 16:49:04 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-31 16:49:04 +01:00
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
|
|
import (
|
2024-03-04 09:16:03 +01:00
|
|
|
"errors"
|
2020-01-31 16:49:04 +01:00
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2022-03-29 14:29:02 +08:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 19:58:28 +08:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2023-06-22 21:08:08 +08:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-31 16:49:04 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2025-01-29 21:40:44 -08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 23:36:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2024-02-27 15:12:22 +08:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2022-12-29 03:57:15 +01:00
|
|
|
"code.gitea.io/gitea/services/convert"
|
2020-01-31 16:49:04 +01:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Transfer transfers the ownership of a repository
|
2021-01-26 23:36:53 +08:00
|
|
|
func Transfer(ctx *context.APIContext) {
|
2020-01-31 16:49:04 +01:00
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/transfer repository repoTransfer
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Transfer a repo ownership
|
|
|
|
|
// produces:
|
|
|
|
|
// - application/json
|
|
|
|
|
// parameters:
|
|
|
|
|
// - name: owner
|
|
|
|
|
// in: path
|
|
|
|
|
// description: owner of the repo to transfer
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: repo
|
|
|
|
|
// in: path
|
|
|
|
|
// description: name of the repo to transfer
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: body
|
|
|
|
|
// in: body
|
|
|
|
|
// description: "Transfer Options"
|
|
|
|
|
// required: true
|
|
|
|
|
// schema:
|
|
|
|
|
// "$ref": "#/definitions/TransferRepoOption"
|
|
|
|
|
// responses:
|
|
|
|
|
// "202":
|
|
|
|
|
// "$ref": "#/responses/Repository"
|
|
|
|
|
// "403":
|
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
// "422":
|
|
|
|
|
// "$ref": "#/responses/validationError"
|
|
|
|
|
|
2021-01-26 23:36:53 +08:00
|
|
|
opts := web.GetForm(ctx).(*api.TransferRepoOption)
|
|
|
|
|
|
2022-05-20 22:08:52 +08:00
|
|
|
newOwner, err := user_model.GetUserByName(ctx, opts.NewOwner)
|
2020-01-31 16:49:04 +01:00
|
|
|
if err != nil {
|
2021-11-24 17:49:20 +08:00
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIError(http.StatusNotFound, "The new owner does not exist or cannot be found")
|
2020-01-31 16:49:04 +01:00
|
|
|
return
|
|
|
|
|
}
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIErrorInternal(err)
|
2020-01-31 16:49:04 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
if newOwner.Type == user_model.UserTypeOrganization {
|
2023-10-03 12:30:41 +02:00
|
|
|
if !ctx.Doer.IsAdmin && newOwner.Visibility == api.VisibleTypePrivate && !organization.OrgFromUser(newOwner).HasMemberWithUserID(ctx, ctx.Doer.ID) {
|
2020-08-16 21:27:08 +01:00
|
|
|
// The user shouldn't know about this organization
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIError(http.StatusNotFound, "The new owner does not exist or cannot be found")
|
2020-08-16 21:27:08 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 14:29:02 +08:00
|
|
|
var teams []*organization.Team
|
2020-01-31 16:49:04 +01:00
|
|
|
if opts.TeamIDs != nil {
|
|
|
|
|
if !newOwner.IsOrganization() {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIError(http.StatusUnprocessableEntity, "Teams can only be added to organization-owned repositories")
|
2020-01-31 16:49:04 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 21:37:34 +08:00
|
|
|
org := convert.ToOrganization(ctx, organization.OrgFromUser(newOwner))
|
2020-01-31 16:49:04 +01:00
|
|
|
for _, tID := range *opts.TeamIDs {
|
2022-05-20 22:08:52 +08:00
|
|
|
team, err := organization.GetTeamByID(ctx, tID)
|
2020-01-31 16:49:04 +01:00
|
|
|
if err != nil {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("team %d not found", tID))
|
2020-01-31 16:49:04 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if team.OrgID != org.ID {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIError(http.StatusForbidden, fmt.Errorf("team %d belongs not to org %d", tID, org.ID))
|
2020-01-31 16:49:04 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
teams = append(teams, team)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 02:01:58 +00:00
|
|
|
if ctx.Repo.GitRepo != nil {
|
2024-12-24 11:43:57 +08:00
|
|
|
_ = ctx.Repo.GitRepo.Close()
|
2021-12-21 02:01:58 +00:00
|
|
|
ctx.Repo.GitRepo = nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 16:05:53 +00:00
|
|
|
oldFullname := ctx.Repo.Repository.FullName()
|
|
|
|
|
|
2022-12-10 10:46:31 +08:00
|
|
|
if err := repo_service.StartRepositoryTransfer(ctx, ctx.Doer, newOwner, ctx.Repo.Repository, teams); err != nil {
|
2025-03-31 22:19:32 +02:00
|
|
|
switch {
|
|
|
|
|
case repo_model.IsErrRepoTransferInProgress(err):
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIError(http.StatusConflict, err)
|
2025-03-31 22:19:32 +02:00
|
|
|
case repo_model.IsErrRepoAlreadyExist(err):
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIError(http.StatusUnprocessableEntity, err)
|
2025-03-31 22:19:32 +02:00
|
|
|
case repo_service.IsRepositoryLimitReached(err):
|
|
|
|
|
ctx.APIError(http.StatusForbidden, err)
|
|
|
|
|
case errors.Is(err, user_model.ErrBlockedUser):
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIError(http.StatusForbidden, err)
|
2025-03-31 22:19:32 +02:00
|
|
|
default:
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIErrorInternal(err)
|
2024-03-04 09:16:03 +01:00
|
|
|
}
|
2025-04-01 14:36:46 -07:00
|
|
|
return
|
2020-01-31 16:49:04 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
if ctx.Repo.Repository.Status == repo_model.RepositoryPendingTransfer {
|
2022-04-21 16:05:53 +00:00
|
|
|
log.Trace("Repository transfer initiated: %s -> %s", oldFullname, ctx.Repo.Repository.FullName())
|
2023-06-22 21:08:08 +08:00
|
|
|
ctx.JSON(http.StatusCreated, convert.ToRepo(ctx, ctx.Repo.Repository, access_model.Permission{AccessMode: perm.AccessModeAdmin}))
|
2020-01-31 16:49:04 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 16:05:53 +00:00
|
|
|
log.Trace("Repository transferred: %s -> %s", oldFullname, ctx.Repo.Repository.FullName())
|
2023-06-22 21:08:08 +08:00
|
|
|
ctx.JSON(http.StatusAccepted, convert.ToRepo(ctx, ctx.Repo.Repository, access_model.Permission{AccessMode: perm.AccessModeAdmin}))
|
2020-01-31 16:49:04 +01:00
|
|
|
}
|
2021-12-24 05:26:52 +01:00
|
|
|
|
|
|
|
|
// AcceptTransfer accept a repo transfer
|
|
|
|
|
func AcceptTransfer(ctx *context.APIContext) {
|
|
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/transfer/accept repository acceptRepoTransfer
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Accept a repo transfer
|
|
|
|
|
// produces:
|
|
|
|
|
// - application/json
|
|
|
|
|
// parameters:
|
|
|
|
|
// - name: owner
|
|
|
|
|
// in: path
|
|
|
|
|
// description: owner of the repo to transfer
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: repo
|
|
|
|
|
// in: path
|
|
|
|
|
// description: name of the repo to transfer
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// responses:
|
|
|
|
|
// "202":
|
|
|
|
|
// "$ref": "#/responses/Repository"
|
|
|
|
|
// "403":
|
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
2025-01-29 21:40:44 -08:00
|
|
|
err := repo_service.AcceptTransferOwnership(ctx, ctx.Repo.Repository, ctx.Doer)
|
2021-12-24 05:26:52 +01:00
|
|
|
if err != nil {
|
2025-01-29 21:40:44 -08:00
|
|
|
switch {
|
|
|
|
|
case repo_model.IsErrNoPendingTransfer(err):
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIError(http.StatusNotFound, err)
|
2025-01-29 21:40:44 -08:00
|
|
|
case errors.Is(err, util.ErrPermissionDenied):
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIError(http.StatusForbidden, err)
|
2025-03-31 22:19:32 +02:00
|
|
|
case repo_service.IsRepositoryLimitReached(err):
|
|
|
|
|
ctx.APIError(http.StatusForbidden, err)
|
2025-01-29 21:40:44 -08:00
|
|
|
default:
|
2025-02-18 04:41:03 +08:00
|
|
|
ctx.APIErrorInternal(err)
|
2025-01-29 21:40:44 -08:00
|
|
|
}
|
2021-12-24 05:26:52 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
ctx.JSON(http.StatusAccepted, convert.ToRepo(ctx, ctx.Repo.Repository, ctx.Repo.Permission))
|
2021-12-24 05:26:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RejectTransfer reject a repo transfer
|
|
|
|
|
func RejectTransfer(ctx *context.APIContext) {
|
|
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/transfer/reject repository rejectRepoTransfer
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Reject a repo transfer
|
|
|
|
|
// produces:
|
|
|
|
|
// - application/json
|
|
|
|
|
// parameters:
|
|
|
|
|
// - name: owner
|
|
|
|
|
// in: path
|
|
|
|
|
// description: owner of the repo to transfer
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: repo
|
|
|
|
|
// in: path
|
|
|
|
|
// description: name of the repo to transfer
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// responses:
|
|
|
|
|
// "200":
|
|
|
|
|
// "$ref": "#/responses/Repository"
|
|
|
|
|
// "403":
|
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
2025-01-29 21:40:44 -08:00
|
|
|
err := repo_service.RejectRepositoryTransfer(ctx, ctx.Repo.Repository, ctx.Doer)
|
2021-12-24 05:26:52 +01:00
|
|
|
if err != nil {
|
2025-01-29 21:40:44 -08:00
|
|
|
switch {
|
|
|
|
|
case repo_model.IsErrNoPendingTransfer(err):
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIError(http.StatusNotFound, err)
|
2025-01-29 21:40:44 -08:00
|
|
|
case errors.Is(err, util.ErrPermissionDenied):
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.APIError(http.StatusForbidden, err)
|
2025-01-29 21:40:44 -08:00
|
|
|
default:
|
2025-02-18 04:41:03 +08:00
|
|
|
ctx.APIErrorInternal(err)
|
2025-01-29 21:40:44 -08:00
|
|
|
}
|
2021-12-24 05:26:52 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
ctx.JSON(http.StatusOK, convert.ToRepo(ctx, ctx.Repo.Repository, ctx.Repo.Permission))
|
2021-12-24 05:26:52 +01:00
|
|
|
}
|