123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- // Copyright 2020 The Gitea Authors. All rights reserved.
- // SPDX-License-Identifier: MIT
-
- package repo
-
- import (
- "errors"
- "fmt"
- "net/http"
- "time"
-
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/models/db"
- repo_model "code.gitea.io/gitea/models/repo"
- "code.gitea.io/gitea/models/unit"
- "code.gitea.io/gitea/modules/context"
- "code.gitea.io/gitea/modules/setting"
- 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"
- "code.gitea.io/gitea/services/convert"
- "code.gitea.io/gitea/services/forms"
- "code.gitea.io/gitea/services/migrations"
- mirror_service "code.gitea.io/gitea/services/mirror"
- )
-
- // 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"
- // "404":
- // "$ref": "#/responses/notFound"
-
- repo := ctx.Repo.Repository
-
- if !ctx.Repo.CanWrite(unit.TypeCode) {
- ctx.Error(http.StatusForbidden, "MirrorSync", "Must have write access")
- }
-
- if !setting.Mirror.Enabled {
- ctx.Error(http.StatusBadRequest, "MirrorSync", "Mirror feature is disabled")
- return
- }
-
- if _, err := repo_model.GetMirrorByRepoID(ctx, repo.ID); err != nil {
- 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
- }
-
- mirror_service.AddPullMirrorToQueue(repo.ID)
-
- ctx.Status(http.StatusOK)
- }
-
- // 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"
- // "404":
- // "$ref": "#/responses/notFound"
-
- 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"
- // "404":
- // "$ref": "#/responses/notFound"
-
- 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 {
- m, err := convert.ToPushMirror(mirror)
- 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"
- // "404":
- // "$ref": "#/responses/notFound"
-
- 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
- pushMirror, err := repo_model.GetPushMirror(ctx, repo_model.PushMirrorOptions{RepoID: ctx.Repo.Repository.ID, RemoteName: mirrorName})
- if err != nil {
- ctx.Error(http.StatusNotFound, "GetPushMirrors", err)
- return
- }
- m, err := convert.ToPushMirror(pushMirror)
- 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:
- // "200":
- // "$ref": "#/responses/PushMirror"
- // "403":
- // "$ref": "#/responses/forbidden"
- // "400":
- // "$ref": "#/responses/error"
- // "404":
- // "$ref": "#/responses/notFound"
-
- 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
- }
-
- 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
- }
-
- remoteAddress, err := util.SanitizeURL(mirrorOption.RemoteAddress)
- if err != nil {
- ctx.ServerError("SanitizeURL", err)
- return
- }
-
- pushMirror := &repo_model.PushMirror{
- RepoID: repo.ID,
- Repo: repo,
- RemoteName: fmt.Sprintf("remote_mirror_%s", remoteSuffix),
- Interval: interval,
- SyncOnCommit: mirrorOption.SyncOnCommit,
- RemoteAddress: remoteAddress,
- }
-
- if err = repo_model.InsertPushMirror(ctx, pushMirror); err != nil {
- ctx.ServerError("InsertPushMirror", err)
- return
- }
-
- // 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)
- }
- ctx.ServerError("AddPushMirrorRemote", err)
- return
- }
- m, err := convert.ToPushMirror(pushMirror)
- 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
- }
- }
|