mirror of
https://gitea.com/gitea/tea.git
synced 2025-10-20 23:04:03 +02:00

## Summary This PR adds comprehensive Actions secrets and variables management functionality to the tea CLI, enabling users to manage their repository's CI/CD configuration directly from the command line. ## Features Added ### Actions Secrets Management - **List secrets**: `tea actions secrets list` - Display all repository action secrets - **Create secrets**: `tea actions secrets create <name>` - Create new secrets with interactive prompts - **Delete secrets**: `tea actions secrets delete <name>` - Remove existing secrets ### Actions Variables Management - **List variables**: `tea actions variables list` - Display all repository action variables - **Set variables**: `tea actions variables set <name> <value>` - Create or update variables - **Delete variables**: `tea actions variables delete <name>` - Remove existing variables ## Implementation Details - **Interactive prompts**: Secure input handling for sensitive secret values - **Input validation**: Proper validation for secret/variable names and values - **Table formatting**: Consistent output formatting with existing tea commands - **Error handling**: Comprehensive error handling and user feedback - **Test coverage**: Full test suite for all functionality ## Usage Examples ```bash # Secrets management tea actions secrets list tea actions secrets create API_KEY # Will prompt securely for value tea actions secrets delete OLD_SECRET # Variables management tea actions variables list tea actions variables set API_URL https://api.example.com tea actions variables delete UNUSED_VAR ``` ## Related Issue Resolves #797 ## Testing - All new functionality includes comprehensive unit tests - Integration with existing tea CLI patterns and conventions - Validated against Gitea Actions API Reviewed-on: https://gitea.com/gitea/tea/pulls/796 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Ross Golder <ross@golder.org> Co-committed-by: Ross Golder <ross@golder.org>
117 lines
3.1 KiB
Go
117 lines
3.1 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package variables
|
|
|
|
import (
|
|
stdctx "context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"code.gitea.io/tea/cmd/flags"
|
|
"code.gitea.io/tea/modules/context"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
// CmdVariablesSet represents a sub command to set action variables
|
|
var CmdVariablesSet = cli.Command{
|
|
Name: "set",
|
|
Aliases: []string{"create", "update"},
|
|
Usage: "Set an action variable",
|
|
Description: "Set a variable for use in repository actions and workflows",
|
|
ArgsUsage: "<variable-name> [variable-value]",
|
|
Action: runVariablesSet,
|
|
Flags: append([]cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "file",
|
|
Usage: "read variable value from file",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "stdin",
|
|
Usage: "read variable value from stdin",
|
|
},
|
|
}, flags.AllDefaultFlags...),
|
|
}
|
|
|
|
func runVariablesSet(ctx stdctx.Context, cmd *cli.Command) error {
|
|
if cmd.Args().Len() == 0 {
|
|
return fmt.Errorf("variable name is required")
|
|
}
|
|
|
|
c := context.InitCommand(cmd)
|
|
client := c.Login.Client()
|
|
|
|
variableName := cmd.Args().First()
|
|
var variableValue string
|
|
|
|
// Determine how to get the variable value
|
|
if cmd.String("file") != "" {
|
|
// Read from file
|
|
content, err := os.ReadFile(cmd.String("file"))
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read file: %w", err)
|
|
}
|
|
variableValue = strings.TrimSpace(string(content))
|
|
} else if cmd.Bool("stdin") {
|
|
// Read from stdin
|
|
content, err := io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read from stdin: %w", err)
|
|
}
|
|
variableValue = strings.TrimSpace(string(content))
|
|
} else if cmd.Args().Len() >= 2 {
|
|
// Use provided argument
|
|
variableValue = cmd.Args().Get(1)
|
|
} else {
|
|
// Interactive prompt
|
|
fmt.Printf("Enter variable value for '%s': ", variableName)
|
|
var input string
|
|
fmt.Scanln(&input)
|
|
variableValue = input
|
|
}
|
|
|
|
if variableValue == "" {
|
|
return fmt.Errorf("variable value cannot be empty")
|
|
}
|
|
|
|
_, err := client.CreateRepoActionVariable(c.Owner, c.Repo, variableName, variableValue)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Printf("Variable '%s' set successfully\n", variableName)
|
|
return nil
|
|
}
|
|
|
|
// validateVariableName validates that a variable name follows the required format
|
|
func validateVariableName(name string) error {
|
|
if name == "" {
|
|
return fmt.Errorf("variable name cannot be empty")
|
|
}
|
|
|
|
// Variable names can contain letters (upper/lower), numbers, and underscores
|
|
// Cannot start with a number
|
|
// Cannot contain spaces or special characters (except underscore)
|
|
validPattern := regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
|
|
if !validPattern.MatchString(name) {
|
|
return fmt.Errorf("variable name must contain only letters, numbers, and underscores, and cannot start with a number")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// validateVariableValue validates that a variable value is acceptable
|
|
func validateVariableValue(value string) error {
|
|
// Variables can be empty or contain whitespace, unlike secrets
|
|
|
|
// Check for maximum size (64KB limit)
|
|
if len(value) > 65536 {
|
|
return fmt.Errorf("variable value cannot exceed 64KB")
|
|
}
|
|
|
|
return nil
|
|
}
|