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