Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
tiloKo committed Sep 18, 2020
2 parents 70d97d5 + 3911845 commit 6d083fe
Show file tree
Hide file tree
Showing 22 changed files with 1,857 additions and 501 deletions.
106 changes: 106 additions & 0 deletions cmd/abapAddonAssemblyKitRegisterPackages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package cmd

import (
"encoding/json"
"io/ioutil"
"path/filepath"

"github.com/SAP/jenkins-library/pkg/abap/aakaas"
abapbuild "github.com/SAP/jenkins-library/pkg/abap/build"
"github.com/SAP/jenkins-library/pkg/abaputils"
"github.com/SAP/jenkins-library/pkg/command"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/pkg/errors"
)

func abapAddonAssemblyKitRegisterPackages(config abapAddonAssemblyKitRegisterPackagesOptions, telemetryData *telemetry.CustomData, cpe *abapAddonAssemblyKitRegisterPackagesCommonPipelineEnvironment) {
// for command execution use Command
c := command.Command{}
// reroute command output to logging framework
c.Stdout(log.Writer())
c.Stderr(log.Writer())

client := piperhttp.Client{}

// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
err := runAbapAddonAssemblyKitRegisterPackages(&config, telemetryData, &client, cpe, reader)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}

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 {
return err
}

// we need a second connector without the added Header
conn2 := new(abapbuild.Connector)
conn2.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, client)
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)
return nil
}

func uploadSarFiles(repos []abaputils.Repository, conn abapbuild.Connector, readFileFunc readFile) error {
for i := range repos {
if repos[i].Status == string(aakaas.PackageStatusPlanned) {
if repos[i].SarXMLFilePath == "" {
return errors.New("Parameter missing. Please provide the path to the SAR file")
}
filename := filepath.Base(repos[i].SarXMLFilePath)
conn.Header["Content-Filename"] = []string{filename}
sarFile, err := readFileFunc(repos[i].SarXMLFilePath)
if err != nil {
return err
}
log.Entry().Infof("Upload SAR file %s", filename)
err = conn.UploadSarFile("/odata/aas_file_upload", sarFile)
if err != nil {
return err
}
} else {
log.Entry().Infof("Package %s has status %s, cannot upload the SAR file of this package", repos[i].PackageName, repos[i].Status)
}
}
return nil
}

// for moocking
type readFile func(path string) ([]byte, error)

func reader(path string) ([]byte, error) {
return ioutil.ReadFile(path)
}

func registerPackages(repos []abaputils.Repository, conn abapbuild.Connector) ([]abaputils.Repository, error) {
for i := range repos {
var pack aakaas.Package
pack.InitPackage(repos[i], conn)
if repos[i].Status == string(aakaas.PackageStatusPlanned) {
err := pack.Register()
if err != nil {
return repos, err
}
pack.ChangeStatus(&repos[i])
} else {
log.Entry().Infof("Package %s has status %s, cannot register this package", pack.PackageName, pack.Status)
}
}
return repos, nil
}
178 changes: 178 additions & 0 deletions cmd/abapAddonAssemblyKitRegisterPackages_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions cmd/abapAddonAssemblyKitRegisterPackages_generated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestAbapAddonAssemblyKitRegisterPackagesCommand(t *testing.T) {

testCmd := AbapAddonAssemblyKitRegisterPackagesCommand()

// only high level testing performed - details are tested in step generation procedure
assert.Equal(t, "abapAddonAssemblyKitRegisterPackages", testCmd.Use, "command name incorrect")

}
Loading

0 comments on commit 6d083fe

Please sign in to comment.