Skip to content

feat: implement organization secret creation API #26566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion modules/structs/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ package structs

import "time"

// User represents a secret
// Secret represents a secret
// swagger:model
type Secret struct {
// the secret's name
Name string `json:"name"`
// swagger:strfmt date-time
Created time.Time `json:"created_at"`
}

// CreateSecretOption options when creating secret
// swagger:model
type CreateSecretOption struct {
// Name of the secret to create
//
// required: true
// unique: true
Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(100)"`
// Data of the secret to create
Data string `json:"data" binding:"Required"`
}
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,7 @@ func Routes() *web.Route {
})
m.Group("/actions/secrets", func() {
m.Get("", reqToken(), reqOrgOwnership(), org.ListActionsSecrets)
m.Post("", reqToken(), reqOrgOwnership(), bind(api.CreateSecretOption{}), org.CreateOrgSecret)
})
m.Group("/public_members", func() {
m.Get("", org.ListPublicMembers)
Expand Down
51 changes: 47 additions & 4 deletions routers/api/v1/org/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ package org
import (
"net/http"

"code.gitea.io/gitea/models/secret"
secret_model "code.gitea.io/gitea/models/secret"
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/routers/web/shared/actions"
"code.gitea.io/gitea/services/convert"
)

// ListActionsSecrets list an organization's actions secrets
Expand Down Expand Up @@ -42,18 +45,18 @@ func ListActionsSecrets(ctx *context.APIContext) {

// listActionsSecrets list an organization's actions secrets
func listActionsSecrets(ctx *context.APIContext) {
opts := &secret.FindSecretsOptions{
opts := &secret_model.FindSecretsOptions{
OwnerID: ctx.Org.Organization.ID,
ListOptions: utils.GetListOptions(ctx),
}

count, err := secret.CountSecrets(ctx, opts)
count, err := secret_model.CountSecrets(ctx, opts)
if err != nil {
ctx.InternalServerError(err)
return
}

secrets, err := secret.FindSecrets(ctx, *opts)
secrets, err := secret_model.FindSecrets(ctx, *opts)
if err != nil {
ctx.InternalServerError(err)
return
Expand All @@ -70,3 +73,43 @@ func listActionsSecrets(ctx *context.APIContext) {
ctx.SetTotalCountHeader(count)
ctx.JSON(http.StatusOK, apiSecrets)
}

// CreateOrgSecret create one secret of the organization
func CreateOrgSecret(ctx *context.APIContext) {
// swagger:operation POST /orgs/{org}/actions/secrets organization createOrgSecret
// ---
// summary: Create 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: body
// in: body
// schema:
// "$ref": "#/definitions/CreateSecretOption"
// responses:
// "201":
// "$ref": "#/responses/Secret"
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
// "403":
// "$ref": "#/responses/forbidden"
opt := web.GetForm(ctx).(*api.CreateSecretOption)
s, err := secret_model.InsertEncryptedSecret(
ctx, ctx.Org.Organization.ID, 0, opt.Name, actions.ReserveLineBreakForTextarea(opt.Data),
)
if err != nil {
ctx.Error(http.StatusInternalServerError, "InsertEncryptedSecret", err)
return
}

ctx.JSON(http.StatusCreated, convert.ToSecret(s))
}
7 changes: 7 additions & 0 deletions routers/api/v1/swagger/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ type swaggerResponseSecretList struct {
// in:body
Body []api.Secret `json:"body"`
}

// Secret
// swagger:response Secret
type swaggerResponseSecret struct {
// in:body
Body api.Secret `json:"body"`
}
3 changes: 3 additions & 0 deletions routers/api/v1/swagger/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,7 @@ type swaggerParameterBodies struct {

// in:body
UpdateRepoAvatarOptions api.UpdateRepoAvatarOption

// in:body
CreateSecretOption api.CreateSecretOption
}
18 changes: 18 additions & 0 deletions services/convert/secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package convert

import (
secret_model "code.gitea.io/gitea/models/secret"
api "code.gitea.io/gitea/modules/structs"
)

// ToSecret converts Secret to API format
func ToSecret(secret *secret_model.Secret) *api.Secret {
result := &api.Secret{
Name: secret.Name,
}

return result
}
74 changes: 72 additions & 2 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.