actionlookup

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2025 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package actionlookup provides GitHub Action version resolution to SHA hashes.

The package handles: - Resolving tags/branches to full commit SHAs via GitHub API - Caching results with TTL - Handling GitHub authentication - Supporting both public and private repositories - Annotated tag resolution

Key Components:

  • Client: Main resolver with configurable HTTP client and caching
  • Options: Functional configuration (token, base URL, cache TTL)

Usage Example:

client := actionlookup.NewClient(
    actionlookup.WithToken("ghp_..."),
    actionlookup.WithCacheTTL(10*time.Minute),
)
sha, err := client.Lookup(context.Background(), "actions/checkout", "v3")

Dependencies: - GitHub API v3 - Standard library net/http for HTTP communication

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsSHA

func IsSHA(s string) bool

IsSHA checks if a string is a valid 40-character Git SHA. Validates length and hexadecimal characters (case-insensitive). Returns true only for exact 40-character SHA-1 hashes.

func SetDefaultClient

func SetDefaultClient(opts ...Option)

SetDefaultClient configures the shared client instance

Types

type Client

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

Client handles resolution of GitHub Action versions to commit SHAs. Provides caching, authentication, and GitHub API communication. Safe for concurrent use by multiple goroutines.

func GetDefaultClient

func GetDefaultClient() *Client

GetDefaultClient returns the singleton instance

func NewClient

func NewClient(opts ...Option) *Client

NewClient creates a new GitHub Action resolver with optional configuration. Defaults to public GitHub API with 5-minute cache TTL and no authentication. Configure using Option arguments:

NewClient(WithToken("ghp_..."), WithCacheTTL(10*time.Minute))

func (*Client) GetFileContent

func (c *Client) GetFileContent(ctx context.Context, owner, repo, path, ref string) (string, error)

GetFileContent fetches the content of a file from a GitHub repository. If ref is empty, the default branch is used.

func (*Client) Lookup

func (c *Client) Lookup(ctx context.Context, action string, version string) (string, error)

Lookup resolves an action reference to a full SHA hash.

Parameters:

  • ctx: Context for cancellation/timeouts
  • action: GitHub Action in "owner/repo" format
  • version: Tag, branch, or partial SHA (40 chars gets verified)

Returns:

  • Full 40-character SHA if found
  • Error for invalid formats, missing refs, or API errors

Resolution order: 1. Check cache 2. Try as tag 3. Try as branch 4. Verify SHA exists (if 40 chars)

type FileContentCache

type FileContentCache interface {
	// GetFileContent retrieves file content from the cache
	// Returns the content, status code, and whether it was found in cache
	GetFileContent(owner, repo, path, sha string) (string, int, bool)

	// SetFileContent stores file content in the cache
	// The statusCode parameter allows caching of 404 responses
	SetFileContent(owner, repo, path, sha string, content string, statusCode int)
}

FileContentCache defines the interface for caching file content

type Option

type Option func(*Client)

Option defines configuration functions for the Client.

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL configures an alternative GitHub API endpoint. Useful for GitHub Enterprise deployments or testing. Example: "https://github.example.com/api/v3"

func WithCacheTTL

func WithCacheTTL(ttl time.Duration) Option

WithCacheTTL sets the duration for cached SHA resolutions. Default: 5 minutes. Set to 0 to disable caching.

func WithFileCache

func WithFileCache(cache FileContentCache) Option

WithFileCache sets the persistent cache for file content.

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

WithHTTPClient provides a custom HTTP client implementation. Use this to configure timeouts, proxies, or custom transport:

&http.Client{Timeout: 30 * time.Second}

func WithRateLimiter

func WithRateLimiter(rateLimiter *RateLimiter) Option

WithRateLimiter configures a custom rate limiter.

func WithRetryOptions

func WithRetryOptions(count int, delay time.Duration) Option

WithRetryOptions configures retry behavior for transient errors.

func WithToken

func WithToken(token string) Option

WithToken sets the GitHub authentication token for private repositories. Token should be in the format "ghp_..." or "github_pat_...".

type RateLimiter

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

RateLimiter implements rate limiting for GitHub API requests.

func NewRateLimiter

func NewRateLimiter(requestsPerHour int, client *http.Client) *RateLimiter

NewRateLimiter creates a new rate limiter with the specified requests per hour limit.

func (*RateLimiter) Do

func (r *RateLimiter) Do(req *http.Request) (*http.Response, error)

Do performs an HTTP request with rate limiting.

func (*RateLimiter) GetRemainingRequests

func (r *RateLimiter) GetRemainingRequests() int

GetRemainingRequests returns the number of remaining requests.

func (*RateLimiter) GetResetTime

func (r *RateLimiter) GetResetTime() time.Time

GetResetTime returns the time when the rate limit will reset.

func (*RateLimiter) Wait

func (r *RateLimiter) Wait(ctx context.Context) error

Wait checks if the rate limit has been exceeded and waits until the reset time if necessary. Returns an error if the context is canceled while waiting.

Jump to

Keyboard shortcuts

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