generated from block/oss-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add GitHub App authentication for git cloning and releases #83
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
Open
nssherpa
wants to merge
1
commit into
main
Choose a base branch
from
github-app-authentication
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -74,24 +74,31 @@ type Config struct { | |
| GitConfig GitTuningConfig | ||
| } | ||
|
|
||
| // CredentialProvider provides credentials for git operations. | ||
| type CredentialProvider interface { | ||
| GetTokenForURL(ctx context.Context, url string) (string, error) | ||
| } | ||
|
|
||
| type Repository struct { | ||
| mu sync.RWMutex | ||
| state State | ||
| path string | ||
| upstreamURL string | ||
| lastFetch time.Time | ||
| lastRefCheck time.Time | ||
| refCheckValid bool | ||
| fetchSem chan struct{} | ||
| mu sync.RWMutex | ||
| state State | ||
| path string | ||
| upstreamURL string | ||
| lastFetch time.Time | ||
| lastRefCheck time.Time | ||
| refCheckValid bool | ||
| fetchSem chan struct{} | ||
| credentialProvider CredentialProvider | ||
| } | ||
|
|
||
| type Manager struct { | ||
| config Config | ||
| clones map[string]*Repository | ||
| clonesMu sync.RWMutex | ||
| config Config | ||
| clones map[string]*Repository | ||
| clonesMu sync.RWMutex | ||
| credentialProvider CredentialProvider | ||
| } | ||
|
|
||
| func NewManager(_ context.Context, config Config) (*Manager, error) { | ||
| func NewManager(_ context.Context, config Config, credentialProvider CredentialProvider) (*Manager, error) { | ||
| if config.RootDir == "" { | ||
| return nil, errors.New("RootDir is required") | ||
| } | ||
|
|
@@ -101,8 +108,9 @@ func NewManager(_ context.Context, config Config) (*Manager, error) { | |
| } | ||
|
|
||
| return &Manager{ | ||
| config: config, | ||
| clones: make(map[string]*Repository), | ||
| config: config, | ||
| clones: make(map[string]*Repository), | ||
| credentialProvider: credentialProvider, | ||
| }, nil | ||
| } | ||
|
|
||
|
|
@@ -125,10 +133,11 @@ func (m *Manager) GetOrCreate(_ context.Context, upstreamURL string) (*Repositor | |
| clonePath := m.clonePathForURL(upstreamURL) | ||
|
|
||
| repo = &Repository{ | ||
| state: StateEmpty, | ||
| path: clonePath, | ||
| upstreamURL: upstreamURL, | ||
| fetchSem: make(chan struct{}, 1), | ||
| state: StateEmpty, | ||
| path: clonePath, | ||
| upstreamURL: upstreamURL, | ||
| fetchSem: make(chan struct{}, 1), | ||
| credentialProvider: m.credentialProvider, | ||
| } | ||
|
|
||
| gitDir := filepath.Join(clonePath, ".git") | ||
|
|
@@ -152,6 +161,10 @@ func (m *Manager) Config() Config { | |
| return m.config | ||
| } | ||
|
|
||
| func (m *Manager) CredentialProvider() CredentialProvider { | ||
| return m.credentialProvider | ||
| } | ||
|
|
||
| func (m *Manager) DiscoverExisting(_ context.Context) ([]*Repository, error) { | ||
| var discovered []*Repository | ||
| err := filepath.Walk(m.config.RootDir, func(path string, info os.FileInfo, err error) error { | ||
|
|
@@ -195,10 +208,11 @@ func (m *Manager) DiscoverExisting(_ context.Context) ([]*Repository, error) { | |
| upstreamURL := "https://" + host + "/" + repoPath | ||
|
|
||
| repo := &Repository{ | ||
| state: StateReady, | ||
| path: path, | ||
| upstreamURL: upstreamURL, | ||
| fetchSem: make(chan struct{}, 1), | ||
| state: StateReady, | ||
| path: path, | ||
| upstreamURL: upstreamURL, | ||
| fetchSem: make(chan struct{}, 1), | ||
| credentialProvider: m.credentialProvider, | ||
| } | ||
| repo.fetchSem <- struct{}{} | ||
|
|
||
|
|
@@ -304,7 +318,7 @@ func (r *Repository) executeClone(ctx context.Context, config Config) error { | |
| r.upstreamURL, r.path, | ||
| } | ||
|
|
||
| cmd, err := gitCommand(ctx, r.upstreamURL, args...) | ||
| cmd, err := gitCommand(ctx, r.upstreamURL, r.credentialProvider, args...) | ||
|
Collaborator
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. I think we should make gitCommand a method on Repository, then we can get rid of these two arguments. |
||
| if err != nil { | ||
| return errors.Wrap(err, "create git command") | ||
| } | ||
|
|
@@ -320,7 +334,7 @@ func (r *Repository) executeClone(ctx context.Context, config Config) error { | |
| return errors.Wrapf(err, "configure fetch refspec: %s", string(output)) | ||
| } | ||
|
|
||
| cmd, err = gitCommand(ctx, r.upstreamURL, "-C", r.path, | ||
| cmd, err = gitCommand(ctx, r.upstreamURL, r.credentialProvider, "-C", r.path, | ||
| "-c", "http.postBuffer="+strconv.Itoa(config.GitConfig.PostBuffer), | ||
| "-c", "http.lowSpeedLimit="+strconv.Itoa(config.GitConfig.LowSpeedLimit), | ||
| "-c", "http.lowSpeedTime="+strconv.Itoa(int(config.GitConfig.LowSpeedTime.Seconds())), | ||
|
|
@@ -357,7 +371,7 @@ func (r *Repository) Fetch(ctx context.Context, config Config) error { | |
| r.mu.Lock() | ||
|
|
||
| // #nosec G204 - r.path is controlled by us | ||
| cmd, err := gitCommand(ctx, r.upstreamURL, "-C", r.path, | ||
| cmd, err := gitCommand(ctx, r.upstreamURL, r.credentialProvider, "-C", r.path, | ||
| "-c", "http.postBuffer="+strconv.Itoa(config.GitConfig.PostBuffer), | ||
| "-c", "http.lowSpeedLimit="+strconv.Itoa(config.GitConfig.LowSpeedLimit), | ||
| "-c", "http.lowSpeedTime="+strconv.Itoa(int(config.GitConfig.LowSpeedTime.Seconds())), | ||
|
|
@@ -447,7 +461,7 @@ func (r *Repository) GetLocalRefs(ctx context.Context) (map[string]string, error | |
|
|
||
| func (r *Repository) GetUpstreamRefs(ctx context.Context) (map[string]string, error) { | ||
| // #nosec G204 - r.upstreamURL is controlled by us | ||
| cmd, err := gitCommand(ctx, r.upstreamURL, "ls-remote", r.upstreamURL) | ||
| cmd, err := gitCommand(ctx, r.upstreamURL, r.credentialProvider, "ls-remote", r.upstreamURL) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "create git command") | ||
| } | ||
|
|
||
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.
I would very much suggest parsing the URL: