code.gitea.io/gitea@v1.22.3/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  	actions_model "code.gitea.io/gitea/models/actions"
    11  	"code.gitea.io/gitea/models/db"
    12  	api "code.gitea.io/gitea/modules/structs"
    13  	"code.gitea.io/gitea/modules/util"
    14  	"code.gitea.io/gitea/modules/web"
    15  	"code.gitea.io/gitea/routers/api/v1/utils"
    16  	actions_service "code.gitea.io/gitea/services/actions"
    17  	"code.gitea.io/gitea/services/context"
    18  	secret_service "code.gitea.io/gitea/services/secrets"
    19  )
    20  
    21  // create or update one secret of the user scope
    22  func CreateOrUpdateSecret(ctx *context.APIContext) {
    23  	// swagger:operation PUT /user/actions/secrets/{secretname} user updateUserSecret
    24  	// ---
    25  	// summary: Create or Update a secret value in a user scope
    26  	// consumes:
    27  	// - application/json
    28  	// produces:
    29  	// - application/json
    30  	// parameters:
    31  	// - name: secretname
    32  	//   in: path
    33  	//   description: name of the secret
    34  	//   type: string
    35  	//   required: true
    36  	// - name: body
    37  	//   in: body
    38  	//   schema:
    39  	//     "$ref": "#/definitions/CreateOrUpdateSecretOption"
    40  	// responses:
    41  	//   "201":
    42  	//     description: response when creating a secret
    43  	//   "204":
    44  	//     description: response when updating a secret
    45  	//   "400":
    46  	//     "$ref": "#/responses/error"
    47  	//   "404":
    48  	//     "$ref": "#/responses/notFound"
    49  
    50  	opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
    51  
    52  	_, created, err := secret_service.CreateOrUpdateSecret(ctx, ctx.Doer.ID, 0, ctx.Params("secretname"), opt.Data)
    53  	if err != nil {
    54  		if errors.Is(err, util.ErrInvalidArgument) {
    55  			ctx.Error(http.StatusBadRequest, "CreateOrUpdateSecret", err)
    56  		} else if errors.Is(err, util.ErrNotExist) {
    57  			ctx.Error(http.StatusNotFound, "CreateOrUpdateSecret", err)
    58  		} else {
    59  			ctx.Error(http.StatusInternalServerError, "CreateOrUpdateSecret", err)
    60  		}
    61  		return
    62  	}
    63  
    64  	if created {
    65  		ctx.Status(http.StatusCreated)
    66  	} else {
    67  		ctx.Status(http.StatusNoContent)
    68  	}
    69  }
    70  
    71  // DeleteSecret delete one secret of the user scope
    72  func DeleteSecret(ctx *context.APIContext) {
    73  	// swagger:operation DELETE /user/actions/secrets/{secretname} user deleteUserSecret
    74  	// ---
    75  	// summary: Delete a secret in a user scope
    76  	// consumes:
    77  	// - application/json
    78  	// produces:
    79  	// - application/json
    80  	// parameters:
    81  	// - name: secretname
    82  	//   in: path
    83  	//   description: name of the secret
    84  	//   type: string
    85  	//   required: true
    86  	// responses:
    87  	//   "204":
    88  	//     description: delete one secret of the user
    89  	//   "400":
    90  	//     "$ref": "#/responses/error"
    91  	//   "404":
    92  	//     "$ref": "#/responses/notFound"
    93  
    94  	err := secret_service.DeleteSecretByName(ctx, ctx.Doer.ID, 0, ctx.Params("secretname"))
    95  	if err != nil {
    96  		if errors.Is(err, util.ErrInvalidArgument) {
    97  			ctx.Error(http.StatusBadRequest, "DeleteSecret", err)
    98  		} else if errors.Is(err, util.ErrNotExist) {
    99  			ctx.Error(http.StatusNotFound, "DeleteSecret", err)
   100  		} else {
   101  			ctx.Error(http.StatusInternalServerError, "DeleteSecret", err)
   102  		}
   103  		return
   104  	}
   105  
   106  	ctx.Status(http.StatusNoContent)
   107  }
   108  
   109  // CreateVariable create a user-level variable
   110  func CreateVariable(ctx *context.APIContext) {
   111  	// swagger:operation POST /user/actions/variables/{variablename} user createUserVariable
   112  	// ---
   113  	// summary: Create a user-level variable
   114  	// consumes:
   115  	// - application/json
   116  	// produces:
   117  	// - application/json
   118  	// parameters:
   119  	// - name: variablename
   120  	//   in: path
   121  	//   description: name of the variable
   122  	//   type: string
   123  	//   required: true
   124  	// - name: body
   125  	//   in: body
   126  	//   schema:
   127  	//     "$ref": "#/definitions/CreateVariableOption"
   128  	// responses:
   129  	//   "201":
   130  	//     description: response when creating a variable
   131  	//   "204":
   132  	//     description: response when creating a variable
   133  	//   "400":
   134  	//     "$ref": "#/responses/error"
   135  	//   "404":
   136  	//     "$ref": "#/responses/notFound"
   137  
   138  	opt := web.GetForm(ctx).(*api.CreateVariableOption)
   139  
   140  	ownerID := ctx.Doer.ID
   141  	variableName := ctx.Params("variablename")
   142  
   143  	v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
   144  		OwnerID: ownerID,
   145  		Name:    variableName,
   146  	})
   147  	if err != nil && !errors.Is(err, util.ErrNotExist) {
   148  		ctx.Error(http.StatusInternalServerError, "GetVariable", err)
   149  		return
   150  	}
   151  	if v != nil && v.ID > 0 {
   152  		ctx.Error(http.StatusConflict, "VariableNameAlreadyExists", util.NewAlreadyExistErrorf("variable name %s already exists", variableName))
   153  		return
   154  	}
   155  
   156  	if _, err := actions_service.CreateVariable(ctx, ownerID, 0, variableName, opt.Value); err != nil {
   157  		if errors.Is(err, util.ErrInvalidArgument) {
   158  			ctx.Error(http.StatusBadRequest, "CreateVariable", err)
   159  		} else {
   160  			ctx.Error(http.StatusInternalServerError, "CreateVariable", err)
   161  		}
   162  		return
   163  	}
   164  
   165  	ctx.Status(http.StatusNoContent)
   166  }
   167  
   168  // UpdateVariable update a user-level variable which is created by current doer
   169  func UpdateVariable(ctx *context.APIContext) {
   170  	// swagger:operation PUT /user/actions/variables/{variablename} user updateUserVariable
   171  	// ---
   172  	// summary: Update a user-level variable which is created by current doer
   173  	// consumes:
   174  	// - application/json
   175  	// produces:
   176  	// - application/json
   177  	// parameters:
   178  	// - name: variablename
   179  	//   in: path
   180  	//   description: name of the variable
   181  	//   type: string
   182  	//   required: true
   183  	// - name: body
   184  	//   in: body
   185  	//   schema:
   186  	//     "$ref": "#/definitions/UpdateVariableOption"
   187  	// responses:
   188  	//   "201":
   189  	//     description: response when updating a variable
   190  	//   "204":
   191  	//     description: response when updating a variable
   192  	//   "400":
   193  	//     "$ref": "#/responses/error"
   194  	//   "404":
   195  	//     "$ref": "#/responses/notFound"
   196  
   197  	opt := web.GetForm(ctx).(*api.UpdateVariableOption)
   198  
   199  	v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
   200  		OwnerID: ctx.Doer.ID,
   201  		Name:    ctx.Params("variablename"),
   202  	})
   203  	if err != nil {
   204  		if errors.Is(err, util.ErrNotExist) {
   205  			ctx.Error(http.StatusNotFound, "GetVariable", err)
   206  		} else {
   207  			ctx.Error(http.StatusInternalServerError, "GetVariable", err)
   208  		}
   209  		return
   210  	}
   211  
   212  	if opt.Name == "" {
   213  		opt.Name = ctx.Params("variablename")
   214  	}
   215  	if _, err := actions_service.UpdateVariable(ctx, v.ID, opt.Name, opt.Value); err != nil {
   216  		if errors.Is(err, util.ErrInvalidArgument) {
   217  			ctx.Error(http.StatusBadRequest, "UpdateVariable", err)
   218  		} else {
   219  			ctx.Error(http.StatusInternalServerError, "UpdateVariable", err)
   220  		}
   221  		return
   222  	}
   223  
   224  	ctx.Status(http.StatusNoContent)
   225  }
   226  
   227  // DeleteVariable delete a user-level variable which is created by current doer
   228  func DeleteVariable(ctx *context.APIContext) {
   229  	// swagger:operation DELETE /user/actions/variables/{variablename} user deleteUserVariable
   230  	// ---
   231  	// summary: Delete a user-level variable which is created by current doer
   232  	// produces:
   233  	// - application/json
   234  	// parameters:
   235  	// - name: variablename
   236  	//   in: path
   237  	//   description: name of the variable
   238  	//   type: string
   239  	//   required: true
   240  	// responses:
   241  	//   "201":
   242  	//     description: response when deleting a variable
   243  	//   "204":
   244  	//     description: response when deleting a variable
   245  	//   "400":
   246  	//     "$ref": "#/responses/error"
   247  	//   "404":
   248  	//     "$ref": "#/responses/notFound"
   249  
   250  	if err := actions_service.DeleteVariableByName(ctx, ctx.Doer.ID, 0, ctx.Params("variablename")); err != nil {
   251  		if errors.Is(err, util.ErrInvalidArgument) {
   252  			ctx.Error(http.StatusBadRequest, "DeleteVariableByName", err)
   253  		} else if errors.Is(err, util.ErrNotExist) {
   254  			ctx.Error(http.StatusNotFound, "DeleteVariableByName", err)
   255  		} else {
   256  			ctx.Error(http.StatusInternalServerError, "DeleteVariableByName", err)
   257  		}
   258  		return
   259  	}
   260  
   261  	ctx.Status(http.StatusNoContent)
   262  }
   263  
   264  // GetVariable get a user-level variable which is created by current doer
   265  func GetVariable(ctx *context.APIContext) {
   266  	// swagger:operation GET /user/actions/variables/{variablename} user getUserVariable
   267  	// ---
   268  	// summary: Get a user-level variable which is created by current doer
   269  	// produces:
   270  	// - application/json
   271  	// parameters:
   272  	// - name: variablename
   273  	//   in: path
   274  	//   description: name of the variable
   275  	//   type: string
   276  	//   required: true
   277  	// responses:
   278  	//   "200":
   279  	//			"$ref": "#/responses/ActionVariable"
   280  	//   "400":
   281  	//     "$ref": "#/responses/error"
   282  	//   "404":
   283  	//     "$ref": "#/responses/notFound"
   284  
   285  	v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
   286  		OwnerID: ctx.Doer.ID,
   287  		Name:    ctx.Params("variablename"),
   288  	})
   289  	if err != nil {
   290  		if errors.Is(err, util.ErrNotExist) {
   291  			ctx.Error(http.StatusNotFound, "GetVariable", err)
   292  		} else {
   293  			ctx.Error(http.StatusInternalServerError, "GetVariable", err)
   294  		}
   295  		return
   296  	}
   297  
   298  	variable := &api.ActionVariable{
   299  		OwnerID: v.OwnerID,
   300  		RepoID:  v.RepoID,
   301  		Name:    v.Name,
   302  		Data:    v.Data,
   303  	}
   304  
   305  	ctx.JSON(http.StatusOK, variable)
   306  }
   307  
   308  // ListVariables list user-level variables
   309  func ListVariables(ctx *context.APIContext) {
   310  	// swagger:operation GET /user/actions/variables user getUserVariablesList
   311  	// ---
   312  	// summary: Get the user-level list of variables which is created by current doer
   313  	// produces:
   314  	// - application/json
   315  	// parameters:
   316  	// - name: page
   317  	//   in: query
   318  	//   description: page number of results to return (1-based)
   319  	//   type: integer
   320  	// - name: limit
   321  	//   in: query
   322  	//   description: page size of results
   323  	//   type: integer
   324  	// responses:
   325  	//   "200":
   326  	//			"$ref": "#/responses/VariableList"
   327  	//   "400":
   328  	//     "$ref": "#/responses/error"
   329  	//   "404":
   330  	//     "$ref": "#/responses/notFound"
   331  
   332  	vars, count, err := db.FindAndCount[actions_model.ActionVariable](ctx, &actions_model.FindVariablesOpts{
   333  		OwnerID:     ctx.Doer.ID,
   334  		ListOptions: utils.GetListOptions(ctx),
   335  	})
   336  	if err != nil {
   337  		ctx.Error(http.StatusInternalServerError, "FindVariables", err)
   338  		return
   339  	}
   340  
   341  	variables := make([]*api.ActionVariable, len(vars))
   342  	for i, v := range vars {
   343  		variables[i] = &api.ActionVariable{
   344  			OwnerID: v.OwnerID,
   345  			RepoID:  v.RepoID,
   346  			Name:    v.Name,
   347  			Data:    v.Data,
   348  		}
   349  	}
   350  
   351  	ctx.SetTotalCountHeader(count)
   352  	ctx.JSON(http.StatusOK, variables)
   353  }