-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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.
Collaborator
Author
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. Yeah I was thinking the same and thought if there was a reason it was a standalone function. |
||
| 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") | ||
| } | ||
|
|
||
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: