Skip to content

Commit

Permalink
[ABAP] Add execution log (SAP#4902)
Browse files Browse the repository at this point in the history
* [ABAP] Add output for execution log

* Add buil comments

* Rename to avoid build issue

---------

Co-authored-by: tiloKo <[email protected]>
  • Loading branch information
2 people authored and maxatsap committed Jul 23, 2024
1 parent 082cc9d commit bc93a41
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 10 deletions.
19 changes: 18 additions & 1 deletion pkg/abaputils/manageGitRepositoryUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions pkg/abaputils/manageGitRepositoryUtils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

})
}
4 changes: 4 additions & 0 deletions pkg/abaputils/sap_com_0510.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/abaputils/sap_com_0948.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
37 changes: 30 additions & 7 deletions pkg/abaputils/sap_com_0948_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

}

Expand Down Expand Up @@ -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)
})
}
16 changes: 14 additions & 2 deletions pkg/abaputils/softwareComponentApiManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/****************************************
Expand Down Expand Up @@ -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"`
}
Expand Down

0 comments on commit bc93a41

Please sign in to comment.