Skip to content

Commit 8fa9503

Browse files
dsymeCopilot
andauthored
🤖 Optimize workflow disabling in trial command (#4009)
* only disable workflows once * Update pkg/cli/trial_command.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent bffde33 commit 8fa9503

1 file changed

Lines changed: 48 additions & 37 deletions

File tree

‎pkg/cli/trial_command.go‎

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,54 @@ func RunWorkflowTrials(workflowSpecs []string, logicalRepoSpec string, cloneRepo
312312
}
313313
}
314314

315+
// Step 2.8: Disable all workflows except the ones being trialled (only in clone-repo mode, done once before all trials)
316+
if cloneRepoSlug != "" {
317+
// Build list of workflow names to keep enabled
318+
var workflowsToKeep []string
319+
for _, spec := range parsedSpecs {
320+
workflowsToKeep = append(workflowsToKeep, spec.WorkflowName)
321+
}
322+
323+
if verbose {
324+
fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Disabling workflows in cloned repository (keeping: %s)", strings.Join(workflowsToKeep, ", "))))
325+
}
326+
327+
// Clone host repository temporarily to access workflows
328+
tempDirForDisable, err := cloneTrialHostRepository(hostRepoSlug, verbose)
329+
if err != nil {
330+
return fmt.Errorf("failed to clone host repository for workflow disabling: %w", err)
331+
}
332+
defer func() {
333+
if err := os.RemoveAll(tempDirForDisable); err != nil {
334+
trialLog.Printf("Failed to cleanup temp directory for workflow disabling: %v", err)
335+
}
336+
}()
337+
338+
// Change to temp directory to access local .github/workflows
339+
originalDir, err := os.Getwd()
340+
if err != nil {
341+
return fmt.Errorf("failed to get current directory: %w", err)
342+
}
343+
344+
if err := os.Chdir(tempDirForDisable); err != nil {
345+
return fmt.Errorf("failed to change to temp directory: %w", err)
346+
}
347+
// Always attempt to change back to the original directory
348+
defer func() {
349+
if err := os.Chdir(originalDir); err != nil {
350+
trialLog.Printf("Failed to change back to original directory: %v", err)
351+
}
352+
}()
353+
354+
// Disable workflows (pass empty string for repoSlug since we're working locally)
355+
disableErr := DisableAllWorkflowsExcept("", workflowsToKeep, verbose)
356+
// Check for disable errors after changing back
357+
if disableErr != nil {
358+
// Log warning but don't fail the trial - workflow disabling is not critical
359+
fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Failed to disable workflows: %v", disableErr)))
360+
}
361+
}
362+
315363
// Function to run all trials once
316364
runAllTrials := func() error {
317365
// Generate a unique datetime-ID for this trial session
@@ -338,43 +386,6 @@ func RunWorkflowTrials(workflowSpecs []string, logicalRepoSpec string, cloneRepo
338386
}
339387
}()
340388

341-
// Step 3.5: Disable all workflows except the ones being trialled (only in clone-repo mode)
342-
if cloneRepoSlug != "" {
343-
// Build list of workflow names to keep enabled
344-
var workflowsToKeep []string
345-
for _, spec := range parsedSpecs {
346-
workflowsToKeep = append(workflowsToKeep, spec.WorkflowName)
347-
}
348-
349-
if verbose {
350-
fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Disabling workflows in cloned repository (keeping: %s)", strings.Join(workflowsToKeep, ", "))))
351-
}
352-
353-
// Change to temp directory to access local .github/workflows
354-
originalDir, err := os.Getwd()
355-
if err != nil {
356-
return fmt.Errorf("failed to get current directory: %w", err)
357-
}
358-
359-
if err := os.Chdir(tempDir); err != nil {
360-
return fmt.Errorf("failed to change to temp directory: %w", err)
361-
}
362-
363-
// Disable workflows (pass empty string for repoSlug since we're working locally)
364-
disableErr := DisableAllWorkflowsExcept("", workflowsToKeep, verbose)
365-
366-
// Change back to original directory
367-
if err := os.Chdir(originalDir); err != nil {
368-
return fmt.Errorf("failed to change back to original directory: %w", err)
369-
}
370-
371-
// Check for disable errors after changing back
372-
if disableErr != nil {
373-
// Log warning but don't fail the trial - workflow disabling is not critical
374-
fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Failed to disable workflows: %v", disableErr)))
375-
}
376-
}
377-
378389
// Step 4: Create trials directory
379390
if err := os.MkdirAll("trials", 0755); err != nil {
380391
return fmt.Errorf("failed to create trials directory: %w", err)

0 commit comments

Comments
 (0)