2020-01-29 08:55:23 +03:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-29 08:55:23 +03:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-04-01 21:29:57 +03:00
|
|
|
"errors"
|
2022-07-30 19:45:59 +03:00
|
|
|
"fmt"
|
2020-01-29 08:55:23 +03:00
|
|
|
"net/http"
|
2022-07-30 19:45:59 +03:00
|
|
|
"time"
|
2020-01-29 08:55:23 +03:00
|
|
|
|
2022-07-30 19:45:59 +03:00
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-04-01 21:29:57 +03:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-09 22:57:58 +03:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2024-08-26 09:54:16 +03:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2021-09-07 18:49:36 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-07-30 19:45:59 +03:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
"code.gitea.io/gitea/modules/web"
|
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2024-02-27 10:12:22 +03:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2022-12-29 05:57:15 +03:00
|
|
|
"code.gitea.io/gitea/services/convert"
|
2022-07-30 19:45:59 +03:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
|
|
|
"code.gitea.io/gitea/services/migrations"
|
|
|
|
mirror_service "code.gitea.io/gitea/services/mirror"
|
2020-01-29 08:55:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// MirrorSync adds a mirrored repository to the sync queue
|
|
|
|
func MirrorSync(ctx *context.APIContext) {
|
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/mirror-sync repository repoMirrorSync
|
|
|
|
// ---
|
|
|
|
// summary: Sync a mirrored repository
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo to sync
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo to sync
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 05:37:54 +03:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
feat(quota): Quota enforcement
The previous commit laid out the foundation of the quota engine, this
one builds on top of it, and implements the actual enforcement.
Enforcement happens at the route decoration level, whenever possible. In
case of the API, when over quota, a 413 error is returned, with an
appropriate JSON payload. In case of web routes, a 413 HTML page is
rendered with similar information.
This implementation is for a **soft quota**: quota usage is checked
before an operation is to be performed, and the operation is *only*
denied if the user is already over quota. This makes it possible to go
over quota, but has the significant advantage of being practically
implementable within the current Forgejo architecture.
The goal of enforcement is to deny actions that can make the user go
over quota, and allow the rest. As such, deleting things should - in
almost all cases - be possible. A prime exemption is deleting files via
the web ui: that creates a new commit, which in turn increases repo
size, thus, is denied if the user is over quota.
Limitations
-----------
Because we generally work at a route decorator level, and rarely
look *into* the operation itself, `size:repos:public` and
`size:repos:private` are not enforced at this level, the engine enforces
against `size:repos:all`. This will be improved in the future.
AGit does not play very well with this system, because AGit PRs count
toward the repo they're opened against, while in the GitHub-style fork +
pull model, it counts against the fork. This too, can be improved in the
future.
There's very little done on the UI side to guard against going over
quota. What this patch implements, is enforcement, not prevention. The
UI will still let you *try* operations that *will* result in a denial.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-07-06 11:30:16 +03:00
|
|
|
// "413":
|
|
|
|
// "$ref": "#/responses/quotaExceeded"
|
2020-01-29 08:55:23 +03:00
|
|
|
|
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
|
2021-11-09 22:57:58 +03:00
|
|
|
if !ctx.Repo.CanWrite(unit.TypeCode) {
|
2020-01-29 08:55:23 +03:00
|
|
|
ctx.Error(http.StatusForbidden, "MirrorSync", "Must have write access")
|
|
|
|
}
|
|
|
|
|
2021-09-07 18:49:36 +03:00
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
ctx.Error(http.StatusBadRequest, "MirrorSync", "Mirror feature is disabled")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
if _, err := repo_model.GetMirrorByRepoID(ctx, repo.ID); err != nil {
|
2022-04-01 21:29:57 +03:00
|
|
|
if errors.Is(err, repo_model.ErrMirrorNotExist) {
|
|
|
|
ctx.Error(http.StatusBadRequest, "MirrorSync", "Repository is not a mirror")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "MirrorSync", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-27 05:24:45 +03:00
|
|
|
mirror_service.AddPullMirrorToQueue(repo.ID)
|
2020-01-29 08:55:23 +03:00
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
2022-07-30 19:45:59 +03:00
|
|
|
|
|
|
|
// PushMirrorSync adds all push mirrored repositories to the sync queue
|
|
|
|
func PushMirrorSync(ctx *context.APIContext) {
|
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/push_mirrors-sync repository repoPushMirrorSync
|
|
|
|
// ---
|
|
|
|
// summary: Sync all push mirrored repository
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo to sync
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo to sync
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 05:37:54 +03:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
feat(quota): Quota enforcement
The previous commit laid out the foundation of the quota engine, this
one builds on top of it, and implements the actual enforcement.
Enforcement happens at the route decoration level, whenever possible. In
case of the API, when over quota, a 413 error is returned, with an
appropriate JSON payload. In case of web routes, a 413 HTML page is
rendered with similar information.
This implementation is for a **soft quota**: quota usage is checked
before an operation is to be performed, and the operation is *only*
denied if the user is already over quota. This makes it possible to go
over quota, but has the significant advantage of being practically
implementable within the current Forgejo architecture.
The goal of enforcement is to deny actions that can make the user go
over quota, and allow the rest. As such, deleting things should - in
almost all cases - be possible. A prime exemption is deleting files via
the web ui: that creates a new commit, which in turn increases repo
size, thus, is denied if the user is over quota.
Limitations
-----------
Because we generally work at a route decorator level, and rarely
look *into* the operation itself, `size:repos:public` and
`size:repos:private` are not enforced at this level, the engine enforces
against `size:repos:all`. This will be improved in the future.
AGit does not play very well with this system, because AGit PRs count
toward the repo they're opened against, while in the GitHub-style fork +
pull model, it counts against the fork. This too, can be improved in the
future.
There's very little done on the UI side to guard against going over
quota. What this patch implements, is enforcement, not prevention. The
UI will still let you *try* operations that *will* result in a denial.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-07-06 11:30:16 +03:00
|
|
|
// "413":
|
|
|
|
// "$ref": "#/responses/quotaExceeded"
|
2022-07-30 19:45:59 +03:00
|
|
|
|
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
ctx.Error(http.StatusBadRequest, "PushMirrorSync", "Mirror feature is disabled")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Get All push mirrors of a specific repo
|
|
|
|
pushMirrors, _, err := repo_model.GetPushMirrorsByRepoID(ctx, ctx.Repo.Repository.ID, db.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusNotFound, "PushMirrorSync", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, mirror := range pushMirrors {
|
|
|
|
ok := mirror_service.SyncPushMirror(ctx, mirror.ID)
|
|
|
|
if !ok {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "PushMirrorSync", "error occurred when syncing push mirror "+mirror.RemoteName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListPushMirrors get list of push mirrors of a repository
|
|
|
|
func ListPushMirrors(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/push_mirrors repository repoListPushMirrors
|
|
|
|
// ---
|
|
|
|
// summary: Get all push mirrors of the 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: 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/PushMirrorList"
|
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 05:37:54 +03:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2022-07-30 19:45:59 +03:00
|
|
|
|
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
ctx.Error(http.StatusBadRequest, "GetPushMirrorsByRepoID", "Mirror feature is disabled")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
// Get all push mirrors for the specified repository.
|
|
|
|
pushMirrors, count, err := repo_model.GetPushMirrorsByRepoID(ctx, repo.ID, utils.GetListOptions(ctx))
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusNotFound, "GetPushMirrorsByRepoID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
responsePushMirrors := make([]*api.PushMirror, 0, len(pushMirrors))
|
|
|
|
for _, mirror := range pushMirrors {
|
2023-10-03 13:30:41 +03:00
|
|
|
m, err := convert.ToPushMirror(ctx, mirror)
|
2022-07-30 19:45:59 +03:00
|
|
|
if err == nil {
|
|
|
|
responsePushMirrors = append(responsePushMirrors, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.SetLinkHeader(len(responsePushMirrors), utils.GetListOptions(ctx).PageSize)
|
|
|
|
ctx.SetTotalCountHeader(count)
|
|
|
|
ctx.JSON(http.StatusOK, responsePushMirrors)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPushMirrorByName get push mirror of a repository by name
|
|
|
|
func GetPushMirrorByName(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/push_mirrors/{name} repository repoGetPushMirrorByRemoteName
|
|
|
|
// ---
|
|
|
|
// summary: Get push mirror of the repository by remoteName
|
|
|
|
// 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: name
|
|
|
|
// in: path
|
|
|
|
// description: remote name of push mirror
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/PushMirror"
|
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 05:37:54 +03:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2022-07-30 19:45:59 +03:00
|
|
|
|
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
ctx.Error(http.StatusBadRequest, "GetPushMirrorByRemoteName", "Mirror feature is disabled")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mirrorName := ctx.Params(":name")
|
|
|
|
// Get push mirror of a specific repo by remoteName
|
2023-12-25 23:25:29 +03:00
|
|
|
pushMirror, exist, err := db.Get[repo_model.PushMirror](ctx, repo_model.PushMirrorOptions{
|
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
|
|
|
RemoteName: mirrorName,
|
|
|
|
}.ToConds())
|
2022-07-30 19:45:59 +03:00
|
|
|
if err != nil {
|
2023-12-25 23:25:29 +03:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetPushMirrors", err)
|
|
|
|
return
|
|
|
|
} else if !exist {
|
|
|
|
ctx.Error(http.StatusNotFound, "GetPushMirrors", nil)
|
2022-07-30 19:45:59 +03:00
|
|
|
return
|
|
|
|
}
|
2023-12-25 23:25:29 +03:00
|
|
|
|
2023-10-03 13:30:41 +03:00
|
|
|
m, err := convert.ToPushMirror(ctx, pushMirror)
|
2022-07-30 19:45:59 +03:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetPushMirrorByRemoteName", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPushMirror adds a push mirror to a repository
|
|
|
|
func AddPushMirror(ctx *context.APIContext) {
|
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/push_mirrors repository repoAddPushMirror
|
|
|
|
// ---
|
|
|
|
// summary: add a push mirror to the repository
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// 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: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/CreatePushMirrorOption"
|
|
|
|
// responses:
|
2023-06-29 01:00:19 +03:00
|
|
|
// "200":
|
2022-07-30 19:45:59 +03:00
|
|
|
// "$ref": "#/responses/PushMirror"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
2023-09-13 05:37:54 +03:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
feat(quota): Quota enforcement
The previous commit laid out the foundation of the quota engine, this
one builds on top of it, and implements the actual enforcement.
Enforcement happens at the route decoration level, whenever possible. In
case of the API, when over quota, a 413 error is returned, with an
appropriate JSON payload. In case of web routes, a 413 HTML page is
rendered with similar information.
This implementation is for a **soft quota**: quota usage is checked
before an operation is to be performed, and the operation is *only*
denied if the user is already over quota. This makes it possible to go
over quota, but has the significant advantage of being practically
implementable within the current Forgejo architecture.
The goal of enforcement is to deny actions that can make the user go
over quota, and allow the rest. As such, deleting things should - in
almost all cases - be possible. A prime exemption is deleting files via
the web ui: that creates a new commit, which in turn increases repo
size, thus, is denied if the user is over quota.
Limitations
-----------
Because we generally work at a route decorator level, and rarely
look *into* the operation itself, `size:repos:public` and
`size:repos:private` are not enforced at this level, the engine enforces
against `size:repos:all`. This will be improved in the future.
AGit does not play very well with this system, because AGit PRs count
toward the repo they're opened against, while in the GitHub-style fork +
pull model, it counts against the fork. This too, can be improved in the
future.
There's very little done on the UI side to guard against going over
quota. What this patch implements, is enforcement, not prevention. The
UI will still let you *try* operations that *will* result in a denial.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-07-06 11:30:16 +03:00
|
|
|
// "413":
|
|
|
|
// "$ref": "#/responses/quotaExceeded"
|
2022-07-30 19:45:59 +03:00
|
|
|
|
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
ctx.Error(http.StatusBadRequest, "AddPushMirror", "Mirror feature is disabled")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pushMirror := web.GetForm(ctx).(*api.CreatePushMirrorOption)
|
|
|
|
CreatePushMirror(ctx, pushMirror)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePushMirrorByRemoteName deletes a push mirror from a repository by remoteName
|
|
|
|
func DeletePushMirrorByRemoteName(ctx *context.APIContext) {
|
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/push_mirrors/{name} repository repoDeletePushMirror
|
|
|
|
// ---
|
|
|
|
// summary: deletes a push mirror from a repository by remoteName
|
|
|
|
// 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: name
|
|
|
|
// in: path
|
|
|
|
// description: remote name of the pushMirror
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
ctx.Error(http.StatusBadRequest, "DeletePushMirrorByName", "Mirror feature is disabled")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteName := ctx.Params(":name")
|
|
|
|
// Delete push mirror on repo by name.
|
|
|
|
err := repo_model.DeletePushMirrors(ctx, repo_model.PushMirrorOptions{RepoID: ctx.Repo.Repository.ID, RemoteName: remoteName})
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusNotFound, "DeletePushMirrors", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreatePushMirror(ctx *context.APIContext, mirrorOption *api.CreatePushMirrorOption) {
|
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
|
|
|
|
interval, err := time.ParseDuration(mirrorOption.Interval)
|
|
|
|
if err != nil || (interval != 0 && interval < setting.Mirror.MinInterval) {
|
|
|
|
ctx.Error(http.StatusBadRequest, "CreatePushMirror", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-26 09:54:16 +03:00
|
|
|
if mirrorOption.UseSSH && !git.HasSSHExecutable {
|
|
|
|
ctx.Error(http.StatusBadRequest, "CreatePushMirror", "SSH authentication not available.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-04 21:46:05 +03:00
|
|
|
if mirrorOption.UseSSH && (mirrorOption.RemoteUsername != "" || mirrorOption.RemotePassword != "") {
|
|
|
|
ctx.Error(http.StatusBadRequest, "CreatePushMirror", "'use_ssh' is mutually exclusive with 'remote_username' and 'remote_passoword'")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-30 19:45:59 +03:00
|
|
|
address, err := forms.ParseRemoteAddr(mirrorOption.RemoteAddress, mirrorOption.RemoteUsername, mirrorOption.RemotePassword)
|
|
|
|
if err == nil {
|
|
|
|
err = migrations.IsMigrateURLAllowed(address, ctx.ContextUser)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
HandleRemoteAddressError(ctx, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteSuffix, err := util.CryptoRandomString(10)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("CryptoRandomString", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-04 21:46:05 +03:00
|
|
|
remoteAddress, err := util.SanitizeURL(address)
|
2023-09-16 19:03:02 +03:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SanitizeURL", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-30 19:45:59 +03:00
|
|
|
pushMirror := &repo_model.PushMirror{
|
2023-09-16 19:03:02 +03:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
RemoteName: fmt.Sprintf("remote_mirror_%s", remoteSuffix),
|
|
|
|
Interval: interval,
|
|
|
|
SyncOnCommit: mirrorOption.SyncOnCommit,
|
|
|
|
RemoteAddress: remoteAddress,
|
2022-07-30 19:45:59 +03:00
|
|
|
}
|
|
|
|
|
2024-08-04 21:46:05 +03:00
|
|
|
var plainPrivateKey []byte
|
|
|
|
if mirrorOption.UseSSH {
|
|
|
|
publicKey, privateKey, err := util.GenerateSSHKeypair()
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GenerateSSHKeypair", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
plainPrivateKey = privateKey
|
|
|
|
pushMirror.PublicKey = string(publicKey)
|
|
|
|
}
|
|
|
|
|
2023-12-25 23:25:29 +03:00
|
|
|
if err = db.Insert(ctx, pushMirror); err != nil {
|
2022-07-30 19:45:59 +03:00
|
|
|
ctx.ServerError("InsertPushMirror", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-04 21:46:05 +03:00
|
|
|
if mirrorOption.UseSSH {
|
|
|
|
if err = pushMirror.SetPrivatekey(ctx, plainPrivateKey); err != nil {
|
|
|
|
ctx.ServerError("SetPrivatekey", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-30 19:45:59 +03:00
|
|
|
// if the registration of the push mirrorOption fails remove it from the database
|
|
|
|
if err = mirror_service.AddPushMirrorRemote(ctx, pushMirror, address); err != nil {
|
|
|
|
if err := repo_model.DeletePushMirrors(ctx, repo_model.PushMirrorOptions{ID: pushMirror.ID, RepoID: pushMirror.RepoID}); err != nil {
|
|
|
|
ctx.ServerError("DeletePushMirrors", err)
|
2024-05-28 12:31:59 +03:00
|
|
|
return
|
2022-07-30 19:45:59 +03:00
|
|
|
}
|
|
|
|
ctx.ServerError("AddPushMirrorRemote", err)
|
|
|
|
return
|
|
|
|
}
|
2023-10-03 13:30:41 +03:00
|
|
|
m, err := convert.ToPushMirror(ctx, pushMirror)
|
2022-07-30 19:45:59 +03:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("ToPushMirror", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleRemoteAddressError(ctx *context.APIContext, err error) {
|
|
|
|
if models.IsErrInvalidCloneAddr(err) {
|
|
|
|
addrErr := err.(*models.ErrInvalidCloneAddr)
|
|
|
|
switch {
|
|
|
|
case addrErr.IsProtocolInvalid:
|
|
|
|
ctx.Error(http.StatusBadRequest, "CreatePushMirror", "Invalid mirror protocol")
|
|
|
|
case addrErr.IsURLError:
|
|
|
|
ctx.Error(http.StatusBadRequest, "CreatePushMirror", "Invalid Url ")
|
|
|
|
case addrErr.IsPermissionDenied:
|
|
|
|
ctx.Error(http.StatusUnauthorized, "CreatePushMirror", "Permission denied")
|
|
|
|
default:
|
|
|
|
ctx.Error(http.StatusBadRequest, "CreatePushMirror", "Unknown error")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|