From d03042f7d3af855252eee07b0bf5337249d842f7 Mon Sep 17 00:00:00 2001
From: jkrvivian <jkrvivian@gmail.com>
Date: Tue, 20 Oct 2020 17:25:38 +0800
Subject: [PATCH] feat: Add commit tag to version

---
 plugins/analysis/client/fpc_heartbeato.go     |  2 +-
 plugins/analysis/client/heartbeat.go          |  2 +-
 plugins/analysis/client/metric_heartbeat.go   |  2 +-
 plugins/analysis/packet/fpc_heartbeat.go      |  2 +-
 plugins/analysis/packet/fpc_heartbeat_test.go |  2 +-
 plugins/analysis/packet/metric_heartbeat.go   |  2 +-
 .../analysis/packet/metric_heartbeat_test.go  |  2 +-
 plugins/analysis/server/plugin.go             |  4 ++--
 plugins/banner/plugin.go                      | 22 +++++++++++++++++--
 plugins/metrics/global_metrics.go             |  4 ++--
 plugins/prometheus/global_metrics.go          |  4 ++--
 plugins/remotelog/plugin.go                   |  2 +-
 12 files changed, 34 insertions(+), 16 deletions(-)

diff --git a/plugins/analysis/client/fpc_heartbeato.go b/plugins/analysis/client/fpc_heartbeato.go
index 4d0f72b1..50467520 100644
--- a/plugins/analysis/client/fpc_heartbeato.go
+++ b/plugins/analysis/client/fpc_heartbeato.go
@@ -39,7 +39,7 @@ func onRoundExecuted(roundStats *vote.RoundStats) {
 		}
 
 		hb := &packet.FPCHeartbeat{
-			Version:    banner.AppVersion,
+			Version:    banner.SimplifiedAppVersion,
 			OwnID:      nodeID,
 			RoundStats: rs,
 		}
diff --git a/plugins/analysis/client/heartbeat.go b/plugins/analysis/client/heartbeat.go
index f5bda898..c3a8397c 100644
--- a/plugins/analysis/client/heartbeat.go
+++ b/plugins/analysis/client/heartbeat.go
@@ -75,5 +75,5 @@ func createHeartbeat() *packet.Heartbeat {
 		copy(inboundIDs[i], neighbor.ID().Bytes())
 	}
 
-	return &packet.Heartbeat{NetworkID: []byte(banner.AppVersion), OwnID: nodeID, OutboundIDs: outboundIDs, InboundIDs: inboundIDs}
+	return &packet.Heartbeat{NetworkID: []byte(banner.SimplifiedAppVersion), OwnID: nodeID, OutboundIDs: outboundIDs, InboundIDs: inboundIDs}
 }
