GitHub App management, installations, and app-specific operations.
The AppsService provides methods for GitHub Apps functionality.
type AppsService struct{}// Get fetches a single GitHub App by slug
func (s *AppsService) Get(ctx context.Context, appSlug string) (*App, *Response, error)
// GetAuthenticated returns the authenticated app
func (s *AppsService) GetAuthenticated(ctx context.Context) (*App, *Response, error)
type App struct {
ID *int64
Slug *string
NodeID *string
Owner *User
Name *string
Description *string
ExternalURL *string
HTMLURL *string
CreatedAt *Timestamp
UpdatedAt *Timestamp
Permissions *InstallationPermissions
Events []string
// Additional fields
InstallationsCount *int
ClientID *string
ClientSecret *string
WebhookSecret *string
PEM *string
}// ListInstallations lists installations for the authenticated app
func (s *AppsService) ListInstallations(ctx context.Context, opts *ListOptions) ([]*Installation, *Response, error)
// GetInstallation returns the specified installation
func (s *AppsService) GetInstallation(ctx context.Context, id int64) (*Installation, *Response, error)
// DeleteInstallation deletes the specified installation
func (s *AppsService) DeleteInstallation(ctx context.Context, id int64) (*Response, error)
// SuspendInstallation suspends the specified installation
func (s *AppsService) SuspendInstallation(ctx context.Context, id int64) (*Response, error)
// UnsuspendInstallation unsuspends the specified installation
func (s *AppsService) UnsuspendInstallation(ctx context.Context, id int64) (*Response, error)
type Installation struct {
ID *int64
AppID *int64
AppSlug *string
TargetID *int64
Account *User
AccessTokensURL *string
RepositoriesURL *string
HTMLURL *string
TargetType *string
SingleFileName *string
RepositorySelection *string
Events []string
Permissions *InstallationPermissions
CreatedAt *Timestamp
UpdatedAt *Timestamp
SingleFilePaths []string
HasMultipleSingleFiles *bool
SuspendedBy *User
SuspendedAt *Timestamp
}// CreateInstallationToken creates an installation access token
func (s *AppsService) CreateInstallationToken(ctx context.Context, id int64, opts *InstallationTokenOptions) (*InstallationToken, *Response, error)
type InstallationTokenOptions struct {
RepositoryIDs []int64
Repositories []string
Permissions *InstallationPermissions
}
type InstallationToken struct {
Token *string
ExpiresAt *Timestamp
Permissions *InstallationPermissions
Repositories []*Repository
}
type InstallationPermissions struct {
Actions *string
Administration *string
Checks *string
Contents *string
Deployments *string
Environments *string
Issues *string
Metadata *string
Packages *string
Pages *string
PullRequests *string
RepositoryHooks *string
RepositoryProjects *string
Secrets *string
SecurityEvents *string
SingleFile *string
Statuses *string
VulnerabilityAlerts *string
Workflows *string
Members *string
OrganizationAdministration *string
OrganizationHooks *string
OrganizationPlan *string
OrganizationProjects *string
OrganizationPackages *string
OrganizationSecrets *string
OrganizationSelfHostedRunners *string
OrganizationUserBlocking *string
TeamDiscussions *string
}Usage:
// Create an installation token with specific permissions
opts := &github.InstallationTokenOptions{
Repositories: []string{"repo1", "repo2"},
Permissions: &github.InstallationPermissions{
Contents: github.Ptr("write"),
Issues: github.Ptr("write"),
PullRequests: github.Ptr("write"),
},
}
token, _, err := client.Apps.CreateInstallationToken(ctx, installationID, opts)
if err != nil {
// Handle error
}
// Use the token to create an authenticated client
authClient := github.NewClient(nil).WithAuthToken(*token.Token)// ListRepos lists repositories accessible to the authenticated installation
func (s *AppsService) ListRepos(ctx context.Context, opts *ListOptions) ([]*Repository, *Response, error)
// ListUserRepos lists repositories accessible to the user for an installation
func (s *AppsService) ListUserRepos(ctx context.Context, id int64, opts *ListOptions) ([]*Repository, *Response, error)
// AddRepository adds a single repository to an installation
func (s *AppsService) AddRepository(ctx context.Context, instID, repoID int64) (*Repository, *Response, error)
// RemoveRepository removes a single repository from an installation
func (s *AppsService) RemoveRepository(ctx context.Context, instID, repoID int64) (*Response, error)
// RevokeInstallationToken revokes the installation token used to authenticate
func (s *AppsService) RevokeInstallationToken(ctx context.Context) (*Response, error)// CompleteAppManifest completes the App manifest handshake flow
func (s *AppsService) CompleteAppManifest(ctx context.Context, code string) (*AppConfig, *Response, error)
// GetAppManifest returns the app manifest configuration
func (s *AppsService) GetAppManifest(ctx context.Context, slug string) (*AppManifest, *Response, error)
type AppConfig struct {
ID *int64
Slug *string
NodeID *string
Owner *User
Name *string
Description *string
ExternalURL *string
HTMLURL *string
CreatedAt *Timestamp
UpdatedAt *Timestamp
ClientID *string
ClientSecret *string
WebhookSecret *string
PEM *string
}// GetHookConfig returns the webhook configuration for a GitHub App
func (s *AppsService) GetHookConfig(ctx context.Context) (*HookConfig, *Response, error)
// UpdateHookConfig updates the webhook configuration for a GitHub App
func (s *AppsService) UpdateHookConfig(ctx context.Context, config *HookConfig) (*HookConfig, *Response, error)
// ListHookDeliveries lists webhook deliveries for a GitHub App
func (s *AppsService) ListHookDeliveries(ctx context.Context, opts *ListCursorOptions) ([]*HookDelivery, *Response, error)
// GetHookDelivery returns a delivery for a GitHub App webhook
func (s *AppsService) GetHookDelivery(ctx context.Context, deliveryID int64) (*HookDelivery, *Response, error)
// RedeliverHookDelivery redelivers a delivery for a GitHub App webhook
func (s *AppsService) RedeliverHookDelivery(ctx context.Context, deliveryID int64) (*HookDelivery, *Response, error)// FindOrganizationInstallation finds the organization's installation information
func (s *AppsService) FindOrganizationInstallation(ctx context.Context, org string) (*Installation, *Response, error)
// FindRepositoryInstallation finds the repository's installation information
func (s *AppsService) FindRepositoryInstallation(ctx context.Context, owner, repo string) (*Installation, *Response, error)
// FindRepositoryInstallationByID finds the repository's installation information by ID
func (s *AppsService) FindRepositoryInstallationByID(ctx context.Context, id int64) (*Installation, *Response, error)
// FindUserInstallation finds the user's installation information
func (s *AppsService) FindUserInstallation(ctx context.Context, user string) (*Installation, *Response, error)// ListInstallationRequests lists the requests to install a GitHub App
func (s *AppsService) ListInstallationRequests(ctx context.Context, opts *ListOptions) ([]*InstallationRequest, *Response, error)import (
"context"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"net/http"
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/google/go-github/v79/github"
)
// Create a JWT for GitHub App authentication
func generateAppJWT(appID int64, privateKeyPath string) (string, error) {
// Read private key
privateKeyData, err := ioutil.ReadFile(privateKeyPath)
if err != nil {
return "", err
}
// Parse private key
block, _ := pem.Decode(privateKeyData)
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return "", err
}
// Create JWT claims
now := time.Now()
claims := jwt.MapClaims{
"iat": now.Unix(),
"exp": now.Add(10 * time.Minute).Unix(),
"iss": appID,
}
// Generate JWT
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
return token.SignedString(privateKey)
}
// Authenticate as GitHub App and get installation token
func authenticateAsApp(appID, installationID int64, privateKeyPath string) (*github.Client, error) {
ctx := context.Background()
// Generate JWT
jwtToken, err := generateAppJWT(appID, privateKeyPath)
if err != nil {
return nil, err
}
// Create client with JWT
tr := http.DefaultTransport
appClient := github.NewClient(&http.Client{Transport: tr})
appClient = appClient.WithAuthToken(jwtToken)
// Get installation token
installToken, _, err := appClient.Apps.CreateInstallationToken(
ctx, installationID, nil)
if err != nil {
return nil, err
}
// Create authenticated client with installation token
authClient := github.NewClient(nil).WithAuthToken(*installToken.Token)
return authClient, nil
}
// Use the authenticated client
func main() {
client, err := authenticateAsApp(12345, 67890, "private-key.pem")
if err != nil {
panic(err)
}
// Now use client to make API calls
repos, _, err := client.Apps.ListRepos(context.Background(), nil)
// ...
}