github

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: LGPL-2.1 Imports: 19 Imported by: 0

README

go-github

With the migration of our GitHub organisation to Single Sign-On (SSO), the GitHub user that was being used in our tools (like Jenkins) was not being added as a new LDAP user. With this in mind, we had the need to have a way of authenticating using GitHub Apps.

The creation of this Go library is to be able to use in any codebase that needs to authenticate on GitHub, like our fetch-bots-ips.

Usage

Get a new Access Token

To generate a new GitHub App access token that can be used to authenticate.

package main

import "github.com/kununu/go-github"

func main() {
	// Create a new GithubApp with JWT authentication
	ghApp, err := github.NewGitHubApp(&github.GitHubAppConfig{
		// GitHub APP ID to use to authenticate
		ApplicationID:  appId,
		// GitHub Installation ID for the APP
		InstallationID: instId,
		// GitHub APP generated private key path to file
		PrivateKeyFile: string,
		// GitHub APP generated private key value
		PrivateKey:     keyBytes,
	})

	token, err := ghApp.GetAccessToken()
	if err != nil {
		panic(err)
	}
}

Provided binary

We also provide a binary that outputs a GitHub token and can be in any a Linux command line by setting the GIT_ASKPASS environment variable to the binary.

The binary can be configured to be used by setting the environment variables GITHUB_APP_ID GITHUB_INST_ID and GITHUB_KEY_PATH or by passing the values using flags. Use the -h flag for more information on the available flags.

NOTE: If both GITHUB_KEY_PATH and GITHUB_KEY_VALUE are passed, only the first one is used.

GIT_ASKPASS=<path_to_binary>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

type Auth struct {
	JWTToken string
	Token    string
}

type GitHubApp

type GitHubApp struct {
	Config *GitHubAppConfig
	Auth   Auth
	// contains filtered or unexported fields
}

GitHubApp is a struct to interact with GitHub App's API

func NewGitHubApp

func NewGitHubApp(cfg *GitHubAppConfig) (*GitHubApp, error)

Creates a new GitHubApp struct configured by GitHubAppConfig

func (*GitHubApp) Add

func (ghApp *GitHubApp) Add(path string) error

Adds files to the repository

func (*GitHubApp) Clone

func (ghApp *GitHubApp) Clone() error

Clones the repository

func (*GitHubApp) Commit

func (ghApp *GitHubApp) Commit(msg string, user UserInfo) error

Commits the changes

func (*GitHubApp) GetAccessToken

func (ghApp *GitHubApp) GetAccessToken() (string, error)

Gets the access token to authenticate with github

func (*GitHubApp) HasChanges

func (ghApp *GitHubApp) HasChanges() bool

Check if repo has local changes

func (*GitHubApp) NewBranch added in v0.1.11

func (ghApp *GitHubApp) NewBranch(name string, checkout bool) error

Create a new branch

func (*GitHubApp) NewPullRequest added in v0.1.11

func (ghApp *GitHubApp) NewPullRequest(source, target, title, body string) error

Create new pull request

func (*GitHubApp) Push

func (ghApp *GitHubApp) Push() error

Pushes the changes

type GitHubAppConfig

type GitHubAppConfig struct {
	RepoURL        string
	ApplicationID  int64
	InstallationID int64
	LocalPath      string
	PrivateKey     []byte
	// contains filtered or unexported fields
}

Configuration for the GitHub App interaction

func ParseParameters added in v1.1.0

func ParseParameters() (*GitHubAppConfig, error)

type PullRequest added in v0.1.11

type PullRequest struct {
	SourceBranch string
	TargetBranch string
}

PullRequest information

type TokenResponse

type TokenResponse struct {
	Token string `json:"token"`
}

TokenResponse is a struct that represents the response from the API

type UserInfo added in v0.1.9

type UserInfo struct {
	Name  string
	Email string
}

User information

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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