github

package
v1.7.11 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package github provides a GitHub API client for Smyklot operations.

It supports PR operations (approve, merge, info), comment posting, and emoji reactions through the GitHub REST API v3.

Index

Constants

View Source
const (
	// LabelReactionApprove indicates PR was approved via 👍 reaction
	LabelReactionApprove = "smyklot:reaction-approve"

	// LabelReactionMerge indicates PR was merged via 🚀 reaction
	LabelReactionMerge = "smyklot:reaction-merge"

	// LabelReactionCleanup indicates cleanup was triggered via ❤️ reaction
	LabelReactionCleanup = "smyklot:reaction-cleanup"
)

Variables

View Source
var (
	// ErrEmptyToken is returned when an empty token is provided
	ErrEmptyToken = errors.New("empty GitHub token provided")

	// ErrEmptyComment is returned when an empty comment body is provided
	ErrEmptyComment = errors.New("empty comment body provided")

	// ErrAPIRequest is returned when an API request fails
	ErrAPIRequest = errors.New("GitHub API request failed")

	// ErrResponseParse is returned when parsing a response fails
	ErrResponseParse = errors.New("failed to parse GitHub API response")
)

Sentinel errors for GitHub API operations.

Functions

func NewAPIError

func NewAPIError(op error, statusCode int, method, path string, err error) error

NewAPIError creates a new API error

Types

type APIError

type APIError struct {
	Op         error
	StatusCode int
	Method     string
	Path       string
	Detail     string
}

APIError represents an error from the GitHub API

func (*APIError) Error

func (e *APIError) Error() string

Error returns the error message

func (*APIError) Is

func (e *APIError) Is(target error) bool

Is checks if the target error matches this error type

func (*APIError) Unwrap

func (e *APIError) Unwrap() error

Unwrap returns the wrapped error

type Client

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

Client is a GitHub API client

func NewClient

func NewClient(token, baseURL string) (*Client, error)

NewClient creates a new GitHub API client

The token parameter is required and must not be empty. The baseURL parameter is optional; if empty, the default GitHub API URL will be used.

func (*Client) AddLabel added in v1.0.0

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

AddLabel adds a label to a pull request

func (*Client) AddReaction

func (c *Client) AddReaction(
	ctx context.Context,
	owner, repo string,
	commentID int,
	reaction ReactionType,
) error

AddReaction adds an emoji reaction to a comment

The reaction parameter should be one of the ReactionType constants (ReactionSuccess, ReactionError, ReactionWarning, ReactionEyes).

func (*Client) ApprovePR

func (c *Client) ApprovePR(ctx context.Context, owner, repo string, prNumber int) error

ApprovePR approves a pull request

This creates a review with the APPROVE event.

func (*Client) DeleteComment added in v1.1.0

func (c *Client) DeleteComment(ctx context.Context, owner, repo string, commentID int) error

DeleteComment deletes a comment from a pull request

func (*Client) DismissReview deprecated added in v1.0.0

func (c *Client) DismissReview(ctx context.Context, owner, repo string, prNumber int) error

DismissReview dismisses all approved reviews by the authenticated user.

Deprecated: This method calls GetAuthenticatedUser which fails with GitHub App installation tokens (403 "Resource not accessible by integration"). Use DismissReviewByUsername instead.

func (*Client) DismissReviewByUsername added in v1.7.6

func (c *Client) DismissReviewByUsername(
	ctx context.Context,
	owner, repo string,
	prNumber int,
	username string,
) error

DismissReviewByUsername dismisses all approved reviews by the specified username

This finds all APPROVED reviews by the specified user and dismisses them. Recommended for GitHub App installations to avoid GET /user permission issues.

func (*Client) EnableAutoMerge added in v1.4.0

func (c *Client) EnableAutoMerge(
	ctx context.Context,
	owner, repo string,
	prNumber int,
	method MergeMethod,
) error

EnableAutoMerge enables auto-merge for a pull request

This will automatically merge the PR when all required checks pass. Uses GraphQL API as auto-merge is not available in REST API.

func (*Client) GetAuthenticatedUser deprecated added in v1.1.0

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

GetAuthenticatedUser retrieves the authenticated user's username.

Deprecated: This method calls GET /user which fails with GitHub App installation tokens (403 "Resource not accessible by integration"). Use the configured bot username (RuntimeConfig.BotUsername) instead. For GitHub Apps, the username format is "{app-slug}[bot]" (e.g., "smyklot[bot]").

func (*Client) GetCodeowners added in v1.0.0

func (c *Client) GetCodeowners(ctx context.Context, owner, repo string) (string, error)

GetCodeowners fetches the CODEOWNERS file content from the repository

Returns the decoded content of .github/CODEOWNERS file. Returns empty string (not error) if file doesn't exist (404).

func (*Client) GetCommentReactions added in v1.0.0

func (c *Client) GetCommentReactions(
	ctx context.Context,
	owner, repo string,
	commentID int,
) ([]Reaction, error)

