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

Signed-off-by: hainenber <[email protected]>
  • Loading branch information
hainenber committed May 28, 2024
1 parent eb81e62 commit 03d6e36
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Main (unreleased)

- Updated Prometheus dependency to [v2.51.2](https://github.com/prometheus/prometheus/releases/tag/v2.51.2) (@thampiotr)

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


v1.1.0
------
Expand Down
15 changes: 9 additions & 6 deletions internal/vcs/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@ 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
key, err := h.SSHKey.Convert()
if err != nil {
return nil, err
}
return key, nil
}
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 03d6e36

Please sign in to comment.