Skip to content

Commit c920931

Browse files
authored
source: copy package contents when fetching source (#2106)
Signed-off-by: Łukasz 'sil2100' Zemczak <[email protected]>
1 parent 8e0e6af commit c920931

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

pkg/source/source.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import (
2424
"os"
2525
"os/exec"
2626
"path/filepath"
27+
"strings"
2728

29+
apkofs "chainguard.dev/apko/pkg/apk/fs"
2830
"chainguard.dev/melange/pkg/build"
2931
"chainguard.dev/melange/pkg/config"
3032
"github.com/chainguard-dev/clog"
@@ -102,6 +104,12 @@ func extractMelangeYamlFromTarball(apkPath, destDir string) error {
102104
func FetchSourceFromMelange(ctx context.Context, filePath, destDir string) (*config.Configuration, error) {
103105
log := clog.FromContext(ctx)
104106

107+
// Make sure destDir is an absolute path
108+
destDir, err := filepath.Abs(destDir)
109+
if err != nil {
110+
return nil, fmt.Errorf("failed to get absolute path for destination directory: %w", err)
111+
}
112+
105113
// Temporary directory for all ephemeral stuff. Best to keep this separate
106114
// as we'll be extracting our pipelines code there, and we wouldn't want
107115
// anyone to cause harm by overwriting the pipelines code.
@@ -176,6 +184,24 @@ func FetchSourceFromMelange(ctx context.Context, filePath, destDir string) (*con
176184
}
177185
defer os.Chdir(wd) //nolint:errcheck
178186

187+
// Also, if we're handling a melange yaml file, check if next to it there is a
188+
// directory called the same name as the melange yaml file, and if so, copy its
189+
// contents to the destination directory. This is needed for patch and others.
190+
if !isApk {
191+
pkgd := strings.TrimSuffix(filePath, filepath.Ext(filePath))
192+
193+
if _, err := os.Stat(pkgd); err == nil {
194+
log.Infof("Found melange directory: %s\nCopying contents to %s\n", pkgd, destDir)
195+
srcFS := apkofs.DirFS(ctx, pkgd)
196+
err := os.CopyFS(destDir, srcFS)
197+
if err != nil {
198+
return nil, fmt.Errorf("failed to copy melange directory contents: %v", err)
199+
}
200+
} else if !os.IsNotExist(err) {
201+
return nil, fmt.Errorf("error checking melange directory: %v", err)
202+
}
203+
}
204+
179205
// Iterate over the pipeline steps and look for any source fetching steps.
180206
for _, step := range cfg.Pipeline {
181207
if step.Uses == "patch" && isApk {

pkg/source/source_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,11 @@ func TestFetchSourceFromMelange(t *testing.T) {
124124
fileName string
125125
expectedSteps []string
126126
expectedName string
127+
expectedFiles []string
127128
}{
128-
{"fetch.yaml", []string{"fetch"}, "fetch"},
129-
{"fetch-with-patch.yaml", []string{"fetch", "patch"}, "fetch-with-patch"},
130-
{"git-checkout.yaml", []string{"git-checkout"}, "git-checkout"},
129+
{"fetch.yaml", []string{"fetch"}, "fetch", nil},
130+
{"fetch-with-patch.yaml", []string{"fetch", "patch"}, "fetch-with-patch", []string{"foo.patch"}},
131+
{"git-checkout.yaml", []string{"git-checkout"}, "git-checkout", nil},
131132
}
132133

133134
// Test each file
@@ -161,6 +162,17 @@ func TestFetchSourceFromMelange(t *testing.T) {
161162
t.Errorf("Expected step %s, got %s", tc.expectedSteps[i], step)
162163
}
163164
}
165+
166+
// Validate the files in the destination directory
167+
if tc.expectedFiles == nil {
168+
return
169+
}
170+
for _, file := range tc.expectedFiles {
171+
filePath := filepath.Join(destDir, file)
172+
if _, err := os.Stat(filePath); os.IsNotExist(err) {
173+
t.Errorf("Expected file %s to exist in %s, but it does not", file, destDir)
174+
}
175+
}
164176
})
165177
}
166178
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TEST CONTENT

0 commit comments

Comments
 (0)