diff --git a/pkg/abaputils/manageGitRepositoryUtils.go b/pkg/abaputils/manageGitRepositoryUtils.go index bc7d596ba4..7d6b8224de 100644 --- a/pkg/abaputils/manageGitRepositoryUtils.go +++ b/pkg/abaputils/manageGitRepositoryUtils.go @@ -40,7 +40,13 @@ func PollEntity(api SoftwareComponentApiInterface, pollIntervall time.Duration) } func PrintLogs(api SoftwareComponentApiInterface) { - // connectionDetails.URL = connectionDetails.URL + "?$expand=to_Log_Overview" + + // Get Execution Logs + executionLogs, err := api.GetExecutionLog() + if err == nil { + printExecutionLogs(executionLogs) + } + results, err := api.GetLogOverview() if err != nil || len(results) == 0 { // return if no logs are available @@ -63,6 +69,17 @@ func PrintLogs(api SoftwareComponentApiInterface) { return } +func printExecutionLogs(executionLogs ExecutionLog) { + log.Entry().Infof("\n") + AddDefaultDashedLine(1) + log.Entry().Infof("Execution Logs") + AddDefaultDashedLine(1) + for _, entry := range executionLogs.Value { + log.Entry().Infof("%7s - %s", entry.Type, entry.Descr) + } + AddDefaultDashedLine(1) +} + func printOverview(results []LogResultsV2) { logOutputPhaseLength, logOutputLineLength := calculateLenghts(results) diff --git a/pkg/abaputils/manageGitRepositoryUtils_test.go b/pkg/abaputils/manageGitRepositoryUtils_test.go index 21ee7785b4..ffd96a614b 100644 --- a/pkg/abaputils/manageGitRepositoryUtils_test.go +++ b/pkg/abaputils/manageGitRepositoryUtils_test.go @@ -300,3 +300,18 @@ func TestCreateRequestBodies(t *testing.T) { assert.Equal(t, `{"sc_name":"/DMO/REPO", "tag_name":"myTag"}`, body, "Expected different body") }) } + +func TestExecutionLogOutput(t *testing.T) { + t.Run("Test execution log output", func(t *testing.T) { + + executionLogValue := []ExecutionLogValue{ + {IndexNo: 1, Type: "Success", Descr: "Something went well", Timestamp: "/Date(1644332299000+0000)/"}, + {IndexNo: 2, Type: "Error", Descr: "Something went wrong", Timestamp: "/Date(1644332299000+0000)/"}, + } + executionLog := ExecutionLog{ + Value: executionLogValue, + } + printExecutionLogs(executionLog) + + }) +} diff --git a/pkg/abaputils/sap_com_0510.go b/pkg/abaputils/sap_com_0510.go index 2874dd557c..b330224f66 100644 --- a/pkg/abaputils/sap_com_0510.go +++ b/pkg/abaputils/sap_com_0510.go @@ -52,6 +52,10 @@ func (api *SAP_COM_0510) init(con ConnectionDetailsHTTP, client piperhttp.Sender api.retryAllowedErrorCodes = append(api.retryAllowedErrorCodes, "A4C_A2G/501") } +func (api *SAP_COM_0510) GetExecutionLog() (execLog ExecutionLog, err error) { + return execLog, errors.New("Not implemented") +} + func (api *SAP_COM_0510) getUUID() string { return api.uuid } diff --git a/pkg/abaputils/sap_com_0948.go b/pkg/abaputils/sap_com_0948.go index d272c988fb..f6b7be68b5 100644 --- a/pkg/abaputils/sap_com_0948.go +++ b/pkg/abaputils/sap_com_0948.go @@ -59,6 +59,36 @@ func (api *SAP_COM_0948) getUUID() string { return api.uuid } +// reads the execution log from the ABAP system +func (api *SAP_COM_0948) GetExecutionLog() (execLog ExecutionLog, err error) { + + connectionDetails := api.con + connectionDetails.URL = api.con.URL + api.path + api.actionsEntity + "/" + api.getUUID() + "/_Execution_log" + resp, err := GetHTTPResponse("GET", connectionDetails, nil, api.client) + if err != nil { + log.SetErrorCategory(log.ErrorInfrastructure) + _, err = HandleHTTPError(resp, err, api.failureMessage, connectionDetails) + return execLog, err + } + defer resp.Body.Close() + + // Parse response + bodyText, _ := io.ReadAll(resp.Body) + + marshallError := json.Unmarshal(bodyText, &execLog) + if marshallError != nil { + return execLog, errors.Wrap(marshallError, "Could not parse response from the ABAP Environment system") + } + + if reflect.DeepEqual(ExecutionLog{}, execLog) { + log.Entry().WithField("StatusCode", resp.Status).Error(api.failureMessage) + log.SetErrorCategory(log.ErrorInfrastructure) + var err = errors.New("Request to ABAP System not successful") + return execLog, err + } + return execLog, nil +} + func (api *SAP_COM_0948) CreateTag(tag Tag) error { if reflect.DeepEqual(Tag{}, tag) { diff --git a/pkg/abaputils/sap_com_0948_test.go b/pkg/abaputils/sap_com_0948_test.go index 72c8fdd158..24f7e6702c 100644 --- a/pkg/abaputils/sap_com_0948_test.go +++ b/pkg/abaputils/sap_com_0948_test.go @@ -11,17 +11,17 @@ import ( "github.com/stretchr/testify/assert" ) -var connection ConnectionDetailsHTTP -var repository Repository +var conTest0948 ConnectionDetailsHTTP +var repoTest0948 Repository func init() { - connection.User = "CC_USER" - connection.Password = "123abc" - connection.URL = "https://example.com" + conTest0948.User = "CC_USER" + conTest0948.Password = "123abc" + conTest0948.URL = "https://example.com" - repository.Name = "/DMO/REPO" - repository.Branch = "main" + repoTest0948.Name = "/DMO/REPO" + repoTest0948.Branch = "main" } @@ -481,3 +481,26 @@ func TestSleepTime0948(t *testing.T) { assert.ErrorContains(t, err, "Exceeded max sleep time") }) } + +func TestGetExecutionLog(t *testing.T) { + t.Run("Test Get Executionlog Success", func(t *testing.T) { + + client := &ClientMock{ + BodyList: []string{ + `{ "value" : [{"index_no":1,"timestamp":"2021-08-23T12:00:00.000Z","type":"Success", "descr":"First log entry"}]}`, + ``, + }, + Token: "myToken", + StatusCode: 200, + } + + apiManager := &SoftwareComponentApiManager{Client: client, PollIntervall: 1 * time.Microsecond} + + api, _ := apiManager.GetAPI(con, Repository{Name: "/DMO/REPO"}) + + results, errAction := api.GetExecutionLog() + assert.NoError(t, errAction) + assert.NotEmpty(t, results) + assert.Equal(t, "First log entry", results.Value[0].Descr) + }) +} diff --git a/pkg/abaputils/softwareComponentApiManager.go b/pkg/abaputils/softwareComponentApiManager.go index 9b5e06332d..813df3c3ef 100644 --- a/pkg/abaputils/softwareComponentApiManager.go +++ b/pkg/abaputils/softwareComponentApiManager.go @@ -59,14 +59,15 @@ type SoftwareComponentApiInterface interface { setSleepTimeConfig(timeUnit time.Duration, maxSleepTime time.Duration) getSleepTime(n int) (time.Duration, error) getUUID() string + GetRepository() (bool, string, error) Clone() error Pull() error CheckoutBranch() error - GetRepository() (bool, string, error) GetAction() (string, error) + CreateTag(tag Tag) error GetLogOverview() ([]LogResultsV2, error) GetLogProtocol(LogResultsV2, int) (result []LogProtocol, count int, err error) - CreateTag(tag Tag) error + GetExecutionLog() (ExecutionLog, error) } /**************************************** @@ -148,6 +149,17 @@ type LogResultsV2 struct { ToLogProtocol LogProtocolDeferred `json:"to_Log_Protocol"` } +type ExecutionLog struct { + Value []ExecutionLogValue `json:"value"` +} + +type ExecutionLogValue struct { + IndexNo int `json:"index_no"` + Type string `json:"type"` + Descr string `json:"descr"` + Timestamp string `json:"timestamp"` +} + type LogProtocolDeferred struct { Deferred URI `json:"__deferred"` }