1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/abapAddonAssemblyKitReleasePackages.go
Oliver Nocon dbc459d6ea
chore: cleanup linting issues in abap steps (#3876)
* chore: cleanup linting issues in abap steps

* update

* do not break on errors during testing

* Fix warning

Co-authored-by: Daniel Mieg <daniel.mieg@sap.com>
2022-07-06 14:29:04 +02:00

118 lines
4.4 KiB
Go

package cmd
import (
"encoding/json"
"time"
"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/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/pkg/errors"
)
func abapAddonAssemblyKitReleasePackages(config abapAddonAssemblyKitReleasePackagesOptions, telemetryData *telemetry.CustomData, cpe *abapAddonAssemblyKitReleasePackagesCommonPipelineEnvironment) {
utils := aakaas.NewAakBundleWithTime(time.Duration(config.MaxRuntimeInMinutes), time.Duration(config.PollingIntervalInSeconds))
// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
if err := runAbapAddonAssemblyKitReleasePackages(&config, telemetryData, &utils, cpe); err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func runAbapAddonAssemblyKitReleasePackages(config *abapAddonAssemblyKitReleasePackagesOptions, telemetryData *telemetry.CustomData, utils *aakaas.AakUtils,
cpe *abapAddonAssemblyKitReleasePackagesCommonPipelineEnvironment) error {
conn := new(abapbuild.Connector)
if err := conn.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, *utils); err != nil {
return err
}
var addonDescriptor abaputils.AddonDescriptor
if err := json.Unmarshal([]byte(config.AddonDescriptor), &addonDescriptor); err != nil {
return err
}
err := checkInput(addonDescriptor.Repositories)
if err != nil {
return err
}
packagesWithReposLocked, packagesWithReposNotLocked := sortByStatus(addonDescriptor.Repositories, *conn)
packagesWithReposLocked, err = releaseAndPoll(packagesWithReposLocked, utils)
if err != nil {
return err
}
addonDescriptor.Repositories = sortingBack(packagesWithReposLocked, packagesWithReposNotLocked)
log.Entry().Info("Writing package status to CommonPipelineEnvironment")
cpe.abap.addonDescriptor = string(addonDescriptor.AsJSON())
return nil
}
func sortingBack(packagesWithReposLocked []aakaas.PackageWithRepository, packagesWithReposNotLocked []aakaas.PackageWithRepository) []abaputils.Repository {
var combinedRepos []abaputils.Repository
for i := range packagesWithReposLocked {
packagesWithReposLocked[i].Package.ChangeStatus(&packagesWithReposLocked[i].Repo)
combinedRepos = append(combinedRepos, packagesWithReposLocked[i].Repo)
}
for i := range packagesWithReposNotLocked {
combinedRepos = append(combinedRepos, packagesWithReposNotLocked[i].Repo)
}
return combinedRepos
}
func checkInput(repos []abaputils.Repository) error {
for i := range repos {
if repos[i].PackageName == "" {
return errors.New("Parameter missing. Please provide the name of the package which should be released")
}
}
return nil
}
func sortByStatus(repos []abaputils.Repository, conn abapbuild.Connector) ([]aakaas.PackageWithRepository, []aakaas.PackageWithRepository) {
var packagesWithReposLocked []aakaas.PackageWithRepository
var packagesWithReposNotLocked []aakaas.PackageWithRepository
for i := range repos {
var pack aakaas.Package
pack.InitPackage(repos[i], conn)
pWR := aakaas.PackageWithRepository{
Package: pack,
Repo: repos[i],
}
if pack.Status == "L" {
packagesWithReposLocked = append(packagesWithReposLocked, pWR)
} else {
log.Entry().Infof("Package %s has status %s, cannot release this package", pack.PackageName, pack.Status)
packagesWithReposNotLocked = append(packagesWithReposNotLocked, pWR)
}
}
return packagesWithReposLocked, packagesWithReposNotLocked
}
func releaseAndPoll(pckgWR []aakaas.PackageWithRepository, utils *aakaas.AakUtils) ([]aakaas.PackageWithRepository, error) {
timeout := time.After((*utils).GetMaxRuntime())
ticker := time.Tick((*utils).GetPollingInterval())
for {
select {
case <-timeout:
return pckgWR, errors.New("Timed out")
case <-ticker:
var allFinished bool = true
for i := range pckgWR {
if pckgWR[i].Package.Status != aakaas.PackageStatusReleased {
err := pckgWR[i].Package.Release()
// if there is an error, release is not yet finished
if err != nil {
log.Entry().Infof("Release of %s is not yet finished, check again in %s", pckgWR[i].Package.PackageName, (*utils).GetPollingInterval())
allFinished = false
}
}
}
if allFinished {
log.Entry().Infof("Release of package(s) was successful")
return pckgWR, nil
}
}
}
}