You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-09-16 09:26:22 +02:00
fix(splunk): add default value for TemplateName (#5404)
This commit is contained in:
@@ -3,8 +3,17 @@ package piperutils
|
||||
import (
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Title(in string) string {
|
||||
return cases.Title(language.English, cases.NoLower).String(in)
|
||||
}
|
||||
|
||||
func StringWithDefault(input, defaultValue string) string {
|
||||
inputCleared := strings.TrimSpace(input)
|
||||
if inputCleared == "" {
|
||||
return defaultValue
|
||||
}
|
||||
return inputCleared
|
||||
}
|
||||
|
@@ -16,3 +16,28 @@ func TestTitle(t *testing.T) {
|
||||
assert.Equal(t, "Test", Title("Test"))
|
||||
assert.Equal(t, "TEST1 Test2 TEsT3 Test4", Title("TEST1 test2 tEsT3 Test4"))
|
||||
}
|
||||
|
||||
func TestStringWithDefault(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
defaultValue string
|
||||
want string
|
||||
}{
|
||||
{"Non-empty input", "foo", "bar", "foo"},
|
||||
{"Input with spaces", " foo ", "bar", "foo"},
|
||||
{"Empty input", "", "bar", "bar"},
|
||||
{"Whitespace input", " ", "bar", "bar"},
|
||||
{"Empty default", "", "", ""},
|
||||
{"Whitespace input, empty default", " ", "", ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := StringWithDefault(tt.input, tt.defaultValue)
|
||||
if got != tt.want {
|
||||
t.Errorf("StringWithDefault(%q, %q) = %q, want %q", tt.input, tt.defaultValue, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ type BaseData struct {
|
||||
BuildURLHash string `json:"buildUrlHash"` // defaults to sha1 of provider.GetJobURL()
|
||||
Orchestrator string `json:"orchestrator"` // defaults to provider.OrchestratorType()
|
||||
// TemplateName indicates what template was used to run the pipeline (gpp, oss, ctp or custom)
|
||||
TemplateName string `json:"templateName"` // defaults to os.Getenv("PIPER_PIPELINE_TEMPLATE_NAME")
|
||||
TemplateName string `json:"templateName"` // defaults to os.Getenv("PIPER_PIPELINE_TEMPLATE_NAME") or "n/a" if not set.
|
||||
}
|
||||
|
||||
var baseData BaseData
|
||||
|
@@ -4,6 +4,7 @@ import (
|
||||
"crypto/sha1"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/SAP/jenkins-library/pkg/piperutils"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -60,7 +61,7 @@ func (t *Telemetry) Initialize(stepName string) {
|
||||
|
||||
t.baseData = BaseData{
|
||||
Orchestrator: t.provider.OrchestratorType(),
|
||||
TemplateName: os.Getenv("PIPER_PIPELINE_TEMPLATE_NAME"),
|
||||
TemplateName: piperutils.StringWithDefault(os.Getenv("PIPER_PIPELINE_TEMPLATE_NAME"), "n/a"),
|
||||
StageName: t.provider.StageName(),
|
||||
URL: LibraryRepository,
|
||||
ActionName: actionName,
|
||||
|
Reference in New Issue
Block a user