mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
04b8a5cbd7
* Add yaml * Initial files * Add inital functions * Create Test file * Initial trigger clone function * Add command * Adapt manageGitRepositoryUtils * Enable Repositories yaml * Add handleHttpError Tests * Adapt CodeClimate Feedback * Add tests for error messages * Adapt error messages and handling * Adapt gitignore
147 lines
5.1 KiB
Go
147 lines
5.1 KiB
Go
package abaputils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"reflect"
|
|
"sort"
|
|
"time"
|
|
|
|
piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// PollEntity periodically polls the pull/import entity to get the status. Check if the import is still running
|
|
func PollEntity(repositoryName string, connectionDetails ConnectionDetailsHTTP, client piperhttp.Sender, pollIntervall time.Duration) (string, error) {
|
|
|
|
log.Entry().Info("Start polling the status...")
|
|
var status string = "R"
|
|
|
|
for {
|
|
var resp, err = GetHTTPResponse("GET", connectionDetails, nil, client)
|
|
if err != nil {
|
|
err = HandleHTTPError(resp, err, "Could not pull the Repository / Software Component "+repositoryName, connectionDetails)
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Parse response
|
|
var abapResp map[string]*json.RawMessage
|
|
var body PullEntity
|
|
bodyText, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
json.Unmarshal(bodyText, &abapResp)
|
|
json.Unmarshal(*abapResp["d"], &body)
|
|
|
|
if reflect.DeepEqual(PullEntity{}, body) {
|
|
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", repositoryName).Error("Could not pull the Repository / Software Component")
|
|
var err = errors.New("Request to ABAP System not successful")
|
|
return "", err
|
|
}
|
|
|
|
status = body.Status
|
|
log.Entry().WithField("StatusCode", resp.Status).Info("Pull Status: " + body.StatusDescription)
|
|
if body.Status != "R" {
|
|
PrintLogs(body)
|
|
break
|
|
}
|
|
time.Sleep(pollIntervall)
|
|
}
|
|
return status, nil
|
|
}
|
|
|
|
// PrintLogs sorts and formats the received transport and execution log of an import
|
|
func PrintLogs(entity PullEntity) {
|
|
|
|
// Sort logs
|
|
sort.SliceStable(entity.ToExecutionLog.Results, func(i, j int) bool {
|
|
return entity.ToExecutionLog.Results[i].Index < entity.ToExecutionLog.Results[j].Index
|
|
})
|
|
|
|
sort.SliceStable(entity.ToTransportLog.Results, func(i, j int) bool {
|
|
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 {
|
|
|
|
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("-------------------------")
|
|
}
|
|
|
|
/****************************************
|
|
* Structs for the A4C_A2G_GHA service *
|
|
****************************************/
|
|
|
|
// PullEntity struct for the Pull/Import entity A4C_A2G_GHA_SC_IMP
|
|
type PullEntity struct {
|
|
Metadata AbapMetadata `json:"__metadata"`
|
|
UUID string `json:"uuid"`
|
|
Namespace string `json:"namepsace"`
|
|
ScName string `json:"sc_name"`
|
|
ImportType string `json:"import_type"`
|
|
BranchName string `json:"branch_name"`
|
|
StartedByUser string `json:"user_name"`
|
|
Status string `json:"status"`
|
|
StatusDescription string `json:"status_descr"`
|
|
CommitID string `json:"commit_id"`
|
|
StartTime string `json:"start_time"`
|
|
ChangeTime string `json:"change_time"`
|
|
ToExecutionLog AbapLogs `json:"to_Execution_log"`
|
|
ToTransportLog AbapLogs `json:"to_Transport_log"`
|
|
}
|
|
|
|
// BranchEntity struct for the Branch entity A4C_A2G_GHA_SC_BRANCH
|
|
type BranchEntity struct {
|
|
Metadata AbapMetadata `json:"__metadata"`
|
|
ScName string `json:"sc_name"`
|
|
Namespace string `json:"namepsace"`
|
|
BranchName string `json:"branch_name"`
|
|
ParentBranch string `json:"derived_from"`
|
|
CreatedBy string `json:"created_by"`
|
|
CreatedOn string `json:"created_on"`
|
|
IsActive bool `json:"is_active"`
|
|
CommitID string `json:"commit_id"`
|
|
CommitMessage string `json:"commit_message"`
|
|
LastCommitBy string `json:"last_commit_by"`
|
|
LastCommitOn string `json:"last_commit_on"`
|
|
}
|
|
|
|
// CloneEntity struct for the Clone entity A4C_A2G_GHA_SC_CLONE
|
|
type CloneEntity struct {
|
|
Metadata AbapMetadata `json:"__metadata"`
|
|
ScName string `json:"sc_name"`
|
|
BranchName string `json:"branch_name"`
|
|
ImportType string `json:"import_type"`
|
|
Namespace string `json:"namepsace"`
|
|
Status string `json:"status"`
|
|
StatusDescription string `json:"status_descr"`
|
|
StartedByUser string `json:"user_name"`
|
|
StartTime string `json:"start_time"`
|
|
ChangeTime string `json:"change_time"`
|
|
}
|
|
|
|
// AbapLogs struct for ABAP logs
|
|
type AbapLogs struct {
|
|
Results []LogResults `json:"results"`
|
|
}
|
|
|
|
// LogResults struct for Execution and Transport Log entities A4C_A2G_GHA_SC_LOG_EXE and A4C_A2G_GHA_SC_LOG_TP
|
|
type LogResults struct {
|
|
Index string `json:"index_no"`
|
|
Type string `json:"type"`
|
|
Description string `json:"descr"`
|
|
Timestamp string `json:"timestamp"`
|
|
}
|