github

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package github provides a wrapper around the GitHub API for IssueOps operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeploymentStateEmoji

func DeploymentStateEmoji(state DeploymentState) string

DeploymentStateEmoji returns an emoji for a deployment state.

func GetCommitMessages

func GetCommitMessages(commits []Commit) []string

GetCommitMessages extracts just the commit messages from a list of commits.

func GetRateLimitReset

func GetRateLimitReset(err error) time.Time

GetRateLimitReset returns the time when the rate limit resets.

func IsForbidden

func IsForbidden(err error) bool

IsForbidden returns true if the error is a 403 Forbidden error.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound returns true if the error is a 404 Not Found error.

func IsRateLimitError

func IsRateLimitError(err error) bool

IsRateLimitError checks if an error is a GitHub rate limit error.

func WrapWithRetry

func WrapWithRetry[T any](ctx context.Context, config RetryConfig, operation func() (T, error)) (T, error)

WrapWithRetry wraps an operation with retry logic for rate limits. This is useful for operations that don't go through the HTTP transport.

Types

type Branch

type Branch struct {
	Name      string `json:"name"`
	SHA       string `json:"sha"`
	Protected bool   `json:"protected"`
}

Branch represents a Git branch.

type Client

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

Client wraps the GitHub API client with IssueOps-specific operations.

func NewClient

func NewClient(ctx context.Context) (*Client, error)

NewClient creates a new GitHub client from environment variables.

func NewClientWithToken

func NewClientWithToken(ctx context.Context, token, owner, repo string) (*Client, error)

NewClientWithToken creates a new GitHub client with explicit parameters.

func NewClientWithTokenAndRetry

func NewClientWithTokenAndRetry(ctx context.Context, token, owner, repo string, retryConfig RetryConfig) (*Client, error)

NewClientWithTokenAndRetry creates a new GitHub client with explicit parameters and retry config.

func (*Client) AddLabelToPR

func (c *Client) AddLabelToPR(ctx context.Context, prNumber int, labels []string) error

AddLabelToPR adds a label to a PR.

func (*Client) AddLabels

func (c *Client) AddLabels(ctx context.Context, number int, labels []string) error

AddLabels adds labels to an issue.

func (*Client) AddReaction

func (c *Client) AddReaction(ctx context.Context, commentID int64, reaction string) error

AddReaction adds a reaction to an issue comment.

func (*Client) AddSubIssue

func (c *Client) AddSubIssue(ctx context.Context, parentIssueNumber int, subIssueID int64) error

AddSubIssue links an existing issue as a sub-issue of a parent issue. This uses the GitHub Sub-Issues API: POST /repos/{owner}/{repo}/issues/{issue_number}/sub_issues Available since December 2024.

func (*Client) CloseIssue

func (c *Client) CloseIssue(ctx context.Context, number int) error

CloseIssue closes an issue.

func (*Client) CloseMilestone

func (c *Client) CloseMilestone(ctx context.Context, number int) error

CloseMilestone closes a milestone.

func (*Client) CompareCommits

func (c *Client) CompareCommits(ctx context.Context, base, head string) ([]Commit, error)

CompareCommits compares two refs and returns the commits between them. base is the older ref, head is the newer ref.

func (*Client) CreateApprovalSubIssue

func (c *Client) CreateApprovalSubIssue(ctx context.Context, parentIssueNumber int, title, body string, labels, assignees []string) (*Issue, error)

CreateApprovalSubIssue creates a new issue and links it as a sub-issue of a parent. This is a convenience method that combines CreateIssue + AddSubIssue.

func (*Client) CreateBranch

func (c *Client) CreateBranch(ctx context.Context, branchName, sourceRef string) (*Branch, error)

CreateBranch creates a new branch from a source ref.

func (*Client) CreateComment

func (c *Client) CreateComment(ctx context.Context, number int, body string) error

CreateComment adds a comment to an issue.

func (*Client) CreateDeployment

func (c *Client) CreateDeployment(ctx context.Context, opts CreateDeploymentOptions) (*Deployment, error)

CreateDeployment creates a new deployment.

func (*Client) CreateDeploymentStatus

func (c *Client) CreateDeploymentStatus(ctx context.Context, opts CreateDeploymentStatusOptions) (*DeploymentStatus, error)

CreateDeploymentStatus creates a status for a deployment.

func (*Client) CreateIssue

func (c *Client) CreateIssue(ctx context.Context, opts CreateIssueOptions) (*Issue, error)

CreateIssue creates a new issue in the repository.

func (*Client) CreateLabel

func (c *Client) CreateLabel(ctx context.Context, name, color, description string) error

CreateLabel creates a label if it doesn't exist.