diff --git a/plugins/analysis/client/metric_heartbeat.go b/plugins/analysis/client/metric_heartbeat.go
index 8586713a..47b54d1c 100644
--- a/plugins/analysis/client/metric_heartbeat.go
+++ b/plugins/analysis/client/metric_heartbeat.go
@@ -35,7 +35,7 @@ func createMetricHeartbeat() *packet.MetricHeartbeat {
 	}
 
 	return &packet.MetricHeartbeat{
-		Version: banner.AppVersion,
+		Version: banner.SimplifiedAppVersion,
 		OwnID:   nodeID,
 		OS:      runtime.GOOS,
 		Arch:    runtime.GOARCH,
diff --git a/plugins/analysis/packet/fpc_heartbeat.go b/plugins/analysis/packet/fpc_heartbeat.go
index 3870a860..4ab7759d 100644
--- a/plugins/analysis/packet/fpc_heartbeat.go
+++ b/plugins/analysis/packet/fpc_heartbeat.go
@@ -62,7 +62,7 @@ func ParseFPCHeartbeat(data []byte) (*FPCHeartbeat, error) {
 		return nil, err
 	}
 
-	if hb.Version != banner.AppVersion {
+	if hb.Version != banner.SimplifiedAppVersion {
 		return nil, ErrInvalidFPCHeartbeatVersion
 	}
 
diff --git a/plugins/analysis/packet/fpc_heartbeat_test.go b/plugins/analysis/packet/fpc_heartbeat_test.go
index 2ccc465c..50bc7bd7 100644
--- a/plugins/analysis/packet/fpc_heartbeat_test.go
+++ b/plugins/analysis/packet/fpc_heartbeat_test.go
@@ -46,7 +46,7 @@ func TestFPCHeartbeat(t *testing.T) {
 	_, err = ParseFPCHeartbeat(packet)
 	require.Error(t, err)
 
-	hb.Version = banner.AppVersion
+	hb.Version = banner.SimplifiedAppVersion
 	packet, err = hb.Bytes()
 	require.NoError(t, err)
 
diff --git a/plugins/analysis/packet/metric_heartbeat.go b/plugins/analysis/packet/metric_heartbeat.go
index ef2a0f32..726a4831 100644
--- a/plugins/analysis/packet/metric_heartbeat.go
+++ b/plugins/analysis/packet/metric_heartbeat.go
@@ -60,7 +60,7 @@ func ParseMetricHeartbeat(data []byte) (*MetricHeartbeat, error) {
 		return nil, err
 	}
 
-	if hb.Version != banner.AppVersion {
+	if hb.Version != banner.SimplifiedAppVersion {
 		return nil, ErrInvalidMetricHeartbeatVersion
 	}
 
diff --git a/plugins/analysis/packet/metric_heartbeat_test.go b/plugins/analysis/packet/metric_heartbeat_test.go
index f3b8cc82..9f5a080d 100644
--- a/plugins/analysis/packet/metric_heartbeat_test.go
+++ b/plugins/analysis/packet/metric_heartbeat_test.go
@@ -45,7 +45,7 @@ func TestMetricHeartbeat(t *testing.T) {
 	_, err = ParseMetricHeartbeat(packet)
 	require.Error(t, err)
 
-	hb.Version = banner.AppVersion
+	hb.Version = banner.SimplifiedAppVersion
 	packet, err = hb.Bytes()
 	require.NoError(t, err)
 
diff --git a/plugins/analysis/server/plugin.go b/plugins/analysis/server/plugin.go
index c6a1a7bd..b2a55e3e 100644
--- a/plugins/analysis/server/plugin.go
+++ b/plugins/analysis/server/plugin.go
@@ -148,7 +148,7 @@ func processHeartbeatPacket(data []byte) {
 }
 
 // processHeartbeatPacket parses the serialized data into a FPC Heartbeat packet and triggers its event.
-// Note that the processFPCHeartbeatPacket function will return an error if the hb version field is different than banner.AppVersion,
+// Note that the processFPCHeartbeatPacket function will return an error if the hb version field is different than banner.SimplifiedAppVersion,
 // thus the hb will be discarded.
 func processFPCHeartbeatPacket(data []byte) {
 	hb, err := packet.ParseFPCHeartbeat(data)
@@ -162,7 +162,7 @@ func processFPCHeartbeatPacket(data []byte) {
 }
 
 // processMetricHeartbeatPacket parses the serialized data into a Metric Heartbeat packet and triggers its event.
-// Note that the ParseMetricHeartbeat function will return an error if the hb version field is different than banner.AppVersion,
+// Note that the ParseMetricHeartbeat function will return an error if the hb version field is different than banner.SimplifiedAppVersion,
 // thus the hb will be discarded.
 func processMetricHeartbeatPacket(data []byte) {
 	hb, err := packet.ParseMetricHeartbeat(data)
diff --git a/plugins/banner/plugin.go b/plugins/banner/plugin.go
index 763a01ea..6172958a 100644
--- a/plugins/banner/plugin.go
+++ b/plugins/banner/plugin.go
@@ -2,6 +2,7 @@ package banner
 
 import (
 	"fmt"
+	"strings"
 	"sync"
 
 	"github.com/iotaledger/hive.go/node"
@@ -14,12 +15,14 @@ var (
 	// plugin is the plugin instance of the banner plugin.
 	plugin *node.Plugin
 	once   sync.Once
-)
 
-const (
 	// AppVersion version number
 	AppVersion = "v0.3.0"
+	// SimplifiedAppVersion is the version number without commit hash
+	SimplifiedAppVersion string
+)
 
+const (
 	// AppName app code name
 	AppName = "GoShimmer"
 )
@@ -44,8 +47,23 @@ func configure(ctx *node.Plugin) {
 `, AppVersion)
 	fmt.Println()
 
+	SimplifiedAppVersion = simplifiedVersion(AppVersion)
+
 	ctx.Node.Logger.Infof("GoShimmer version %s ...", AppVersion)
 	ctx.Node.Logger.Info("Loading plugins ...")
 }
 
 func run(ctx *node.Plugin) {}
+
+func simplifiedVersion(version string) string {
+	// ignore commit hash
+	ver := version
+	if strings.Contains(ver, "-") {
+		ver = strings.Split(ver, "-")[0]
+	}
+	// attach a "v" at front
+	if !strings.Contains(ver, "v") {
+		ver = "v" + ver
+	}
+	return ver
+}
diff --git a/plugins/metrics/global_metrics.go b/plugins/metrics/global_metrics.go
index 80e32fdd..07f3f2cd 100644
--- a/plugins/metrics/global_metrics.go
+++ b/plugins/metrics/global_metrics.go
@@ -58,8 +58,8 @@ func NodesMetrics() map[string]NodeInfo {
 func calculateNetworkDiameter() {
 	diameter := 0
 	// TODO: send data for all available networkIDs, not just current
-	if analysisserver.Networks[banner.AppVersion] != nil {
-		g := analysisserver.Networks[banner.AppVersion].NetworkGraph()
+	if analysisserver.Networks[banner.SimplifiedAppVersion] != nil {
+		g := analysisserver.Networks[banner.SimplifiedAppVersion].NetworkGraph()
 		diameter = g.Diameter()
 	}
 	networkDiameter.Store(int32(diameter))
diff --git a/plugins/prometheus/global_metrics.go b/plugins/prometheus/global_metrics.go
index 6767a5cc..37282493 100644
--- a/plugins/prometheus/global_metrics.go
+++ b/plugins/prometheus/global_metrics.go
@@ -182,8 +182,8 @@ func collectNodesInfo() {
 	}
 
 	// TODO: send data for all available networkIDs, not just current
-	if analysisserver.Networks[banner.AppVersion] != nil {
-		for nodeID, neighborCount := range analysisserver.Networks[banner.AppVersion].NumOfNeighbors() {
+	if analysisserver.Networks[banner.SimplifiedAppVersion] != nil {
+		for nodeID, neighborCount := range analysisserver.Networks[banner.SimplifiedAppVersion].NumOfNeighbors() {
 			nodesNeighborCount.WithLabelValues(nodeID, "in").Set(float64(neighborCount.Inbound))
 			nodesNeighborCount.WithLabelValues(nodeID, "out").Set(float64(neighborCount.Outbound))
 		}
diff --git a/plugins/remotelog/plugin.go b/plugins/remotelog/plugin.go
index a7018f70..80f7bc32 100644
--- a/plugins/remotelog/plugin.go
+++ b/plugins/remotelog/plugin.go
@@ -105,7 +105,7 @@ func run(plugin *node.Plugin) {
 
 func sendLogMsg(level logger.Level, name string, msg string) {
 	m := logMessage{
-		banner.AppVersion,
+		banner.SimplifiedAppVersion,
 		myGitHead,
 		myGitBranch,
 		myID,
-- 
GitLab