1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-04-13 11:50:43 +02:00

fix: too long string written to cpe git.commitMessage (#5147)

* fix too long CPE string written to git/commitMessage

* Add debug log

* Fix debug log

* Truncate long git commit message title

* Add tests for truncateString

* Fix test

* Fix tests

* Fix tests

---------

Co-authored-by: Ivan Nikiforov <ivan.nikiforov@sap.com>
This commit is contained in:
Ivan Nikiforov 2024-10-16 13:45:23 +02:00 committed by GitHub
parent bd8b08b93e
commit 36c555ea61
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 1 deletions

View File

@ -237,7 +237,13 @@ func runArtifactPrepareVersion(config *artifactPrepareVersionOptions, telemetryD
commonPipelineEnvironment.git.commitID = gitCommitID // this commitID changes and is not necessarily the HEAD commitID
commonPipelineEnvironment.artifactVersion = newVersion
commonPipelineEnvironment.originalArtifactVersion = version
commonPipelineEnvironment.git.commitMessage = gitCommitMessage
gitCommitMessages := strings.Split(gitCommitMessage, "\n")
commitMessage := truncateString(gitCommitMessages[0], 50) // Github recommends to keep commit message title less than 50 chars
commonPipelineEnvironment.git.commitMessage = commitMessage
log.Entry().Debug("CPE git commitMessage:", commitMessage)
// we may replace GetVersion() above with GetCoordinates() at some point ...
coordinates, err := artifact.GetCoordinates()
@ -254,6 +260,15 @@ func runArtifactPrepareVersion(config *artifactPrepareVersionOptions, telemetryD
return nil
}
func truncateString(str string, maxLength int) string {
chars := []rune(str)
if len(chars) > maxLength {
return string(chars[:maxLength]) + "..."
}
return str
}
func openGit() (gitRepository, error) {
workdir, _ := os.Getwd()
return gitUtils.PlainOpen(workdir)

View File

@ -887,3 +887,33 @@ func TestPropagateVersion(t *testing.T) {
assert.Contains(t, fmt.Sprint(err), "failed to retrieve artifact")
})
}
func TestTruncateString(t *testing.T) {
t.Run("input string longer than maxLength - truncate", func(t *testing.T) {
inputStr := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor"
expected := "Lorem ipsum dolor sit amet, consectetur adipiscing..."
outputStr := truncateString(inputStr, 50)
assert.Equal(t, outputStr, expected)
})
t.Run("input string shorter than maxLength - return as is", func(t *testing.T) {
inputStr := "Lorem ipsum dolor sit amet"
outputStr := truncateString(inputStr, 50)
assert.Equal(t, outputStr, inputStr)
})
t.Run("input string contains unicode chars", func(t *testing.T) {
inputStr := "パイパーは素晴らしい図書館です"
expected := "パイパーは..."
outputStr := truncateString(inputStr, 5)
assert.Equal(t, outputStr, expected)
})
t.Run("input string is empty", func(t *testing.T) {
outputStr := truncateString("", 5)
assert.Equal(t, outputStr, "")
})
}