func (*Client) CreateMilestone

func (c *Client) CreateMilestone(ctx context.Context, title, description string) (*Milestone, error)

CreateMilestone creates a new milestone.

func (*Client) CreateTag

func (c *Client) CreateTag(ctx context.Context, opts CreateTagOptions) (*Tag, error)

CreateTag creates a new annotated tag.

func (*Client) DeleteBranch

func (c *Client) DeleteBranch(ctx context.Context, branchName string) error

DeleteBranch deletes a branch.

func (*Client) DeleteTag

func (c *Client) DeleteTag(ctx context.Context, name string) error

DeleteTag deletes a tag by name.

func (*Client) ExpandTeamToUsers

func (c *Client) ExpandTeamToUsers(ctx context.Context, approvers []string) ([]string, error)

ExpandTeamToUsers converts team references to individual user logins. For non-team references (plain usernames), returns them as-is.

func (*Client) GetBranch

func (c *Client) GetBranch(ctx context.Context, branchName string) (*Branch, error)

GetBranch gets information about a branch.

func (*Client) GetCommitsBetweenBranches

func (c *Client) GetCommitsBetweenBranches(ctx context.Context, baseBranch, headBranch string) ([]Commit, error)

GetCommitsBetweenBranches returns commits in head branch that are not in base branch.

func (*Client) GetCommitsBetweenTags

func (c *Client) GetCommitsBetweenTags(ctx context.Context, oldTag, newTag string) ([]Commit, error)

GetCommitsBetweenTags gets all commits between two tags.

func (*Client) GetCommitsSinceTag

func (c *Client) GetCommitsSinceTag(ctx context.Context, tag, branch string) ([]Commit, error)

GetCommitsSinceTag gets all commits since a tag on a branch.

func (*Client) GetDeployment

func (c *Client) GetDeployment(ctx context.Context, deploymentID int64) (*Deployment, error)

GetDeployment retrieves a deployment by ID.

func (*Client) GetEnvironmentDeploymentStatus

func (c *Client) GetEnvironmentDeploymentStatus(ctx context.Context, environment string) (*Deployment, *DeploymentStatus, error)

GetEnvironmentDeploymentStatus returns the status of the latest deployment to an environment.

func (*Client) GetFileContents

func (c *Client) GetFileContents(ctx context.Context, owner, repo, path string) ([]byte, error)

GetFileContents fetches the contents of a file from a repository.

func (*Client) GetFileContentsFromRepo

func (c *Client) GetFileContentsFromRepo(ctx context.Context, repoFullName, path string) ([]byte, error)

GetFileContentsFromRepo fetches file contents from any repo (for external config). The repo parameter should be in "owner/repo" format.

func (*Client) GetIssue

func (c *Client) GetIssue(ctx context.Context, number int) (*Issue, error)

GetIssue retrieves an issue by number.

func (*Client) GetIssueByNumber

func (c *Client) GetIssueByNumber(ctx context.Context, number int) (*github.Issue, *github.Response, error)

GetIssueByNumber retrieves the full GitHub issue object by number. Returns the underlying github.Issue which includes the ID field.

func (*Client) GetLatestDeploymentStatus

func (c *Client) GetLatestDeploymentStatus(ctx context.Context, deploymentID int64) (*DeploymentStatus, error)

GetLatestDeploymentStatus gets the latest status for a deployment.

func (*Client) GetLatestTag

func (c *Client) GetLatestTag(ctx context.Context) (string, error)

GetLatestTag returns the latest semver tag.

func (*Client) GetLatestTagWithPrefix

func (c *Client) GetLatestTagWithPrefix(ctx context.Context, prefix string) (string, error)

GetLatestTagWithPrefix returns the latest tag matching a specific prefix. This is useful for environment-specific tags like "dev-v1.0.0", "staging-v1.0.0".

func (*Client) GetMergedPRsBetween

func (c *Client) GetMergedPRsBetween(ctx context.Context, base, head string) ([]PullRequest, error)

GetMergedPRsBetween returns PRs merged between two refs. Note: Requires pull-requests: read permission in the workflow.

func (*Client) GetMilestoneByTitle

func (c *Client) GetMilestoneByTitle(ctx context.Context, title string) (*Milestone, error)

GetMilestoneByTitle finds a milestone by its title.

func (*Client) GetPRsByLabel

func (c *Client) GetPRsByLabel(ctx context.Context, label string) ([]PullRequest, error)

GetPRsByLabel returns all merged PRs with a specific label.

func (*Client) GetPRsByMilestone

func (c *Client) GetPRsByMilestone(ctx context.Context, milestoneNumber int) ([]PullRequest, error)

GetPRsByMilestone returns all PRs assigned to a milestone.

