From 9464b345d0cb86f9271a85164b36b67e95abe884 Mon Sep 17 00:00:00 2001 From: tiloKo <70266685+tiloKo@users.noreply.github.com> Date: Tue, 2 Nov 2021 11:00:01 +0100 Subject: [PATCH] abapEnvironmentAssemblePackages store file in workspace instead of cpe (#3232) * initial refac * fix unit test, no publish * fix typo --- cmd/abapAddonAssemblyKitRegisterPackages.go | 22 ++++-- ...apAddonAssemblyKitRegisterPackages_test.go | 2 + cmd/abapEnvironmentAssemblePackages.go | 78 ++++++++++--------- pkg/abap/build/connector.go | 18 ++++- pkg/abaputils/abaputils.go | 5 ++ pkg/abaputils/addonDescriptor.go | 10 +++ 6 files changed, 86 insertions(+), 49 deletions(-) diff --git a/cmd/abapAddonAssemblyKitRegisterPackages.go b/cmd/abapAddonAssemblyKitRegisterPackages.go index 241145c45..46395ab02 100644 --- a/cmd/abapAddonAssemblyKitRegisterPackages.go +++ b/cmd/abapAddonAssemblyKitRegisterPackages.go @@ -33,27 +33,33 @@ func abapAddonAssemblyKitRegisterPackages(config abapAddonAssemblyKitRegisterPac func runAbapAddonAssemblyKitRegisterPackages(config *abapAddonAssemblyKitRegisterPackagesOptions, telemetryData *telemetry.CustomData, client piperhttp.Sender, cpe *abapAddonAssemblyKitRegisterPackagesCommonPipelineEnvironment, fileReader readFile) error { + var addonDescriptor abaputils.AddonDescriptor json.Unmarshal([]byte(config.AddonDescriptor), &addonDescriptor) conn := new(abapbuild.Connector) - conn.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, client) - err := uploadSarFiles(addonDescriptor.Repositories, *conn, fileReader) - if err != nil { + if err := conn.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, client); err != nil { return err } - // we need a second connector without the added Header - conn2 := new(abapbuild.Connector) - conn2.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, client) + if err := uploadSarFiles(addonDescriptor.Repositories, *conn, fileReader); err != nil { + return err + } + + conn2 := new(abapbuild.Connector) // we need a second connector without the added Header + if err := conn2.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, client); err != nil { + return err + } + + var err error addonDescriptor.Repositories, err = registerPackages(addonDescriptor.Repositories, *conn2) if err != nil { return err } log.Entry().Info("Writing package status to CommonPipelineEnvironment") - backToCPE, _ := json.Marshal(addonDescriptor) - cpe.abap.addonDescriptor = string(backToCPE) + cpe.abap.addonDescriptor = addonDescriptor.AsJSONstring() + return nil } diff --git a/cmd/abapAddonAssemblyKitRegisterPackages_test.go b/cmd/abapAddonAssemblyKitRegisterPackages_test.go index 060c7c042..dcc7b7748 100644 --- a/cmd/abapAddonAssemblyKitRegisterPackages_test.go +++ b/cmd/abapAddonAssemblyKitRegisterPackages_test.go @@ -22,6 +22,8 @@ func mockReader(path string) ([]byte, error) { func TestRegisterPackagesStep(t *testing.T) { var config abapAddonAssemblyKitRegisterPackagesOptions + config.Username = "dummy" + config.Password = "dummy" var cpe abapAddonAssemblyKitRegisterPackagesCommonPipelineEnvironment t.Run("step success", func(t *testing.T) { client := &abaputils.ClientMock{ diff --git a/cmd/abapEnvironmentAssemblePackages.go b/cmd/abapEnvironmentAssemblePackages.go index c3e2bfc03..95f2af551 100644 --- a/cmd/abapEnvironmentAssemblePackages.go +++ b/cmd/abapEnvironmentAssemblePackages.go @@ -1,7 +1,6 @@ package cmd import ( - "encoding/json" "path" "path/filepath" "time" @@ -40,33 +39,17 @@ func abapEnvironmentAssemblePackages(config abapEnvironmentAssemblePackagesOptio } func runAbapEnvironmentAssemblePackages(config *abapEnvironmentAssemblePackagesOptions, telemetryData *telemetry.CustomData, com abaputils.Communication, client abapbuild.HTTPSendLoader, cpe *abapEnvironmentAssemblePackagesCommonPipelineEnvironment) error { - conn := new(abapbuild.Connector) - var connConfig abapbuild.ConnectorConfiguration - connConfig.CfAPIEndpoint = config.CfAPIEndpoint - connConfig.CfOrg = config.CfOrg - connConfig.CfSpace = config.CfSpace - connConfig.CfServiceInstance = config.CfServiceInstance - connConfig.CfServiceKeyName = config.CfServiceKeyName - connConfig.Host = config.Host - connConfig.Username = config.Username - connConfig.Password = config.Password - connConfig.AddonDescriptor = config.AddonDescriptor - connConfig.MaxRuntimeInMinutes = config.MaxRuntimeInMinutes - - err := conn.InitBuildFramework(connConfig, com, client) - if err != nil { - return errors.Wrap(err, "Connector initialization for communication with the ABAP system failed") + connBuild := new(abapbuild.Connector) + if errConBuild := initAssemblePackagesConnection(connBuild, config, com, client); errConBuild != nil { + return errConBuild } - var addonDescriptor abaputils.AddonDescriptor - err = json.Unmarshal([]byte(config.AddonDescriptor), &addonDescriptor) - if err != nil { + addonDescriptor := new(abaputils.AddonDescriptor) + if err := addonDescriptor.InitFromJSONstring(config.AddonDescriptor); err != nil { return errors.Wrap(err, "Reading AddonDescriptor failed [Make sure abapAddonAssemblyKit...CheckCVs|CheckPV|ReserveNextPackages steps have been run before]") } - maxRuntimeInMinutes := time.Duration(config.MaxRuntimeInMinutes) * time.Minute - pollInterval := time.Duration(config.PollIntervalsInMilliseconds) * time.Millisecond - builds, err := executeBuilds(addonDescriptor.Repositories, *conn, maxRuntimeInMinutes, pollInterval) + builds, err := executeBuilds(addonDescriptor.Repositories, *connBuild, time.Duration(config.MaxRuntimeInMinutes)*time.Minute, time.Duration(config.PollIntervalsInMilliseconds)*time.Millisecond) if err != nil { return errors.Wrap(err, "Starting Builds for Repositories with reserved AAKaaS packages failed") } @@ -76,28 +59,26 @@ func runAbapEnvironmentAssemblePackages(config *abapEnvironmentAssemblePackagesO return errors.Wrap(err, "Checking for failed Builds and Printing Build Logs failed") } - var filesToPublish []piperutils.Path - filesToPublish, err = downloadResultToFile(builds, "SAR_XML", filesToPublish) + _, err = downloadResultToFile(builds, "SAR_XML", false) //File is present in ABAP build system and uploaded to AAKaaS, no need to fill up jenkins with it if err != nil { return errors.Wrap(err, "Download of Build Artifact SAR_XML failed") } - filesToPublish, err = downloadResultToFile(builds, "DELIVERY_LOGS.ZIP", filesToPublish) + var filesToPublish []piperutils.Path + filesToPublish, err = downloadResultToFile(builds, "DELIVERY_LOGS.ZIP", true) if err != nil { - //changed result storage with 2105, thus ignore errors for now - log.Entry().Error(errors.Wrap(err, "Download of Build Artifact DELIVERY_LOGS.ZIP failed")) + return errors.Wrap(err, "Download of Build Artifact DELIVERY_LOGS.ZIP failed") } - log.Entry().Infof("Publsihing %v files", len(filesToPublish)) + log.Entry().Infof("Publishing %v files", len(filesToPublish)) piperutils.PersistReportsAndLinks("abapEnvironmentAssemblePackages", "", filesToPublish, nil) var reposBackToCPE []abaputils.Repository for _, b := range builds { reposBackToCPE = append(reposBackToCPE, b.repo) } - addonDescriptor.Repositories = reposBackToCPE - backToCPE, _ := json.Marshal(addonDescriptor) - cpe.abap.addonDescriptor = string(backToCPE) + addonDescriptor.SetRepositories(reposBackToCPE) + cpe.abap.addonDescriptor = addonDescriptor.AsJSONstring() return nil } @@ -205,8 +186,9 @@ func (br *buildWithRepository) start() error { return br.build.Start(phase, valuesInput) } -func downloadResultToFile(builds []buildWithRepository, resultName string, filesToPublish []piperutils.Path) ([]piperutils.Path, error) { - envPath := filepath.Join(GeneralConfig.EnvRootPath, "commonPipelineEnvironment", "abap") +func downloadResultToFile(builds []buildWithRepository, resultName string, publish bool) ([]piperutils.Path, error) { + envPath := filepath.Join(GeneralConfig.EnvRootPath, "abapBuild") + var filesToPublish []piperutils.Path for i, b := range builds { if b.repo.Status != "P" { @@ -231,9 +213,10 @@ func downloadResultToFile(builds []buildWithRepository, resultName string, files if resultName == "SAR_XML" { builds[i].repo.SarXMLFilePath = downloadPath } - - log.Entry().Infof("Add %s to be published", resultName) - filesToPublish = append(filesToPublish, piperutils.Path{Target: downloadPath, Name: resultName, Mandatory: true}) + if publish { + log.Entry().Infof("Add %s to be published", resultName) + filesToPublish = append(filesToPublish, piperutils.Path{Target: downloadPath, Name: resultName, Mandatory: true}) + } } return filesToPublish, nil } @@ -252,3 +235,24 @@ func checkIfFailedAndPrintLogs(builds []buildWithRepository) error { } return nil } + +func initAssemblePackagesConnection(conn *abapbuild.Connector, config *abapEnvironmentAssemblePackagesOptions, com abaputils.Communication, client abapbuild.HTTPSendLoader) error { + var connConfig abapbuild.ConnectorConfiguration + connConfig.CfAPIEndpoint = config.CfAPIEndpoint + connConfig.CfOrg = config.CfOrg + connConfig.CfSpace = config.CfSpace + connConfig.CfServiceInstance = config.CfServiceInstance + connConfig.CfServiceKeyName = config.CfServiceKeyName + connConfig.Host = config.Host + connConfig.Username = config.Username + connConfig.Password = config.Password + connConfig.AddonDescriptor = config.AddonDescriptor + connConfig.MaxRuntimeInMinutes = config.MaxRuntimeInMinutes + + err := conn.InitBuildFramework(connConfig, com, client) + if err != nil { + return errors.Wrap(err, "Connector initialization for communication with the ABAP system failed") + } + + return nil +} diff --git a/pkg/abap/build/connector.go b/pkg/abap/build/connector.go index 4040ae9fc..506089f70 100644 --- a/pkg/abap/build/connector.go +++ b/pkg/abap/build/connector.go @@ -114,7 +114,7 @@ func (conn Connector) Download(appendum string, downloadPath string) error { } // InitAAKaaS : initialize Connector for communication with AAKaaS backend -func (conn *Connector) InitAAKaaS(aAKaaSEndpoint string, username string, password string, inputclient piperhttp.Sender) { +func (conn *Connector) InitAAKaaS(aAKaaSEndpoint string, username string, password string, inputclient piperhttp.Sender) error { conn.Client = inputclient conn.Header = make(map[string][]string) conn.Header["Accept"] = []string{"application/json"} @@ -128,6 +128,12 @@ func (conn *Connector) InitAAKaaS(aAKaaSEndpoint string, username string, passwo CookieJar: cookieJar, }) conn.Baseurl = aAKaaSEndpoint + + if username == "" || password == "" { + return errors.New("username/password for AAKaaS must not be initial") //leads to redirect to login page which causes HTTP200 instead of HTTP401 and thus side effects + } else { + return nil + } } // InitBuildFramework : initialize Connector for communication with ABAP SCP instance @@ -211,9 +217,13 @@ func (conn Connector) UploadSarFileInChunks(appendum string, fileName string, sa response, err := conn.Client.SendRequest("POST", url, nextChunk, header, nil) if err != nil { - errorbody, _ := ioutil.ReadAll(response.Body) - response.Body.Close() - return errors.Wrapf(err, "Upload of SAR file failed: %v", string(errorbody)) + if response != nil && response.Body != nil { + errorbody, _ := ioutil.ReadAll(response.Body) + response.Body.Close() + return errors.Wrapf(err, "Upload of SAR file failed: %v", string(errorbody)) + } else { + return err + } } response.Body.Close() diff --git a/pkg/abaputils/abaputils.go b/pkg/abaputils/abaputils.go index bd9aaad63..d00ca0251 100644 --- a/pkg/abaputils/abaputils.go +++ b/pkg/abaputils/abaputils.go @@ -323,6 +323,11 @@ func (c *ClientMock) SendRequest(method, url string, bdy io.Reader, hdr http.Hea }, c.Error } +// DownloadFile : Empty file download +func (c *ClientMock) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error { + return nil +} + // AUtilsMock mock type AUtilsMock struct { ReturnedConnectionDetailsHTTP ConnectionDetailsHTTP diff --git a/pkg/abaputils/addonDescriptor.go b/pkg/abaputils/addonDescriptor.go index f0aeb4e62..34d761f31 100644 --- a/pkg/abaputils/addonDescriptor.go +++ b/pkg/abaputils/addonDescriptor.go @@ -137,6 +137,11 @@ func (me *AddonDescriptor) initFromJSON(JSON []byte) error { return json.Unmarshal(JSON, me) } +// initFromJSON : Init from json string +func (me *AddonDescriptor) InitFromJSONstring(JSONstring string) error { + return me.initFromJSON([]byte(JSONstring)) +} + // AsJSON : dito func (me *AddonDescriptor) AsJSON() []byte { //hopefully no errors should happen here or they are covered by the users unit tests @@ -144,6 +149,11 @@ func (me *AddonDescriptor) AsJSON() []byte { return jsonBytes } +// AsJSONstring : dito +func (me *AddonDescriptor) AsJSONstring() string { + return string(me.AsJSON()) +} + // SetRepositories : dito func (me *AddonDescriptor) SetRepositories(Repositories []Repository) { me.Repositories = Repositories