Skip to content

Commit 7af1a6b

Browse files
fix(KONFLUX-11897): handle leading 0 incrementer tags
* Use 10# prefix to force decimal input preventing leading zeros from being treated as octal. * Limit incrementer regex to 1-6 digits ignoring 7+ digit tags. Signed-off-by: Sean Conroy <sconroy@redhat.com>
1 parent b69faf8 commit 7af1a6b

3 files changed

Lines changed: 71 additions & 2 deletions

File tree

tasks/managed/apply-mapping/apply-mapping.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,17 @@ spec:
187187
# Remove `{{ incrementer }}` placeholder to get the version prefix for regex pattern
188188
# shellcheck disable=SC2001
189189
version_prefix=$(echo "${tag_template}" | sed 's/{{ incrementer }}//g')
190-
tag_pattern="^${version_prefix}[0-9]+$" # Build regex pattern dynamically
190+
# Match tags with 1–6 digit increments only. Ignore 7+ digit tags to avoid
191+
# treating short commit SHAs as incrementer values
192+
tag_pattern="^${version_prefix}[0-9]{1,6}$"
191193
192194
# Extract the numeric part of existing tags and find the max increment
193195
max_increment=$(echo "${existing_tags}" | { grep -E "${tag_pattern}" || true; } \
194196
| sed -E "s/${version_prefix}//" | sort -nr | head -n1)
195197
196198
# Calculate the next increment (default to 1 if max_increment is empty or unset)
197-
increment=$((max_increment + 1))
199+
# Use 10# to force decimal input preventing leading 0 from being treated as octal
200+
increment=$((10#${max_increment:-0} + 1))
198201
199202
# Substitute `{{ incrementer }}` in the tag template with the calculated increment
200203
tag="${tag_template//\{\{ incrementer \}\}/${increment}}"

tasks/managed/apply-mapping/tests/mocks.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ function skopeo() {
4646
return
4747
fi
4848

49+
# 7 digit tags: ignored by {1,6} regex, incrementer starts at 1
50+
if [[ "$*" =~ list-tags\ --retry-times\ 3\ docker://repo-leadingzero ]]; then
51+
echo '{"Tags": ["v0.7.0-0760387", "v0.7.0-0760386"]}'
52+
return
53+
fi
54+
55+
# Leading 0 with invalid octal digit: 10# fix prevents octal error
56+
if [[ "$*" =~ list-tags\ --retry-times\ 3\ docker://repo-octal ]]; then
57+
echo '{"Tags": ["v1.0.0-08", "v1.0.0-07"]}'
58+
return
59+
fi
60+
4961
# Raw manifest inspections (for annotations and config.mediaType) - these use the digest from get-image-architectures
5062
if [[ "$*" == "inspect --retry-times 3 --no-tags --raw docker://quay.io/myorg/helm-chart"* ]]
5163
then

tasks/managed/apply-mapping/tests/test-apply-mapping-tag-expansion.yaml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,28 @@ spec:
113113
"url": "repo3a"
114114
}
115115
]
116+
},
117+
{
118+
"name": "comp4-leadingzero",
119+
"repositories": [
120+
{
121+
"url": "repo-leadingzero",
122+
"tags": [
123+
"v0.7.0-{{ incrementer }}"
124+
]
125+
}
126+
]
127+
},
128+
{
129+
"name": "comp5-octal",
130+
"repositories": [
131+
{
132+
"url": "repo-octal",
133+
"tags": [
134+
"v1.0.0-{{ incrementer }}"
135+
]
136+
}
137+
]
116138
}
117139
],
118140
"defaults": {
@@ -148,6 +170,26 @@ spec:
148170
"name": "comp3",
149171
"containerImage": "registry.io/image3@sha256:123456",
150172
"repository": "repo3"
173+
},
174+
{
175+
"name": "comp4-leadingzero",
176+
"containerImage": "registry.io/image4@sha256:123456",
177+
"source": {
178+
"git": {
179+
"revision": "testrevision",
180+
"url": "myurl"
181+
}
182+
}
183+
},
184+
{
185+
"name": "comp5-octal",
186+
"containerImage": "registry.io/image5@sha256:123456",
187+
"source": {
188+
"git": {
189+
"revision": "testrevision",
190+
"url": "myurl"
191+
}
192+
}
151193
}
152194
]
153195
}
@@ -272,3 +314,15 @@ spec:
272314
jq -r '.components[] | select(.name=="comp3") | .repositories[0].tags | length' \
273315
< "$(params.dataDir)/$(context.pipelineRun.uid)/test_snapshot_spec.json"
274316
)" -eq 1
317+
318+
echo Test that comp4 ignores 7 digit tags and starts at 1
319+
test "$(
320+
jq -c '.components[] | select(.name=="comp4-leadingzero").repositories[0].tags' \
321+
< "$(params.dataDir)/$(context.pipelineRun.uid)/test_snapshot_spec.json"
322+
)" == '["defaultTag","v0.7.0-1"]'
323+
324+
echo Test that comp5 handles leading 0 with invalid octal
325+
test "$(
326+
jq -c '.components[] | select(.name=="comp5-octal").repositories[0].tags' \
327+
< "$(params.dataDir)/$(context.pipelineRun.uid)/test_snapshot_spec.json"
328+
)" == '["defaultTag","v1.0.0-9"]'

0 commit comments

Comments
 (0)