This is an automated email from the ASF dual-hosted git repository.

pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit a88601969333d5f4486e365994e7c2c8ce3b792f
Author: Luis Sergio Carneiro <luis.carne...@sensedia.com>
AuthorDate: Mon Aug 12 08:52:24 2024 -0300

    Creating compareBuilds function
---
 pkg/apis/camel/v1/build_types_support.go | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/pkg/apis/camel/v1/build_types_support.go 
b/pkg/apis/camel/v1/build_types_support.go
index 978c941f3..da0802207 100644
--- a/pkg/apis/camel/v1/build_types_support.go
+++ b/pkg/apis/camel/v1/build_types_support.go
@@ -326,14 +326,9 @@ func (bl BuildList) HasMatchingBuild(build *Build) (bool, 
*Build) {
                        if allMatching && len(required) == len(dependencies) {
                                // seems like both builds require exactly the 
same list of dependencies
                                // additionally check for the creation timestamp
-                               if 
b.CreationTimestamp.Before(&build.CreationTimestamp) {
+                               if compareBuilds(&b, build) < 0 {
                                        return true, &b
-                               } else if 
b.CreationTimestamp.Equal(&build.CreationTimestamp) {
-                                       if strings.Compare(b.Name, build.Name) 
< 0 {
-                                               return true, &b
-                                       }
                                }
-
                        } else if !allMatching && commonDependencies > 0 {
                                // there are common dependencies. let's compare 
the total number of dependencies
                                // in each build. whichever build has less 
dependencies should run first
@@ -341,10 +336,7 @@ func (bl BuildList) HasMatchingBuild(build *Build) (bool, 
*Build) {
                                        return true, &b
                                } else if len(dependencies) == len(required) {
                                        // same number of total dependencies.
-                                       if 
b.CreationTimestamp.Before(&build.CreationTimestamp) {
-                                               return true, &b
-                                       } else if 
b.CreationTimestamp.Equal(&build.CreationTimestamp) &&
-                                               strings.Compare(b.Name, 
build.Name) < 0 {
+                                       if compareBuilds(&b, build) < 0 {
                                                return true, &b
                                        }
                                }
@@ -355,3 +347,13 @@ func (bl BuildList) HasMatchingBuild(build *Build) (bool, 
*Build) {
 
        return false, nil
 }
+
+func compareBuilds(b1 *Build, b2 *Build) int {
+       if b1.CreationTimestamp.Before(&b2.CreationTimestamp) {
+               return -1
+       } else if b2.CreationTimestamp.Before(&b1.CreationTimestamp) {
+               return 1
+       } else {
+               return strings.Compare(b1.Name, b2.Name)
+       }
+}

Reply via email to