Skip to content

Commit d75d528

Browse files
authored
Git name and email not properly reflect in commit message (#113)
When making a commit using the export command to a Git repository, the configure name and email from the configuration file isn't properly populating the git message. That issue is addressed and the git commit message is now properly updated. This change also adds full unit testing for the runners repository implementation.
1 parent 8f7f191 commit d75d528

30 files changed

Lines changed: 5665 additions & 63 deletions

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ require (
5353
github.com/sourcegraph/conc v0.3.0 // indirect
5454
github.com/spf13/afero v1.11.0 // indirect
5555
github.com/spf13/cast v1.6.0 // indirect
56+
github.com/stretchr/objx v0.5.2 // indirect
5657
github.com/subosito/gotenv v1.6.0 // indirect
5758
github.com/xanzy/ssh-agent v0.3.3 // indirect
5859
github.com/xdg-go/pbkdf2 v1.0.0 // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+
125125
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
126126
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
127127
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
128+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
128129
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
129130
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
130131
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

internal/runners/adapters.go

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ func NewAdapterRunner(c client.Client, cfg *config.Config) *AdapterRunner {
3535
}
3636
}
3737

38-
//////////////////////////////////////////////////////////////////////////////
39-
// Reader Interface
40-
//
38+
/*
39+
******************************************************************************
40+
Reader Interface
41+
******************************************************************************
42+
*/
4143

4244
// Get is the implementation of the command `get adapters`
4345
func (r *AdapterRunner) Get(in Request) (*Response, error) {
@@ -74,9 +76,11 @@ func (r *AdapterRunner) Describe(in Request) (*Response, error) {
7476
}, nil
7577
}
7678

77-
//////////////////////////////////////////////////////////////////////////////
78-
// Writer Interface
79-
//
79+
/*
80+
******************************************************************************
81+
Writer Interface
82+
******************************************************************************
83+
*/
8084

8185
func (r *AdapterRunner) Create(in Request) (*Response, error) {
8286
logger.Trace()
@@ -163,9 +167,11 @@ func (r *AdapterRunner) Clear(in Request) (*Response, error) {
163167
return notImplemented(in)
164168
}
165169

166-
//////////////////////////////////////////////////////////////////////////////
167-
// Editor Interface
168-
//
170+
/*
171+
******************************************************************************
172+
Editor Interface
173+
******************************************************************************
174+
*/
169175

170176
// Edit implements the `edti adapter ...` command
171177
func (r *AdapterRunner) Edit(in Request) (*Response, error) {
@@ -193,9 +199,11 @@ func (r *AdapterRunner) Edit(in Request) (*Response, error) {
193199
}, nil
194200
}
195201

196-
//////////////////////////////////////////////////////////////////////////////
197-
// Copier Interface
198-
//
202+
/*
203+
******************************************************************************
204+
Copier Interface
205+
******************************************************************************
206+
*/
199207

200208
func (r *AdapterRunner) Copy(in Request) (*Response, error) {
201209
logger.Trace()
@@ -280,9 +288,11 @@ func (r *AdapterRunner) CopyTo(profile string, in any, replace bool) (any, error
280288

281289
}
282290

283-
//////////////////////////////////////////////////////////////////////////////
284-
// Importer Interface
285-
//
291+
/*
292+
******************************************************************************
293+
Importer Interface
294+
******************************************************************************
295+
*/
286296

287297
// Import provides the implementation for `import adapter <filepath>`
288298
func (r *AdapterRunner) Import(in Request) (*Response, error) {
@@ -305,9 +315,11 @@ func (r *AdapterRunner) Import(in Request) (*Response, error) {
305315
}, nil
306316
}
307317

308-
//////////////////////////////////////////////////////////////////////////////
309-
// Exporter Interface
310-
//
318+
/*
319+
******************************************************************************
320+
Exporter Interface
321+
******************************************************************************
322+
*/
311323

312324
// Export) provides the implementation for `export adapter <name>`
313325
func (r *AdapterRunner) Export(in Request) (*Response, error) {
@@ -333,7 +345,7 @@ func (r *AdapterRunner) Export(in Request) (*Response, error) {
333345

334346
/*
335347
*******************************************************************************
336-
Inspector interfaceo
348+
Inspector interface
337349
*******************************************************************************
338350
*/
339351

@@ -352,9 +364,11 @@ func (r *AdapterRunner) Inspect(in Request) (*Response, error) {
352364
}, nil
353365
}
354366

355-
//////////////////////////////////////////////////////////////////////////////
356-
// Controller interface
357-
//
367+
/*
368+
*******************************************************************************
369+
Controller interface
370+
*******************************************************************************
371+
*/
358372

359373
func (r *AdapterRunner) Start(in Request) (*Response, error) {
360374
logger.Trace()
@@ -398,9 +412,11 @@ func (r *AdapterRunner) Restart(in Request) (*Response, error) {
398412
}, nil
399413
}
400414

401-
//////////////////////////////////////////////////////////////////////////////
402-
// Dumper Interface
403-
//
415+
/*
416+
*******************************************************************************
417+
Dumper interface
418+
*******************************************************************************
419+
*/
404420

405421
// Dump implements the `dump adapters...` command
406422
func (r *AdapterRunner) Dump(in Request) (*Response, error) {
@@ -426,9 +442,11 @@ func (r *AdapterRunner) Dump(in Request) (*Response, error) {
426442
}, nil
427443
}
428444

429-
//////////////////////////////////////////////////////////////////////////////
430-
// Loader Interface
431-
//
445+
/*
446+
*******************************************************************************
447+
Loader interface
448+
*******************************************************************************
449+
*/
432450

433451
// Load implements the `load adapters ...` command
434452
func (r *AdapterRunner) Load(in Request) (*Response, error) {
@@ -473,9 +491,11 @@ func (r *AdapterRunner) Load(in Request) (*Response, error) {
473491
}, nil
474492
}
475493

476-
//////////////////////////////////////////////////////////////////////////////
477-
// Private functions
478-
//
494+
/*
495+
*******************************************************************************
496+
Private functions
497+
*******************************************************************************
498+
*/
479499

480500
func (r *AdapterRunner) importAdapter(in services.Adapter, replace bool) error {
481501
logger.Trace()

internal/runners/exporter.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ func exportAssets(in Request, assets map[string]interface{}) error {
8686
return e
8787
}
8888

89-
repoPath, e = repo.Clone()
89+
repoPath, e = repo.Clone(
90+
&FileReaderImpl{},
91+
&ClonerImpl{},
92+
)
9093
if e != nil {
9194
return e
9295
}

internal/runners/git.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2024 Itential Inc. All Rights Reserved
2+
// Unauthorized copying of this file, via any medium is strictly prohibited
3+
// Proprietary and confidential
4+
5+
package runners
6+
7+
import (
8+
"github.com/go-git/go-git/v5"
9+
"github.com/go-git/go-git/v5/plumbing"
10+
)
11+
12+
// GitProviderImpl is the real implementation that uses go-git
13+
type GitProviderImpl struct{}
14+
15+
func (p *GitProviderImpl) Open(path string) (GitRepository, error) {
16+
repo, err := git.PlainOpen(path)
17+
if err != nil {
18+
return nil, err
19+
}
20+
return &GitRepositoryImpl{repo}, nil
21+
}
22+
23+
// GitRepositoryImpl wraps go-git's Repository
24+
type GitRepositoryImpl struct {
25+
repo *git.Repository
26+
}
27+
28+
func (r *GitRepositoryImpl) Worktree() (GitWorktree, error) {
29+
w, err := r.repo.Worktree()
30+
if err != nil {
31+
return nil, err
32+
}
33+
return &GitWorktreeImpl{w}, nil
34+
}
35+
36+
func (r *GitRepositoryImpl) Push(opts *PushOptions) error {
37+
return r.repo.Push(opts)
38+
}
39+
40+
// GitWorktreeImpl wraps go-git's Worktree
41+
type GitWorktreeImpl struct {
42+
w *git.Worktree
43+
}
44+
45+
func (w *GitWorktreeImpl) AddGlob(pattern string) error {
46+
return w.w.AddGlob(pattern)
47+
}
48+
49+
func (w *GitWorktreeImpl) Status() (GitStatus, error) {
50+
status, err := w.w.Status()
51+
if err != nil {
52+
return nil, err
53+
}
54+
return &GitStatusImpl{status}, nil
55+
}
56+
57+
func (w *GitWorktreeImpl) Commit(msg string, opts *CommitOptions) (Hash, error) {
58+
commitHash, err := w.w.Commit(msg, opts)
59+
return plumbing.Hash(commitHash), err
60+
}
61+
62+
// GitStatusImpl wraps go-git's Status object
63+
type GitStatusImpl struct {
64+
status git.Status
65+
}
66+
67+
func (s *GitStatusImpl) IsClean() bool {
68+
return s.status.IsClean()
69+
}

internal/runners/importer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func importGetPathFromRequest(in Request) (string, error) {
7474
return "", err
7575
}
7676

77-
p, err := CloneRepository(r)
77+
p, err := r.Clone(&FileReaderImpl{}, &ClonerImpl{})
7878
if err != nil {
7979
return "", err
8080
}

internal/runners/interfaces.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
package runners
66

7+
import (
8+
"github.com/go-git/go-git/v5"
9+
"github.com/go-git/go-git/v5/plumbing"
10+
)
11+
712
type Runner interface {
813
}
914

@@ -55,3 +60,34 @@ type Dumper interface {
5560
type Loader interface {
5661
Load(Request) (*Response, error)
5762
}
63+
64+
type FileReader interface {
65+
Read(path string) ([]byte, error)
66+
}
67+
68+
type Cloner interface {
69+
Clone(r RepositoryPayload) (string, error)
70+
}
71+
72+
type GitRepository interface {
73+
Worktree() (GitWorktree, error)
74+
Push(options *PushOptions) error
75+
}
76+
77+
type GitWorktree interface {
78+
AddGlob(pattern string) error
79+
Status() (GitStatus, error)
80+
Commit(msg string, opts *CommitOptions) (Hash, error)
81+
}
82+
83+
type GitStatus interface {
84+
IsClean() bool
85+
}
86+
87+
type GitProvider interface {
88+
Open(path string) (GitRepository, error)
89+
}
90+
91+
type PushOptions = git.PushOptions
92+
type CommitOptions = git.CommitOptions
93+
type Hash = plumbing.Hash

internal/runners/models.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,10 @@ func (r *ModelRunner) Export(in Request) (*Response, error) {
445445

446446
var e error
447447

448-
repoPath, e = repo.Clone()
448+
repoPath, e = repo.Clone(
449+
&FileReaderImpl{},
450+
&ClonerImpl{},
451+
)
449452
if e != nil {
450453
return nil, e
451454
}

internal/runners/prebuilts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ func (r *PrebuiltRunner) Export(in Request) (*Response, error) {
352352

353353
var e error
354354

355-
repoPath, e = repo.Clone()
355+
repoPath, e = repo.Clone(&FileReaderImpl{}, &ClonerImpl{})
356356
if e != nil {
357357
return nil, e
358358
}

internal/runners/projects.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func (r *ProjectRunner) Export(in Request) (*Response, error) {
359359

360360
var e error
361361

362-
repoPath, e = repo.Clone()
362+
repoPath, e = repo.Clone(&FileReaderImpl{}, &ClonerImpl{})
363363
if e != nil {
364364
return nil, e
365365
}

0 commit comments

Comments
 (0)