|
4 | 4 | package packages
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "errors" |
7 | 8 | "net/http"
|
8 | 9 |
|
9 | 10 | "code.gitea.io/gitea/models/packages"
|
| 11 | + repo_model "code.gitea.io/gitea/models/repo" |
10 | 12 | "code.gitea.io/gitea/modules/optional"
|
11 | 13 | api "code.gitea.io/gitea/modules/structs"
|
| 14 | + "code.gitea.io/gitea/modules/util" |
12 | 15 | "code.gitea.io/gitea/routers/api/v1/utils"
|
13 | 16 | "code.gitea.io/gitea/services/context"
|
14 | 17 | "code.gitea.io/gitea/services/convert"
|
@@ -213,3 +216,116 @@ func ListPackageFiles(ctx *context.APIContext) {
|
213 | 216 |
|
214 | 217 | ctx.JSON(http.StatusOK, apiPackageFiles)
|
215 | 218 | }
|
| 219 | + |
| 220 | +// LinkPackage sets a repository link for a package |
| 221 | +func LinkPackage(ctx *context.APIContext) { |
| 222 | + // swagger:operation POST /packages/{owner}/{type}/{name}/-/link/{repo_name} package linkPackage |
| 223 | + // --- |
| 224 | + // summary: Link a package to a repository |
| 225 | + // parameters: |
| 226 | + // - name: owner |
| 227 | + // in: path |
| 228 | + // description: owner of the package |
| 229 | + // type: string |
| 230 | + // required: true |
| 231 | + // - name: type |
| 232 | + // in: path |
| 233 | + // description: type of the package |
| 234 | + // type: string |
| 235 | + // required: true |
| 236 | + // - name: name |
| 237 | + // in: path |
| 238 | + // description: name of the package |
| 239 | + // type: string |
| 240 | + // required: true |
| 241 | + // - name: repo_name |
| 242 | + // in: query |
| 243 | + // description: Name of the repository to link. |
| 244 | + // type: string |
| 245 | + // required: false |
| 246 | + // responses: |
| 247 | + // "201": |
| 248 | + // "$ref": "#/responses/empty" |
| 249 | + // "404": |
| 250 | + // "$ref": "#/responses/notFound" |
| 251 | + |
| 252 | + pkg, err := packages.GetPackageByName(ctx, ctx.ContextUser.ID, packages.Type(ctx.PathParam("type")), ctx.PathParam("name")) |
| 253 | + if err != nil { |
| 254 | + if errors.Is(err, util.ErrNotExist) { |
| 255 | + ctx.Error(http.StatusNotFound, "GetPackageByName", err) |
| 256 | + } else { |
| 257 | + ctx.Error(http.StatusInternalServerError, "GetPackageByName", err) |
| 258 | + } |
| 259 | + return |
| 260 | + } |
| 261 | + |
| 262 | + repo, err := repo_model.GetRepositoryByName(ctx, ctx.ContextUser.ID, ctx.PathParam("repo_name")) |
| 263 | + if err != nil { |
| 264 | + if errors.Is(err, util.ErrNotExist) { |
| 265 | + ctx.Error(http.StatusNotFound, "GetRepositoryByName", err) |
| 266 | + } else { |
| 267 | + ctx.Error(http.StatusInternalServerError, "GetRepositoryByName", err) |
| 268 | + } |
| 269 | + return |
| 270 | + } |
| 271 | + |
| 272 | + err = packages_service.LinkToRepository(ctx, pkg, repo, ctx.Doer) |
| 273 | + if err != nil { |
| 274 | + if errors.Is(err, util.ErrNotExist) { |
| 275 | + ctx.Error(http.StatusNotFound, "GetPackageByName", err) |
| 276 | + } else { |
| 277 | + ctx.Error(http.StatusInternalServerError, "GetPackageByName", err) |
| 278 | + } |
| 279 | + return |
| 280 | + } |
| 281 | + ctx.Status(http.StatusCreated) |
| 282 | +} |
| 283 | + |
| 284 | +// UnlinkPackage sets a repository link for a package |
| 285 | +func UnlinkPackage(ctx *context.APIContext) { |
| 286 | + // swagger:operation POST /packages/{owner}/{type}/{name}/-/unlink package unlinkPackage |
| 287 | + // --- |
| 288 | + // summary: Unlink a package from a repository |
| 289 | + // parameters: |
| 290 | + // - name: owner |
| 291 | + // in: path |
| 292 | + // description: owner of the package |
| 293 | + // type: string |
| 294 | + // required: true |
| 295 | + // - name: type |
| 296 | + // in: path |
| 297 | + // description: type of the package |
| 298 | + // type: string |
| 299 | + // required: true |
| 300 | + // - name: name |
| 301 | + // in: path |
| 302 | + // description: name of the package |
| 303 | + // type: string |
| 304 | + // required: true |
| 305 | + // responses: |
| 306 | + // "201": |
| 307 | + // "$ref": "#/responses/empty" |
| 308 | + // "404": |
| 309 | + // "$ref": "#/responses/notFound" |
| 310 | + |
| 311 | + pkg, err := packages.GetPackageByName(ctx, ctx.ContextUser.ID, packages.Type(ctx.PathParam("type")), ctx.PathParam("name")) |
| 312 | + if err != nil { |
| 313 | + if errors.Is(err, util.ErrNotExist) { |
| 314 | + ctx.Error(http.StatusNotFound, "GetPackageByName", err) |
| 315 | + } else { |
| 316 | + ctx.Error(http.StatusInternalServerError, "GetPackageByName", err) |
| 317 | + } |
| 318 | + return |
| 319 | + } |
| 320 | + |
| 321 | + err = packages_service.UnlinkFromRepository(ctx, pkg, ctx.Doer) |
| 322 | + if err != nil { |
| 323 | + if errors.Is(err, util.ErrNotExist) { |
| 324 | + ctx.Error(http.StatusNotFound, "UnlinkFromRepository", err) |
| 325 | + } else { |
| 326 | + ctx.Error(http.StatusInternalServerError, "UnlinkFromRepository", err) |
| 327 | + } |
| 328 | + return |
| 329 | + } |
| 330 | + ctx.Status(http.StatusNoContent) |
| 331 | +} |
0 commit comments