mirror of
https://github.com/ivuorinen/gh-action-readme.git
synced 2026-01-26 11:14:04 +00:00
* chore(lint): added nlreturn, run linting * chore(lint): replace some fmt.Sprintf calls * chore(lint): replace fmt.Sprintf with strconv * chore(lint): add goconst, use http lib for status codes, and methods * chore(lint): use errors lib, errCodes from internal/errors * chore(lint): dupl, thelper and usetesting * chore(lint): fmt.Errorf %v to %w, more linters * chore(lint): paralleltest, where possible * perf(test): optimize test performance by 78% - Implement shared binary building with package-level cache to eliminate redundant builds - Add strategic parallelization to 15+ tests while preserving environment variable isolation - Implement thread-safe fixture caching with RWMutex to reduce I/O operations - Remove unnecessary working directory changes by leveraging embedded templates - Add embedded template system with go:embed directive for reliable template resolution - Fix linting issues: rename sharedBinaryError to errSharedBinary, add nolint directive Performance improvements: - Total test execution time: 12+ seconds → 2.7 seconds (78% faster) - Binary build overhead: 14+ separate builds → 1 shared build (93% reduction) - Parallel execution: Limited → 15+ concurrent tests (60-70% better CPU usage) - I/O operations: 66+ fixture reads → cached with sync.RWMutex (50% reduction) All tests maintain 100% success rate and coverage while running nearly 4x faster.
70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package validation
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"regexp"
|
|
|
|
"github.com/ivuorinen/gh-action-readme/internal/git"
|
|
)
|
|
|
|
// IsCommitSHA checks if a version string is a commit SHA.
|
|
func IsCommitSHA(version string) bool {
|
|
// Check if it's a 40-character hex string (full SHA) or 7+ character hex (short SHA)
|
|
re := regexp.MustCompile(`^[a-f0-9]{7,40}$`)
|
|
|
|
return len(version) >= 7 && re.MatchString(version)
|
|
}
|
|
|
|
// IsSemanticVersion checks if a version string follows semantic versioning.
|
|
func IsSemanticVersion(version string) bool {
|
|
// Check for vX.Y.Z format (requires major.minor.patch)
|
|
re := regexp.MustCompile(`^v?\d+\.\d+\.\d+(-[a-zA-Z0-9.-]+)?(\+[a-zA-Z0-9.-]+)?$`)
|
|
|
|
return re.MatchString(version)
|
|
}
|
|
|
|
// IsVersionPinned checks if a semantic version is pinned to a specific version.
|
|
func IsVersionPinned(version string) bool {
|
|
// Consider it pinned if it specifies patch version (v1.2.3) or is a full commit SHA
|
|
return IsSemanticVersion(version) || (IsCommitSHA(version) && len(version) == 40)
|
|
}
|
|
|
|
// ValidateGitBranch checks if a branch exists in the given repository.
|
|
func ValidateGitBranch(repoRoot, branch string) bool {
|
|
cmd := exec.Command(
|
|
"git",
|
|
"show-ref",
|
|
"--verify",
|
|
"--quiet",
|
|
"refs/heads/"+branch,
|
|
) // #nosec G204 -- branch name validated by git
|
|
cmd.Dir = repoRoot
|
|
|
|
return cmd.Run() == nil
|
|
}
|
|
|
|
// ValidateActionYMLPath validates that a path points to a valid action.yml file.
|
|
func ValidateActionYMLPath(path string) error {
|
|
// Check if file exists
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
|
|
// Check if it's an action.yml or action.yaml file
|
|
filename := filepath.Base(path)
|
|
if filename != "action.yml" && filename != "action.yaml" {
|
|
return os.ErrInvalid
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// IsGitRepository checks if the given path is within a git repository.
|
|
func IsGitRepository(path string) bool {
|
|
_, err := git.FindRepositoryRoot(path)
|
|
|
|
return err == nil
|
|
}
|