1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-11-06 09:09:19 +02:00

Add commit to clone and pull (#2258)

* commit ID for clone

* commit ID for clone

* Remove old default values

* Add commitID to Pull

* Print http response on debug level

* Fix tests

* Adapt tests

* Shorten sleep

* Fix clone tests

* Add ignore commit option

* Adapt tests

* Adapt docu

* Implement feedback

* fix codeclimate issue
This commit is contained in:
Daniel Mieg
2020-11-02 14:17:13 +01:00
committed by GitHub
parent edd5ef7709
commit 7639175def
15 changed files with 412 additions and 149 deletions

View File

@@ -29,6 +29,7 @@ type Repository struct {
Name string `json:"name"`
Tag string `json:"tag"`
Branch string `json:"branch"`
CommitID string `json:"commitID"`
VersionYAML string `json:"version"`
Version string `json:"versionAAK"`
PackageName string

View File

@@ -44,7 +44,11 @@ func PollEntity(repositoryName string, connectionDetails ConnectionDetailsHTTP,
status = body.Status
log.Entry().WithField("StatusCode", resp.Status).Info("Pull Status: " + body.StatusDescription)
if body.Status != "R" {
PrintLogs(body)
if body.Status == "E" {
PrintLogs(body, true)
} else {
PrintLogs(body, false)
}
break
}
time.Sleep(pollIntervall)
@@ -53,7 +57,7 @@ func PollEntity(repositoryName string, connectionDetails ConnectionDetailsHTTP,
}
// PrintLogs sorts and formats the received transport and execution log of an import
func PrintLogs(entity PullEntity) {
func PrintLogs(entity PullEntity, errorOnSystem bool) {
// Sort logs
sort.SliceStable(entity.ToExecutionLog.Results, func(i, j int) bool {
@@ -64,21 +68,41 @@ func PrintLogs(entity PullEntity) {
return entity.ToTransportLog.Results[i].Index < entity.ToTransportLog.Results[j].Index
})
log.Entry().Info("-------------------------")
log.Entry().Info("Transport Log")
log.Entry().Info("-------------------------")
for _, logEntry := range entity.ToTransportLog.Results {
// Show transport and execution log if either the action was erroenous on the system or the log level is set to "debug" (verbose = true)
if errorOnSystem {
log.Entry().Info("-------------------------")
log.Entry().Info("Transport Log")
log.Entry().Info("-------------------------")
for _, logEntry := range entity.ToTransportLog.Results {
log.Entry().WithField("Timestamp", ConvertTime(logEntry.Timestamp)).Info(logEntry.Description)
log.Entry().WithField("Timestamp", ConvertTime(logEntry.Timestamp)).Info(logEntry.Description)
}
log.Entry().Info("-------------------------")
log.Entry().Info("Execution Log")
log.Entry().Info("-------------------------")
for _, logEntry := range entity.ToExecutionLog.Results {
log.Entry().WithField("Timestamp", ConvertTime(logEntry.Timestamp)).Info(logEntry.Description)
}
log.Entry().Info("-------------------------")
} else {
log.Entry().Debug("-------------------------")
log.Entry().Debug("Transport Log")
log.Entry().Debug("-------------------------")
for _, logEntry := range entity.ToTransportLog.Results {
log.Entry().WithField("Timestamp", ConvertTime(logEntry.Timestamp)).Debug(logEntry.Description)
}
log.Entry().Debug("-------------------------")
log.Entry().Debug("Execution Log")
log.Entry().Debug("-------------------------")
for _, logEntry := range entity.ToExecutionLog.Results {
log.Entry().WithField("Timestamp", ConvertTime(logEntry.Timestamp)).Debug(logEntry.Description)
}
log.Entry().Debug("-------------------------")
}
log.Entry().Info("-------------------------")
log.Entry().Info("Execution Log")
log.Entry().Info("-------------------------")
for _, logEntry := range entity.ToExecutionLog.Results {
log.Entry().WithField("Timestamp", ConvertTime(logEntry.Timestamp)).Info(logEntry.Description)
}
log.Entry().Info("-------------------------")
}
//GetRepositories for parsing one or multiple branches and repositories from repositories file or branchName and repositoryName configuration
@@ -112,6 +136,15 @@ func GetRepositories(config *RepositoriesConfig) ([]Repository, error) {
return repositories, nil
}
//GetCommitStrings for getting the commit_id property for the http request and a string for logging output
func GetCommitStrings(commitID string) (commitQuery string, commitString string) {
if commitID != "" {
commitQuery = `, "commit_id":"` + commitID + `"`
commitString = ", commit '" + commitID + "'"
}
return commitQuery, commitString
}
/****************************************
* Structs for the A4C_A2G_GHA service *
****************************************/

View File

@@ -215,3 +215,16 @@ repositories:
assert.EqualError(t, err, expectedErrorMessage)
})
}
func TestGetCommitStrings(t *testing.T) {
t.Run("CommitID available", func(t *testing.T) {
commitQuery, commitString := GetCommitStrings("ABCD1234")
assert.Equal(t, `, "commit_id":"ABCD1234"`, commitQuery, "Expected different query")
assert.Equal(t, `, commit 'ABCD1234'`, commitString, "Expected different string")
})
t.Run("CommitID available", func(t *testing.T) {
commitQuery, commitString := GetCommitStrings("")
assert.Equal(t, ``, commitQuery, "Expected empty query")
assert.Equal(t, ``, commitString, "Expected empty string")
})
}