Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkg/deployment/trigger/devtronApps/preStageHandlerCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@
runtimeParams = runtimeParams.AddSystemVariable(bean.APP_NAME, cdPipeline.App.AppName)
buildRegistryConfig, err := impl.ciTemplateService.GetBaseDockerConfigForCiPipeline(cdPipeline.AppId)
if err != nil {
impl.logger.Errorw("error in getting build configurations", "err", err)

Check failure on line 824 in pkg/deployment/trigger/devtronApps/preStageHandlerCode.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "error in getting build configurations" 4 times.

See more on https://sonarcloud.io/project/issues?id=devtron-labs_devtron&issues=AaFo9VOR4ZlgmNeq2Qcx&open=AaFo9VOR4ZlgmNeq2Qcx&pullRequest=7069
return nil, fmt.Errorf("error in getting build configurations")
}
adapter.UpdateRegistryDetailsToWrfReq(cdStageWorkflowRequest, buildRegistryConfig)
Expand Down Expand Up @@ -962,7 +962,7 @@
If the ci_pipeline_type type is CI_JOB
We will always fetch the registry credentials from the ci_template_override table
*/
func (impl *HandlerServiceImpl) getBuildRegistryConfigForArtifact(sourceCiPipeline *pipelineConfig.CiPipeline, artifact *repository.CiArtifact, appId int) (*types.DockerArtifactStoreBean, error) {

Check failure on line 965 in pkg/deployment/trigger/devtronApps/preStageHandlerCode.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=devtron-labs_devtron&issues=AaFo9VOR4ZlgmNeq2Qcy&open=AaFo9VOR4ZlgmNeq2Qcy&pullRequest=7069
var buildRegistryConfig *types.DockerArtifactStoreBean
var err error

Expand Down Expand Up @@ -1092,8 +1092,12 @@
}

func ReplaceImageTagWithDigest(image, digest string) string {
imageWithoutTag := strings.Split(image, ":")[0]
imageWithDigest := fmt.Sprintf("%s@%s", imageWithoutTag, digest)
// the registry host can carry a port (registry.local:5000/app:v1), so a tag or a digest can only start inside the last path segment
nameStart := strings.LastIndex(image, "/") + 1
if end := strings.IndexAny(image[nameStart:], ":@"); end != -1 {
image = image[:nameStart+end]
}
imageWithDigest := fmt.Sprintf("%s@%s", image, digest)
return imageWithDigest
}

Expand Down
44 changes: 44 additions & 0 deletions pkg/deployment/trigger/devtronApps/preStageHandlerCode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package devtronApps

import "testing"

func TestReplaceImageTagWithDigest(t *testing.T) {
const digest = "sha256:0123456789abcdef"
tests := []struct {
name string
image string
want string
}{
{"tag", "nginx:1.25", "nginx@" + digest},
{"repository path and tag", "team/app:v1", "team/app@" + digest},
{"registry and tag", "registry.example.com/team/app:v1", "registry.example.com/team/app@" + digest},
{"registry with port and tag", "registry.local:5000/team/app:v1", "registry.local:5000/team/app@" + digest},
{"registry with port and no tag", "registry.local:5000/team/app", "registry.local:5000/team/app@" + digest},
{"no tag", "team/app", "team/app@" + digest},
{"tag and old digest", "registry.local:5000/team/app:v1@sha256:fedcba", "registry.local:5000/team/app@" + digest},
{"old digest only", "registry.local:5000/team/app@sha256:fedcba", "registry.local:5000/team/app@" + digest},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ReplaceImageTagWithDigest(tt.image, digest); got != tt.want {
t.Errorf("ReplaceImageTagWithDigest(%q) = %q, want %q", tt.image, got, tt.want)
}
})
}
}