common

package
v1.20250314.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxPathLength defines the maximum allowed path length
	MaxPathLength = 255
)

Variables

This section is empty.

Functions

func AddLabelsToIssue

func AddLabelsToIssue(ctx context.Context, client *github.Client, owner, repo string, number int, labels []string) error

AddLabelsToIssue adds labels to an issue or pull request with retry logic

func AppendToFile

func AppendToFile(path string, data []byte) error

AppendToFile appends content to a file

func AppendToFileString

func AppendToFileString(path string, content string) error

AppendToFileString appends a string to a file

func ContainsAll

func ContainsAll(s string, substrings ...string) bool

ContainsAll checks if a string contains all of the given substrings

func ContainsAny

func ContainsAny(s string, substrings ...string) bool

ContainsAny checks if a string contains any of the given substrings

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies a file from src to dst

func CreatePullRequest

func CreatePullRequest(ctx context.Context, client *github.Client, owner, repo string, pull *github.NewPullRequest) (*github.PullRequest, error)

CreatePullRequest creates a pull request with retry logic

func CreateRef

func CreateRef(ctx context.Context, client *github.Client, owner, repo string, ref *github.Reference) error

CreateRef creates a reference (branch, tag) with retry logic

func ExecuteWithRetry

func ExecuteWithRetry(ctx context.Context, client *github.Client, maxRetries int, retryDelay time.Duration,
	fn func() (*github.Response, error)) error

ExecuteWithRetry executes a GitHub API request with retry logic for rate limiting

func FileExists

func FileExists(path string) bool

FileExists checks if a file exists

func FindFilesWithExtension

func FindFilesWithExtension(dir string, ext string) ([]string, error)

FindFilesWithExtension finds all files with the given extension in a directory

func GetLatestRelease

func GetLatestRelease(ctx context.Context, client *github.Client, owner, repo string) (*github.RepositoryRelease, error)

GetLatestRelease gets the latest release for a repository with retry logic

func GetRef

func GetRef(ctx context.Context, client *github.Client, owner, repo, ref string) (*github.Reference, error)

GetRef gets a reference (branch, tag) with retry logic

func IsDirectory

func IsDirectory(path string) bool

IsDirectory checks if a path is a directory

func IsHexString

func IsHexString(s string) bool

IsHexString checks if a string is a valid hexadecimal string (for commit SHAs)

func IsPathSafe

func IsPathSafe(baseDir, path string) bool

IsPathSafe is a simplified version that just checks if a path is within a base directory

func IsRegularFile

func IsRegularFile(path string) bool

IsRegularFile checks if a path is a regular file

func JoinAndValidatePath

func JoinAndValidatePath(baseDir string, elements ...string) (string, error)

JoinAndValidatePath joins path elements and validates the result

func JoinNonEmpty

func JoinNonEmpty(sep string, parts ...string) string

JoinNonEmpty joins non-empty strings with the given separator

func ModifyLines

func ModifyLines(path string, fn func(line string, lineNum int) string) error

ModifyLines modifies lines in a file using a function

func NewGitHubClient

func NewGitHubClient(options GitHubClientOptions) *github.Client

NewGitHubClient creates a new GitHub client with the given options

func NewGitHubClientWithToken

func NewGitHubClientWithToken(token string) *github.Client

NewGitHubClientWithToken creates a new GitHub client with just a token

func ReadFile

func ReadFile(path string) ([]byte, error)

ReadFile reads a file with default options

func ReadFileString

func ReadFileString(path string) (string, error)

ReadFileString reads a file and returns its contents as a string

func ReadFileWithOptions

func ReadFileWithOptions(path string, options FileOptions) ([]byte, error)

ReadFileWithOptions reads a file with the given options

func ReadLines

func ReadLines(path string) ([]string, error)

ReadLines reads a file and returns its contents as lines

func ReplaceInFile

func ReplaceInFile(path string, oldStr, newStr string) error

ReplaceInFile replaces content in a file

func SafeAbs

func SafeAbs(baseDir, path string) (string, error)

SafeAbs returns the absolute path if it's safe, otherwise returns an error

func SplitAndTrim

func SplitAndTrim(s, sep string) []string

SplitAndTrim splits a string by the given separator and trims each part

func TrimPrefixAny

func TrimPrefixAny(s string, prefixes ...string) string

TrimPrefixAny trims any of the given prefixes from the string

func TrimSuffixAny

