@@ -2,13 +2,14 @@ package commandsummary
22
33import (
44 "fmt"
5+ "net/url"
6+ "path"
7+ "strings"
8+
59 buildInfo "github.com/jfrog/build-info-go/entities"
610 "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
711 "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/container"
812 "github.com/jfrog/jfrog-client-go/utils/log"
9- "net/url"
10- "path"
11- "strings"
1213)
1314
1415const (
@@ -62,8 +63,14 @@ func (bis *BuildInfoSummary) GenerateMarkdownFromFiles(dataFilePaths []string) (
6263 return
6364 }
6465
65- buildInfoTableMarkdown := bis .buildInfoTable (builds )
66- publishedModulesMarkdown := bis .buildInfoModules (builds )
66+ buildInfoTableMarkdown , err := bis .buildInfoTable (builds )
67+ if err != nil {
68+ return "" , err
69+ }
70+ publishedModulesMarkdown , err := bis .buildInfoModules (builds )
71+ if err != nil {
72+ return "" , err
73+ }
6774 if publishedModulesMarkdown != "" {
6875 publishedModulesMarkdown = WrapCollapsableMarkdown (modulesTitle , publishedModulesMarkdown , 2 )
6976 }
@@ -73,61 +80,75 @@ func (bis *BuildInfoSummary) GenerateMarkdownFromFiles(dataFilePaths []string) (
7380}
7481
7582// Create a table with published builds and possible scan results.
76- func (bis * BuildInfoSummary ) buildInfoTable (builds []* buildInfo.BuildInfo ) string {
83+ func (bis * BuildInfoSummary ) buildInfoTable (builds []* buildInfo.BuildInfo ) ( string , error ) {
7784 var tableBuilder strings.Builder
7885 tableBuilder .WriteString (getBuildInfoTableHeader ())
7986 for _ , build := range builds {
80- appendBuildInfoRow (& tableBuilder , build )
87+ if err := appendBuildInfoRow (& tableBuilder , build ); err != nil {
88+ return "" , err
89+ }
8190 }
8291 tableBuilder .WriteString ("\n \n " )
83- return tableBuilder .String ()
92+ return tableBuilder .String (), nil
8493}
8594
8695// Generates a view for published modules within the build.
8796// Modules are displayed as tables if they are scannable via CLI command,
8897// otherwise, they are shown as an artifact tree.
89- func (bis * BuildInfoSummary ) buildInfoModules (builds []* buildInfo.BuildInfo ) string {
98+ func (bis * BuildInfoSummary ) buildInfoModules (builds []* buildInfo.BuildInfo ) ( string , error ) {
9099 var markdownBuilder strings.Builder
91100 markdownBuilder .WriteString ("\n \n <h3>Published Modules</h3>\n \n " )
92101 var shouldGenerate bool
93102 for _ , build := range builds {
94103 supportedModules := filterModules (build .Modules ... )
95- if modulesMarkdown := bis .generateModulesMarkdown (supportedModules ... ); modulesMarkdown != "" {
104+ modulesMarkdown , err := bis .generateModulesMarkdown (supportedModules ... )
105+ if err != nil {
106+ return "" , err
107+ }
108+ if modulesMarkdown != "" {
96109 markdownBuilder .WriteString (modulesMarkdown )
97110 shouldGenerate = true
98111 }
99112 }
100113 if ! shouldGenerate {
101- return ""
114+ return "" , nil
102115 }
103- return markdownBuilder .String ()
116+ return markdownBuilder .String (), nil
104117}
105118
106- func (bis * BuildInfoSummary ) generateModulesMarkdown (modules ... buildInfo.Module ) string {
119+ func (bis * BuildInfoSummary ) generateModulesMarkdown (modules ... buildInfo.Module ) ( string , error ) {
107120 var modulesMarkdown strings.Builder
108121 // Modules could include nested modules inside of them
109122 // Group the modules by their root module ID
110123 // If a module has no root, it is considered as a root module itself.
111124 groupedModuleMap := groupModules (modules )
112125 if len (groupedModuleMap ) == 0 {
113- return ""
126+ return "" , nil
114127 }
115128 for rootModuleID , subModules := range groupedModuleMap {
116129 if len (subModules ) == 0 {
117130 continue
118131 }
119132 if ! scannableModuleType [subModules [0 ].Type ] {
120- modulesMarkdown .WriteString (bis .generateModuleArtifactTree (rootModuleID , subModules ))
133+ tree , err := bis .generateModuleArtifactTree (rootModuleID , subModules )
134+ if err != nil {
135+ return "" , err
136+ }
137+ modulesMarkdown .WriteString (tree )
121138 } else {
122- modulesMarkdown .WriteString (bis .generateModuleTableView (rootModuleID , subModules ))
139+ view , err := bis .generateModuleTableView (rootModuleID , subModules )
140+ if err != nil {
141+ return "" , err
142+ }
143+ modulesMarkdown .WriteString (view )
123144 }
124145 }
125- return modulesMarkdown .String ()
146+ return modulesMarkdown .String (), nil
126147}
127148
128- func (bis * BuildInfoSummary ) generateModuleArtifactTree (rootModuleID string , nestedModules []buildInfo.Module ) string {
149+ func (bis * BuildInfoSummary ) generateModuleArtifactTree (rootModuleID string , nestedModules []buildInfo.Module ) ( string , error ) {
129150 if len (nestedModules ) == 0 {
130- return ""
151+ return "" , nil
131152 }
132153 var markdownBuilder strings.Builder
133154 isMultiModule := len (nestedModules ) > 1
@@ -140,26 +161,33 @@ func (bis *BuildInfoSummary) generateModuleArtifactTree(rootModuleID string, nes
140161 if isMultiModule && rootModuleID == module .Id {
141162 continue
142163 }
143- markdownBuilder .WriteString (fmt .Sprintf ("\n \n <pre>%s</pre>\n \n " , bis .generateModuleArtifactsTree (& module , isMultiModule )))
164+ tree , err := bis .generateModuleArtifactsTree (& module , isMultiModule )
165+ if err != nil {
166+ return "" , err
167+ }
168+ markdownBuilder .WriteString (fmt .Sprintf ("\n \n <pre>%s</pre>\n \n " , tree ))
144169 }
145- return markdownBuilder .String ()
170+ return markdownBuilder .String (), nil
146171}
147172
148- func (bis * BuildInfoSummary ) generateModuleTableView (rootModuleID string , subModules []buildInfo.Module ) string {
173+ func (bis * BuildInfoSummary ) generateModuleTableView (rootModuleID string , subModules []buildInfo.Module ) ( string , error ) {
149174 var markdownBuilder strings.Builder
150175 markdownBuilder .WriteString (generateModuleHeader (rootModuleID ))
151176 markdownBuilder .WriteString (generateModuleTableHeader ())
152177 isMultiModule := len (subModules ) > 1
153- nestedModuleMarkdownTree := bis .generateTableModuleMarkdown (subModules , rootModuleID , isMultiModule )
178+ nestedModuleMarkdownTree , err := bis .generateTableModuleMarkdown (subModules , rootModuleID , isMultiModule )
179+ if err != nil {
180+ return "" , err
181+ }
154182 scanResult := getScanResults (extractDockerImageTag (subModules ))
155183 markdownBuilder .WriteString (generateTableRow (nestedModuleMarkdownTree , scanResult ))
156- return markdownBuilder .String ()
184+ return markdownBuilder .String (), nil
157185}
158186
159- func (bis * BuildInfoSummary ) generateTableModuleMarkdown (nestedModules []buildInfo.Module , parentModuleID string , isMultiModule bool ) string {
187+ func (bis * BuildInfoSummary ) generateTableModuleMarkdown (nestedModules []buildInfo.Module , parentModuleID string , isMultiModule bool ) ( string , error ) {
160188 var nestedModuleMarkdownTree strings.Builder
161189 if len (nestedModules ) == 0 {
162- return ""
190+ return "" , nil
163191 }
164192
165193 if ! StaticMarkdownConfig .IsExtendedSummary () {
@@ -174,19 +202,26 @@ func (bis *BuildInfoSummary) generateTableModuleMarkdown(nestedModules []buildIn
174202 if isMultiModule && parentModuleID == module .Id {
175203 continue
176204 }
177- nestedModuleMarkdownTree .WriteString (bis .generateModuleArtifactsTree (& module , isMultiModule ))
205+ tree , err := bis .generateModuleArtifactsTree (& module , isMultiModule )
206+ if err != nil {
207+ return "" , err
208+ }
209+ nestedModuleMarkdownTree .WriteString (tree )
178210 }
179211 nestedModuleMarkdownTree .WriteString (appendSpacesToTableColumn ("" ))
180212 nestedModuleMarkdownTree .WriteString ("</pre>" )
181- return nestedModuleMarkdownTree .String ()
213+ return nestedModuleMarkdownTree .String (), nil
182214}
183215
184- func (bis * BuildInfoSummary ) generateModuleArtifactsTree (module * buildInfo.Module , shouldCollapseArtifactsTree bool ) string {
185- artifactsTree := bis .createArtifactsTree (module )
216+ func (bis * BuildInfoSummary ) generateModuleArtifactsTree (module * buildInfo.Module , shouldCollapseArtifactsTree bool ) (string , error ) {
217+ artifactsTree , err := bis .createArtifactsTree (module )
218+ if err != nil {
219+ return "" , err
220+ }
186221 if shouldCollapseArtifactsTree {
187- return bis .generateModuleCollapsibleSection (module , artifactsTree )
222+ return bis .generateModuleCollapsibleSection (module , artifactsTree ), nil
188223 }
189- return artifactsTree
224+ return artifactsTree , nil
190225}
191226
192227func (bis * BuildInfoSummary ) generateModuleCollapsibleSection (module * buildInfo.Module , sectionContent string ) string {
@@ -198,30 +233,41 @@ func (bis *BuildInfoSummary) generateModuleCollapsibleSection(module *buildInfo.
198233 }
199234}
200235
201- func (bis * BuildInfoSummary ) createArtifactsTree (module * buildInfo.Module ) string {
236+ func (bis * BuildInfoSummary ) createArtifactsTree (module * buildInfo.Module ) ( string , error ) {
202237 artifactsTree := utils .NewFileTree ()
203238 for _ , artifact := range module .Artifacts {
204239 var artifactUrlInArtifactory string
240+ var err error
205241 if StaticMarkdownConfig .IsExtendedSummary () {
206- artifactUrlInArtifactory = generateArtifactUrl (artifact )
242+ artifactUrlInArtifactory , err = generateArtifactUrl (artifact , * module )
243+ if err != nil {
244+ return "" , err
245+ }
207246 }
208247 if artifact .OriginalDeploymentRepo == "" {
209248 artifact .OriginalDeploymentRepo = " "
210249 }
211250 artifactTreePath := path .Join (artifact .OriginalDeploymentRepo , artifact .Path )
212251 artifactsTree .AddFile (artifactTreePath , artifactUrlInArtifactory )
213252 if artifactsTree .IsTreeExceedsMax () {
214- return ""
253+ return "" , nil
215254 }
216255 }
217- return artifactsTree .String ()
256+ return artifactsTree .String (), nil
218257}
219258
220- func generateArtifactUrl (artifact buildInfo.Artifact ) string {
259+ func generateArtifactUrl (artifact buildInfo.Artifact , module buildInfo. Module ) ( string , error ) {
221260 if strings .TrimSpace (artifact .OriginalDeploymentRepo ) == "" {
222- return ""
261+ return "" , nil
262+ }
263+ var section summarySection
264+
265+ if module .Type == buildInfo .Generic {
266+ section = artifactsSection
267+ } else {
268+ section = packagesSection
223269 }
224- return GenerateArtifactUrl (path .Join (artifact .OriginalDeploymentRepo , artifact .Path ))
270+ return GenerateArtifactUrl (path .Join (artifact .OriginalDeploymentRepo , artifact .Path ), section )
225271}
226272
227273func groupModules (modules []buildInfo.Module ) map [string ][]buildInfo.Module {
@@ -280,16 +326,21 @@ func appendSpacesToTableColumn(str string) string {
280326 return str
281327}
282328
283- func appendBuildInfoRow (tableBuilder * strings.Builder , build * buildInfo.BuildInfo ) {
329+ func appendBuildInfoRow (tableBuilder * strings.Builder , build * buildInfo.BuildInfo ) error {
284330 buildName := build .Name + " " + build .Number
285331 buildScanResult := getScanResults (buildName )
286332 if StaticMarkdownConfig .IsExtendedSummary () {
287- tableBuilder .WriteString (fmt .Sprintf ("| [%s](%s) %s | %s | %s | \n " , buildName , build .BuildUrl , appendSpacesToTableColumn ("" ), appendSpacesToTableColumn (buildScanResult .GetViolations ()), appendSpacesToTableColumn (buildScanResult .GetVulnerabilities ())))
333+ buildInfoUrl , err := addGitHubTrackingToUrl (build .BuildUrl , buildInfoSection )
334+ if err != nil {
335+ return err
336+ }
337+ tableBuilder .WriteString (fmt .Sprintf ("| [%s](%s) %s | %s | %s | \n " , buildName , buildInfoUrl , appendSpacesToTableColumn ("" ), appendSpacesToTableColumn (buildScanResult .GetViolations ()), appendSpacesToTableColumn (buildScanResult .GetVulnerabilities ())))
288338 } else {
289339 upgradeMessage := fmt .Sprintf (basicSummaryUpgradeNotice , StaticMarkdownConfig .GetExtendedSummaryLangPage ())
290340 buildName = fmt .Sprintf (" %s %s" , upgradeMessage , buildName )
291341 tableBuilder .WriteString (fmt .Sprintf ("| %s %s | %s | %s |\n " , fitInsideMarkdownTable (buildName ), appendSpacesToTableColumn ("" ), appendSpacesToTableColumn (buildScanResult .GetViolations ()), appendSpacesToTableColumn (buildScanResult .GetVulnerabilities ())))
292342 }
343+ return nil
293344}
294345
295346func getBuildInfoTableHeader () string {
0 commit comments