mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-07-27 20:08:54 +00:00
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/13143 The details of the refactor are in [a separate branch](https://codeberg.org/limiting-factor/forgejo/commits/branch/wip-api-context-accessors-save) which details how it was done. This pull request contains a squash of those commits rebased on top of the current Forgejo branch, which involved conflict resolution. Given the magnitude of the change, there is a high chance that a large number of future pull requests merged in Forgejo will create conflicts. They will be dealt with in the same way as chore: refactor orgAssignment to two separate middlewares (#13155). ## Review The only risk I can think of is a getter/setter implementation being incorrectly implemented. This is probably where a detailed review is most needed, in the `services/context/api.go` file. To not increase the size of the pull request, the `GetX()` accessors were preserved although they are redundant with `X()`. They will be removed in a followup pull request. ## Backporting strategy. Cherry-picking the commit to v15 has a significant amount of conflicts in 8 files out of 111. ``` unmerged routers/api/v1/activitypub/repository.go unmerged routers/api/v1/admin/user.go unmerged routers/api/v1/api.go unmerged routers/api/v1/org/team.go unmerged routers/api/v1/repo/action.go unmerged routers/api/v1/repo/pull.go unmerged routers/api/v1/user/follower.go unmerged services/context/api.go ``` Instead of resolving them, I think it is easier and safer to repeat the [refactor steps](https://codeberg.org/limiting-factor/forgejo/commits/branch/wip-api-context-accessors-save) in v15. I also think it is worth the effort since it will avoid trivial backport conflicts on over 100 files. ## Context This is a followup pull request discussed originally at https://codeberg.org/forgejo/forgejo/pulls/12512 Co-authored-by: limiting-factor <limiting-factor@posteo.com> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/13213 Reviewed-by: limiting-factor <limiting-factor@noreply.codeberg.org> Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
180 lines
4.3 KiB
Go
180 lines
4.3 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package admin
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
repo_model "forgejo.org/models/repo"
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/util"
|
|
"forgejo.org/routers/api/v1/utils"
|
|
"forgejo.org/services/context"
|
|
repo_service "forgejo.org/services/repository"
|
|
)
|
|
|
|
// ListUnadoptedRepositories lists the unadopted repositories that match the provided names
|
|
func ListUnadoptedRepositories(ctx *context.APIContext) {
|
|
// swagger:operation GET /admin/unadopted admin adminUnadoptedList
|
|
// ---
|
|
// summary: List unadopted repositories
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: page
|
|
// in: query
|
|
// description: page number of results to return (1-based)
|
|
// type: integer
|
|
// - name: limit
|
|
// in: query
|
|
// description: page size of results
|
|
// type: integer
|
|
// - name: pattern
|
|
// in: query
|
|
// description: pattern of repositories to search for
|
|
// type: string
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/StringSlice"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
listOptions := utils.GetListOptions(ctx)
|
|
if listOptions.Page == 0 {
|
|
listOptions.Page = 1
|
|
}
|
|
repoNames, count, err := repo_service.ListUnadoptedRepositories(ctx, ctx.FormString("query"), &listOptions)
|
|
if err != nil {
|
|
ctx.InternalServerError(err)
|
|
return
|
|
}
|
|
|
|
ctx.SetTotalCountHeader(int64(count))
|
|
|
|
ctx.JSON(http.StatusOK, repoNames)
|
|
}
|
|
|
|
// AdoptRepository will adopt an unadopted repository
|
|
func AdoptRepository(ctx *context.APIContext) {
|
|
// swagger:operation POST /admin/unadopted/{owner}/{repo} admin adminAdoptRepository
|
|
// ---
|
|
// summary: Adopt unadopted files as 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
|
|
// responses:
|
|
// "204":
|
|
// "$ref": "#/responses/empty"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
ownerName := ctx.Params(":username")
|
|
repoName := ctx.Params(":reponame")
|
|
|
|
ctxUser, err := user_model.GetUserByName(ctx, ownerName)
|
|
if err != nil {
|
|
if user_model.IsErrUserNotExist(err) {
|
|
ctx.NotFound()
|
|
return
|
|
}
|
|
ctx.InternalServerError(err)
|
|
return
|
|
}
|
|
|
|
// check not a repo
|
|
has, err := repo_model.IsRepositoryModelExist(ctx, ctxUser, repoName)
|
|
if err != nil {
|
|
ctx.InternalServerError(err)
|
|
return
|
|
}
|
|
isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName))
|
|
if err != nil {
|
|
ctx.InternalServerError(err)
|
|
return
|
|
}
|
|
if has || !isDir {
|
|
ctx.NotFound()
|
|
return
|
|
}
|
|
if _, err := repo_service.AdoptRepository(ctx, ctx.Doer(), ctxUser, repo_service.CreateRepoOptions{
|
|
Name: repoName,
|
|
IsPrivate: true,
|
|
}); err != nil {
|
|
ctx.InternalServerError(err)
|
|
return
|
|
}
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// DeleteUnadoptedRepository will delete an unadopted repository
|
|
func DeleteUnadoptedRepository(ctx *context.APIContext) {
|
|
// swagger:operation DELETE /admin/unadopted/{owner}/{repo} admin adminDeleteUnadoptedRepository
|
|
// ---
|
|
// summary: Delete unadopted files
|
|
// 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:
|
|
// "204":
|
|
// "$ref": "#/responses/empty"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
ownerName := ctx.Params(":username")
|
|
repoName := ctx.Params(":reponame")
|
|
|
|
ctxUser, err := user_model.GetUserByName(ctx, ownerName)
|
|
if err != nil {
|
|
if user_model.IsErrUserNotExist(err) {
|
|
ctx.NotFound()
|
|
return
|
|
}
|
|
ctx.InternalServerError(err)
|
|
return
|
|
}
|
|
|
|
// check not a repo
|
|
has, err := repo_model.IsRepositoryModelExist(ctx, ctxUser, repoName)
|
|
if err != nil {
|
|
ctx.InternalServerError(err)
|
|
return
|
|
}
|
|
isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName))
|
|
if err != nil {
|
|
ctx.InternalServerError(err)
|
|
return
|
|
}
|
|
if has || !isDir {
|
|
ctx.NotFound()
|
|
return
|
|
}
|
|
|
|
if err := repo_service.DeleteUnadoptedRepository(ctx, ctx.Doer(), ctxUser, repoName); err != nil {
|
|
ctx.InternalServerError(err)
|
|
return
|
|
}
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|