2023-08-30 04:54:49 +08:00
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
2023-09-05 17:21:02 +02:00
"errors"
2023-08-30 04:54:49 +08:00
"net/http"
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/gitea/modules/structs"
2023-09-05 17:21:02 +02:00
"code.gitea.io/gitea/modules/util"
2023-08-30 04:54:49 +08:00
"code.gitea.io/gitea/modules/web"
2023-09-05 17:21:02 +02:00
secret_service "code.gitea.io/gitea/services/secrets"
2023-08-30 04:54:49 +08:00
)
// create or update one secret of the repository
func CreateOrUpdateSecret ( ctx * context . APIContext ) {
// swagger:operation PUT /repos/{owner}/{repo}/actions/secrets/{secretname} repository updateRepoSecret
// ---
// summary: Create or Update a secret value in a repository
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repository
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repository
// type: string
// required: true
// - name: secretname
// in: path
// description: name of the secret
// type: string
// required: true
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/CreateOrUpdateSecretOption"
// responses:
// "201":
// description: response when creating a secret
// "204":
// description: response when updating a secret
// "400":
// "$ref": "#/responses/error"
2023-09-05 17:21:02 +02:00
// "404":
// "$ref": "#/responses/notFound"
2023-08-30 04:54:49 +08:00
owner := ctx . Repo . Owner
repo := ctx . Repo . Repository
opt := web . GetForm ( ctx ) . ( * api . CreateOrUpdateSecretOption )
2023-09-05 17:21:02 +02:00
_ , created , err := secret_service . CreateOrUpdateSecret ( ctx , owner . ID , repo . ID , ctx . Params ( "secretname" ) , opt . Data )
2023-08-30 04:54:49 +08:00
if err != nil {
2023-09-05 17:21:02 +02:00
if errors . Is ( err , util . ErrInvalidArgument ) {
ctx . Error ( http . StatusBadRequest , "CreateOrUpdateSecret" , err )
} else if errors . Is ( err , util . ErrNotExist ) {
ctx . Error ( http . StatusNotFound , "CreateOrUpdateSecret" , err )
} else {
ctx . Error ( http . StatusInternalServerError , "CreateOrUpdateSecret" , err )
}
2023-08-30 04:54:49 +08:00
return
}
2023-09-05 17:21:02 +02:00
if created {
2023-08-30 04:54:49 +08:00
ctx . Status ( http . StatusCreated )
2023-09-05 17:21:02 +02:00
} else {
ctx . Status ( http . StatusNoContent )
2023-08-30 04:54:49 +08:00
}
}
2023-09-01 21:02:49 +08:00
// DeleteSecret delete one secret of the repository
func DeleteSecret ( ctx * context . APIContext ) {
// swagger:operation DELETE /repos/{owner}/{repo}/actions/secrets/{secretname} repository deleteRepoSecret
// ---
// summary: Delete a secret in a repository
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repository
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repository
// type: string
// required: true
// - name: secretname
// in: path
// description: name of the secret
// type: string
// required: true
// responses:
// "204":
// description: delete one secret of the organization
2023-09-05 17:21:02 +02:00
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
2023-09-01 21:02:49 +08:00
owner := ctx . Repo . Owner
repo := ctx . Repo . Repository
2023-09-05 17:21:02 +02:00
err := secret_service . DeleteSecretByName ( ctx , owner . ID , repo . ID , ctx . Params ( "secretname" ) )
2023-09-01 21:02:49 +08:00
if err != nil {
2023-09-05 17:21:02 +02:00
if errors . Is ( err , util . ErrInvalidArgument ) {
ctx . Error ( http . StatusBadRequest , "DeleteSecret" , err )
} else if errors . Is ( err , util . ErrNotExist ) {
ctx . Error ( http . StatusNotFound , "DeleteSecret" , err )
} else {
ctx . Error ( http . StatusInternalServerError , "DeleteSecret" , err )
}
2023-09-01 21:02:49 +08:00
return
}
ctx . Status ( http . StatusNoContent )
}