func (*Client) GetPRsMergedToBranch

func (c *Client) GetPRsMergedToBranch(ctx context.Context, branchName string) ([]PullRequest, error)

GetPRsMergedToBranch returns PRs merged to a specific branch.

func (*Client) GetParentIssue

func (c *Client) GetParentIssue(ctx context.Context, subIssueNumber int) (*github.Issue, error)

GetParentIssue retrieves the parent issue for a given sub-issue. GET /repos/{owner}/{repo}/issues/{issue_number}/parent

func (*Client) GetPreviousTag

func (c *Client) GetPreviousTag(ctx context.Context, currentTag string) (string, error)

GetPreviousTag returns the tag before the given tag.

func (*Client) GetTeamMembers

func (c *Client) GetTeamMembers(ctx context.Context, team string) ([]TeamMember, error)

GetTeamMembers retrieves all members of a team. The team can be specified as "team-slug" or "org/team-slug".

func (*Client) GitHubClient

func (c *Client) GitHubClient() *github.Client

GitHubClient returns the underlying go-github client for advanced operations.

func (*Client) IsSubIssue

func (c *Client) IsSubIssue(ctx context.Context, issueNumber int) (bool, error)

IsSubIssue checks if an issue is a sub-issue (has a parent).

func (*Client) IsUserInTeam

func (c *Client) IsUserInTeam(ctx context.Context, team, user string) (bool, error)

IsUserInTeam checks if a user is a member of a team.

func (*Client) ListComments

func (c *Client) ListComments(ctx context.Context, number int) ([]IssueComment, error)

ListComments retrieves all comments on an issue.

func (*Client) ListDeployments

func (c *Client) ListDeployments(ctx context.Context, environment string, ref string) ([]Deployment, error)

ListDeployments lists deployments for the repository.

func (*Client) ListSubIssues

func (c *Client) ListSubIssues(ctx context.Context, parentIssueNumber int) ([]*github.Issue, error)

ListSubIssues retrieves all sub-issues for a given parent issue. GET /repos/{owner}/{repo}/issues/{issue_number}/sub_issues

func (*Client) ListTags

func (c *Client) ListTags(ctx context.Context, limit int) ([]string, error)

ListTags lists tags in the repository, ordered by creation date (newest first).

func (*Client) Owner

func (c *Client) Owner() string

Owner returns the repository owner.

func (*Client) RemoveLabelFromPR

func (c *Client) RemoveLabelFromPR(ctx context.Context, prNumber int, label string) error

RemoveLabelFromPR removes a label from a PR.

func (*Client) RemoveSubIssue

func (c *Client) RemoveSubIssue(ctx context.Context, parentIssueNumber int, subIssueID int64) error

RemoveSubIssue removes a sub-issue from its parent. DELETE /repos/{owner}/{repo}/issues/{issue_number}/sub_issue

func (*Client) ReopenIssue

func (c *Client) ReopenIssue(ctx context.Context, number int) error

ReopenIssue reopens a closed issue.

func (*Client) Repo

func (c *Client) Repo() string

Repo returns the repository name.

func (*Client) TagExists

func (c *Client) TagExists(ctx context.Context, name string) (bool, error)

TagExists checks if a tag already exists.

func (*Client) UpdateIssueBody

func (c *Client) UpdateIssueBody(ctx context.Context, number int, body string) error

UpdateIssueBody updates the body of an issue.

func (*Client) UpdateIssueTitle

func (c *Client) UpdateIssueTitle(ctx context.Context, number int, title string) error

UpdateIssueTitle updates the title of an issue.

type Commit

type Commit struct {
	SHA     string `json:"sha"`
	Message string `json:"message"`
	Author  string `json:"author"`
	Date    string `json:"date"`
	URL     string `json:"url"`
}

Commit represents a Git commit.

type CreateDeploymentOptions

type CreateDeploymentOptions struct {
	Ref              string   // Git ref (branch, tag, SHA)
	Environment      string   // Target environment (e.g., "production", "staging")
	Description      string   // Description of the deployment
	AutoMerge        bool     // Auto-merge the default branch into the ref
	RequiredContexts []string // Required status contexts (empty = no required contexts)
	Payload          string   // JSON payload with extra information
	TransientEnv     bool     // Mark environment as transient (will be destroyed)
	ProductionEnv    bool     // Mark as production environment
}

CreateDeploymentOptions contains options for creating a deployment.

type CreateDeploymentStatusOptions

type CreateDeploymentStatusOptions struct {
	DeploymentID   int64
	State          DeploymentState
	Description    string
	LogURL         string // URL to deployment logs or approval issue
	Environment    string
	EnvironmentURL string // URL to the deployed environment
	AutoInactive   bool   // Mark previous deployments as inactive
}

