@@ -23,6 +23,7 @@ package templates
23
23
import (
24
24
"context"
25
25
"embed"
26
+ "encoding/json"
26
27
"fmt"
27
28
"html/template"
28
29
"io"
@@ -142,6 +143,16 @@ func BulkBuildInfo(w io.Writer, b *ddao.Build) {
142
143
t .ExecuteTemplate (w , "bulk_build_info.html" , b )
143
144
}
144
145
146
+ // marshalToJSON marshals the given data to a JSON string that can be used in JavaScript.
147
+ // Returns a template.JS value to prevent HTML escaping of the JSON.
148
+ func marshalToJSON (data interface {}) (template.JS , error ) {
149
+ jsonBytes , err := json .Marshal (data )
150
+ if err != nil {
151
+ return "" , err
152
+ }
153
+ return template .JS (jsonBytes ), nil
154
+ }
155
+
145
156
func PkgInfo (w io.Writer , res ddao.GetSingleResultRow ) {
146
157
// Define what to fetch - these represent the stages of the build process
147
158
// and are used to fetch the corresponding log files.
@@ -215,18 +226,32 @@ func PkgInfo(w io.Writer, res ddao.GetSingleResultRow) {
215
226
// Wait for all requests to complete
216
227
wg .Wait ()
217
228
229
+ // Create a map of URL results for JSON marshalling
230
+ urlDataMap := make (map [string ]URLRequestResult )
231
+ for _ , result := range results {
232
+ urlDataMap [result .ID ] = result
233
+ }
234
+
235
+ // Marshal URL data to JSON
236
+ jsonData , err := marshalToJSON (urlDataMap )
237
+ if err != nil {
238
+ log .Errorf (ctx , "templates.PkgInfo: Error marshalling URL data: %v" , err )
239
+ }
240
+
218
241
// Create the structure to pass to the template
219
242
s := struct {
220
- Res * ddao.GetSingleResultRow
221
- URLData []URLRequestResult
243
+ Res * ddao.GetSingleResultRow
244
+ URLData []URLRequestResult
245
+ URLDataJSON template.JS
222
246
bp
223
247
}{
224
- Res : & res ,
225
- URLData : results ,
248
+ Res : & res ,
249
+ URLData : results ,
250
+ URLDataJSON : jsonData ,
226
251
}
227
252
228
253
// Execute the template
229
- err : = t .ExecuteTemplate (w , "pkg_info.html" , s )
254
+ err = t .ExecuteTemplate (w , "pkg_info.html" , s )
230
255
if err != nil {
231
256
log .Errorf (ctx , "templates.PkgInfo: %v" , err )
232
257
}
0 commit comments