2020-01-29 00:55:23 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-04-01 14:29:57 -04:00
|
|
|
"errors"
|
2020-01-29 00:55:23 -05:00
|
|
|
"net/http"
|
|
|
|
|
2022-04-01 14:29:57 -04:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-09 14:57:58 -05:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2020-01-29 00:55:23 -05:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-07-08 15:45:12 -04:00
|
|
|
mirror_module "code.gitea.io/gitea/modules/mirror"
|
2021-09-07 11:49:36 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-01-29 00:55:23 -05: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"
|
|
|
|
|
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
|
2021-11-09 14:57:58 -05:00
|
|
|
if !ctx.Repo.CanWrite(unit.TypeCode) {
|
2020-01-29 00:55:23 -05:00
|
|
|
ctx.Error(http.StatusForbidden, "MirrorSync", "Must have write access")
|
|
|
|
}
|
|
|
|
|
2021-09-07 11:49:36 -04:00
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
ctx.Error(http.StatusBadRequest, "MirrorSync", "Mirror feature is disabled")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
if _, err := repo_model.GetMirrorByRepoID(ctx, repo.ID); err != nil {
|
2022-04-01 14:29:57 -04: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
|
|
|
|
}
|
|
|
|
|
2022-07-08 15:45:12 -04:00
|
|
|
mirror_module.AddPullMirrorToQueue(repo.ID)
|
2020-01-29 00:55:23 -05:00
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|