GetCommentReactions retrieves all reactions for a comment

Returns a slice of Reaction structs containing user and reaction type information.

func (*Client) GetLabels added in v1.0.0

func (c *Client) GetLabels(ctx context.Context, owner, repo string, prNumber int) ([]string, error)

GetLabels retrieves all labels from a pull request

func (*Client) GetOpenPRs added in v1.3.0

func (c *Client) GetOpenPRs(ctx context.Context, owner, repo string) ([]map[string]interface{}, error)

GetOpenPRs retrieves all open pull requests in a repository

Returns a slice of PR data including number, title, and state.

func (*Client) GetPRComments added in v1.1.0

func (c *Client) GetPRComments(
	ctx context.Context,
	owner, repo string,
	prNumber int,
) ([]map[string]interface{}, error)

GetPRComments retrieves all comments on a pull request

Returns a slice of comment data including ID, user, and body.

func (*Client) GetPRInfo

func (c *Client) GetPRInfo(ctx context.Context, owner, repo string, prNumber int) (*PRInfo, error)

GetPRInfo retrieves information about a pull request

Returns a PRInfo struct with details about the PR including number, state, mergeable status, author, and approvers.

func (*Client) GetPRReactions added in v1.7.6

func (c *Client) GetPRReactions(ctx context.Context, owner, repo string, prNumber int) ([]Reaction, error)

GetPRReactions retrieves all reactions for a pull request (issue)

Returns a slice of Reaction structs containing user and reaction type information. This gets reactions on the PR description/body, not on comments.

func (*Client) HasWritePermission added in v1.7.0

func (c *Client) HasWritePermission(ctx context.Context, owner, repo, username string) (bool, error)

HasWritePermission checks if the user has write/admin permission to the repository

func (*Client) IsMergeQueueEnabled added in v1.4.0

func (c *Client) IsMergeQueueEnabled(ctx context.Context, owner, repo, branch string) (bool, error)

IsMergeQueueEnabled checks if merge queue is enabled for a branch

func (*Client) MergePR

func (c *Client) MergePR(ctx context.Context, owner, repo string, prNumber int, method MergeMethod) error

MergePR merges a pull request using the specified merge method

Supported merge methods: merge, squash, rebase

func (*Client) PostComment

func (c *Client) PostComment(ctx context.Context, owner, repo string, prNumber int, body string) error

PostComment posts a comment on a pull request

The body parameter must not be empty.

func (*Client) RemoveLabel added in v1.0.0

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

RemoveLabel removes a label from a pull request

func (*Client) RemoveReaction added in v0.3.2

func (c *Client) RemoveReaction(
	ctx context.Context,
	owner, repo string,
	commentID int,
	reaction ReactionType,
) error

RemoveReaction removes an emoji reaction from a comment

The reaction parameter should be one of the ReactionType constants. This retrieves all reactions on the comment and deletes matching ones.

type MergeMethod added in v1.1.0

type MergeMethod string

MergeMethod represents the type of merge method to use

const (
	// MergeMethodMerge creates a merge commit
	MergeMethodMerge MergeMethod = "merge"

	// MergeMethodSquash squashes all commits into one
	MergeMethodSquash MergeMethod = "squash"

	// MergeMethodRebase rebases and merges
	MergeMethodRebase MergeMethod = "rebase"
)

type PRInfo

type PRInfo struct {
	// Number is the PR number
	Number int

	// State is the current state (open, closed, merged)
	State string

	// Mergeable indicates whether the PR can be merged
	Mergeable bool

	// Author is the username of the PR author
	Author string

	// ApprovedBy contains usernames of approvers
	ApprovedBy []string

	// Title is the PR title
	Title string

	// Body is the PR description
	Body string

	// BaseBranch is the base branch (e.g. "main", "master")
	BaseBranch string
}

PRInfo contains information about a pull request

type Reaction added in v1.0.0

type Reaction struct {
	// Type is the reaction type
	Type ReactionType

	// User is the username of the user who reacted
	User string
}

Reaction represents a reaction on a comment

type ReactionType

type ReactionType string

ReactionType represents the type of emoji reaction

const (
	// ReactionSuccess represents success (✅)
	ReactionSuccess ReactionType = "+1"

	// ReactionError represents error (❌)
	ReactionError ReactionType = "-1"

	// ReactionWarning represents warning (⚠️)
	ReactionWarning ReactionType = "confused"

	// ReactionEyes represents acknowledgment (👀)
	ReactionEyes ReactionType = "eyes"

	// ReactionApprove represents approve command (👍)
	ReactionApprove ReactionType = "+1"

	// ReactionMerge represents merge command (🚀)
	ReactionMerge ReactionType = "rocket"

	// ReactionCleanup represents cleanup command (❤️)
	ReactionCleanup ReactionType = "heart"
)

Jump to

Keyboard shortcuts

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