2023-08-15 18:02:43 +05:30
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package org
|
|
|
|
|
|
|
|
import (
|
2023-09-05 20:51:02 +05:30
|
|
|
"errors"
|
2023-08-15 18:02:43 +05:30
|
|
|
"net/http"
|
|
|
|
|
2023-11-24 09:19:41 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2023-08-22 08:50:34 +05:30
|
|
|
secret_model "code.gitea.io/gitea/models/secret"
|
2023-08-15 18:02:43 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-09-05 20:51:02 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-08-22 08:50:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2023-08-15 18:02:43 +05:30
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2023-09-05 20:51:02 +05:30
|
|
|
secret_service "code.gitea.io/gitea/services/secrets"
|
2023-08-15 18:02:43 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// 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"
|
2023-09-13 08:07:54 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2023-08-15 18:02:43 +05:30
|
|
|
|
2023-08-22 08:50:34 +05:30
|
|
|
opts := &secret_model.FindSecretsOptions{
|
2023-08-15 18:02:43 +05:30
|
|
|
OwnerID: ctx.Org.Organization.ID,
|
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
|
|
|
}
|
|
|
|
|
2023-11-24 09:19:41 +05:30
|
|
|
secrets, count, err := db.FindAndCount[secret_model.Secret](ctx, opts)
|
2023-08-15 18:02:43 +05:30
|
|
|
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-22 08:50:34 +05:30
|
|
|
|
2023-08-28 10:38:19 +05:30
|
|
|
// create or update one secret of the organization
|
2023-08-30 02:24:49 +05:30
|
|
|
func CreateOrUpdateSecret(ctx *context.APIContext) {
|
2023-08-24 07:37:00 +05:30
|
|
|
// swagger:operation PUT /orgs/{org}/actions/secrets/{secretname} organization updateOrgSecret
|
|
|
|
// ---
|
2023-08-28 10:38:19 +05:30
|
|
|
// summary: Create or Update a secret value in an organization
|
2023-08-24 07:37:00 +05:30
|
|
|
// 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 10:38:19 +05:30
|
|
|
// "$ref": "#/definitions/CreateOrUpdateSecretOption"
|
2023-08-24 07:37:00 +05:30
|
|
|
// responses:
|
2023-08-28 10:38:19 +05:30
|
|
|
// "201":
|
|
|
|
// description: response when creating a secret
|
2023-08-24 07:37:00 +05:30
|
|
|
// "204":
|
2023-08-28 10:38:19 +05:30
|
|
|
// description: response when updating a secret
|
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
2023-09-05 20:51:02 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
2023-08-28 10:38:19 +05:30
|
|
|
opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
|
2023-09-05 20:51:02 +05:30
|
|
|
|
|
|
|
_, created, err := secret_service.CreateOrUpdateSecret(ctx, ctx.Org.Organization.ID, 0, ctx.Params("secretname"), opt.Data)
|
2023-08-30 02:24:49 +05:30
|
|
|
if err != nil {
|
2023-09-05 20:51:02 +05:30
|
|
|
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-24 07:37:00 +05:30
|
|
|
return
|
|
|
|
}
|
2023-09-05 20:51:02 +05:30
|
|
|
|
|
|
|
if created {
|
2023-08-30 02:24:49 +05:30
|
|
|
ctx.Status(http.StatusCreated)
|
2023-09-05 20:51:02 +05:30
|
|
|
} else {
|
|
|
|
ctx.Status(http.StatusNoContent)
|
2023-08-24 07:37:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 18:32:49 +05:30
|
|
|
// DeleteSecret delete one secret of the organization
|
|
|
|
func DeleteSecret(ctx *context.APIContext) {
|
2023-08-24 07:37:00 +05:30
|
|
|
// 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
|
2023-09-05 20:51:02 +05:30
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
|
|
err := secret_service.DeleteSecretByName(ctx, ctx.Org.Organization.ID, 0, ctx.Params("secretname"))
|
2023-08-24 07:37:00 +05:30
|
|
|
if err != nil {
|
2023-09-05 20:51:02 +05:30
|
|
|
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-08-24 07:37:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|