Skip to content

Commit

Permalink
feat(vcs): bubble up SSH key conversion error for better debugging ex…
Browse files Browse the repository at this point in the history
…perience (#943)

* feat(vcs): bubble up SSH key conversion error for better debugging experience

Signed-off-by: hainenber <dotronghai96@gmail.com>

* chore: refactor code to be more succinct

Signed-off-by: hainenber <dotronghai96@gmail.com>

---------

Signed-off-by: hainenber <dotronghai96@gmail.com>
  • Loading branch information
hainenber authored May 29, 2024
1 parent 71c904c commit 037893f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Main (unreleased)
- Update `prometheus.exporter.snowflake` with the [latest](https://github.com/grafana/snowflake-prometheus-exporter) version of the exporter as of May 28, 2024 (@StefanKurek)
- Fixes issue where returned `NULL` values from database could cause unexpected errors.

- Bubble up SSH key conversion error to facilitate failed `import.git`. (@hainenber)

### Other changes

- `pyroscope.ebpf`, `pyroscope.java`, `pyroscope.scrape`, `pyroscope.write` and `discovery.process` components are now GA. (@korniltsev)
Expand Down
12 changes: 5 additions & 7 deletions internal/vcs/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ type GitAuthConfig struct {

// Convert converts HTTPClientConfig to the native Prometheus type. If h is
// nil, the default client config is returned.
func (h *GitAuthConfig) Convert() transport.AuthMethod {
func (h *GitAuthConfig) Convert() (transport.AuthMethod, error) {
if h == nil {
return nil
return nil, nil
}

if h.BasicAuth != nil {
return h.BasicAuth.Convert()
return h.BasicAuth.Convert(), nil
}

if h.SSHKey != nil {
key, _ := h.SSHKey.Convert()
return key
return h.SSHKey.Convert()
}
return nil
return nil, nil
}

type BasicAuth struct {
Expand Down
22 changes: 19 additions & 3 deletions internal/vcs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ func NewGitRepo(ctx context.Context, storagePath string, opts GitRepoOptions) (*
err error
)

authConfig, err := opts.Auth.Convert()
if err != nil {
return nil, DownloadFailedError{
Repository: opts.Repository,
Inner: err,
}
}

if !isRepoCloned(storagePath) {
repo, err = git.PlainCloneContext(ctx, storagePath, false, &git.CloneOptions{
URL: opts.Repository,
ReferenceName: plumbing.HEAD,
Auth: opts.Auth.Convert(),
Auth: authConfig,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
Tags: git.AllTags,
})
Expand All @@ -69,7 +77,7 @@ func NewGitRepo(ctx context.Context, storagePath string, opts GitRepoOptions) (*
pullRepoErr := wt.PullContext(ctx, &git.PullOptions{
RemoteName: "origin",
Force: true,
Auth: opts.Auth.Convert(),
Auth: authConfig,
})
if pullRepoErr != nil && !errors.Is(pullRepoErr, git.NoErrAlreadyUpToDate) {
workTree, err := repo.Worktree()
Expand Down Expand Up @@ -109,10 +117,18 @@ func isRepoCloned(dir string) bool {
// Update updates the repository by pulling new content and re-checking out to
// latest version of Revision.
func (repo *GitRepo) Update(ctx context.Context) error {
authConfig, err := repo.opts.Auth.Convert()
if err != nil {
return UpdateFailedError{
Repository: repo.opts.Repository,
Inner: err,
}
}

pullRepoErr := repo.workTree.PullContext(ctx, &git.PullOptions{
RemoteName: "origin",
Force: true,
Auth: repo.opts.Auth.Convert(),
Auth: authConfig,
})
if pullRepoErr != nil && !errors.Is(pullRepoErr, git.NoErrAlreadyUpToDate) {
return UpdateFailedError{
Expand Down

0 comments on commit 037893f

Please sign in to comment.