ensureCopilotSetupStepsWithUpgrade creates .github/workflows/copilot-setup-steps.yml If the file already exists, it renders console instructions instead of editing When upgradeVersion is true and called from upgrade command, this is a special case
(ctx context.Context, verbose bool, actionMode workflow.ActionMode, version string, upgradeVersion bool)
| 171 | // If the file already exists, it renders console instructions instead of editing |
| 172 | // When upgradeVersion is true and called from upgrade command, this is a special case |
| 173 | func ensureCopilotSetupStepsWithUpgrade(ctx context.Context, verbose bool, actionMode workflow.ActionMode, version string, upgradeVersion bool) error { |
| 174 | copilotSetupLog.Printf("Creating copilot-setup-steps.yml with action mode: %s, version: %s, upgradeVersion: %v", actionMode, version, upgradeVersion) |
| 175 | |
| 176 | // Create a SHA resolver for release/action mode to enable SHA-pinned action references |
| 177 | var resolver workflow.SHAResolver |
| 178 | if actionMode.IsRelease() || actionMode.IsAction() { |
| 179 | cache := workflow.NewActionCache(".") |
| 180 | _ = cache.Load() // Ignore errors if cache doesn't exist yet |
| 181 | resolver = workflow.NewActionResolver(cache) |
| 182 | } |
| 183 | |
| 184 | workflowsDir := constants.GetWorkflowDir() |
| 185 | setupStepsPath := filepath.Join(workflowsDir, "copilot-setup-steps.yml") |
| 186 | if err := fileutil.EnsureParentDir(setupStepsPath, constants.DirPermPublic); err != nil { |
| 187 | return fmt.Errorf("failed to create workflows directory: %w", err) |
| 188 | } |
| 189 | copilotSetupLog.Printf("Ensured directory exists: %s", workflowsDir) |
| 190 | |
| 191 | // Check if file already exists |
| 192 | if _, err := os.Stat(setupStepsPath); err == nil { |
| 193 | copilotSetupLog.Printf("File already exists: %s", setupStepsPath) |
| 194 | |
| 195 | // Read existing file to check if extension install step exists |
| 196 | content, err := os.ReadFile(setupStepsPath) |
| 197 | if err != nil { |
| 198 | return fmt.Errorf("failed to read existing copilot-setup-steps.yml: %w", err) |
| 199 | } |
| 200 | |
| 201 | // Check if the extension install step is already present (check for both modes) |
| 202 | contentStr := string(content) |
| 203 | hasLegacyInstall := strings.Contains(contentStr, "install-gh-aw.sh") || |
| 204 | (strings.Contains(contentStr, "Install gh-aw extension") && strings.Contains(contentStr, "curl -fsSL")) |
| 205 | hasActionInstall := strings.Contains(contentStr, "actions/setup-cli") |
| 206 | |
| 207 | // If we have an install step and upgradeVersion is true, this is from upgrade command |
| 208 | // In this case, we still update the file for backward compatibility |
| 209 | if (hasLegacyInstall || hasActionInstall) && upgradeVersion { |
| 210 | copilotSetupLog.Print("Extension install step exists, attempting version upgrade (upgrade command)") |
| 211 | |
| 212 | upgraded, updatedContent, err := upgradeSetupCliVersionInContent(ctx, content, actionMode, version, resolver) |
| 213 | if err != nil { |
| 214 | return fmt.Errorf("failed to upgrade setup-cli version: %w", err) |
| 215 | } |
| 216 | |
| 217 | if !upgraded { |
| 218 | copilotSetupLog.Print("No version upgrade needed") |
| 219 | if verbose { |
| 220 | fmt.Fprintln(os.Stderr, console.FormatInfoMessageStderr("No version upgrade needed for "+setupStepsPath)) |
| 221 | } |
| 222 | return nil |
| 223 | } |
| 224 | |
| 225 | if err := os.WriteFile(setupStepsPath, updatedContent, constants.FilePermSensitive); err != nil { |
| 226 | return fmt.Errorf("failed to update copilot-setup-steps.yml: %w", err) |
| 227 | } |
| 228 | copilotSetupLog.Printf("Upgraded version in file: %s", setupStepsPath) |
| 229 | |
| 230 | if verbose { |
no test coverage detected