ensureMaintenanceWorkflow checks existing workflows for expires field and generates/updates the maintenance workflow file if any workflows use it
(ctx context.Context, verbose bool)
| 236 | // ensureMaintenanceWorkflow checks existing workflows for expires field and generates/updates |
| 237 | // the maintenance workflow file if any workflows use it |
| 238 | func ensureMaintenanceWorkflow(ctx context.Context, verbose bool) error { |
| 239 | initLog.Print("Checking for workflows with expires field") |
| 240 | |
| 241 | // Find git root |
| 242 | gitRoot, err := gitutil.FindGitRoot() |
| 243 | if err != nil { |
| 244 | return fmt.Errorf("failed to find git root: %w", err) |
| 245 | } |
| 246 | |
| 247 | // Determine the workflows directory |
| 248 | workflowsDir := filepath.Join(gitRoot, constants.GetWorkflowDir()) |
| 249 | if _, err := os.Stat(workflowsDir); os.IsNotExist(err) { |
| 250 | // No workflows directory yet, skip maintenance workflow generation |
| 251 | initLog.Print("No workflows directory found, skipping maintenance workflow generation") |
| 252 | return nil |
| 253 | } |
| 254 | |
| 255 | // Find all workflow markdown files |
| 256 | files, err := filepath.Glob(filepath.Join(workflowsDir, "*.md")) |
| 257 | if err != nil { |
| 258 | return fmt.Errorf("failed to find workflow files: %w", err) |
| 259 | } |
| 260 | |
| 261 | // Filter out README.md files |
| 262 | files = filterWorkflowFiles(files) |
| 263 | |
| 264 | // Create a compiler to parse workflows (version and action mode auto-detected) |
| 265 | compiler := workflow.NewCompiler() |
| 266 | initLog.Printf("Action mode detected for maintenance workflow: %s", compiler.GetActionMode()) |
| 267 | |
| 268 | // Parse all workflows to collect WorkflowData |
| 269 | var workflowDataList []*workflow.WorkflowData |
| 270 | for _, file := range files { |
| 271 | initLog.Printf("Parsing workflow: %s", file) |
| 272 | workflowData, err := compiler.ParseWorkflowFile(file) |
| 273 | if err != nil { |
| 274 | // Ignore parse errors - workflows might be incomplete during init |
| 275 | initLog.Printf("Skipping workflow %s due to parse error: %v", file, err) |
| 276 | continue |
| 277 | } |
| 278 | |
| 279 | workflowDataList = append(workflowDataList, workflowData) |
| 280 | } |
| 281 | |
| 282 | // Always call GenerateMaintenanceWorkflow even with empty list |
| 283 | // This allows it to delete existing maintenance workflow if no workflows have expires |
| 284 | initLog.Printf("Generating maintenance workflow for %d workflows", len(workflowDataList)) |
| 285 | |
| 286 | // Load repo-level configuration (optional; errors are non-fatal during init). |
| 287 | repoConfig, err := workflow.LoadRepoConfig(gitRoot) |
| 288 | if err != nil { |
| 289 | initLog.Printf("Failed to load repo config, using defaults: %v", err) |
| 290 | repoConfig = nil |
| 291 | } |
| 292 | |
| 293 | if err := workflow.GenerateMaintenanceWorkflow(ctx, workflow.GenerateMaintenanceWorkflowOptions{ |
| 294 | WorkflowDataList: workflowDataList, |
| 295 | WorkflowDir: workflowsDir, |