From 450f355bc7dd68c449700c8c5f3ff89d45103474 Mon Sep 17 00:00:00 2001
From: Vladimir Kozhukalov <kozhukalov@gmail.com>
Date: Sat, 21 Dec 2024 11:47:15 -0600
Subject: [PATCH] Append metadata suffix when building charts

Change-Id: I04332e9f18a5f4c5973aae51dbd746f4e1eadc7c
---
 Makefile               |  6 +++++-
 tools/chart_version.sh | 24 ++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100755 tools/chart_version.sh

diff --git a/Makefile b/Makefile
index ec8b48d9de..35fbd38c12 100644
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,8 @@ ifdef PACKAGE_DIR
 	PKG_ARGS += --destination $(PACKAGE_DIR)
 endif
 
+BASE_VERSION ?= 2024.2.0
+
 CHART_DIRS := $(subst /,,$(dir $(wildcard */Chart.yaml)))
 CHARTS := $(sort helm-toolkit $(CHART_DIRS))
 
@@ -60,7 +62,9 @@ lint-%: init-%
 	if [ -d $* ]; then $(HELM) lint $*; fi
 
 build-%: lint-%
-	if [ -d $* ]; then $(HELM) package $* $(PKG_ARGS); fi
+	if [ -d $* ]; then \
+		$(HELM) package $* --version $$(tools/chart_version.sh $* $(BASE_VERSION)) $(PKG_ARGS); \
+	fi
 
 # This is used exclusively with helm3 building in the gate to publish
 package-%: init-%
diff --git a/tools/chart_version.sh b/tools/chart_version.sh
new file mode 100755
index 0000000000..17a35b3ac6
--- /dev/null
+++ b/tools/chart_version.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+if [[ $# -lt 2 ]]; then
+    echo "Usage: $0 <chart_dir> <base_version>"
+    echo "  <chart_dir> - The chart directory."
+    echo "  <base_version> - The base version. For example 2024.2.0."
+    echo "                   Will be modified to 2024.2.<patch>+<commit_sha>"
+    exit 1
+fi
+
+CHART_DIR=$1
+BASE_VERSION=$2
+MAJOR=$(echo $BASE_VERSION | cut -d. -f1);
+MINOR=$(echo $BASE_VERSION | cut -d. -f2);
+
+if git show-ref --tags $BASE_VERSION --quiet; then
+    PATCH=$(git log --oneline ${BASE_VERSION}.. $CHART_DIR | wc -l)
+else
+    PATCH=$(git log --oneline $CHART_DIR | wc -l)
+fi
+OSH_COMMIT_SHA=$(git rev-parse --short HEAD);
+OSH_INFRA_COMMIT_SHA=$(cd ../openstack-helm-infra; git rev-parse --short HEAD);
+
+echo "${MAJOR}.${MINOR}.${PATCH}+${OSH_COMMIT_SHA}-${OSH_INFRA_COMMIT_SHA}"