github_handler

package module
v1.3.8 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

README

GitHub Handler Library

This library provides a utility for interacting with the GitHub API using a GitHub App's credentials. It includes functionality for loading configuration, authenticating with GitHub, and accessing the GitHub API client.

Features

  • Load GitHub App configuration from environment variables.
  • Authenticate with GitHub using a private key, App ID, and Installation ID.
  • Retrieve an authenticated GitHub client and access token.

Configuration

The library expects the following environment variables to be set:

  • GITHUB_APP_PRIVATE_KEY: The private key of the GitHub App.
  • GITHUB_APP_ID: The App ID of the GitHub App.
  • GITHUB_APP_INSTALLATION_ID: The Installation ID of the GitHub App.

Usage

Loading Configuration

Use the GithubLoadConfig function to load the configuration from environment variables:

config, err := GithubLoadConfig()
if err != nil {
    log.Fatalf("Failed to load GitHub configuration: %v", err.Error())
}
Creating a GitHub Session

Use the NewGithubSession function to create a new session:

session, err := NewGithubSession(config.Pem, config.AppId, config.InstallId)
if err != nil {
    log.Fatalf("Failed to create GitHub session: %v", err.Error())
}
Accessing the Authenticated Client

Retrieve the authenticated GitHub client and token:

client := session.Client()
token := session.AuthToken()

You can now use the client to interact with the GitHub API.

Error Handling

Ensure proper error handling when loading configuration or creating a session, as invalid credentials or missing environment variables will result in errors.

Dependencies

This library uses the following dependencies:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneRepository

func CloneRepository(session GithubSessionInterface,
	repository_url,
	workdir,
	branch string,
	basicAuth *ghhttp.BasicAuth,
	gitCloneOptions *git.CloneOptions,
) (*git.Repository, error)

CloneRepository clones the specified GitHub repository into a local directory named "/work". It uses the provided GithubSession for authentication and clones the specific branch indicated in the data map. repository_url: The URL of the GitHub repository to clone. Requires https:// format. branch: The branch to clone from the repository. Do not include the "refs/heads/" prefix, just the branch name. gitCloneOptions: Optional git clone options. If nil, default options will be used. basicAuth: Optional basic authentication credentials. If nil, it will use the token from the session. Returns a pointer to the cloned git.Repository or an error if the clone operation fails.

func CreateCheckRun

func CreateCheckRun(
	ctx context.Context,
	s GithubSessionInterface,
	headSha, repo, org, name, title, conclusion, status, summary string,
) error

CreateCheckRun creates a check run in the specified GitHub repository. It uses the provided GithubSession for authentication and requires the head SHA, repository name, organization name, check run name, title, conclusion, status, and summary. headSha: The SHA of the commit to create the check run for. repo: The name of the repository where the check run will be created. org: The name of the organization that owns the repository. name: The name of the check run. title: The title of the check run. conclusion: The conclusion of the check run (e.g., "success", "failure"). status: The status of the check run (e.g., "queued", "in_progress", "completed"). summary: A summary of the check run results. Returns an error if the check run creation fails.

Types

type GithubConfig

type GithubConfig struct {
	Pem       string `mapstructure:"pem"`
	AppId     string `mapstructure:"app_id"`
	InstallId string `mapstructure:"install_id"`
}

func GithubLoadConfig

func GithubLoadConfig() (*GithubConfig, error)

type GithubSession

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

func NewGithubSession

func NewGithubSession(pem, appID, installID string) (*GithubSession, error)

NewGithubSession creates a new Github session using the provided PEM file, App ID, and Install ID

func (*GithubSession) AuthToken

func (s *GithubSession) AuthToken() *oauth2.Token

Get AuthToken returns the authentication token

func (*GithubSession) Client

func (s *GithubSession) Client() *github.Client

Get Client returns the authenticated Github client

type GithubSessionInterface

type GithubSessionInterface interface {
	AuthToken() *oauth2.Token
	Client() *github.Client
}

GithubSessionInterface defines the interface for Github sessions

Jump to

Keyboard shortcuts

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