func TrimSuffixAny(s string, suffixes ...string) string

TrimSuffixAny trims any of the given suffixes from the string

func ValidatePath

func ValidatePath(baseDir, path string, options PathValidationOptions) error

ValidatePath ensures the path is safe and within the allowed directory baseDir is the root directory that all paths must be contained within path is the path to validate options provides configuration for the validation process

func ValidatePathWithDefaults

func ValidatePathWithDefaults(baseDir, path string) error

ValidatePathWithDefaults validates a path using default options

func WriteFile

func WriteFile(path string, data []byte) error

WriteFile writes data to a file with default options

func WriteFileString

func WriteFileString(path string, content string) error

WriteFileString writes a string to a file

func WriteFileWithOptions

func WriteFileWithOptions(path string, data []byte, options FileOptions) error

WriteFileWithOptions writes data to a file with the given options

func WriteLines

func WriteLines(path string, lines []string) error

WriteLines writes lines to a file

Types

type FileLockManager

type FileLockManager struct {
	// contains filtered or unexported fields
}

FileLockManager manages locks for file operations to prevent concurrent access

func NewFileLockManager

func NewFileLockManager() *FileLockManager

NewFileLockManager creates a new file lock manager

func (*FileLockManager) GetLock

func (m *FileLockManager) GetLock(path string) *sync.Mutex

GetLock gets or creates a lock for the given file path

func (*FileLockManager) LockFile

func (m *FileLockManager) LockFile(path string)

LockFile locks a file for exclusive access

func (*FileLockManager) UnlockFile

func (m *FileLockManager) UnlockFile(path string)

UnlockFile unlocks a file

func (*FileLockManager) WithFileLock

func (m *FileLockManager) WithFileLock(path string, fn func() error) error

WithFileLock executes a function with a lock on the given file

type FileOptions

type FileOptions struct {
	// CreateDirs if true, creates parent directories if they don't exist
	CreateDirs bool
	// Mode is the file mode to use when creating files
	Mode os.FileMode
	// BaseDir is the base directory for path validation
	BaseDir string
	// ValidateOptions are the options for path validation
	ValidateOptions PathValidationOptions
}

FileOptions provides options for file operations

func DefaultFileOptions

func DefaultFileOptions() FileOptions

DefaultFileOptions returns the default options for file operations

type GitHubClientOptions

type GitHubClientOptions struct {
	// Token is the GitHub API token
	Token string
	// BaseURL is the base URL for the GitHub API (optional, for GitHub Enterprise)
	BaseURL string
	// Timeout is the default timeout for API requests
	Timeout time.Duration
	// RetryCount is the number of times to retry failed requests
	RetryCount int
	// RetryDelay is the delay between retries
	RetryDelay time.Duration
}

GitHubClientOptions provides configuration options for GitHub client creation

func DefaultGitHubClientOptions

func DefaultGitHubClientOptions() GitHubClientOptions

DefaultGitHubClientOptions returns the default options for GitHub client creation

type PathValidationOptions

type PathValidationOptions struct {
	// RequireRegularFile if true, validates that the path points to a regular file
	RequireRegularFile bool
	// AllowNonExistent if true, allows paths that don't exist yet
	AllowNonExistent bool
	// CheckSymlinks if true, validates that symlinks don't point outside the base directory
	CheckSymlinks bool
	// MaxPathLength specifies the maximum allowed path length (defaults to 255 if not set)
	MaxPathLength int
}

PathValidationOptions provides configuration options for path validation

func DefaultPathValidationOptions

func DefaultPathValidationOptions() PathValidationOptions

DefaultPathValidationOptions returns the default options for path validation

type RateLimitHandler

type RateLimitHandler struct {
	// contains filtered or unexported fields
}

RateLimitHandler provides rate limit handling for GitHub API requests

func NewRateLimitHandler

func NewRateLimitHandler(client *github.Client, maxRetries int, retryDelay time.Duration) *RateLimitHandler

NewRateLimitHandler creates a new rate limit handler for the given client

func (*RateLimitHandler) GetRateLimitInfo

func (h *RateLimitHandler) GetRateLimitInfo() string

GetRateLimitInfo returns information about the current rate limit

func (*RateLimitHandler) HandleRateLimit

func (h *RateLimitHandler) HandleRateLimit(resp *github.Response, err error) bool

HandleRateLimit handles rate limiting for GitHub API requests It returns true if the request should be retried, and false otherwise

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL