Skip to content

Commit 8c42fca

Browse files
authored
fix(image): avoid repeating registryURL in image.GetFullNameWithoutTag and GetFullNameWithTag func (#1310)
Signed-off-by: Cheng Fang <cfang@redhat.com>
1 parent 5bc368c commit 8c42fca

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

registry-scanner/pkg/image/image.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ func (img *ContainerImage) String() string {
8080
func (img *ContainerImage) GetFullNameWithoutTag() string {
8181
str := ""
8282
if img.RegistryURL != "" {
83-
str += img.RegistryURL + "/"
83+
if !strings.HasPrefix(img.ImageName, img.RegistryURL+"/") {
84+
str += img.RegistryURL + "/"
85+
}
8486
}
8587
str += img.ImageName
8688
return str
@@ -91,7 +93,9 @@ func (img *ContainerImage) GetFullNameWithoutTag() string {
9193
func (img *ContainerImage) GetFullNameWithTag() string {
9294
str := ""
9395
if img.RegistryURL != "" {
94-
str += img.RegistryURL + "/"
96+
if !strings.HasPrefix(img.ImageName, img.RegistryURL+"/") {
97+
str += img.RegistryURL + "/"
98+
}
9599
}
96100
str += img.ImageName
97101
if img.ImageTag != nil {

registry-scanner/pkg/image/image_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ func Test_ParseImageTags(t *testing.T) {
6363
assert.Equal(t, "0.1", image.ImageTag.TagName)
6464
assert.Equal(t, "docker.io/jannfis/test-image:0.1", image.GetFullNameWithTag())
6565
assert.Equal(t, "docker.io/jannfis/test-image", image.GetFullNameWithoutTag())
66+
67+
// if the image name starts with registryURL, GetFullNameWithoutTag and GetFullNameWithTag
68+
// should return the correct full image name without repeating registryURL.
69+
// Wrong full image name: docker.io/docker.io/jannfis/test-image
70+
image.ImageName = "docker.io/jannfis/test-image"
71+
assert.Equal(t, "docker.io/jannfis/test-image:0.1", image.GetFullNameWithTag())
72+
assert.Equal(t, "docker.io/jannfis/test-image", image.GetFullNameWithoutTag())
6673
})
6774

6875
t.Run("Parse valid image name with digest tag", func(t *testing.T) {

0 commit comments

Comments
 (0)