aboutsummaryrefslogtreecommitdiffstats
path: root/routers/private/restore_repo.go
blob: c002de874a96e013f3d0ca90294fc7a1645331f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2021 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 private

import (
	"io/ioutil"

	myCtx "code.gitea.io/gitea/modules/context"
	"code.gitea.io/gitea/modules/migrations"
	jsoniter "github.com/json-iterator/go"
)

// RestoreRepo restore a repository from data
func RestoreRepo(ctx *myCtx.PrivateContext) {
	json := jsoniter.ConfigCompatibleWithStandardLibrary
	bs, err := ioutil.ReadAll(ctx.Req.Body)
	if err != nil {
		ctx.JSON(500, map[string]string{
			"err": err.Error(),
		})
		return
	}
	var params = struct {
		RepoDir   string
		OwnerName string
		RepoName  string
		Units     []string
	}{}
	if err = json.Unmarshal(bs, &params); err != nil {
		ctx.JSON(500, map[string]string{
			"err": err.Error(),
		})
		return
	}

	if err := migrations.RestoreRepository(
		ctx.Req.Context(),
		params.RepoDir,
		params.OwnerName,
		params.RepoName,
		params.Units,
	); err != nil {
		ctx.JSON(500, map[string]string{
			"err": err.Error(),
		})
	} else {
		ctx.Status(200)
	}
}