You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gf...@apache.org on 2024/08/14 14:09:25 UTC

(camel-k) 02/04: Creating compareBuilds function

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

gfournier pushed a commit to branch release-2.4.x
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit dab96b6e5c9c2383ee65b252d3c5ae5e5391a30a
Author: Luis Sergio Carneiro <lu...@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)
+	}
+}