2023-08-15 08:32:43 -04:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package org
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-08-21 23:20:34 -04:00
|
|
|
secret_model "code.gitea.io/gitea/models/secret"
|
2023-08-15 08:32:43 -04:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-08-21 23:20:34 -04:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2023-08-15 08:32:43 -04:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2023-08-21 23:20:34 -04:00
|
|
|
"code.gitea.io/gitea/routers/web/shared/actions"
|
2023-08-15 08:32:43 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListActionsSecrets list an organization's actions secrets
|
|
|
|
func ListActionsSecrets(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /orgs/{org}/actions/secrets organization orgListActionsSecrets
|
|
|
|
// ---
|
|
|
|
// summary: List an organization's actions secrets
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of the organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
|
|
|
// description: page size of results
|
|
|
|
// type: integer
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/SecretList"
|
|
|
|
|
|
|
|
listActionsSecrets(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// listActionsSecrets list an organization's actions secrets
|
|
|
|
func listActionsSecrets(ctx *context.APIContext) {
|
2023-08-21 23:20:34 -04:00
|
|
|
opts := &secret_model.FindSecretsOptions{
|
2023-08-15 08:32:43 -04:00
|
|
|
OwnerID: ctx.Org.Organization.ID,
|
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
|
|
|
}
|
|
|
|
|
2023-08-21 23:20:34 -04:00
|
|
|
count, err := secret_model.CountSecrets(ctx, opts)
|
2023-08-15 08:32:43 -04:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-21 23:20:34 -04:00
|
|
|
secrets, err := secret_model.FindSecrets(ctx, *opts)
|
2023-08-15 08:32:43 -04:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiSecrets := make([]*api.Secret, len(secrets))
|
|
|
|
for k, v := range secrets {
|
|
|
|
apiSecrets[k] = &api.Secret{
|
|
|
|
Name: v.Name,
|
|
|
|
Created: v.CreatedUnix.AsTime(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.SetTotalCountHeader(count)
|
|
|
|
ctx.JSON(http.StatusOK, apiSecrets)
|
|
|
|
}
|
2023-08-21 23:20:34 -04:00
|
|
|
|
2023-08-28 01:08:19 -04:00
|
|
|
// create or update one secret of the organization
|
2023-08-29 16:54:49 -04:00
|
|
|
func CreateOrUpdateSecret(ctx *context.APIContext) {
|
2023-08-23 22:07:00 -04:00
|
|
|
// swagger:operation PUT /orgs/{org}/actions/secrets/{secretname} organization updateOrgSecret
|
|
|
|
// ---
|
2023-08-28 01:08:19 -04:00
|
|
|
// summary: Create or Update a secret value in an organization
|
2023-08-23 22:07:00 -04:00
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: secretname
|
|
|
|
// in: path
|
|
|
|
// description: name of the secret
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
2023-08-28 01:08:19 -04:00
|
|
|
// "$ref": "#/definitions/CreateOrUpdateSecretOption"
|
2023-08-23 22:07:00 -04:00
|
|
|
// responses:
|
2023-08-28 01:08:19 -04:00
|
|
|
// "201":
|
|
|
|
// description: response when creating a secret
|
2023-08-23 22:07:00 -04:00
|
|
|
// "204":
|
2023-08-28 01:08:19 -04:00
|
|
|
// description: response when updating a secret
|
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
2023-08-23 22:07:00 -04:00
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
secretName := ctx.Params(":secretname")
|
2023-08-28 01:08:19 -04:00
|
|
|
if err := actions.NameRegexMatch(secretName); err != nil {
|
2023-08-29 16:54:49 -04:00
|
|
|
ctx.Error(http.StatusBadRequest, "CreateOrUpdateSecret", err)
|
2023-08-28 01:08:19 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
|
2023-08-29 16:54:49 -04:00
|
|
|
isCreated, err := secret_model.CreateOrUpdateSecret(ctx, ctx.Org.Organization.ID, 0, secretName, opt.Data)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "CreateOrUpdateSecret", err)
|
2023-08-23 22:07:00 -04:00
|
|
|
return
|
|
|
|
}
|
2023-08-29 16:54:49 -04:00
|
|
|
if isCreated {
|
|
|
|
ctx.Status(http.StatusCreated)
|
2023-08-23 22:07:00 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2023-09-01 09:02:49 -04:00
|
|
|
// DeleteSecret delete one secret of the organization
|
|
|
|
func DeleteSecret(ctx *context.APIContext) {
|
2023-08-23 22:07:00 -04:00
|
|
|
// swagger:operation DELETE /orgs/{org}/actions/secrets/{secretname} organization deleteOrgSecret
|
|
|
|
// ---
|
|
|
|
// summary: Delete a secret in an organization
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of organization
|
|
|
|
// 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
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
secretName := ctx.Params(":secretname")
|
2023-09-01 09:02:49 -04:00
|
|
|
if err := actions.NameRegexMatch(secretName); err != nil {
|
|
|
|
ctx.Error(http.StatusBadRequest, "DeleteSecret", err)
|
|
|
|
return
|
|
|
|
}
|
2023-08-23 22:07:00 -04:00
|
|
|
err := secret_model.DeleteSecret(
|
|
|
|
ctx, ctx.Org.Organization.ID, 0, secretName,
|
|
|
|
)
|
|
|
|
if secret_model.IsErrSecretNotFound(err) {
|
|
|
|
ctx.NotFound(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteSecret", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|