gogithubapp

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: MIT Imports: 8 Imported by: 0

README

gogithubapp

gogithubapp is a go client library that unifies github oauth2 user flows and github app installation workflows, providing advanced abstractions for common operations.


Installation

go get github.com/aquaticcalf/gogithubapp

Usage

1. oauth2 ( user flow )
import (
  "context"
  "golang.org/x/oauth2"
  "github.com/aquaticcalf/gogithubapp"
)

// 1. build config
cfg := gogithubapp.OAuthConfig(
  os.Getenv("GITHUB_CLIENT_ID"),
  os.Getenv("GITHUB_CLIENT_SECRET"),
  "https://yourapp/callback",
  []string{"repo", "workflow"},
)

// 2. redirect user to
state := "random-state"
authURL := cfg.AuthCodeURL(state, oauth2.AccessTypeOffline)

// 3. on callback
tok, err := gogithubapp.ExchangeToken(context.Background(), cfg, code)
client := gogithubapp.NewClientFromToken(context.Background(), tok)

// 4. use client
repos, err := client.ListRepositories(context.Background())
2. github app ( installation flow )
import (
  "context"
  "github.com/aquaticcalf/gogithubapp"
)

// provide raw or base64-encoded PEM key
appID := int64(123456)
installID := int64(789012)
pemKey := []byte(os.Getenv("GITHUB_PRIVATE_KEY"))

client, err := gogithubapp.NewClientFromApp(
  context.Background(), appID, installID, pemKey,
)

api reference

repositories
client.ListRepositories(ctx)              // []*github.Repository
client.CreateRepository(ctx, name, desc, private)
client.CreateFromTemplate(ctx, owner, tmplRepo, newName)
branches
client.CreateBranch(ctx, owner, repo, newBranch, baseBranch)
client.ListBranches(ctx, owner, repo)
commits
client.CommitMultipleFiles(ctx, owner, repo, branch, message, files) // files map[string][]byte
client.ListCommits(ctx context.Context, owner, repo, opts)
issues & pr
client.CreateIssue(ctx, owner, repo, title, body)
client.ListIssues(ctx, owner, repo, opts)
client.CreatePullRequest(ctx, owner, repo, title, head, base, body)
client.ListPullRequests(ctx, owner, repo, opts)
client.MergePullRequest(ctx, owner, repo, prNumber, msg)
client.CreateComment(ctx, owner, repo, number, comment)
client.AddLabels(ctx, owner, repo, number, labels)

license

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExchangeToken

func ExchangeToken(ctx context.Context, cfg *oauth2.Config, code string) (*oauth2.Token, error)

ExchangeToken exchanges the oauth2 code for a token

func OAuthConfig

func OAuthConfig(clientID, clientSecret, redirectURL string, scopes []string) *oauth2.Config

OAuthConfig constructs an oauth2 configuration for user auth via github

Types

type Client

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

Client wraps a go-github client for unified api operations

func NewClientFromApp

func NewClientFromApp(ctx context.Context, appID, installID int64, privateKeyPEM []byte) (*Client, error)

NewClientFromApp creates a client using a gitHub app installation privateKeyPEM may be raw or base64-encoded PEM

func NewClientFromToken

func NewClientFromToken(ctx context.Context, token *oauth2.Token) *Client

NewClientFromToken creates a client using an oauth2 token

func (*Client) AddLabels

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

AddLabels adds labels to an issue or pr

func (*Client) CommitMultipleFiles

func (c *Client) CommitMultipleFiles(ctx context.Context, owner, repo, branch, message string, files map[string][]byte) (*githubapi.Commit, error)

CommitMultipleFiles commits many files atomically to a branch

func (*Client) CreateBranch

func (c *Client) CreateBranch(ctx context.Context, owner, repo, branchName, baseBranch string) (*githubapi.Reference, error)

CreateBranch creates a new branch from baseBranch

func (*Client) CreateComment

func (c *Client) CreateComment(ctx context.Context, owner, repo string, number int, comment string) (*githubapi.IssueComment, error)

CreateComment adds a comment to an issue or pr

func (*Client) CreateFromTemplate

func (c *Client) CreateFromTemplate(ctx context.Context, tmplOwner, tmplRepo, newName, owner string) (*githubapi.Repository, error)

CreateFromTemplate makes a new repository from a template

func (*Client) CreateIssue

func (c *Client) CreateIssue(ctx context.Context, owner, repo, title, body string) (*githubapi.Issue, error)

CreateIssue opens a new issue

func (*Client) CreatePullRequest

func (c *Client) CreatePullRequest(ctx context.Context, owner, repo, title, head, base, body string) (*githubapi.PullRequest, error)

CreatePullRequest opens a pull request

func (*Client) CreateRepository

func (c *Client) CreateRepository(ctx context.Context, name, description string, private bool) (*githubapi.Repository, error)

CreateRepository creates a new repository under the authenticated actor

func (*Client) GetFileContent

func (c *Client) GetFileContent(ctx context.Context, owner, repo, path, ref string) (string, *githubapi.RepositoryContent, error)

GetFileContent retrieves file content from a repo.

func (*Client) GetInstallation added in v0.0.3

func (c *Client) GetInstallation(ctx context.Context, installationID int64) (*githubapi.Installation, error)

GetInstallation retrieves details about a specific installation using the client's authentication.

func (*Client) ListBranches

func (c *Client) ListBranches(ctx context.Context, owner, repo string) ([]*githubapi.Branch, error)

ListBranches lists all branches in a repository

func (*Client) ListCommits

func (c *Client) ListCommits(ctx context.Context, owner, repo string, opts *githubapi.CommitsListOptions) ([]*githubapi.RepositoryCommit, error)

ListCommits lists commits on a branch.

func (*Client) ListIssues

func (c *Client) ListIssues(ctx context.Context, owner, repo string, opts *githubapi.IssueListByRepoOptions) ([]*githubapi.Issue, error)

ListIssues lists issues for a repo

func (*Client) ListPullRequests

func (c *Client) ListPullRequests(ctx context.Context, owner, repo string, opts *githubapi.PullRequestListOptions) ([]*githubapi.PullRequest, error)

ListPullRequests lists pull requests for a repo

func (*Client) ListRepositories

func (c *Client) ListRepositories(ctx context.Context) ([]*githubapi.Repository, error)

ListRepositories lists repositories accessible to the client

func (*Client) MergePullRequest

func (c *Client) MergePullRequest(ctx context.Context, owner, repo string, number int, commitMessage string) (*githubapi.PullRequestMergeResult, error)

MergePullRequest merges a pull request

Jump to

Keyboard shortcuts

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