-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement GitHubClientFactory #3203
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2024 Stacklok, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package clients contains the GitHub client factory | ||
package clients | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
|
||
gogithub "github.com/google/go-github/v61/github" | ||
"github.com/motemen/go-loghttp" | ||
"github.com/rs/zerolog" | ||
"golang.org/x/oauth2" | ||
|
||
"github.com/stacklok/minder/internal/db" | ||
"github.com/stacklok/minder/internal/providers/telemetry" | ||
provifv1 "github.com/stacklok/minder/pkg/providers/v1" | ||
) | ||
|
||
// GitHubClientFactory creates instances of the GitHub API client | ||
type GitHubClientFactory interface { | ||
// Build creates an instance of the GitHub Client | ||
// `baseURL` should be set to the empty string if there is no need to | ||
// override the default GitHub URL | ||
Build(baseURL string, credential provifv1.GitHubCredential) (*gogithub.Client, error) | ||
} | ||
|
||
type githubClientFactory struct { | ||
metrics telemetry.HttpClientMetrics | ||
} | ||
|
||
// NewGitHubClientFactory creates a new instance of GitHubClientFactory | ||
func NewGitHubClientFactory(metrics telemetry.HttpClientMetrics) GitHubClientFactory { | ||
return &githubClientFactory{metrics: metrics} | ||
} | ||
|
||
func (g *githubClientFactory) Build(baseURL string, credential provifv1.GitHubCredential) (*gogithub.Client, error) { | ||
tc := &http.Client{ | ||
Transport: &oauth2.Transport{ | ||
Base: http.DefaultClient.Transport, | ||
Source: credential.GetAsOAuth2TokenSource(), | ||
}, | ||
} | ||
|
||
transport, err := g.metrics.NewDurationRoundTripper(tc.Transport, db.ProviderTypeGithub) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating duration round tripper: %w", err) | ||
} | ||
|
||
// If $MINDER_LOG_GITHUB_REQUESTS is set, wrap the transport in a logger | ||
// to record all calls and responses to from GitHub: | ||
if os.Getenv("MINDER_LOG_GITHUB_REQUESTS") != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not for this PR, but it would be nice to keep a histogram of the values of |
||
transport = &loghttp.Transport{ | ||
Transport: transport, | ||
LogRequest: func(req *http.Request) { | ||
zerolog.Ctx(req.Context()).Debug(). | ||
Str("type", "REQ"). | ||
Str("method", req.Method). | ||
Msg(req.URL.String()) | ||
}, | ||
LogResponse: func(resp *http.Response) { | ||
zerolog.Ctx(resp.Request.Context()).Debug(). | ||
Str("type", "RESP"). | ||
Str("method", resp.Request.Method). | ||
Str("status", fmt.Sprintf("%d", resp.StatusCode)). | ||
Str("rate-limit", fmt.Sprintf("%s/%s", | ||
resp.Request.Header.Get("x-ratelimit-used"), | ||
resp.Request.Header.Get("x-ratelimit-remaining"), | ||
)). | ||
Msg(resp.Request.URL.String()) | ||
|
||
}, | ||
} | ||
} | ||
|
||
tc.Transport = transport | ||
ghClient := gogithub.NewClient(tc) | ||
|
||
if baseURL != "" { | ||
parsedURL, err := url.Parse(baseURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ghClient.BaseURL = parsedURL | ||
} | ||
|
||
return ghClient, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should move this to some actual configuration at some point, or remove this hook.
(Doesn't need to be part of this PR)