• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SAP / cloud-mta-build-tool / 5932

17 Oct 2023 04:05AM UTC coverage: 90.237% (-6.4%) from 96.682%
5932

push

circle-ci

web-flow
MBT Support SBom Generation (#1046)

* modified:   cmd/cmd.go
	modified:   cmd/cmd_test.go
	modified:   cmd/init.go
	modified:   cmd/init_test.go
	new file:   cmd/sbom.go
	new file:   cmd/sbom_test.go
	modified:   configs/builder_type_cfg.yaml
	modified:   internal/archive/fsops.go
	modified:   internal/archive/mta_location.go
	modified:   internal/artifacts/artifacts_msg.go
	modified:   internal/artifacts/project.go
	modified:   internal/artifacts/project_test.go
	new file:   internal/artifacts/sbom.go
	new file:   internal/artifacts/sbom_test.go
	modified:   internal/commands/commands.go
	modified:   internal/commands/commands_msg.go
	modified:   internal/platform/model.go
	modified:   internal/tpl/base_args.txt
	modified:   internal/tpl/base_post.txt

* modified:   internal/artifacts/sbom_test.go
	modified:   internal/commands/builder_type_cfg.go
	modified:   internal/tpl/base_args.go
	modified:   internal/tpl/base_post.go

* modified:   internal/artifacts/sbom_test.go

* modified:   cmd/cmd.go
	modified:   cmd/init_test.go
	modified:   cmd/sbom.go
	modified:   cmd/sbom_test.go
	modified:   internal/artifacts/project.go
	modified:   internal/artifacts/sbom.go
	modified:   internal/artifacts/sbom_test.go
	modified:   internal/tpl/base_post.go
	modified:   internal/tpl/base_post.txt

* modified:   cmd/init_test.go
	modified:   cmd/sbom_test.go
	modified:   internal/artifacts/sbom.go
	modified:   internal/artifacts/sbom_test.go

* modified:   cmd/init.go
	modified:   cmd/init_test.go
	modified:   cmd/sbom.go
	modified:   cmd/sbom_test.go
	modified:   internal/artifacts/sbom.go
	modified:   internal/artifacts/sbom_test.go
	modified:   internal/commands/commands.go

* modified:   internal/artifacts/artifacts_msg.go
	modified:   internal/artifacts/sbom.go
	modified:   internal/commands/commands.go

* new file:   cmd/testdata/mta-sbom/golang/go.mod
	new file:   cmd/testdata/mta-sbom/golang/go.sum
	ne... (continued)

421 of 421 new or added lines in 8 files covered. (100.0%)

2976 of 3298 relevant lines covered (90.24%)

1.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.93
/internal/archive/mta_location.go
1
package dir
2

3
import (
4
        "fmt"
5
        "os"
6
        "path/filepath"
7

8
        "github.com/pkg/errors"
9

10
        "github.com/SAP/cloud-mta/mta"
11
)
12

13
const (
14
        //Dep - deployment descriptor
15
        Dep = "dep"
16
        //Dev - development descriptor
17
        Dev = "dev"
18
        // TempFolderSuffix - temporary folder suffix
19
        TempFolderSuffix = "_mta_build_tmp"
20
        // Mtad - deployment descriptor file name
21
        Mtad = "mtad.yaml"
22
        // MtarFolder - default archives folder
23
        MtarFolder = "mta_archives"
24
        // SBomTempFolderSuffix - sbom temporary folder suffix
25
        SBomTempFolderSuffix = "_mta_sbom_tmp"
26
)
27

28
// IMtaParser - MTA Parser interface
29
type IMtaParser interface {
30
        ParseFile() (*mta.MTA, error)
31
}
32

33
// IDescriptor - descriptor interface
34
type IDescriptor interface {
35
        IsDeploymentDescriptor() bool
36
        GetDescriptor() string
37
}
38

39
// ISourceModule - source module interface
40
type ISourceModule interface {
41
        GetSourceModuleDir(modulePath string) string
42
        GetSourceModuleArtifactRelPath(modulePath, artifactPath string) (string, error)
43
}
44

45
// IMtaYaml - MTA Yaml interface
46
type IMtaYaml interface {
47
        GetMtaYamlFilename() string
48
        GetMtaYamlPath() string
49
}
50

51
// IMtaExtYaml - MTA Extension Yaml interface
52
type IMtaExtYaml interface {
53
        GetMtaExtYamlPath(platform string) string
54
}
55

56
// ITargetPath - target path interface
57
type ITargetPath interface {
58
        GetTarget() string
59
        GetTargetTmpDir() string
60
}
61

62
// ITargetModule - Target Module interface
63
type ITargetModule interface {
64
        GetTargetModuleDir(moduleName string) string
65
        GetTargetTmpRoot() string
66
}
67

68
// IModule - module interface
69
type IModule interface {
70
        ISourceModule
71
        ITargetModule
72
}
73

74
// ITargetArtifacts - target artifacts interface
75
type ITargetArtifacts interface {
76
        GetMetaPath() string
77
        GetMtadPath() string
78
        GetManifestPath() string
79
        GetMtarDir(targetProvided bool) string
80
}
81

82
// Loc - MTA tool file properties
83
type Loc struct {
84
        // SourcePath - Path to MTA project
85
        SourcePath string
86
        // TargetPath - Path to results
87
        TargetPath string
88
        // MtaFilename - MTA yaml filename "mta.yaml" by default
89
        MtaFilename string
90
        // Descriptor - indicator of deployment descriptor usage (mtad.yaml)
91
        Descriptor string
92
        // ExtensionFileNames - list of MTA extension descriptors (could be empty)
93
        ExtensionFileNames []string
94
}
95

96
// GetSource gets the processed project path;
97
// if it is not provided, use the current directory.
98
func (ep *Loc) GetSource() string {
1✔
99
        return ep.SourcePath
1✔
100
}
1✔
101

102
// GetDescriptor - gets descriptor type of Location
103
func (ep *Loc) GetDescriptor() string {
1✔
104
        if ep.Descriptor == "" {
2✔
105
                return Dev
1✔
106
        }
1✔
107

108
        return ep.Descriptor
1✔
109
}
110

111
// GetMtarDir - gets archive folder
112
// if the target folder provided archive will be saved in the target folder
113
// otherwise archives folder - "mta_archives" subfolder in the project folder
114
func (ep *Loc) GetMtarDir(targetProvided bool) string {
1✔
115
        if !targetProvided {
2✔
116
                return filepath.Join(ep.GetTarget(), MtarFolder)
1✔
117
        }
1✔
118

119
        return ep.GetTarget()
1✔
120
}
121

122
// GetTarget gets the target path;
123
// if it is not provided, use the path of the processed project.
124
func (ep *Loc) GetTarget() string {
1✔
125
        return ep.TargetPath
1✔
126
}
1✔
127

128
// GetTargetTmpDir gets the temporary target directory path.
129
// The subdirectory in the target folder is named as the source project folder suffixed with "_mta_build_tmp".
130
// Subdirectory name is prefixed with "." as a hidden folder
131
func (ep *Loc) GetTargetTmpDir() string {
1✔
132
        source := ep.GetSource()
1✔
133
        _, file := filepath.Split(source)
1✔
134
        file = "." + file + TempFolderSuffix
1✔
135
        target := ep.GetTarget()
1✔
136
        // append to the currentPath the file name
1✔
137
        return filepath.Join(target, file)
1✔
138
}
1✔
139

140
// GetTargetTmpRoot gets the build results directory root path.
141
func (ep *Loc) GetTargetTmpRoot() string {
1✔
142
        return ep.GetTargetTmpDir()
1✔
143
}
1✔
144

145
// GetTargetModuleDir gets the path to the packed module directory.
146
// The subdirectory in the temporary target directory is named by the module name.
147
func (ep *Loc) GetTargetModuleDir(moduleName string) string {
1✔
148
        dir := ep.GetTargetTmpDir()
1✔
149

1✔
150
        return filepath.Join(dir, moduleName)
1✔
151
}
1✔
152

153
// GetSourceModuleDir gets the path to the module to be packed.
154
// The subdirectory is in the source.
155
func (ep *Loc) GetSourceModuleDir(modulePath string) string {
1✔
156
        return filepath.Join(ep.GetSource(), filepath.Clean(modulePath))
1✔
157
}
1✔
158

159
// GetSourceModuleArtifactRelPath gets the relative path to the module's artifact
160
func (ep *Loc) GetSourceModuleArtifactRelPath(moduleRelPath, artifactAbsPath string) (string, error) {
1✔
161
        info, err := os.Stat(artifactAbsPath)
1✔
162
        if err != nil {
2✔
163
                return "", err
1✔
164
        }
1✔
165
        isFolder := info.IsDir()
1✔
166
        modulePath := ep.GetSourceModuleDir(moduleRelPath)
1✔
167
        if isFolder {
2✔
168
                return filepath.Rel(modulePath, artifactAbsPath)
1✔
169
        } else if artifactAbsPath == modulePath {
3✔
170
                return "", nil
1✔
171
        }
1✔
172
        return filepath.Rel(modulePath, filepath.Dir(artifactAbsPath))
1✔
173
}
174

175
// GetMtaYamlFilename - Gets the MTA .yaml file name.
176
func (ep *Loc) GetMtaYamlFilename() string {
1✔
177
        if ep.MtaFilename == "" {
2✔
178
                if ep.Descriptor == Dep {
2✔
179
                        return Mtad
1✔
180
                }
1✔
181
                return "mta.yaml"
1✔
182
        }
183
        return ep.MtaFilename
1✔
184
}
185

186
// GetMtaYamlPath gets the MTA .yaml file path.
187
func (ep *Loc) GetMtaYamlPath() string {
1✔
188
        return filepath.Join(ep.GetSource(), ep.GetMtaYamlFilename())
1✔
189
}
1✔
190

191
// GetMtaExtYamlPath gets the full MTA extension file path by file name or path.
192
// If the file name is an absolute path it's returned as is. Otherwise the returned path is relative
193
// to the source folder.
194
func (ep *Loc) GetMtaExtYamlPath(extFileName string) string {
1✔
195
        if filepath.IsAbs(extFileName) {
2✔
196
                return extFileName
1✔
197
        }
1✔
198

199
        return filepath.Join(ep.GetSource(), extFileName)
1✔
200
}
201

202
// GetMetaPath gets the path to the generated META-INF directory.
203
func (ep *Loc) GetMetaPath() string {
1✔
204
        return filepath.Join(ep.GetTargetTmpDir(), "META-INF")
1✔
205
}
1✔
206

207
// GetMtadPath gets the path to the generated MTAD file.
208
func (ep *Loc) GetMtadPath() string {
1✔
209
        return filepath.Join(ep.GetMetaPath(), Mtad)
1✔
210
}
1✔
211

212
// GetManifestPath gets the path to the generated manifest file.
213
func (ep *Loc) GetManifestPath() string {
1✔
214
        return filepath.Join(ep.GetMetaPath(), "MANIFEST.MF")
1✔
215
}
1✔
216

217
// GetSBomFileTmpDir - sbomFileTmpDir is a temp folder under project root
218
func (ep *Loc) GetSBomFileTmpDir(mtaObj *mta.MTA) string {
×
219
        source := ep.GetSource()
×
220
        sbomtmpdir := "." + mtaObj.ID + SBomTempFolderSuffix
×
221
        return filepath.Join(source, sbomtmpdir)
×
222
}
×
223

224
// ValidateDeploymentDescriptor validates the deployment descriptor.
225
func ValidateDeploymentDescriptor(descriptor string) error {
1✔
226
        if descriptor != "" && descriptor != Dev && descriptor != Dep {
2✔
227
                return fmt.Errorf(InvalidDescMsg, descriptor)
1✔
228
        }
1✔
229
        return nil
1✔
230
}
231

232
// IsDeploymentDescriptor checks whether the flag is related to the deployment descriptor.
233
func (ep *Loc) IsDeploymentDescriptor() bool {
1✔
234
        return ep.Descriptor == Dep
1✔
235
}
1✔
236

237
// ParseFile returns a reference to the MTA object resulting from the given mta.yaml file merged with the extension descriptors.
238
func (ep *Loc) ParseFile() (*mta.MTA, error) {
1✔
239
        mtaFile, _, err := mta.GetMtaFromFile(ep.GetMtaYamlPath(), ep.GetExtensionFilePaths(), true)
1✔
240
        return mtaFile, err
1✔
241
}
1✔
242

243
// GetExtensionFilePaths returns the MTA extension descriptor full paths
244
func (ep *Loc) GetExtensionFilePaths() []string {
1✔
245
        paths := make([]string, len(ep.ExtensionFileNames))
1✔
246
        for i, fileName := range ep.ExtensionFileNames {
2✔
247
                paths[i] = ep.GetMtaExtYamlPath(fileName)
1✔
248
        }
1✔
249
        return paths
1✔
250
}
251

252
// Location - provides Location parameters of MTA
253
func Location(source, target, descriptor string, extensions []string, wdGetter func() (string, error)) (*Loc, error) {
1✔
254
        err := ValidateDeploymentDescriptor(descriptor)
1✔
255
        if err != nil {
2✔
256
                return nil, err
1✔
257
        }
1✔
258

259
        var mtaFilename string
1✔
260
        if descriptor == Dev || descriptor == "" {
2✔
261
                mtaFilename = "mta.yaml"
1✔
262
                descriptor = Dev
1✔
263
        } else {
2✔
264
                mtaFilename = "mtad.yaml"
1✔
265
                descriptor = Dep
1✔
266
        }
1✔
267

268
        if source == "" {
2✔
269
                source, err = wdGetter()
1✔
270
                if err != nil {
2✔
271
                        return nil, errors.Wrap(err, InitLocFailedOnWorkDirMsg)
1✔
272
                }
1✔
273
        }
274

275
        if target == "" {
2✔
276
                target = source
1✔
277
        }
1✔
278
        return &Loc{
1✔
279
                SourcePath:         filepath.Join(source),
1✔
280
                TargetPath:         filepath.Join(target),
1✔
281
                MtaFilename:        mtaFilename,
1✔
282
                Descriptor:         descriptor,
1✔
283
                ExtensionFileNames: extensions,
1✔
284
        }, nil
1✔
285
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc