Skip to content

Commit 74df2dc

Browse files
fix: [CI-18487]: Maintaining new functionality with backward compatibility.
1 parent 204d344 commit 74df2dc

File tree

2 files changed

+100
-34
lines changed

2 files changed

+100
-34
lines changed

plugin.go

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (p *Plugin) Exec() error {
141141

142142
// Validate strip prefix pattern and precompile regex once
143143
normalizedStrip := strings.ReplaceAll(p.StripPrefix, "\\", "/")
144-
if p.StripPrefix != "" {
144+
if p.StripPrefix != "" && strings.HasPrefix(normalizedStrip, "/") {
145145
if err := validateStripPrefix(p.StripPrefix); err != nil {
146146
log.WithFields(log.Fields{
147147
"error": err,
@@ -152,7 +152,7 @@ func (p *Plugin) Exec() error {
152152
}
153153

154154
var compiled *regexp.Regexp
155-
if normalizedStrip != "" && strings.ContainsAny(normalizedStrip, "*?") {
155+
if normalizedStrip != "" && strings.HasPrefix(normalizedStrip, "/") && strings.ContainsAny(normalizedStrip, "*?") {
156156
var err error
157157
compiled, err = patternToRegex(normalizedStrip)
158158
if err != nil {
@@ -172,30 +172,49 @@ func (p *Plugin) Exec() error {
172172
continue
173173
}
174174

175-
// Preview stripping (using precompiled regex when available)
175+
// Preview stripping (wildcard for absolute patterns, literal for relative patterns)
176176
stripped := match
177177
matched := false
178178
if normalizedStrip != "" {
179-
var err error
180-
stripped, matched, err = stripWildcardPrefixWithRegex(match, normalizedStrip, compiled)
181-
if err != nil {
182-
log.WithFields(log.Fields{
183-
"error": err,
184-
"path": match,
185-
"pattern": p.StripPrefix,
186-
}).Warn("Failed to strip prefix, using original path")
187-
stripped = match
179+
if strings.HasPrefix(normalizedStrip, "/") {
180+
var err error
181+
stripped, matched, err = stripWildcardPrefixWithRegex(match, normalizedStrip, compiled)
182+
if err != nil {
183+
log.WithFields(log.Fields{
184+
"error": err,
185+
"path": match,
186+
"pattern": p.StripPrefix,
187+
}).Warn("Failed to strip prefix, using original path")
188+
stripped = match
189+
}
190+
} else {
191+
// Backward-compat: literal TrimPrefix for relative strip_prefix (no leading '/')
192+
m := filepath.ToSlash(match)
193+
trimmed := strings.TrimPrefix(m, normalizedStrip)
194+
if trimmed != m {
195+
matched = true
196+
stripped = trimmed
197+
} else {
198+
stripped = match
199+
}
188200
}
189201
}
190202
if matched {
191203
anyMatched = true
192204
}
193205

194-
// Build final key (ensure relative component for join)
195-
rel := strings.TrimPrefix(filepath.ToSlash(stripped), "/")
196-
target := filepath.ToSlash(filepath.Join(p.Target, rel))
197-
if !strings.HasPrefix(target, "/") {
198-
target = "/" + target
206+
// Build final key
207+
var target string
208+
if normalizedStrip != "" && !strings.HasPrefix(normalizedStrip, "/") {
209+
// Relative strip_prefix: use master resolveKey behavior
210+
target = resolveKey(p.Target, filepath.ToSlash(match), p.StripPrefix)
211+
} else {
212+
// Absolute strip_prefix (wildcards): join stripped suffix under target
213+
rel := strings.TrimPrefix(filepath.ToSlash(stripped), "/")
214+
target = filepath.ToSlash(filepath.Join(p.Target, rel))
215+
if !strings.HasPrefix(target, "/") {
216+
target = "/" + target
217+
}
199218
}
200219

201220
contentType := matchExtension(match, p.ContentType)
@@ -376,21 +395,7 @@ func assumeRole(roleArn, roleSessionName, externalID string) *credentials.Creden
376395
// resolveKey is a helper function that returns s3 object key where file present at srcPath is uploaded to.
377396
// srcPath is assumed to be in forward slash format
378397
func resolveKey(target, srcPath, stripPrefix string) string {
379-
// Use wildcard-aware prefix stripping
380-
stripped, err := stripWildcardPrefix(srcPath, stripPrefix)
381-
if err != nil {
382-
// Log error but continue with original path
383-
log.WithFields(log.Fields{
384-
"error": err,
385-
"path": srcPath,
386-
"pattern": stripPrefix,
387-
}).Warn("Failed to strip prefix, using original path")
388-
stripped = srcPath
389-
}
390-
// Ensure we never drop the target when the stripped path is absolute.
391-
// Always join with a relative path component.
392-
stripped = strings.TrimPrefix(stripped, "/")
393-
key := filepath.Join(target, stripped)
398+
key := filepath.Join(target, strings.TrimPrefix(srcPath, filepath.ToSlash(stripPrefix)))
394399
key = filepath.ToSlash(key)
395400
if !strings.HasPrefix(key, "/") {
396401
key = "/" + key

wildcard_strip_test.go

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ func TestPatternToRegex(t *testing.T) {
185185
}
186186

187187
// ===============================
188-
// INTEGRATION TESTS - ResolveKey
188+
// INTEGRATION TESTS - Wildcard build (absolute patterns)
189189
// ===============================
190190

191-
func TestResolveKeyWithWildcards(t *testing.T) {
191+
func TestBuildKeyWithWildcards(t *testing.T) {
192192
tests := []struct {
193193
name string
194194
target string
@@ -233,6 +233,67 @@ func TestResolveKeyWithWildcards(t *testing.T) {
233233
},
234234
}
235235

236+
for _, tt := range tests {
237+
t.Run(tt.name, func(t *testing.T) {
238+
// Use wildcard/literal strip for absolute patterns, then join under target
239+
suffix, err := stripWildcardPrefix(tt.srcPath, tt.stripPrefix)
240+
if err != nil {
241+
t.Fatalf("stripWildcardPrefix(%q, %q) error: %v", tt.srcPath, tt.stripPrefix, err)
242+
}
243+
rel := strings.TrimPrefix(filepath.ToSlash(suffix), "/")
244+
key := filepath.ToSlash(filepath.Join(tt.target, rel))
245+
if !strings.HasPrefix(key, "/") {
246+
key = "/" + key
247+
}
248+
if key != tt.expected {
249+
t.Errorf("built key = %q, want %q", key, tt.expected)
250+
}
251+
})
252+
}
253+
}
254+
255+
// ===============================
256+
// INTEGRATION TESTS - ResolveKey (backward compat literal behavior)
257+
// ===============================
258+
259+
func TestResolveKey_BackCompat(t *testing.T) {
260+
tests := []struct {
261+
name string
262+
target string
263+
srcPath string
264+
stripPrefix string
265+
expected string
266+
}{
267+
{
268+
name: "Relative strip_prefix trims relative path",
269+
target: "hello",
270+
srcPath: "foo/bar.zip",
271+
stripPrefix: "foo/",
272+
expected: "/hello/bar.zip",
273+
},
274+
{
275+
name: "Relative strip_prefix does not trim absolute path",
276+
target: "hello",
277+
srcPath: "/foo/bar.zip",
278+
stripPrefix: "foo/",
279+
expected: "/hello/foo/bar.zip",
280+
},
281+
{
282+
name: "Absolute literal prefix trims absolute path",
283+
target: "hello",
284+
srcPath: "/foo/bar.zip",
285+
stripPrefix: "/foo/",
286+
expected: "/hello/bar.zip",
287+
},
288+
{
289+
name: "No strip_prefix provided",
290+
target: "/hello",
291+
srcPath: "/foo/bar.zip",
292+
stripPrefix: "",
293+
expected: "/hello/foo/bar.zip",
294+
},
295+
}
296+
236297
for _, tt := range tests {
237298
t.Run(tt.name, func(t *testing.T) {
238299
result := resolveKey(tt.target, tt.srcPath, tt.stripPrefix)

0 commit comments

Comments
 (0)