Skip to content
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

CR-15777 #409

Merged
merged 4 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.4.9
kim-codefresh marked this conversation as resolved.
Show resolved Hide resolved
VERSION=v0.4.10
OUT_DIR=dist

CLI_NAME?=argocd-autopilot
Expand Down
50 changes: 50 additions & 0 deletions pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ func (o *CloneOptions) GetRepo(ctx context.Context) (Repository, fs.FS, error) {
}
}

if o.CloneForWrite {
kim-codefresh marked this conversation as resolved.
Show resolved Hide resolved
err = validateRepoWritePermission(ctx, r)
if err != nil {
return nil, nil, fmt.Errorf("failed to validate repository write permissions: %w", err)
kim-codefresh marked this conversation as resolved.
Show resolved Hide resolved
}
}

bootstrapFS, err := o.FS.Chroot(o.path)
if err != nil {
return nil, nil, err
Expand All @@ -252,6 +259,49 @@ func (o *CloneOptions) GetRepo(ctx context.Context) (Repository, fs.FS, error) {
return r, fs.Create(bootstrapFS), nil
}

var validateRepoWritePermission = func(ctx context.Context, r *repo) error {
cert, err := r.auth.GetCertificate()
if err != nil {
return fmt.Errorf("failed getting repository certificates: %w", err)
}

err = r.Repository.PushContext(ctx, &gg.PushOptions{
Auth: getAuth(r.auth),
Progress: r.progress,
CABundle: cert,
})
if err != nil {
if !errors.Is(err, gg.NoErrAlreadyUpToDate) {
kim-codefresh marked this conversation as resolved.
Show resolved Hide resolved
// means there was already a commit to push, and it failed to push it
return err
}
// no commit to push, all up to date
if err := r.PushDummyCommit("Validating repository write permissions", ctx); err != nil {
kim-codefresh marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("failed to push dummy commit: %w", err)
}
}

return nil
}

func (r *repo) PushDummyCommit(commitMsg string, ctx context.Context) error {

_, err := r.Persist(ctx, &PushOptions{
CommitMsg: commitMsg,
})

if err != nil {
return fmt.Errorf("failed pushing commit to repository: %w", err)
}

if err != nil {
log.G().Warnf("failed to reset commit: %v", err)
}
kim-codefresh marked this conversation as resolved.
Show resolved Hide resolved

return nil

}

func (o *CloneOptions) URL() string {
return o.url
}
Expand Down
82 changes: 75 additions & 7 deletions pkg/git/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,12 +596,13 @@ func Test_clone(t *testing.T) {

func TestGetRepo(t *testing.T) {
tests := map[string]struct {
opts *CloneOptions
wantErr string
cloneFn func(context.Context, *CloneOptions) (*repo, error)
createRepoFn func(context.Context, *CloneOptions) (defaultBranch string, err error)
initRepoFn func(context.Context, *CloneOptions, string) (*repo, error)
assertFn func(*testing.T, Repository, fs.FS, error)
opts *CloneOptions
wantErr string
cloneFn func(context.Context, *CloneOptions) (*repo, error)
validateRepoWritePermissionFn func(ctx context.Context, r *repo) error
createRepoFn func(context.Context, *CloneOptions) (defaultBranch string, err error)
initRepoFn func(context.Context, *CloneOptions, string) (*repo, error)
assertFn func(*testing.T, Repository, fs.FS, error)
}{
"Should get a repo": {
opts: &CloneOptions{
Expand All @@ -611,6 +612,9 @@ func TestGetRepo(t *testing.T) {
cloneFn: func(_ context.Context, opts *CloneOptions) (*repo, error) {
return &repo{}, nil
},
validateRepoWritePermissionFn: func(ctx context.Context, r *repo) error {
return nil
},
assertFn: func(t *testing.T, r Repository, f fs.FS, e error) {
assert.NotNil(t, r)
assert.NotNil(t, f)
Expand Down Expand Up @@ -708,20 +712,25 @@ func TestGetRepo(t *testing.T) {
assert.NotNil(t, f)
assert.Nil(t, e)
},
validateRepoWritePermissionFn: func(ctx context.Context, r *repo) error {
return nil
},
},
}

origClone, origCreateRepo, origInitRepo := clone, createRepo, initRepo
origClone, origCreateRepo, origInitRepo, origValidateRepoWritePermission := clone, createRepo, initRepo, validateRepoWritePermission
defer func() {
clone = origClone
createRepo = origCreateRepo
initRepo = origInitRepo
validateRepoWritePermission = origValidateRepoWritePermission
}()
for tname, tt := range tests {
t.Run(tname, func(t *testing.T) {
clone = tt.cloneFn
createRepo = tt.createRepoFn
initRepo = tt.initRepoFn
validateRepoWritePermission = tt.validateRepoWritePermissionFn
if tt.opts != nil {
tt.opts.Parse()
}
Expand Down Expand Up @@ -1550,3 +1559,62 @@ func Test_repo_commit(t *testing.T) {
})
}
}
func Test_validateRepoWritePermission(t *testing.T) {
tests := map[string]struct {
opts *PushOptions
wantErr bool
beforeFn func(r *mocks.MockRepository, w *mocks.MockWorktree)
}{
"Should fail getting git log": {
opts: nil,
wantErr: true,
beforeFn: func(r *mocks.MockRepository, w *mocks.MockWorktree) {
r.EXPECT().PushContext(gomock.Any(), &gg.PushOptions{
Auth: nil,
Progress: os.Stderr,
}).
Times(1).
Return(gg.NoErrAlreadyUpToDate).
Times(1).
Return(fmt.Errorf("some error"))
},
},
}

gitConfig := &config.Config{
User: struct {
Name string
Email string
}{
Name: "name",
Email: "email",
},
}

for tname, tt := range tests {
t.Run(tname, func(t *testing.T) {
ctrl := gomock.NewController(t)
mockRepo := mocks.NewMockRepository(ctrl)
mockWt := mocks.NewMockWorktree(ctrl)
mockProvider := &mockProvider{}

mockRepo.EXPECT().ConfigScoped(gomock.Any()).Return(gitConfig, nil).AnyTimes()
getProvider = func(providerType, repoURL string, auth *Auth) (Provider, error) { return mockProvider, nil }
worktree = func(r gogit.Repository) (gogit.Worktree, error) { return mockWt, nil }

r := &repo{
Repository: mockRepo,
progress: os.Stderr,
}

tt.beforeFn(mockRepo, mockWt)

err := validateRepoWritePermission(context.Background(), r)
if (err != nil) != tt.wantErr {
t.Errorf("error = %v, wantErr %v", err, tt.wantErr)
return
}

})
}
}