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

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     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 repository
    18  func CreateOrUpdateSecret(ctx *context.APIContext) {
    19  	// swagger:operation PUT /repos/{owner}/{repo}/actions/secrets/{secretname} repository updateRepoSecret
    20  	// ---
    21  	// summary: Create or Update a secret value in a repository
    22  	// consumes:
    23  	// - application/json
    24  	// produces:
    25  	// - application/json
    26  	// parameters:
    27  	// - name: owner
    28  	//   in: path
    29  	//   description: owner of the repository
    30  	//   type: string
    31  	//   required: true
    32  	// - name: repo
    33  	//   in: path
    34  	//   description: name of the repository
    35  	//   type: string
    36  	//   required: true
    37  	// - name: secretname
    38  	//   in: path
    39  	//   description: name of the secret
    40  	//   type: string
    41  	//   required: true
    42  	// - name: body
    43  	//   in: body
    44  	//   schema:
    45  	//     "$ref": "#/definitions/CreateOrUpdateSecretOption"
    46  	// responses:
    47  	//   "201":
    48  	//     description: response when creating a secret
    49  	//   "204":
    50  	//     description: response when updating a secret
    51  	//   "400":
    52  	//     "$ref": "#/responses/error"
    53  	//   "404":
    54  	//     "$ref": "#/responses/notFound"
    55  
    56  	owner := ctx.Repo.Owner
    57  	repo := ctx.Repo.Repository
    58  
    59  	opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
    60  
    61  	_, created, err := secret_service.CreateOrUpdateSecret(ctx, owner.ID, repo.ID, ctx.Params("secretname"), opt.Data)
    62  	if err != nil {
    63  		if errors.Is(err, util.ErrInvalidArgument) {
    64  			ctx.Error(http.StatusBadRequest, "CreateOrUpdateSecret", err)
    65  		} else if errors.Is(err, util.ErrNotExist) {
    66  			ctx.Error(http.StatusNotFound, "CreateOrUpdateSecret", err)
    67  		} else {
    68  			ctx.Error(http.StatusInternalServerError, "CreateOrUpdateSecret", err)
    69  		}
    70  		return
    71  	}
    72  
    73  	if created {
    74  		ctx.Status(http.StatusCreated)
    75  	} else {
    76  		ctx.Status(http.StatusNoContent)
    77  	}
    78  }
    79  
    80  // DeleteSecret delete one secret of the repository
    81  func DeleteSecret(ctx *context.APIContext) {
    82  	// swagger:operation DELETE /repos/{owner}/{repo}/actions/secrets/{secretname} repository deleteRepoSecret
    83  	// ---
    84  	// summary: Delete a secret in a repository
    85  	// consumes:
    86  	// - application/json
    87  	// produces:
    88  	// - application/json
    89  	// parameters:
    90  	// - name: owner
    91  	//   in: path
    92  	//   description: owner of the repository
    93  	//   type: string
    94  	//   required: true
    95  	// - name: repo
    96  	//   in: path
    97  	//   description: name of the repository
    98  	//   type: string
    99  	//   required: true
   100  	// - name: secretname
   101  	//   in: path
   102  	//   description: name of the secret
   103  	//   type: string
   104  	//   required: true
   105  	// responses:
   106  	//   "204":
   107  	//     description: delete one secret of the organization
   108  	//   "400":
   109  	//     "$ref": "#/responses/error"
   110  	//   "404":
   111  	//     "$ref": "#/responses/notFound"
   112  
   113  	owner := ctx.Repo.Owner
   114  	repo := ctx.Repo.Repository
   115  
   116  	err := secret_service.DeleteSecretByName(ctx, owner.ID, repo.ID, ctx.Params("secretname"))
   117  	if err != nil {
   118  		if errors.Is(err, util.ErrInvalidArgument) {
   119  			ctx.Error(http.StatusBadRequest, "DeleteSecret", err)
   120  		} else if errors.Is(err, util.ErrNotExist) {
   121  			ctx.Error(http.StatusNotFound, "DeleteSecret", err)
   122  		} else {
   123  			ctx.Error(http.StatusInternalServerError, "DeleteSecret", err)
   124  		}
   125  		return
   126  	}
   127  
   128  	ctx.Status(http.StatusNoContent)
   129  }