mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-02-03 20:51:07 -05:00
Fixed action variables and secrets according to [Docu](https://forgejo.org/docs/next/user/actions/basic-concepts/#name-constraints): > Variable names must not start with the FORGEJO_, GITHUB_ or GITEA_ prefix. This wasn't correctly enforced, so I changed the regex ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [x] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [ ] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10682 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org> Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org> Co-authored-by: zokki <zokki.softwareschmiede@gmail.com> Co-committed-by: zokki <zokki.softwareschmiede@gmail.com>
92 lines
2.5 KiB
Go
92 lines
2.5 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"context"
|
|
"regexp"
|
|
"strings"
|
|
|
|
actions_model "forgejo.org/models/actions"
|
|
"forgejo.org/modules/log"
|
|
"forgejo.org/modules/util"
|
|
secrets_service "forgejo.org/services/secrets"
|
|
)
|
|
|
|
func CreateVariable(ctx context.Context, ownerID, repoID int64, name, data string) (*actions_model.ActionVariable, error) {
|
|
if err := secrets_service.ValidateName(name); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := envNameCIRegexMatch(name); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
v, err := actions_model.InsertVariable(ctx, ownerID, repoID, name, util.ReserveLineBreakForTextarea(data))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return v, nil
|
|
}
|
|
|
|
func UpdateVariable(ctx context.Context, variableID, ownerID, repoID int64, name, data string) (bool, error) {
|
|
if err := secrets_service.ValidateName(name); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
if err := envNameCIRegexMatch(name); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return actions_model.UpdateVariable(ctx, &actions_model.ActionVariable{
|
|
ID: variableID,
|
|
Name: strings.ToUpper(name),
|
|
Data: util.ReserveLineBreakForTextarea(data),
|
|
OwnerID: ownerID,
|
|
RepoID: repoID,
|
|
})
|
|
}
|
|
|
|
func DeleteVariableByName(ctx context.Context, ownerID, repoID int64, name string) error {
|
|
v, err := GetVariable(ctx, actions_model.FindVariablesOpts{
|
|
OwnerID: ownerID,
|
|
RepoID: repoID,
|
|
Name: name,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = actions_model.DeleteVariable(ctx, v.ID, ownerID, repoID)
|
|
return err
|
|
}
|
|
|
|
func GetVariable(ctx context.Context, opts actions_model.FindVariablesOpts) (*actions_model.ActionVariable, error) {
|
|
vars, err := actions_model.FindVariables(ctx, opts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(vars) != 1 {
|
|
return nil, util.NewNotExistErrorf("variable not found")
|
|
}
|
|
return vars[0], nil
|
|
}
|
|
|
|
// some regular expression of `variables` and `secrets`
|
|
// reference to:
|
|
// https://docs.github.com/en/actions/learn-github-actions/variables#naming-conventions-for-configuration-variables
|
|
// https://docs.github.com/en/actions/security-guides/encrypted-secrets#naming-your-secrets
|
|
// https://forgejo.org/docs/next/user/actions/basic-concepts/#name-constraints
|
|
var (
|
|
forbiddenEnvNameCIRx = regexp.MustCompile("(?i)^CI$")
|
|
)
|
|
|
|
func envNameCIRegexMatch(name string) error {
|
|
if forbiddenEnvNameCIRx.MatchString(name) {
|
|
log.Error("Env Name cannot be ci")
|
|
return util.NewInvalidArgumentErrorf("env name cannot be ci")
|
|
}
|
|
return nil
|
|
}
|