-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from thegalactiks/github-app-authn
feat: add support for github app installation auth
- Loading branch information
Showing
13 changed files
with
186 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,5 +26,10 @@ go.work | |
# gitignore file | ||
.env | ||
|
||
# Giteway files | ||
dist/ | ||
giteway | ||
config.yaml | ||
|
||
# Github files | ||
*.private-key.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,75 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/bradleyfalzon/ghinstallation/v2" | ||
"github.com/google/go-github/v60/github" | ||
"github.com/thegalactiks/giteway/hosting" | ||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
"github.com/thegalactiks/giteway/internal/config" | ||
internalhttp "github.com/thegalactiks/giteway/internal/http" | ||
) | ||
|
||
type GithubService struct { | ||
client *github.Client | ||
hasToken bool | ||
cfg *config.GithubConfig | ||
client *github.Client | ||
user *github.User | ||
} | ||
|
||
var _ hosting.GitHostingService = (*GithubService)(nil) | ||
|
||
func NewGithubService(token *string) (*GithubService, error) { | ||
hasToken := false | ||
httpClient := &http.Client{ | ||
Transport: otelhttp.NewTransport(http.DefaultTransport), | ||
func NewGithubService(cfg *config.Config) *GithubService { | ||
client := github.NewClient(internalhttp.NewHttpClient(nil)) | ||
return &GithubService{ | ||
cfg: &cfg.GithubConfig, | ||
client: client, | ||
} | ||
} | ||
|
||
func (s *GithubService) IsKnownInstallation(owner string) bool { | ||
_, ok := s.cfg.Installations[owner] | ||
return ok | ||
} | ||
|
||
func (s *GithubService) WithInstallationOwner(owner string) (*GithubService, error) { | ||
installation, ok := s.cfg.Installations[owner] | ||
if !ok { | ||
return nil, fmt.Errorf("unknown installation owner: %s", owner) | ||
} | ||
client := github.NewClient(httpClient) | ||
if token != nil { | ||
client = client.WithAuthToken(*token) | ||
hasToken = true | ||
|
||
return s.WithInstallation(installation.ID) | ||
} | ||
|
||
func (s *GithubService) WithInstallation(installationID int64) (*GithubService, error) { | ||
// load private key file from path | ||
privateKey, err := os.ReadFile(s.cfg.PrivateKeyPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var httpTransport http.RoundTripper | ||
itr, err := ghinstallation.New(http.DefaultTransport, s.cfg.AppID, installationID, privateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
httpTransport = itr | ||
|
||
client := github.NewClient(internalhttp.NewHttpClient(&httpTransport)) | ||
return &GithubService{client: client}, nil | ||
} | ||
|
||
func (s *GithubService) WithAuthToken(ctx context.Context, token string) (*GithubService, error) { | ||
client := s.client.WithAuthToken(token) | ||
user, _, err := client.Users.Get(ctx, "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &GithubService{ | ||
client: client, | ||
hasToken: hasToken, | ||
client: client, | ||
user: user, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.