-
Notifications
You must be signed in to change notification settings - Fork 582
/
descriptor.go
184 lines (160 loc) · 5.37 KB
/
descriptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package abaputils
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
)
/*
* The AddonDescriptor
* ===================
* contains information about the Add-on Product and the comprised Add-on Software Component Git Repositories
*
* Lifecycle
* =========
* addon.yml file is read by abapAddonAssemblyKitCheckPV|CheckCV
* addonDesriptor is stored in CPE [commonPipelineEnvironment]
* subsequent steps enrich and update the data in CPE
*/
// AddonDescriptor contains fields about the addonProduct
type AddonDescriptor struct {
AddonProduct string `json:"addonProduct"`
AddonVersionYAML string `json:"addonVersion"`
AddonVersion string `json:"addonVersionAAK"`
AddonSpsLevel string
AddonPatchLevel string
TargetVectorID string
Repositories []Repository `json:"repositories"`
}
// Repository contains fields for the repository/component version
type Repository struct {
Name string `json:"name"`
UseClassicCTS bool `json:"useClassicCTS"`
Tag string `json:"tag"`
Branch string `json:"branch"`
CommitID string `json:"commitID"`
VersionYAML string `json:"version"`
Version string `json:"versionAAK"`
AdditionalPiecelist string `json:"additionalPiecelist"`
PackageName string
PackageType string
SpLevel string
PatchLevel string
PredecessorCommitID string
Status string
Namespace string
SarXMLFilePath string
Languages []string `json:"languages"`
InBuildScope bool
}
// ReadAddonDescriptorType is the type for ReadAddonDescriptor for mocking
type ReadAddonDescriptorType func(FileName string) (AddonDescriptor, error)
type readFileFunc func(FileName string) ([]byte, error)
// ReadAddonDescriptor parses AddonDescriptor YAML file
func ReadAddonDescriptor(FileName string) (AddonDescriptor, error) {
var addonDescriptor AddonDescriptor
err := addonDescriptor.initFromYmlFile(FileName, readFile)
return addonDescriptor, err
}
// ConstructAddonDescriptorFromJSON : Create new AddonDescriptor filled with data from JSON
func ConstructAddonDescriptorFromJSON(JSON []byte) (AddonDescriptor, error) {
var addonDescriptor AddonDescriptor
err := addonDescriptor.initFromJSON(JSON)
return addonDescriptor, err
}
func readFile(FileName string) ([]byte, error) {
fileLocations, err := filepath.Glob(FileName)
if err != nil || len(fileLocations) != 1 {
return nil, errors.New(fmt.Sprintf("Could not find %v", FileName))
}
absoluteFilename, err := filepath.Abs(fileLocations[0])
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not get path of %v", FileName))
}
var fileContent []byte
fileContent, err = os.ReadFile(absoluteFilename)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not read %v", FileName))
}
return fileContent, nil
}
// initFromYmlFile : Reads from file
func (me *AddonDescriptor) initFromYmlFile(FileName string, readFile readFileFunc) error {
fileContent, err := readFile(FileName)
if err != nil {
return err
}
var jsonBytes []byte
jsonBytes, err = yaml.YAMLToJSON(fileContent)
if err != nil {
return errors.New(fmt.Sprintf("Could not parse %v", FileName))
}
err = me.initFromJSON(jsonBytes)
if err != nil {
return errors.New(fmt.Sprintf("Could not unmarshal %v", FileName))
}
return nil
}
// CheckAddonDescriptorForRepositories checks AddonDescriptor struct if it contains any repositories. If not it will return an error
func CheckAddonDescriptorForRepositories(addonDescriptor AddonDescriptor) error {
//checking if parsing went wrong
if len(addonDescriptor.Repositories) == 0 {
return errors.New("AddonDescriptor doesn't contain any repositories")
}
//
emptyRepositoryCounter := 0
for counter, repo := range addonDescriptor.Repositories {
if reflect.DeepEqual(Repository{}, repo) {
emptyRepositoryCounter++
}
if counter+1 == len(addonDescriptor.Repositories) && emptyRepositoryCounter == len(addonDescriptor.Repositories) {
return errors.New("AddonDescriptor doesn't contain any repositories")
}
}
return nil
}
// initFromJSON : Init from json
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
jsonBytes, _ := json.Marshal(me)
return jsonBytes
}
// AsJSONstring : dito
func (me *AddonDescriptor) AsJSONstring() string {
return string(me.AsJSON())
}
// SetRepositories : dito
func (me *AddonDescriptor) SetRepositories(Repositories []Repository) {
me.Repositories = Repositories
}
// GetAakAasLanguageVector : dito
func (me *Repository) GetAakAasLanguageVector() string {
if len(me.Languages) <= 0 {
return `ISO-DEEN`
}
languageVector := `ISO-`
for _, language := range me.Languages {
languageVector = languageVector + language
}
return languageVector
}
func (me *AddonDescriptor) GetRepositoriesInBuildScope() []Repository {
var RepositoriesInBuildScope []Repository
for _, repo := range me.Repositories {
if repo.InBuildScope {
RepositoriesInBuildScope = append(RepositoriesInBuildScope, repo)
}
}
return RepositoriesInBuildScope
}