From f14217f82d9673076f3156c4eb43c402f04721a8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rafa=C5=82=20Strzali=C5=84ski?= <rafal@quesma.com>
Date: Thu, 6 Jun 2024 10:52:57 +0200
Subject: [PATCH] Make Version Version Again (#271)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Changes:
- add build date and sent it to the telemetry
- change `private-release.yml` workflow, docker tag is different than
version name now

<img width="528" alt="Screenshot 2024-06-05 at 17 11 01"
src="https://github.com/QuesmaOrg/quesma/assets/1474/2c2743a0-f4f7-4dfe-ba42-230555421b17">


Workflow works.  There is image build using it:

<img width="1350" alt="Screenshot 2024-06-05 at 17 13 30"
src="https://github.com/QuesmaOrg/quesma/assets/1474/d9448b13-3f72-4ffb-8b48-9f6a64679a6b">


Will alter production telemetry in another PR.

@mieciu  Please take a look.

---------

Co-authored-by: Przemysław Hejman <przemyslaw.hejman@quesma.com>
---
 .github/workflows/private-release.yml              | 14 +++++++++-----
 bin/lib.sh                                         |  9 +++++++--
 bin/rebuild-attach.sh                              |  2 +-
 bin/rebuild.sh                                     |  2 +-
 bin/up.sh                                          |  2 +-
 .../clean-clickhouse/schema/05-phone-home-data.sql |  1 +
 quesma/Dockerfile                                  |  4 ++++
 quesma/buildinfo/build.go                          |  1 +
 quesma/main.go                                     |  4 ++--
 quesma/telemetry/phone_home.go                     |  2 ++
 10 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/.github/workflows/private-release.yml b/.github/workflows/private-release.yml
index 8c34ca0dd..d967cde04 100644
--- a/.github/workflows/private-release.yml
+++ b/.github/workflows/private-release.yml
@@ -3,11 +3,11 @@ name: Build Quesma container image for customers and prospects (private beta rep
 on:
   workflow_dispatch:
     inputs:
-      GIT_REF:
-        description: 'Commit hash to use for the release (can be a branch name or tag)'
+      RELEASE_TAG:
+        description: 'Tag to use for the release. This tag will be show as version in telemetry.'
         required: true
       VERSION_TAG:
-        description: 'Version tag for the released Docker image'
+        description: 'Version tag for the released Docker image. This tag will be used to pull the image.'
         required: true
       LICENSE_KEY:
         description: 'License key - when empty, development key will be used'
@@ -23,7 +23,10 @@ jobs:
     steps:
       - uses: actions/checkout@v4
         with:
-          ref: ${{ github.event.inputs.GIT_REF }}
+          ref: ${{ github.event.inputs.RELEASE_TAG }}
+
+      - name: Set build date
+        run: echo QUESMA_BUILD_DATE=$(git --no-pager log -1 --date=format:'%Y-%m-%d' --format="%ad")  >> $GITHUB_ENV
 
       - name: Set up Docker Buildx
         uses: docker/setup-buildx-action@v3
@@ -43,6 +46,7 @@ jobs:
           push: ${{ github.event_name == 'workflow_dispatch' }}
           build-args: |
             QUESMA_BUILD_SHA=${{ github.sha }}
-            QUESMA_VERSION=${{ github.event.inputs.VERSION_TAG }}
+            QUESMA_VERSION=${{ github.event.inputs.RELEASE_TAG }}
             QUESMA_LICENSE_KEY=${{ github.event.inputs.LICENSE_KEY }}
+            QUESMA_BUILD_DATE=${{ env.QUESMA_BUILD_DATE }}
           platforms: linux/amd64,linux/arm64
diff --git a/bin/lib.sh b/bin/lib.sh
index cf5273e36..777e64d0e 100644
--- a/bin/lib.sh
+++ b/bin/lib.sh
@@ -17,5 +17,10 @@ if [ ! -f "$QUESMA_COMPOSE_FILE" ]; then
 fi
 
 # Get the current git commit hash
-QUESMA_VERSION=$(git rev-parse --short HEAD)
-DOCKER_COMPOSE_BUILD_ARGS="--build-arg QUESMA_VERSION=$QUESMA_VERSION"
+QUESMA_BUILD_DATE=$(git --no-pager log -1 --date=format:'%Y-%m-%d' --format="%ad")
+QUESMA_VERSION=$(git describe)
+QUESMA_BUILD_SHA=$(git rev-parse --short HEAD)
+
+
+
+
diff --git a/bin/rebuild-attach.sh b/bin/rebuild-attach.sh
index 1566165f3..3afb8a5a0 100755
--- a/bin/rebuild-attach.sh
+++ b/bin/rebuild-attach.sh
@@ -5,6 +5,6 @@ set -e
 cd "$(dirname "$0/")/.."
 source bin/lib.sh
 
-docker compose  -f "$QUESMA_COMPOSE_FILE" build ${DOCKER_COMPOSE_BUILD_ARGS} quesma && \
+docker compose  -f "$QUESMA_COMPOSE_FILE" build "$QUESMA_COMPOSE_FILE" build --build-arg QUESMA_BUILD_DATE="$QUESMA_BUILD_DATE" --build-arg QUESMA_VERSION="$QUESMA_VERSION" --build-arg QUESMA_BUILD_SHA="$QUESMA_BUILD_SHA" quesma && \
   docker compose -f "$QUESMA_COMPOSE_FILE" stop quesma && \
   docker compose -f "$QUESMA_COMPOSE_FILE" up --no-deps quesma
diff --git a/bin/rebuild.sh b/bin/rebuild.sh
index 03d9a03f6..0d4152b4d 100755
--- a/bin/rebuild.sh
+++ b/bin/rebuild.sh
@@ -5,6 +5,6 @@ set -e
 cd "$(dirname "$0/")/.."
 source bin/lib.sh
 
-docker compose -f "$QUESMA_COMPOSE_FILE" build ${DOCKER_COMPOSE_BUILD_ARGS} quesma && \
+docker compose -f "$QUESMA_COMPOSE_FILE" build "$QUESMA_COMPOSE_FILE" build --build-arg QUESMA_BUILD_DATE="$QUESMA_BUILD_DATE" --build-arg QUESMA_VERSION="$QUESMA_VERSION" --build-arg QUESMA_BUILD_SHA="$QUESMA_BUILD_SHA" quesma && \
   docker compose -f "$QUESMA_COMPOSE_FILE" stop quesma && \
   docker compose -f "$QUESMA_COMPOSE_FILE" up -d --no-deps quesma
diff --git a/bin/up.sh b/bin/up.sh
index 5555ce8e2..e17f148b9 100755
--- a/bin/up.sh
+++ b/bin/up.sh
@@ -4,7 +4,7 @@ set -e
 cd "$(dirname "$0/")/.."
 source bin/lib.sh
 
-docker compose  -f "$QUESMA_COMPOSE_FILE" build ${DOCKER_COMPOSE_BUILD_ARGS} && docker compose -f "$QUESMA_COMPOSE_FILE" up -d
+docker compose  -f "$QUESMA_COMPOSE_FILE" build --build-arg QUESMA_BUILD_DATE="$QUESMA_BUILD_DATE" --build-arg QUESMA_VERSION="$QUESMA_VERSION" --build-arg QUESMA_BUILD_SHA="$QUESMA_BUILD_SHA" && docker compose -f "$QUESMA_COMPOSE_FILE" up -d
 
 cat <<"EOF"
                ________
diff --git a/docker/clean-clickhouse/schema/05-phone-home-data.sql b/docker/clean-clickhouse/schema/05-phone-home-data.sql
index aafa155bf..f02f98f0a 100644
--- a/docker/clean-clickhouse/schema/05-phone-home-data.sql
+++ b/docker/clean-clickhouse/schema/05-phone-home-data.sql
@@ -122,6 +122,7 @@ CREATE TABLE IF NOT EXISTS "phone_home_data"
     "config_mode" Nullable(String),
     "top_errors"  Array(String),
     "build_hash" Nullable(String),
+    "build_date" Nullable(DateTime64(3)),
     "number_of_failed_requests" Nullable(Int64),
 
     )
diff --git a/quesma/Dockerfile b/quesma/Dockerfile
index c8d418770..3815944f2 100644
--- a/quesma/Dockerfile
+++ b/quesma/Dockerfile
@@ -12,6 +12,7 @@ ENV GOCACHE=/root/.cache/go-build
 ARG QUESMA_BUILD_SHA
 ARG QUESMA_VERSION
 ARG QUESMA_LICENSE_KEY
+ARG QUESMA_BUILD_DATE
 
 COPY util/healthcheck util/healthcheck
 
@@ -31,6 +32,9 @@ RUN --mount=type=cache,target="/root/.cache/go-build" \
     -X 'buildinfo.Version=$QUESMA_VERSION'  \
     -X 'mitmproxy/quesma/buildinfo.LicenseKey=$QUESMA_LICENSE_KEY'  \
     -X 'buildinfo.LicenseKey=$QUESMA_LICENSE_KEY' \
+    -X 'mitmproxy/quesma/buildinfo.BuildDate=$QUESMA_BUILD_DATE'  \
+    -X 'buildinfo.BuildDate=$QUESMA_BUILD_DATE' \
+
     " \
     -o bin
 
diff --git a/quesma/buildinfo/build.go b/quesma/buildinfo/build.go
index ef17dd3db..4237b1ef3 100644
--- a/quesma/buildinfo/build.go
+++ b/quesma/buildinfo/build.go
@@ -6,4 +6,5 @@ const (
 
 var Version = "development"
 var BuildHash = ""
+var BuildDate = ""
 var LicenseKey = DevelopmentLicenseKey
diff --git a/quesma/main.go b/quesma/main.go
index 643e8fb77..196fd092c 100644
--- a/quesma/main.go
+++ b/quesma/main.go
@@ -31,8 +31,8 @@ const banner = `
 
 func main() {
 	println(banner)
-	fmt.Printf("Quesma build info: version=[%s], build hash=[%s], license key=[%s]\n",
-		buildinfo.Version, buildinfo.BuildHash, config.MaskLicenseKey(buildinfo.LicenseKey))
+	fmt.Printf("Quesma build info: version=[%s], build hash=[%s], build date=[%s] license key=[%s]\n",
+		buildinfo.Version, buildinfo.BuildHash, buildinfo.BuildDate, config.MaskLicenseKey(buildinfo.LicenseKey))
 
 	sig := make(chan os.Signal, 1)
 	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
diff --git a/quesma/telemetry/phone_home.go b/quesma/telemetry/phone_home.go
index c810b5f8b..8bb2bd3bc 100644
--- a/quesma/telemetry/phone_home.go
+++ b/quesma/telemetry/phone_home.go
@@ -66,6 +66,7 @@ type PhoneHomeStats struct {
 	Hostname       string `json:"hostname"`
 	QuesmaVersion  string `json:"quesma_version"`
 	BuildHash      string `json:"build_hash"`
+	BuildDate      string `json:"build_date"`
 	InstanceID     string `json:"instanceId"`
 
 	// add more stats here about running
@@ -470,6 +471,7 @@ func (a *agent) collect(ctx context.Context, reportType string) (stats PhoneHome
 	stats.TakenAt = time.Now().Unix()
 	stats.QuesmaVersion = buildinfo.Version
 	stats.BuildHash = buildinfo.BuildHash
+	stats.BuildDate = buildinfo.BuildDate
 	stats.NumberOfPanics = recovery.PanicCounter.Load()
 	stats.InstanceID = a.instanceId