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]>
181 lines
4.9 KiB
Go
181 lines
4.9 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/hanzoai/git/models/organization"
|
|
"github.com/hanzoai/git/models/perm"
|
|
access_model "github.com/hanzoai/git/models/perm/access"
|
|
repo_model "github.com/hanzoai/git/models/repo"
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
"github.com/hanzoai/git/modules/optional"
|
|
api "github.com/hanzoai/git/modules/structs"
|
|
"github.com/hanzoai/git/modules/util"
|
|
"github.com/hanzoai/git/modules/web"
|
|
"github.com/hanzoai/git/routers/api/v1/utils"
|
|
"github.com/hanzoai/git/services/context"
|
|
"github.com/hanzoai/git/services/convert"
|
|
repo_service "github.com/hanzoai/git/services/repository"
|
|
)
|
|
|
|
// ListForks list a repository's forks
|
|
func ListForks(ctx *context.APIContext) {
|
|
// swagger:operation GET /repos/{owner}/{repo}/forks repository listForks
|
|
// ---
|
|
// summary: List a repository's forks
|
|
// 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: 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
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/RepositoryList"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
listOptions := utils.GetListOptions(ctx)
|
|
forks, total, err := repo_service.FindForks(ctx, ctx.Repo.Repository, ctx.Doer, listOptions)
|
|
if err != nil {
|
|
ctx.APIErrorInternal(err)
|
|
return
|
|
}
|
|
if err := repo_model.RepositoryList(forks).LoadOwners(ctx); err != nil {
|
|
ctx.APIErrorInternal(err)
|
|
return
|
|
}
|
|
if err := repo_model.RepositoryList(forks).LoadUnits(ctx); err != nil {
|
|
ctx.APIErrorInternal(err)
|
|
return
|
|
}
|
|
|
|
apiForks := make([]*api.Repository, len(forks))
|
|
for i, fork := range forks {
|
|
permission, err := access_model.GetDoerRepoPermission(ctx, fork, ctx.Doer)
|
|
if err != nil {
|
|
ctx.APIErrorInternal(err)
|
|
return
|
|
}
|
|
apiForks[i] = convert.ToRepo(ctx, fork, permission)
|
|
}
|
|
|
|
ctx.SetLinkHeader(total, listOptions.PageSize)
|
|
ctx.SetTotalCountHeader(total)
|
|
ctx.JSON(http.StatusOK, apiForks)
|
|
}
|
|
|
|
func prepareDoerCreateRepoInOrg(ctx *context.APIContext, orgName string) *organization.Organization {
|
|
org, err := organization.GetOrgByName(ctx, orgName)
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
ctx.APIErrorNotFound()
|
|
return nil
|
|
} else if err != nil {
|
|
ctx.APIErrorInternal(err)
|
|
return nil
|
|
}
|
|
|
|
if !organization.HasOrgOrUserVisible(ctx, org.AsUser(), ctx.Doer) {
|
|
ctx.APIErrorNotFound()
|
|
return nil
|
|
}
|
|
|
|
if !ctx.Doer.IsAdmin {
|
|
canCreate, err := org.CanCreateOrgRepo(ctx, ctx.Doer.ID)
|
|
if err != nil {
|
|
ctx.APIErrorInternal(err)
|
|
return nil
|
|
}
|
|
if !canCreate {
|
|
ctx.APIError(http.StatusForbidden, "User is not allowed to create repositories in this organization.")
|
|
return nil
|
|
}
|
|
}
|
|
return org
|
|
}
|
|
|
|
// CreateFork create a fork of a repo
|
|
func CreateFork(ctx *context.APIContext) {
|
|
// swagger:operation POST /repos/{owner}/{repo}/forks repository createFork
|
|
// ---
|
|
// summary: Fork a repository
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: owner
|
|
// in: path
|
|
// description: owner of the repo to fork
|
|
// type: string
|
|
// required: true
|
|
// - name: repo
|
|
// in: path
|
|
// description: name of the repo to fork
|
|
// type: string
|
|
// required: true
|
|
// - name: body
|
|
// in: body
|
|
// schema:
|
|
// "$ref": "#/definitions/CreateForkOption"
|
|
// responses:
|
|
// "202":
|
|
// "$ref": "#/responses/Repository"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
// "409":
|
|
// description: The repository with the same name already exists.
|
|
// "422":
|
|
// "$ref": "#/responses/validationError"
|
|
|
|
form := web.GetForm(ctx).(*api.CreateForkOption)
|
|
forkOwner := ctx.Doer // user/org that will own the fork
|
|
if form.Organization != nil {
|
|
org := prepareDoerCreateRepoInOrg(ctx, *form.Organization)
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
forkOwner = org.AsUser()
|
|
}
|
|
|
|
repo := ctx.Repo.Repository
|
|
name := optional.FromPtr(form.Name).ValueOrDefault(repo.Name)
|
|
fork, err := repo_service.ForkRepository(ctx, ctx.Doer, forkOwner, repo_service.ForkRepoOptions{
|
|
BaseRepo: repo,
|
|
Name: name,
|
|
Description: repo.Description,
|
|
})
|
|
if err != nil {
|
|
if errors.Is(err, util.ErrAlreadyExist) || repo_model.IsErrReachLimitOfRepo(err) {
|
|
ctx.APIError(http.StatusConflict, err.Error())
|
|
} else if errors.Is(err, user_model.ErrBlockedUser) {
|
|
ctx.APIError(http.StatusForbidden, err.Error())
|
|
} else {
|
|
ctx.APIErrorInternal(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// TODO change back to 201
|
|
ctx.JSON(http.StatusAccepted, convert.ToRepo(ctx, fork, access_model.Permission{AccessMode: perm.AccessModeOwner}))
|
|
}
|