githubhttp

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package githubhttp provides GitHub API-specific HTTP helpers shared across multiple packages (server, proxy, etc.).

Index

Constants

View Source
const GitHubUserAgent = "awmg/1.0"

GitHubUserAgent is the User-Agent header value sent on all GitHub API requests.

Variables

This section is empty.

Functions

func ApplyGitHubAPIHeaders

func ApplyGitHubAPIHeaders(req *http.Request, authHeader string)

ApplyGitHubAPIHeaders sets the standard GitHub API request headers on req. authHeader should be the full Authorization header value (e.g. "token xyz" or "******"). When authHeader is empty no Authorization header is set, which is appropriate when the caller has already decided that no auth is available.

func ComputeRetryAfter

func ComputeRetryAfter(resetAt time.Time) int

ComputeRetryAfter returns the number of seconds a client should wait before retrying after a rate-limit response. It accepts the parsed reset time from ParseRateLimitResetHeader. When resetAt is in the future the delay is clamped to [1, 3600] seconds. When resetAt is zero or already in the past a default of 60 seconds is returned.

func DoGitHubGET

func DoGitHubGET(ctx context.Context, apiBaseURL, path, authHeader string) (*http.Response, error)

DoGitHubGET sends an authenticated GET request to the GitHub API and returns the response. apiBaseURL is the API root (e.g. "https://api.github.com"), path is the request path (e.g. "/repos/owner/repo"), and authHeader is the full Authorization header value (e.g. "token xyz"). The caller is responsible for closing the response body. Request duration is bounded by whichever happens first: ctx cancellation/deadline or the helper client timeout.

func FetchCollaboratorPermission

func FetchCollaboratorPermission(
	ctx context.Context,
	owner, repo, username string,
	fetch func(ctx context.Context, apiPath string) (*http.Response, error),
	logPrintf func(format string, args ...interface{}),
) (interface{}, error)

FetchCollaboratorPermission executes a get_collaborator_permission REST call using the provided fetch function and returns the wrapped MCP text response.

The fetch callback should perform the authenticated HTTP request for the given API path and return the upstream response.

func IsRateLimitText added in v0.4.5

func IsRateLimitText(text string) bool

IsRateLimitText returns true when the message indicates a GitHub rate-limit error.

func ParseCollaboratorPermissionArgs

func ParseCollaboratorPermissionArgs(argsMap map[string]interface{}) (owner, repo, username string, err error)

ParseCollaboratorPermissionArgs extracts and validates the owner, repo, and username fields from an args map for a get_collaborator_permission call. It returns the (possibly partial) values even on error so that callers can include them in diagnostic log messages.

func ParseRateLimitResetFromText added in v0.4.0

func ParseRateLimitResetFromText(text string) time.Time

ParseRateLimitResetFromText attempts to extract a reset timestamp from GitHub rate-limit error text such as "API rate limit exceeded [rate reset in 42s]". Returns zero time when the value cannot be parsed or is 0 seconds.

func ParseRateLimitResetHeader

func ParseRateLimitResetHeader(value string) time.Time

ParseRateLimitResetHeader parses the Unix-timestamp value of the X-RateLimit-Reset HTTP header into a time.Time. Returns zero time when the header value is absent or malformed.

func RateLimitSignal added in v0.4.5

func RateLimitSignal(resp *http.Response) (bool, string, string)

RateLimitSignal reports whether an HTTP response indicates an upstream rate limit. It returns the reset and remaining header values for downstream retry and logging.

func VerifySinkVisibility added in v0.4.0

func VerifySinkVisibility(ctx context.Context, apiBaseURL, nwo, authHeader, configuredVisibility string) (string, bool, error)

VerifySinkVisibility compares the configured sink-visibility against the actual repository visibility from the GitHub API. Only overrides the configured value in the security-critical case: repo is actually public but config doesn't say "public".

Returns:

  • The effective visibility to use (configured value unless overridden to "public")
  • Whether an override occurred
  • Any error encountered (non-fatal — callers should log and use configured value)

func WrapCollaboratorPermission added in v0.4.0

func WrapCollaboratorPermission(
	body []byte,
	owner, repo, username string,
	statusCode int,
	logPrintf func(format string, args ...interface{}),
) interface{}

WrapCollaboratorPermission parses the raw GitHub API response body for a get_collaborator_permission request, logs the resolved permission level for observability, and returns the body wrapped in MCP text-response format.

This helper is shared between the server and proxy packages to eliminate duplicated parse/log/wrap logic. Callers pass their own debug logger's Printf method so that log lines appear under the correct namespace.

Types

type RepoVisibility added in v0.4.0

type RepoVisibility string

RepoVisibility represents the visibility of a GitHub repository.

const (
	// RepoVisibilityPublic indicates a public repository.
	RepoVisibilityPublic RepoVisibility = "public"
	// RepoVisibilityPrivate indicates a private repository.
	RepoVisibilityPrivate RepoVisibility = "private"
	// RepoVisibilityInternal indicates an organization-internal repository.
	RepoVisibilityInternal RepoVisibility = "internal"
)

func FetchRepoVisibility added in v0.4.0

func FetchRepoVisibility(ctx context.Context, apiBaseURL, nwo, authHeader string) (RepoVisibility, error)

FetchRepoVisibility calls GET /repos/{owner}/{repo} and returns the repository's visibility. The nwo parameter should be in "owner/repo" format. apiBaseURL is the API root (e.g. "https://api.github.com") and authHeader is the full Authorization header value (e.g. "token xyz").

Returns RepoVisibilityPublic, RepoVisibilityPrivate, or RepoVisibilityInternal. On API errors (network, 404, 403) returns an error — callers should treat this as non-fatal and fall back to the configured value.

Jump to

Keyboard shortcuts

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