|
| 1 | +// Copyright 2023 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package user |
| 5 | + |
| 6 | +import ( |
| 7 | + "net/http" |
| 8 | + |
| 9 | + secret_model "code.gitea.io/gitea/models/secret" |
| 10 | + "code.gitea.io/gitea/modules/context" |
| 11 | + api "code.gitea.io/gitea/modules/structs" |
| 12 | + "code.gitea.io/gitea/modules/web" |
| 13 | + "code.gitea.io/gitea/routers/web/shared/actions" |
| 14 | +) |
| 15 | + |
| 16 | +// create or update one secret of the user scope |
| 17 | +func CreateOrUpdateSecret(ctx *context.APIContext) { |
| 18 | + // swagger:operation PUT /user/actions/secrets/{secretname} user updateUserSecret |
| 19 | + // --- |
| 20 | + // summary: Create or Update a secret value in a user scope |
| 21 | + // consumes: |
| 22 | + // - application/json |
| 23 | + // produces: |
| 24 | + // - application/json |
| 25 | + // parameters: |
| 26 | + // - name: secretname |
| 27 | + // in: path |
| 28 | + // description: name of the secret |
| 29 | + // type: string |
| 30 | + // required: true |
| 31 | + // - name: body |
| 32 | + // in: body |
| 33 | + // schema: |
| 34 | + // "$ref": "#/definitions/CreateOrUpdateSecretOption" |
| 35 | + // responses: |
| 36 | + // "201": |
| 37 | + // description: response when creating a secret |
| 38 | + // "204": |
| 39 | + // description: response when updating a secret |
| 40 | + // "400": |
| 41 | + // "$ref": "#/responses/error" |
| 42 | + // "404": |
| 43 | + // "$ref": "#/responses/notFound" |
| 44 | + |
| 45 | + secretName := ctx.Params(":secretname") |
| 46 | + if err := actions.NameRegexMatch(secretName); err != nil { |
| 47 | + ctx.Error(http.StatusBadRequest, "CreateOrUpdateSecret", err) |
| 48 | + return |
| 49 | + } |
| 50 | + opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption) |
| 51 | + isCreated, err := secret_model.CreateOrUpdateSecret(ctx, ctx.Doer.ID, 0, secretName, opt.Data) |
| 52 | + if err != nil { |
| 53 | + ctx.Error(http.StatusInternalServerError, "CreateOrUpdateSecret", err) |
| 54 | + return |
| 55 | + } |
| 56 | + if isCreated { |
| 57 | + ctx.Status(http.StatusCreated) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + ctx.Status(http.StatusNoContent) |
| 62 | +} |
| 63 | + |
| 64 | +// DeleteSecret delete one secret of the user scope |
| 65 | +func DeleteSecret(ctx *context.APIContext) { |
| 66 | + // swagger:operation DELETE /user/actions/secrets/{secretname} user deleteUserSecret |
| 67 | + // --- |
| 68 | + // summary: Delete a secret in a user scope |
| 69 | + // consumes: |
| 70 | + // - application/json |
| 71 | + // produces: |
| 72 | + // - application/json |
| 73 | + // parameters: |
| 74 | + // - name: secretname |
| 75 | + // in: path |
| 76 | + // description: name of the secret |
| 77 | + // type: string |
| 78 | + // required: true |
| 79 | + // responses: |
| 80 | + // "204": |
| 81 | + // description: delete one secret of the user |
| 82 | + // "400": |
| 83 | + // "$ref": "#/responses/error" |
| 84 | + // "404": |
| 85 | + // "$ref": "#/responses/notFound" |
| 86 | + |
| 87 | + secretName := ctx.Params(":secretname") |
| 88 | + if err := actions.NameRegexMatch(secretName); err != nil { |
| 89 | + ctx.Error(http.StatusBadRequest, "DeleteSecret", err) |
| 90 | + return |
| 91 | + } |
| 92 | + err := secret_model.DeleteSecret( |
| 93 | + ctx, ctx.Doer.ID, 0, secretName, |
| 94 | + ) |
| 95 | + if secret_model.IsErrSecretNotFound(err) { |
| 96 | + ctx.NotFound(err) |
| 97 | + return |
| 98 | + } |
| 99 | + if err != nil { |
| 100 | + ctx.Error(http.StatusInternalServerError, "DeleteSecret", err) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + ctx.Status(http.StatusNoContent) |
| 105 | +} |
0 commit comments