CreateDeploymentStatusOptions contains options for creating a deployment status.

type CreateIssueOptions

type CreateIssueOptions struct {
	Title     string
	Body      string
	Labels    []string
	Assignees []string
}

CreateIssueOptions contains options for creating an issue.

type CreateTagOptions

type CreateTagOptions struct {
	Name    string // Tag name (e.g., "v1.2.3")
	SHA     string // Commit SHA to tag (empty = use default branch HEAD)
	Message string // Tag message
}

CreateTagOptions contains options for creating a tag.

type Deployment

type Deployment struct {
	ID          int64  `json:"id"`
	SHA         string `json:"sha"`
	Ref         string `json:"ref"`
	Environment string `json:"environment"`
	Description string `json:"description"`
	Creator     string `json:"creator"`
	URL         string `json:"url"`
}

Deployment represents a GitHub deployment.

type DeploymentState

type DeploymentState string

DeploymentState represents the state of a deployment.

const (
	DeploymentStatePending    DeploymentState = "pending"
	DeploymentStateQueued     DeploymentState = "queued"
	DeploymentStateInProgress DeploymentState = "in_progress"
	DeploymentStateSuccess    DeploymentState = "success"
	DeploymentStateFailure    DeploymentState = "failure"
	DeploymentStateError      DeploymentState = "error"
	DeploymentStateInactive   DeploymentState = "inactive"
)

type DeploymentStatus

type DeploymentStatus struct {
	ID          int64           `json:"id"`
	State       DeploymentState `json:"state"`
	Description string          `json:"description"`
	LogURL      string          `json:"log_url"`
	Environment string          `json:"environment"`
}

DeploymentStatus represents the status of a deployment.

type Issue

type Issue struct {
	Number  int
	Title   string
	Body    string
	State   string
	HTMLURL string
	Labels  []string
}

Issue represents a GitHub issue.

type IssueComment

type IssueComment struct {
	ID        int64
	User      string
	Body      string
	CreatedAt string
}

IssueComment represents a comment on a GitHub issue.

type Milestone

type Milestone struct {
	Number       int    `json:"number"`
	Title        string `json:"title"`
	Description  string `json:"description"`
	State        string `json:"state"` // "open" or "closed"
	OpenIssues   int    `json:"open_issues"`
	ClosedIssues int    `json:"closed_issues"`
	URL          string `json:"url"`
}

Milestone represents a GitHub milestone.

type PRWithLabels

type PRWithLabels struct {
	PullRequest
	Labels []string `json:"labels"`
}

PRWithLabels represents a PR with its labels for filtering.

type PullRequest

type PullRequest struct {
	Number   int    `json:"number"`
	Title    string `json:"title"`
	Author   string `json:"author"`
	URL      string `json:"url"`
	MergedAt string `json:"merged_at,omitempty"`
	MergeSHA string `json:"merge_sha,omitempty"`
}

PullRequest represents a GitHub pull request.

type RetryConfig

type RetryConfig struct {
	// MaxRetries is the maximum number of retry attempts.
	MaxRetries int
	// InitialBackoff is the initial backoff duration.
	InitialBackoff time.Duration
	// MaxBackoff is the maximum backoff duration.
	MaxBackoff time.Duration
	// BackoffMultiplier is multiplied to backoff after each retry.
	BackoffMultiplier float64
}

RetryConfig configures retry behavior for API calls.

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns sensible defaults for retry configuration.

type SubIssueRequest

type SubIssueRequest struct {
	SubIssueID int64 `json:"sub_issue_id"`
}

SubIssueRequest represents a request to add a sub-issue to a parent issue.

type SubIssueResponse

type SubIssueResponse struct {
	ID        int64          `json:"id"`
	Number    int            `json:"number"`
	Title     string         `json:"title"`
	State     string         `json:"state"`
	HTMLURL   string         `json:"html_url"`
	Assignees []*github.User `json:"assignees,omitempty"`
}

SubIssueResponse represents the response from sub-issue operations.

type SubIssueState

type SubIssueState struct {
	IssueNumber int    `json:"issue_number"`
	Stage       string `json:"stage"`
	Status      string `json:"status"` // "pending", "approved", "denied"
	ClosedBy    string `json:"closed_by,omitempty"`
	ClosedAt    string `json:"closed_at,omitempty"`
}

SubIssueState represents the state of a sub-issue for approval tracking.

type Tag

type Tag struct {
	Name   string
	SHA    string
	Tagger string
}

Tag represents a Git tag.

type TeamMember

type TeamMember struct {
	Login string
}

TeamMember represents a member of a GitHub team.

Jump to

Keyboard shortcuts

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