MCPcopy Create free account
hub / github.com/github/gh-aw / migrateEngineFieldToTopLevel

Function migrateEngineFieldToTopLevel

pkg/cli/codemod_engine_to_top_level_helpers.go:19–113  ·  view source on GitHub ↗
(
	content string,
	frontmatter map[string]any,
	opts migrateEngineFieldToTopLevelOptions,
)

Source from the content-addressed store, hash-verified

17}
18
19func migrateEngineFieldToTopLevel(
20 content string,
21 frontmatter map[string]any,
22 opts migrateEngineFieldToTopLevelOptions,
23) (string, bool, error) {
24 engineValue, hasEngine := frontmatter["engine"]
25 if !hasEngine {
26 return content, false, nil
27 }
28 engineMap, ok := engineValue.(map[string]any)
29 if !ok {
30 return content, false, nil
31 }
32 if _, hasEngineField := engineMap[opts.engineField]; !hasEngineField {
33 return content, false, nil
34 }
35
36 hasPreservedTopLevelField := false
37 for _, field := range opts.preserveTopLevelFields {
38 if _, exists := frontmatter[field]; exists {
39 hasPreservedTopLevelField = true
40 break
41 }
42 }
43
44 return applyFrontmatterLineTransform(content, func(lines []string) ([]string, bool) {
45 for _, line := range lines {
46 trimmed := strings.TrimSpace(line)
47 if !isTopLevelKey(line) || !strings.HasPrefix(trimmed, "engine:") {
48 continue
49 }
50 inlineValue := strings.TrimSpace(strings.TrimPrefix(trimmed, "engine:"))
51 if strings.HasPrefix(inlineValue, "{") && strings.Contains(inlineValue, opts.engineField+":") {
52 if opts.log != nil {
53 opts.log.Print(opts.skipInlineMessage)
54 }
55 return lines, false
56 }
57 }
58
59 fieldSuffix := ""
60 inEngineBlock := false
61 engineIndent := ""
62 engineFieldPrefix := opts.engineField + ":"
63 for _, line := range lines {
64 trimmed := strings.TrimSpace(line)
65 if isTopLevelKey(line) && strings.HasPrefix(trimmed, "engine:") {
66 inEngineBlock = true
67 engineIndent = getIndentation(line)
68 continue
69 }
70 if inEngineBlock && len(trimmed) > 0 && !strings.HasPrefix(trimmed, "#") && len(getIndentation(line)) <= len(engineIndent) {
71 inEngineBlock = false
72 }
73 if inEngineBlock && strings.HasPrefix(trimmed, engineFieldPrefix) {
74 parts := strings.SplitN(line, ":", 2)
75 if len(parts) == 2 {
76 fieldSuffix = parts[1]

Calls 5

isTopLevelKeyFunction · 0.85
getIndentationFunction · 0.85
removeFieldFromBlockFunction · 0.85
PrintMethod · 0.80

Tested by

no test coverage detected