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

Function renameGitHubReposToAllowedRepos

pkg/cli/codemod_github_repos.go:61–123  ·  view source on GitHub ↗

renameGitHubReposToAllowedRepos renames 'repos:' to 'allowed-repos:' within the tools.github configuration block.

(lines []string)

Source from the content-addressed store, hash-verified

59// renameGitHubReposToAllowedRepos renames 'repos:' to 'allowed-repos:' within the
60// tools.github configuration block.
61func renameGitHubReposToAllowedRepos(lines []string) ([]string, bool) {
62 var result []string
63 modified := false
64
65 var inTools, inToolsGithub bool
66 var toolsIndent, toolsGithubIndent string
67
68 for i, line := range lines {
69 trimmed := strings.TrimSpace(line)
70
71 // Skip empty lines without resetting state
72 if trimmed == "" {
73 result = append(result, line)
74 continue
75 }
76
77 // Exit blocks when indentation signals we've left them
78 if !strings.HasPrefix(trimmed, "#") {
79 if inToolsGithub && hasExitedBlock(line, toolsGithubIndent) {
80 inToolsGithub = false
81 }
82 if inTools && hasExitedBlock(line, toolsIndent) {
83 inTools = false
84 inToolsGithub = false
85 }
86 }
87
88 // Detect 'tools:' block
89 if strings.HasPrefix(trimmed, "tools:") {
90 inTools = true
91 inToolsGithub = false
92 toolsIndent = getIndentation(line)
93 result = append(result, line)
94 continue
95 }
96
97 // Detect 'github:' block inside 'tools:'
98 if inTools && strings.HasPrefix(trimmed, "github:") {
99 inToolsGithub = true
100 toolsGithubIndent = getIndentation(line)
101 result = append(result, line)
102 continue
103 }
104
105 // Rename 'repos:' to 'allowed-repos:' when inside tools.github
106 if inToolsGithub && strings.HasPrefix(trimmed, "repos:") {
107 lineIndent := getIndentation(line)
108 if isDescendant(lineIndent, toolsGithubIndent) {
109 newLine, replaced := findAndReplaceInLine(line, "repos", "allowed-repos")
110 if replaced {
111 result = append(result, newLine)
112 modified = true
113 githubReposCodemodLog.Printf("Renamed 'repos' to 'allowed-repos' on line %d", i+1)
114 continue
115 }
116 }
117 }
118

Callers

nothing calls this directly

Calls 5

hasExitedBlockFunction · 0.85
getIndentationFunction · 0.85
isDescendantFunction · 0.85
findAndReplaceInLineFunction · 0.85
PrintfMethod · 0.45

Tested by

no test coverage detected