github.com/xgoffin/jenkins-library@v1.154.0/pkg/abaputils/descriptor.go (about) 1 package abaputils 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 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 Tag string `json:"tag"` 41 Branch string `json:"branch"` 42 CommitID string `json:"commitID"` 43 VersionYAML string `json:"version"` 44 Version string `json:"versionAAK"` 45 PackageName string 46 PackageType string 47 SpLevel string 48 PatchLevel string 49 PredecessorCommitID string 50 Status string 51 Namespace string 52 SarXMLFilePath string 53 Languages []string `json:"languages"` 54 InBuildScope bool 55 } 56 57 // ReadAddonDescriptorType is the type for ReadAddonDescriptor for mocking 58 type ReadAddonDescriptorType func(FileName string) (AddonDescriptor, error) 59 type readFileFunc func(FileName string) ([]byte, error) 60 61 // ReadAddonDescriptor parses AddonDescriptor YAML file 62 func ReadAddonDescriptor(FileName string) (AddonDescriptor, error) { 63 var addonDescriptor AddonDescriptor 64 err := addonDescriptor.initFromYmlFile(FileName, readFile) 65 return addonDescriptor, err 66 } 67 68 // ConstructAddonDescriptorFromJSON : Create new AddonDescriptor filled with data from JSON 69 func ConstructAddonDescriptorFromJSON(JSON []byte) (AddonDescriptor, error) { 70 var addonDescriptor AddonDescriptor 71 err := addonDescriptor.initFromJSON(JSON) 72 return addonDescriptor, err 73 } 74 75 func readFile(FileName string) ([]byte, error) { 76 fileLocations, err := filepath.Glob(FileName) 77 if err != nil || len(fileLocations) != 1 { 78 return nil, errors.New(fmt.Sprintf("Could not find %v", FileName)) 79 } 80 81 absoluteFilename, err := filepath.Abs(fileLocations[0]) 82 if err != nil { 83 return nil, errors.New(fmt.Sprintf("Could not get path of %v", FileName)) 84 } 85 86 var fileContent []byte 87 fileContent, err = ioutil.ReadFile(absoluteFilename) 88 if err != nil { 89 return nil, errors.New(fmt.Sprintf("Could not read %v", FileName)) 90 } 91 92 return fileContent, nil 93 } 94 95 // initFromYmlFile : Reads from file 96 func (me *AddonDescriptor) initFromYmlFile(FileName string, readFile readFileFunc) error { 97 fileContent, err := readFile(FileName) 98 if err != nil { 99 return err 100 } 101 102 var jsonBytes []byte 103 jsonBytes, err = yaml.YAMLToJSON(fileContent) 104 if err != nil { 105 return errors.New(fmt.Sprintf("Could not parse %v", FileName)) 106 } 107 108 err = me.initFromJSON(jsonBytes) 109 if err != nil { 110 return errors.New(fmt.Sprintf("Could not unmarshal %v", FileName)) 111 } 112 113 return nil 114 } 115 116 // CheckAddonDescriptorForRepositories checks AddonDescriptor struct if it contains any repositories. If not it will return an error 117 func CheckAddonDescriptorForRepositories(addonDescriptor AddonDescriptor) error { 118 //checking if parsing went wrong 119 if len(addonDescriptor.Repositories) == 0 { 120 return errors.New("AddonDescriptor doesn't contain any repositories") 121 } 122 // 123 emptyRepositoryCounter := 0 124 for counter, repo := range addonDescriptor.Repositories { 125 if reflect.DeepEqual(Repository{}, repo) { 126 emptyRepositoryCounter++ 127 } 128 if counter+1 == len(addonDescriptor.Repositories) && emptyRepositoryCounter == len(addonDescriptor.Repositories) { 129 return errors.New("AddonDescriptor doesn't contain any repositories") 130 } 131 } 132 return nil 133 } 134 135 // initFromJSON : Init from json 136 func (me *AddonDescriptor) initFromJSON(JSON []byte) error { 137 return json.Unmarshal(JSON, me) 138 } 139 140 // initFromJSON : Init from json string 141 func (me *AddonDescriptor) InitFromJSONstring(JSONstring string) error { 142 return me.initFromJSON([]byte(JSONstring)) 143 } 144 145 // AsJSON : dito 146 func (me *AddonDescriptor) AsJSON() []byte { 147 //hopefully no errors should happen here or they are covered by the users unit tests 148 jsonBytes, _ := json.Marshal(me) 149 return jsonBytes 150 } 151 152 // AsJSONstring : dito 153 func (me *AddonDescriptor) AsJSONstring() string { 154 return string(me.AsJSON()) 155 } 156 157 // SetRepositories : dito 158 func (me *AddonDescriptor) SetRepositories(Repositories []Repository) { 159 me.Repositories = Repositories 160 } 161 162 // GetAakAasLanguageVector : dito 163 func (me *Repository) GetAakAasLanguageVector() string { 164 if len(me.Languages) <= 0 { 165 return `ISO-DEEN` 166 } 167 languageVector := `ISO-` 168 for _, language := range me.Languages { 169 languageVector = languageVector + language 170 } 171 return languageVector 172 } 173 174 func (me *AddonDescriptor) GetRepositoriesInBuildScope() []Repository { 175 var RepositoriesInBuildScope []Repository 176 for _, repo := range me.Repositories { 177 if repo.InBuildScope { 178 RepositoriesInBuildScope = append(RepositoriesInBuildScope, repo) 179 } 180 } 181 return RepositoriesInBuildScope 182 }