Skip to content

Commit 06ff9fc

Browse files
authored
Added support for artifact being single file (#177)
1 parent ec051d6 commit 06ff9fc

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

pkg/artifact/git/repository.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,24 @@ func PullRepository(ctx context.Context, a *artifact.Artifact, opts *RepositoryO
8787
tmpDstDir = filepath.Join(tmpDstDir, a.RelativePath)
8888
}
8989

90-
// Move the directory to the final destination
91-
if err := os.Rename(
92-
tmpDstDir,
93-
a.DestDir()); err != nil {
94-
return nil, fmt.Errorf("renaming directory: %w", err)
90+
fi, err := os.Stat(tmpDstDir)
91+
if err != nil {
92+
return nil, fmt.Errorf("stat temporary artifact: %w", err)
93+
}
94+
95+
finalDest := a.DestDir()
96+
97+
if !fi.IsDir() {
98+
// The artifact is a single file. Create the destination directory
99+
// and adjust the destination path to include the filename.
100+
if err = os.MkdirAll(finalDest, file.FileModeNewDirectory); err != nil {
101+
return nil, fmt.Errorf("creating destination directory for file: %w", err)
102+
}
103+
finalDest = filepath.Join(finalDest, filepath.Base(tmpDstDir))
104+
}
105+
106+
if err = os.Rename(tmpDstDir, finalDest); err != nil {
107+
return nil, fmt.Errorf("moving artifact to destination: %w", err)
95108
}
96109

97110
artifactVersion := artifact.FirstVersionOrLatest(a.Version)

pkg/artifact/git/repository_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,54 @@ func TestPullRepository(t *testing.T) {
4444
assert.DirExists(t, artifactDir)
4545
})
4646

47+
t.Run("success_with_single_file_artifact", func(t *testing.T) {
48+
tmpDir, err := os.MkdirTemp("", "go-common-test-")
49+
require.NoError(t, err)
50+
defer os.RemoveAll(tmpDir)
51+
52+
art := &artifact.Artifact{
53+
Name: "my-repo",
54+
URL: "https://github.com/example/my-repo.git",
55+
BaseDir: tmpDir,
56+
RelativePath: "single-file.yaml",
57+
}
58+
59+
mockRepo := git.NewMockRepository(t)
60+
mockRepo.On("PlainCloneContext", mock.Anything, mock.Anything, mock.Anything).
61+
Run(func(args mock.Arguments) {
62+
cloneDestPath := args.Get(1).(string)
63+
64+
err = os.WriteFile(filepath.Join(cloneDestPath, "single-file.yaml"), []byte("file content"), 0644)
65+
require.NoError(t, err)
66+
err = os.WriteFile(filepath.Join(cloneDestPath, "other-file.yaml"), []byte("file content"), 0644)
67+
require.NoError(t, err)
68+
err = os.MkdirAll(filepath.Join(cloneDestPath, "example"), 0755)
69+
require.NoError(t, err)
70+
err = os.MkdirAll(filepath.Join(cloneDestPath, ".git"), 0755)
71+
require.NoError(t, err)
72+
}).
73+
Return(&gogit.Repository{}, nil)
74+
75+
opts := &RepositoryOptions{
76+
Repository: mockRepo,
77+
}
78+
79+
result, err := PullRepository(context.Background(), art, opts)
80+
81+
require.NoError(t, err)
82+
require.NotNil(t, result)
83+
84+
finalDir := art.DestDir()
85+
finalFile := filepath.Join(finalDir, "single-file.yaml")
86+
noExistDir := filepath.Join(finalDir, "example")
87+
noExistFile := filepath.Join(finalDir, "other-file.yaml")
88+
89+
assert.DirExists(t, finalDir, "The final destination directory should be created")
90+
assert.FileExists(t, finalFile, "The artifact file should exist in the destination directory")
91+
assert.NoDirExists(t, noExistDir, "The final destination should not contain example directory")
92+
assert.NoFileExists(t, noExistFile, "The final destination should not contain other-file.yaml")
93+
})
94+
4795
t.Run("error_no_repository", func(t *testing.T) {
4896
art := &artifact.Artifact{
4997
Name: "my-repo",

0 commit comments

Comments
 (0)