Documentation
¶
Overview ¶
Package repo provides GitHub repository operations.
Index ¶
- Variables
- func BranchExists(ctx context.Context, gh *github.Client, owner, repo, branch string) (bool, error)
- func CreateBranch(ctx context.Context, gh *github.Client, owner, repo, branch, baseSHA string) error
- func CreateCommit(ctx context.Context, gh *github.Client, owner, repo, branch, message string, ...) (string, error)
- func DeleteBranch(ctx context.Context, gh *github.Client, owner, repo, branch string) error
- func DownloadDirectory(ctx context.Context, gh *github.Client, owner, repo, dirPath string, ...) (map[string][]byte, error)
- func EnsureFork(ctx context.Context, gh *github.Client, ...) (string, string, error)
- func FileExists(ctx context.Context, gh *github.Client, owner, repo, filePath string, ...) (bool, error)
- func GetBranchSHA(ctx context.Context, gh *github.Client, owner, repo, branch string) (string, error)
- func GetContributorStats(ctx context.Context, gh *github.Client, owner, repo, username string) (*github.ContributorStats, error)
- func GetDefaultBranch(ctx context.Context, gh *github.Client, owner, repo string) (string, error)
- func GetFileContent(ctx context.Context, gh *github.Client, owner, repo, filePath string, ...) ([]byte, error)
- func GetFileContentString(ctx context.Context, gh *github.Client, owner, repo, filePath string, ...) (string, error)
- func GetMultipleFiles(ctx context.Context, gh *github.Client, owner, repo string, filePaths []string, ...) (map[string][]byte, error)
- func GetRawFileURL(owner, repo, ref, filePath string) string
- func GetRepo(ctx context.Context, gh *github.Client, owner, repo string) (*github.Repository, error)
- func JoinPath(segments ...string) string
- func ListContributorStats(ctx context.Context, gh *github.Client, owner, repo string) ([]*github.ContributorStats, error)
- func ListOrgRepos(ctx context.Context, gh *github.Client, org string) ([]*github.Repository, error)
- func ListUserRepos(ctx context.Context, gh *github.Client, user string) ([]*github.Repository, error)
- func ParseRepoName(fullName string) (owner, repo string, err error)
- func ParseRepoURL(repoURL string) (owner, repo string, err error)
- type Batch
- type BatchError
- type BatchOperation
- type BatchOperationType
- type BatchOption
- type BranchError
- type CommitError
- type ContentOptions
- type ContributorSummary
- type FileContent
- type FileInfo
- type ForkError
Constants ¶
This section is empty.
Variables ¶
var ErrBatchCommitted = errors.New("batch already committed")
ErrBatchCommitted is returned when operations are attempted on a committed batch.
var ErrEmptyPath = errors.New("empty path not allowed")
ErrEmptyPath is returned when an empty path is provided.
Functions ¶
func BranchExists ¶
BranchExists checks if a branch exists.
func CreateBranch ¶
func CreateBranch(ctx context.Context, gh *github.Client, owner, repo, branch, baseSHA string) error
CreateBranch creates a new branch from the given base SHA.
func CreateCommit ¶
func CreateCommit(ctx context.Context, gh *github.Client, owner, repo, branch, message string, files []FileContent) (string, error)
CreateCommit creates a commit with the given files using the Git tree API.
func DeleteBranch ¶
DeleteBranch deletes a branch.
func DownloadDirectory ¶ added in v0.10.0
func DownloadDirectory(ctx context.Context, gh *github.Client, owner, repo, dirPath string, opts *ContentOptions) (map[string][]byte, error)
DownloadDirectory downloads all files from a directory recursively. Returns a map of relative file path to content.
func EnsureFork ¶
func EnsureFork(ctx context.Context, gh *github.Client, upstreamOwner, upstreamRepo, forkOwner string) (string, string, error)
EnsureFork ensures a fork exists for the given repository. Returns the fork owner and repo name.
func FileExists ¶ added in v0.10.0
func FileExists(ctx context.Context, gh *github.Client, owner, repo, filePath string, opts *ContentOptions) (bool, error)
FileExists checks if a file exists in a repository.
func GetBranchSHA ¶
func GetBranchSHA(ctx context.Context, gh *github.Client, owner, repo, branch string) (string, error)
GetBranchSHA returns the SHA of the given branch.
func GetContributorStats ¶ added in v0.8.0
func GetContributorStats(ctx context.Context, gh *github.Client, owner, repo, username string) (*github.ContributorStats, error)
GetContributorStats returns statistics for a specific contributor in a repository. Returns nil if the user has not contributed to the repository.
func GetDefaultBranch ¶
GetDefaultBranch returns the default branch of a repository.
func GetFileContent ¶ added in v0.10.0
func GetFileContent(ctx context.Context, gh *github.Client, owner, repo, filePath string, opts *ContentOptions) ([]byte, error)
GetFileContent fetches the content of a single file from a repository. Returns the decoded file content as bytes.
func GetFileContentString ¶ added in v0.10.0
func GetFileContentString(ctx context.Context, gh *github.Client, owner, repo, filePath string, opts *ContentOptions) (string, error)
GetFileContentString fetches the content of a single file as a string.
func GetMultipleFiles ¶ added in v0.10.0
func GetMultipleFiles(ctx context.Context, gh *github.Client, owner, repo string, filePaths []string, opts *ContentOptions) (map[string][]byte, error)
GetMultipleFiles fetches multiple files from a repository. Returns a map of file path to content.
func GetRawFileURL ¶ added in v0.10.0
GetRawFileURL returns the raw content URL for a file. This URL can be used to download the file without authentication for public repos.
func GetRepo ¶
func GetRepo(ctx context.Context, gh *github.Client, owner, repo string) (*github.Repository, error)
GetRepo retrieves a repository by owner and name.
func ListContributorStats ¶ added in v0.8.0
func ListContributorStats(ctx context.Context, gh *github.Client, owner, repo string) ([]*github.ContributorStats, error)
ListContributorStats returns contributor statistics for a repository. This wraps the GitHub REST API endpoint: GET /repos/{owner}/{repo}/stats/contributors
Note: GitHub may return 202 Accepted while computing statistics. This function automatically retries with exponential backoff until stats are available or the context is cancelled.
func ListOrgRepos ¶
ListOrgRepos lists all repositories for an organization with pagination.
func ListUserRepos ¶
func ListUserRepos(ctx context.Context, gh *github.Client, user string) ([]*github.Repository, error)
ListUserRepos lists all repositories for a user with pagination.
func ParseRepoName ¶
ParseRepoName splits a full repo name (owner/repo) into owner and repo.
func ParseRepoURL ¶ added in v0.10.0
ParseRepoURL parses a GitHub repository URL and returns owner and repo. Supports formats:
- https://github.com/owner/repo
- https://github.com/owner/repo.git
- git@github.com:owner/repo.git
- owner/repo
Types ¶
type Batch ¶
type Batch struct {
// contains filtered or unexported fields
}
Batch accumulates multiple file operations to be committed atomically. Use NewBatch to create a batch, then call Write/Delete to queue operations, and finally Commit to apply all changes in a single commit.
func NewBatch ¶
func NewBatch(ctx context.Context, gh *github.Client, owner, repo, branch, message string, opts ...BatchOption) (*Batch, error)
NewBatch creates a new batch for accumulating file operations. The message is used as the commit message when Commit is called.
func (*Batch) Commit ¶
Commit applies all queued operations in a single commit. This uses the Git Data API (Trees and Commits) to create an atomic commit.
The process:
- Get the current commit SHA from the branch reference
- Get the current tree SHA from that commit
- Create blobs for all new/updated file contents
- Create a new tree with the changes
- Create a new commit pointing to the new tree
- Update the branch reference to the new commit
Returns the new commit SHA on success.
func (*Batch) Delete ¶
Delete queues a file delete operation. The file will be deleted when Commit is called. If the file doesn't exist at commit time, it is ignored (no error).
func (*Batch) Operations ¶
func (b *Batch) Operations() []BatchOperation
Operations returns a copy of the queued operations.
type BatchError ¶
BatchError indicates a failure during batch operations.
func (*BatchError) Error ¶
func (e *BatchError) Error() string
func (*BatchError) Unwrap ¶
func (e *BatchError) Unwrap() error
type BatchOperation ¶
type BatchOperation struct {
Type BatchOperationType
Path string
Content []byte
}
BatchOperation represents a single operation in a batch.
type BatchOperationType ¶
type BatchOperationType int
BatchOperationType indicates the type of batch operation.
const ( // BatchOpWrite represents a write (create/update) operation. BatchOpWrite BatchOperationType = iota // BatchOpDelete represents a delete operation. BatchOpDelete )
type BatchOption ¶
type BatchOption func(*Batch)
BatchOption configures a Batch.
func WithCommitAuthor ¶
func WithCommitAuthor(name, email string) BatchOption
WithCommitAuthor sets the commit author for the batch.
type BranchError ¶
BranchError indicates a failure to create or update a branch.
func (*BranchError) Error ¶
func (e *BranchError) Error() string
func (*BranchError) Unwrap ¶
func (e *BranchError) Unwrap() error
type CommitError ¶
CommitError indicates a failure to create a commit.
func (*CommitError) Error ¶
func (e *CommitError) Error() string
func (*CommitError) Unwrap ¶
func (e *CommitError) Unwrap() error
type ContentOptions ¶ added in v0.10.0
type ContentOptions struct {
// Ref is the git reference (branch, tag, or commit SHA). Default: default branch.
Ref string
}
ContentOptions specifies options for fetching repository content.
type ContributorSummary ¶ added in v0.8.0
type ContributorSummary struct {
Username string
TotalCommits int
TotalAdditions int
TotalDeletions int
FirstCommit time.Time
LastCommit time.Time
}
ContributorSummary provides a simplified view of contributor statistics.
func GetContributorSummary ¶ added in v0.8.0
func GetContributorSummary(ctx context.Context, gh *github.Client, owner, repo, username string) (*ContributorSummary, error)
GetContributorSummary returns a summarized view of a contributor's statistics.
type FileContent ¶
FileContent represents a file to be committed.
func ReadLocalFiles ¶
func ReadLocalFiles(dir, prefix string) ([]FileContent, error)
ReadLocalFiles reads all files from a local directory recursively. The prefix is prepended to relative paths for the destination.
type FileInfo ¶ added in v0.10.0
type FileInfo struct {
Path string
Name string
Type string // "file" or "dir"
Size int
SHA string
DownloadURL string
}
FileInfo represents information about a file or directory in a repository.
func ListDirectory ¶ added in v0.10.0
func ListDirectory(ctx context.Context, gh *github.Client, owner, repo, dirPath string, opts *ContentOptions) ([]FileInfo, error)
ListDirectory lists the contents of a directory in a repository.
func ListDirectoryRecursive ¶ added in v0.10.0
func ListDirectoryRecursive(ctx context.Context, gh *github.Client, owner, repo, dirPath string, opts *ContentOptions) ([]FileInfo, error)
ListDirectoryRecursive lists all files in a directory recursively.