getUploadAssetsCodemod creates a codemod for migrating upload-assets to upload-asset (plural to singular)
()
| 10 | |
| 11 | // getUploadAssetsCodemod creates a codemod for migrating upload-assets to upload-asset (plural to singular) |
| 12 | func getUploadAssetsCodemod() Codemod { |
| 13 | return Codemod{ |
| 14 | ID: "upload-assets-to-upload-asset-migration", |
| 15 | Name: "Migrate upload-assets to upload-asset", |
| 16 | Description: "Replaces deprecated 'safe-outputs.upload-assets' field with 'safe-outputs.upload-asset' (plural to singular)", |
| 17 | IntroducedIn: "0.3.0", |
| 18 | Apply: func(content string, frontmatter map[string]any) (string, bool, error) { |
| 19 | // Check if safe-outputs.upload-assets exists |
| 20 | safeOutputsValue, hasSafeOutputs := frontmatter["safe-outputs"] |
| 21 | if !hasSafeOutputs { |
| 22 | return content, false, nil |
| 23 | } |
| 24 | |
| 25 | safeOutputsMap, ok := safeOutputsValue.(map[string]any) |
| 26 | if !ok { |
| 27 | return content, false, nil |
| 28 | } |
| 29 | |
| 30 | // Check if upload-assets field exists in safe-outputs (plural is deprecated) |
| 31 | _, hasUploadAssets := safeOutputsMap["upload-assets"] |
| 32 | if !hasUploadAssets { |
| 33 | return content, false, nil |
| 34 | } |
| 35 | |
| 36 | newContent, applied, err := applyFrontmatterLineTransform(content, func(lines []string) ([]string, bool) { |
| 37 | var modified bool |
| 38 | var inSafeOutputsBlock bool |
| 39 | var safeOutputsIndent string |
| 40 | result := make([]string, len(lines)) |
| 41 | for i, line := range lines { |
| 42 | trimmedLine := strings.TrimSpace(line) |
| 43 | |
| 44 | // Track if we're in the safe-outputs block |
| 45 | if strings.HasPrefix(trimmedLine, "safe-outputs:") { |
| 46 | inSafeOutputsBlock = true |
| 47 | safeOutputsIndent = getIndentation(line) |
| 48 | result[i] = line |
| 49 | continue |
| 50 | } |
| 51 | |
| 52 | // Check if we've left the safe-outputs block |
| 53 | if inSafeOutputsBlock && len(trimmedLine) > 0 && !strings.HasPrefix(trimmedLine, "#") { |
| 54 | if hasExitedBlock(line, safeOutputsIndent) { |
| 55 | inSafeOutputsBlock = false |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Replace upload-assets with upload-asset if in safe-outputs block |
| 60 | if inSafeOutputsBlock && strings.HasPrefix(trimmedLine, "upload-assets:") { |
| 61 | replacedLine, didReplace := findAndReplaceInLine(line, "upload-assets", "upload-asset") |
| 62 | if didReplace { |
| 63 | result[i] = replacedLine |
| 64 | modified = true |
| 65 | uploadAssetsCodemodLog.Printf("Replaced safe-outputs.upload-assets with safe-outputs.upload-asset on line %d", i+1) |
| 66 | } else { |
| 67 | result[i] = line |
| 68 | } |
| 69 | } else { |