aboutsummaryrefslogtreecommitdiffstats
path: root/routers/api/v1/admin/adopt.go
blob: fdef94e6c843c5513cc640f4035c9197b8b20d31 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 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 admin

import (
	"fmt"
	"net/http"

	"code.gitea.io/gitea/models"
	"code.gitea.io/gitea/modules/context"
	"code.gitea.io/gitea/modules/repository"
	"code.gitea.io/gitea/modules/util"
	"code.gitea.io/gitea/routers/api/v1/utils"
)

// ListUnadoptedRepositories lists the unadopted repositories that match the provided names
func ListUnadoptedRepositories(ctx *context.APIContext) {
	// swagger:operation GET /admin/unadopted admin adminUnadoptedList
	// ---
	// summary: List unadopted repositories
	// produces:
	// - application/json
	// parameters:
	// - 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
	// - name: pattern
	//   in: query
	//   description: pattern of repositories to search for
	//   type: string
	// responses:
	//   "200":
	//     "$ref": "#/responses/StringSlice"
	//   "403":
	//     "$ref": "#/responses/forbidden"

	listOptions := utils.GetListOptions(ctx)
	repoNames, count, err := repository.ListUnadoptedRepositories(ctx.Query("query"), &listOptions)
	if err != nil {
		ctx.InternalServerError(err)
	}

	ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
	ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count")

	ctx.JSON(http.StatusOK, repoNames)
}

// AdoptRepository will adopt an unadopted repository
func AdoptRepository(ctx *context.APIContext) {
	// swagger:operation POST /admin/unadopted/{owner}/{repo} admin adminAdoptRepository
	// ---
	// summary: Adopt unadopted files as a 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
	// responses:
	//   "204":
	//     "$ref": "#/responses/empty"
	//   "404":
	//     "$ref": "#/responses/notFound"
	//   "403":
	//     "$ref": "#/responses/forbidden"
	ownerName := ctx.Params(":username")
	repoName := ctx.Params(":reponame")

	ctxUser, err := models.GetUserByName(ownerName)
	if err != nil {
		if models.IsErrUserNotExist(err) {
			ctx.NotFound()
			return
		}
		ctx.InternalServerError(err)
		return
	}

	// check not a repo
	has, err := models.IsRepositoryExist(ctxUser, repoName)
	if err != nil {
		ctx.InternalServerError(err)
		return
	}
	isDir, err := util.IsDir(models.RepoPath(ctxUser.Name, repoName))
	if err != nil {
		ctx.InternalServerError(err)
		return
	}
	if has || !isDir {
		ctx.NotFound()
		return
	}
	if _, err := repository.AdoptRepository(ctx.User, ctxUser, models.CreateRepoOptions{
		Name:      repoName,
		IsPrivate: true,
	}); err != nil {
		ctx.InternalServerError(err)
		return
	}

	ctx.Status(http.StatusNoContent)
}

// DeleteUnadoptedRepository will delete an unadopted repository
func DeleteUnadoptedRepository(ctx *context.APIContext) {
	// swagger:operation DELETE /admin/unadopted/{owner}/{repo} admin adminDeleteUnadoptedRepository
	// ---
	// summary: Delete unadopted files
	// 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
	// responses:
	//   "204":
	//     "$ref": "#/responses/empty"
	//   "403":
	//     "$ref": "#/responses/forbidden"
	ownerName := ctx.Params(":username")
	repoName := ctx.Params(":reponame")

	ctxUser, err := models.GetUserByName(ownerName)
	if err != nil {
		if models.IsErrUserNotExist(err) {
			ctx.NotFound()
			return
		}
		ctx.InternalServerError(err)
		return
	}

	// check not a repo
	has, err := models.IsRepositoryExist(ctxUser, repoName)
	if err != nil {
		ctx.InternalServerError(err)
		return
	}
	isDir, err := util.IsDir(models.RepoPath(ctxUser.Name, repoName))
	if err != nil {
		ctx.InternalServerError(err)
		return
	}
	if has || !isDir {
		ctx.NotFound()
		return
	}

	if err := repository.DeleteUnadoptedRepository(ctx.User, ctxUser, repoName); err != nil {
		ctx.InternalServerError(err)
		return
	}

	ctx.Status(http.StatusNoContent)
}