mirror of https://github.com/go-gitea/gitea
feat(api): enhance Actions Secrets Management API for repository (#30656)
- Add endpoint to list repository action secrets in API routes - Implement `ListActionsSecrets` function to retrieve action secrets from the database - Update Swagger documentation to include the new `/repos/{owner}/{repo}/actions/secrets` endpoint - Add `actions` package import and define new routes for actions, secrets, variables, and runners in `api.go`. - Refactor action-related API functions into `Action` struct methods in `org/action.go` and `repo/action.go`. - Remove `actionAPI` struct and related functions, replacing them with `NewAction()` calls. - Rename `variables.go` to `action.go` in `org` directory. - Delete `runners.go` and `secrets.go` in both `org` and `repo` directories, consolidating their content into `action.go`. - Update copyright year and add new imports in `org/action.go`. - Implement `API` interface in `services/actions/interface.go` for action-related methods. - Remove individual action-related functions and replace them with methods on the `Action` struct in `repo/action.go`. --------- Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> Signed-off-by: appleboy <appleboy.tw@gmail.com>pull/30670/head^2
parent
993736d838
commit
852547d0dc
@ -1,31 +0,0 @@ |
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package org |
||||
|
||||
import ( |
||||
"code.gitea.io/gitea/routers/api/v1/shared" |
||||
"code.gitea.io/gitea/services/context" |
||||
) |
||||
|
||||
// https://docs.github.com/en/rest/actions/self-hosted-runners?apiVersion=2022-11-28#create-a-registration-token-for-an-organization
|
||||
|
||||
// GetRegistrationToken returns the token to register org runners
|
||||
func GetRegistrationToken(ctx *context.APIContext) { |
||||
// swagger:operation GET /orgs/{org}/actions/runners/registration-token organization orgGetRunnerRegistrationToken
|
||||
// ---
|
||||
// summary: Get an organization's actions runner registration token
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: org
|
||||
// in: path
|
||||
// description: name of the organization
|
||||
// type: string
|
||||
// required: true
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/RegistrationToken"
|
||||
|
||||
shared.GetRegistrationToken(ctx, ctx.Org.Organization.ID, 0) |
||||
} |
@ -1,166 +0,0 @@ |
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package org |
||||
|
||||
import ( |
||||
"errors" |
||||
"net/http" |
||||
|
||||
"code.gitea.io/gitea/models/db" |
||||
secret_model "code.gitea.io/gitea/models/secret" |
||||
api "code.gitea.io/gitea/modules/structs" |
||||
"code.gitea.io/gitea/modules/util" |
||||
"code.gitea.io/gitea/modules/web" |
||||
"code.gitea.io/gitea/routers/api/v1/utils" |
||||
"code.gitea.io/gitea/services/context" |
||||
secret_service "code.gitea.io/gitea/services/secrets" |
||||
) |
||||
|
||||
// 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"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
opts := &secret_model.FindSecretsOptions{ |
||||
OwnerID: ctx.Org.Organization.ID, |
||||
ListOptions: utils.GetListOptions(ctx), |
||||
} |
||||
|
||||
secrets, count, err := db.FindAndCount[secret_model.Secret](ctx, opts) |
||||
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) |
||||
} |
||||
|
||||
// create or update one secret of the organization
|
||||
func CreateOrUpdateSecret(ctx *context.APIContext) { |
||||
// swagger:operation PUT /orgs/{org}/actions/secrets/{secretname} organization updateOrgSecret
|
||||
// ---
|
||||
// summary: Create or Update a secret value 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
|
||||
// - 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"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption) |
||||
|
||||
_, created, err := secret_service.CreateOrUpdateSecret(ctx, ctx.Org.Organization.ID, 0, ctx.Params("secretname"), opt.Data) |
||||
if err != nil { |
||||
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) |
||||
} |
||||
return |
||||
} |
||||
|
||||
if created { |
||||
ctx.Status(http.StatusCreated) |
||||
} else { |
||||
ctx.Status(http.StatusNoContent) |
||||
} |
||||
} |
||||
|
||||
// DeleteSecret delete one secret of the organization
|
||||
func DeleteSecret(ctx *context.APIContext) { |
||||
// 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
|
||||
// "400":
|
||||
// "$ref": "#/responses/error"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
err := secret_service.DeleteSecretByName(ctx, ctx.Org.Organization.ID, 0, ctx.Params("secretname")) |
||||
if err != nil { |
||||
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) |
||||
} |
||||
return |
||||
} |
||||
|
||||
ctx.Status(http.StatusNoContent) |
||||
} |
@ -1,34 +0,0 @@ |
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repo |
||||
|
||||
import ( |
||||
"code.gitea.io/gitea/routers/api/v1/shared" |
||||
"code.gitea.io/gitea/services/context" |
||||
) |
||||
|
||||
// GetRegistrationToken returns the token to register repo runners
|
||||
func GetRegistrationToken(ctx *context.APIContext) { |
||||
// swagger:operation GET /repos/{owner}/{repo}/runners/registration-token repository repoGetRunnerRegistrationToken
|
||||
// ---
|
||||
// summary: Get a repository's actions runner registration token
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: owner
|
||||
// in: path
|
||||
// description: owner of the repo
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: repo
|
||||
// in: path
|
||||
// description: name of the repo
|
||||
// type: string
|
||||
// required: true
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/RegistrationToken"
|
||||
|
||||
shared.GetRegistrationToken(ctx, ctx.Repo.Repository.OwnerID, ctx.Repo.Repository.ID) |
||||
} |
@ -0,0 +1,28 @@ |
||||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package actions |
||||
|
||||
import "code.gitea.io/gitea/services/context" |
||||
|
||||
// API for actions of a repository or organization
|
||||
type API interface { |
||||
// ListActionsSecrets list secrets
|
||||
ListActionsSecrets(*context.APIContext) |
||||
// CreateOrUpdateSecret create or update a secret
|
||||
CreateOrUpdateSecret(*context.APIContext) |
||||
// DeleteSecret delete a secret
|
||||
DeleteSecret(*context.APIContext) |
||||
// ListVariables list variables
|
||||
ListVariables(*context.APIContext) |
||||
// GetVariable get a variable
|
||||
GetVariable(*context.APIContext) |
||||
// DeleteVariable delete a variable
|
||||
DeleteVariable(*context.APIContext) |
||||
// CreateVariable create a variable
|
||||
CreateVariable(*context.APIContext) |
||||
// UpdateVariable update a variable
|
||||
UpdateVariable(*context.APIContext) |
||||
// GetRegistrationToken get registration token
|
||||
GetRegistrationToken(*context.APIContext) |
||||
} |
Loading…
Reference in new issue