Skip to content

Commit

Permalink
add github app support
Browse files Browse the repository at this point in the history
  • Loading branch information
DrFaust92 committed Jul 26, 2024
1 parent 8d9ae37 commit b5ec8eb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
10 changes: 8 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func init() {
zerolog.LevelDebugValue,
zerolog.LevelTraceValue,
).
withDefault("info").
withDefault(zerolog.LevelInfoValue).
withShortHand("l"),
)
boolFlag(flags, "persist-log-level", "Persists the set log level down to other module loggers.")
Expand All @@ -55,6 +55,9 @@ func init() {
withChoices("github", "gitlab").
withDefault("gitlab"))
stringFlag(flags, "vcs-token", "VCS API token.")
stringFlag(flags, "github-private-key", "Github App Private Key.")
int64Flag(flags, "github-app-id", "Github App ID.")
int64Flag(flags, "github-installation-id", "Github Installation ID.")
stringFlag(flags, "argocd-api-token", "ArgoCD API token.")
stringFlag(flags, "argocd-api-server-addr", "ArgoCD API Server Address.",
newStringOpts().
Expand Down Expand Up @@ -117,7 +120,10 @@ func setupLogOutput() {

// Default level is info, unless debug flag is present
levelFlag := viper.GetString("log-level")
level, _ := zerolog.ParseLevel(levelFlag)
level, err := zerolog.ParseLevel(levelFlag)
if err != nil {
log.Error().Err(err).Msg("Invalid log level")
}

zerolog.SetGlobalLevel(level)
log.Debug().Msg("Debug level logging enabled.")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ toolchain go1.21.6
require (
github.com/argoproj/argo-cd/v2 v2.11.6
github.com/argoproj/gitops-engine v0.7.1-0.20240715141605-18ba62e1f1fb
github.com/bradleyfalzon/ghinstallation/v2 v2.6.0
github.com/cenkalti/backoff/v4 v4.3.0
github.com/chainguard-dev/git-urls v1.0.2
github.com/creasty/defaults v1.7.0
Expand Down Expand Up @@ -84,7 +85,6 @@ require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/bmatcuk/doublestar/v4 v4.6.0 // indirect
github.com/bombsimon/logrusr/v2 v2.0.1 // indirect
github.com/bradleyfalzon/ghinstallation/v2 v2.6.0 // indirect
github.com/bufbuild/protocompile v0.6.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ type ServerConfig struct {
VcsToken string `mapstructure:"vcs-token"`
VcsType string `mapstructure:"vcs-type"`

//github
GithubPrivateKey string `mapstructure:"github-private-key"`
GithubAppID int64 `mapstructure:"github-app-id"`
GithubInstallationID int64 `mapstructure:"github-installation-id"`

// webhooks
EnsureWebhooks bool `mapstructure:"ensure-webhooks"`
WebhookSecret string `mapstructure:"webhook-secret"`
Expand Down
50 changes: 35 additions & 15 deletions pkg/vcs/github_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"strconv"
"strings"

"github.com/chainguard-dev/git-urls"
"github.com/bradleyfalzon/ghinstallation/v2"
giturls "github.com/chainguard-dev/git-urls"
"github.com/google/go-github/v62/github"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -46,34 +47,53 @@ func CreateGithubClient(cfg config.ServerConfig) (*Client, error) {
err error
googleClient *github.Client
shurcoolClient *githubv4.Client
githubClient *http.Client
)

// Initialize the GitLab client with access token
t := cfg.VcsToken
if t == "" {
log.Fatal().Msg("github token needs to be set")
if (cfg.GithubAppID == 0 || cfg.GithubInstallationID == 0 || cfg.GithubPrivateKey == "") && cfg.VcsToken == "" {
log.Fatal().Msg("github token or app credentials need to be set")
}
log.Debug().Msgf("Token Length - %d", len(t))

// Initialize the GitHub client with app key
if cfg.GithubAppID != 0 && cfg.GithubInstallationID != 0 && cfg.GithubPrivateKey != "" {
appTransport, err := ghinstallation.New(http.DefaultTransport, cfg.GithubAppID, cfg.GithubInstallationID, []byte(cfg.GithubPrivateKey))
if err != nil {
log.Fatal().Err(err).Msg("failed to create github app transport")
}
githubClient = &http.Client{Transport: appTransport}
}

ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: t},
)
tc := oauth2.NewClient(ctx, ts)

// Initialize the GitHub client with access token, dont init if app details are passed above
if githubClient == nil {
vscToken := cfg.VcsToken
if vscToken != "" {
log.Debug().Msgf("Token Length - %d", len(vscToken))
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: vscToken},
)
githubClient = oauth2.NewClient(ctx, ts)
}
}

if githubClient == nil {
log.Fatal().Msg("github client is not initialized")
}

githubUrl := cfg.VcsBaseUrl
githubUploadUrl := cfg.VcsUploadUrl
// we need both urls to be set for github enterprise
if githubUrl == "" || githubUploadUrl == "" {
googleClient = github.NewClient(tc) // If this has failed, we'll catch it on first call
googleClient = github.NewClient(githubClient) // If this has failed, we'll catch it on first call

// Use the client from shurcooL's githubv4 library for queries.
shurcoolClient = githubv4.NewClient(tc)
shurcoolClient = githubv4.NewClient(githubClient)
} else {
googleClient, err = github.NewClient(tc).WithEnterpriseURLs(githubUrl, githubUploadUrl)
googleClient, err = github.NewClient(githubClient).WithEnterpriseURLs(githubUrl, githubUploadUrl)
if err != nil {
log.Fatal().Err(err).Msg("failed to create github enterprise client")
}
shurcoolClient = githubv4.NewEnterpriseClient(githubUrl, tc)
shurcoolClient = githubv4.NewEnterpriseClient(githubUrl, githubClient)
}

user, _, err := googleClient.Users.Get(ctx, "")
Expand Down

0 comments on commit b5ec8eb

Please sign in to comment.