code.gitea.io/gitea@v1.21.7/routers/api/v1/user/action.go (about)

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