generateCacheMemoryValidation generates validation steps for cache-memory file types This should be called after agent execution to validate files before upload/save
(builder *strings.Builder, data *WorkflowData)
| 665 | // generateCacheMemoryValidation generates validation steps for cache-memory file types |
| 666 | // This should be called after agent execution to validate files before upload/save |
| 667 | func generateCacheMemoryValidation(builder *strings.Builder, data *WorkflowData) { |
| 668 | if data.CacheMemoryConfig == nil || len(data.CacheMemoryConfig.Caches) == 0 { |
| 669 | return |
| 670 | } |
| 671 | |
| 672 | cacheLog.Printf("Generating cache-memory validation steps for %d caches", len(data.CacheMemoryConfig.Caches)) |
| 673 | |
| 674 | // Use backward-compatible paths only when there's a single cache with ID "default" |
| 675 | useBackwardCompatiblePaths := len(data.CacheMemoryConfig.Caches) == 1 && data.CacheMemoryConfig.Caches[0].ID == "default" |
| 676 | |
| 677 | for _, cache := range data.CacheMemoryConfig.Caches { |
| 678 | // Skip restore-only caches |
| 679 | if cache.RestoreOnly { |
| 680 | continue |
| 681 | } |
| 682 | |
| 683 | // Skip validation step if allowed extensions is empty (means all files are allowed) |
| 684 | if len(cache.AllowedExtensions) == 0 { |
| 685 | cacheLog.Printf("Skipping validation step for cache %s (empty allowed-extensions means all files are allowed)", cache.ID) |
| 686 | continue |
| 687 | } |
| 688 | |
| 689 | cacheDir := cacheMemoryDirFor(cache.ID) |
| 690 | |
| 691 | // Prepare allowed extensions array for JavaScript |
| 692 | allowedExtsJSON, _ := json.Marshal(cache.AllowedExtensions) //nolint:jsonmarshalignoredeerror // marshaling a string slice cannot fail |
| 693 | |
| 694 | // Build validation script |
| 695 | var validationScript strings.Builder |
| 696 | validationScript.WriteString(" const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs');\n") |
| 697 | validationScript.WriteString(" setupGlobals(core, github, context, exec, io, getOctokit);\n") |
| 698 | validationScript.WriteString(" const { validateMemoryFiles } = require('${{ runner.temp }}/gh-aw/actions/validate_memory_files.cjs');\n") |
| 699 | fmt.Fprintf(&validationScript, " const allowedExtensions = %s;\n", allowedExtsJSON) |
| 700 | fmt.Fprintf(&validationScript, " const result = validateMemoryFiles('%s', 'cache', allowedExtensions);\n", cacheDir) |
| 701 | validationScript.WriteString(" if (!result.valid) {\n") |
| 702 | fmt.Fprintf(&validationScript, " core.setFailed(`File type validation failed: Found $${result.invalidFiles.length} file(s) with invalid extensions. Only %s are allowed.`);\n", strings.Join(cache.AllowedExtensions, ", ")) |
| 703 | validationScript.WriteString(" }\n") |
| 704 | |
| 705 | // Generate validation step using helper |
| 706 | stepName := "Validate cache-memory file types" |
| 707 | if !useBackwardCompatiblePaths { |
| 708 | stepName = fmt.Sprintf("Validate cache-memory file types (%s)", cache.ID) |
| 709 | } |
| 710 | builder.WriteString(generateInlineGitHubScriptStep(stepName, validationScript.String(), "always()", data)) |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | // generateCacheMemoryArtifactUpload generates artifact upload steps for cache-memory. |
| 715 | // This should be called after agent execution steps to ensure cache is uploaded after the agent has finished. |
no test coverage detected