Skip to content

Commit eb11524

Browse files
authored
add --repo options to more commands (#4007)
1 parent 14ee052 commit eb11524

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
lines changed

cmd/gh-aw/main.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ var enableCmd = &cobra.Command{
128128
Examples:
129129
` + constants.CLIExtensionPrefix + ` enable # Enable all workflows
130130
` + constants.CLIExtensionPrefix + ` enable ci-doctor # Enable specific workflow
131-
` + constants.CLIExtensionPrefix + ` enable ci-doctor daily # Enable multiple workflows`,
131+
` + constants.CLIExtensionPrefix + ` enable ci-doctor daily # Enable multiple workflows
132+
` + constants.CLIExtensionPrefix + ` enable ci-doctor --repo owner/repo # Enable workflow in specific repository`,
132133
Run: func(cmd *cobra.Command, args []string) {
133-
if err := cli.EnableWorkflowsByNames(args); err != nil {
134+
repoOverride, _ := cmd.Flags().GetString("repo")
135+
if err := cli.EnableWorkflowsByNames(args, repoOverride); err != nil {
134136
fmt.Fprintln(os.Stderr, console.FormatErrorMessage(err.Error()))
135137
os.Exit(1)
136138
}
@@ -145,9 +147,11 @@ var disableCmd = &cobra.Command{
145147
Examples:
146148
` + constants.CLIExtensionPrefix + ` disable # Disable all workflows
147149
` + constants.CLIExtensionPrefix + ` disable ci-doctor # Disable specific workflow
148-
` + constants.CLIExtensionPrefix + ` disable ci-doctor daily # Disable multiple workflows`,
150+
` + constants.CLIExtensionPrefix + ` disable ci-doctor daily # Disable multiple workflows
151+
` + constants.CLIExtensionPrefix + ` disable ci-doctor --repo owner/repo # Disable workflow in specific repository`,
149152
Run: func(cmd *cobra.Command, args []string) {
150-
if err := cli.DisableWorkflowsByNames(args); err != nil {
153+
repoOverride, _ := cmd.Flags().GetString("repo")
154+
if err := cli.DisableWorkflowsByNames(args, repoOverride); err != nil {
151155
fmt.Fprintln(os.Stderr, console.FormatErrorMessage(err.Error()))
152156
os.Exit(1)
153157
}
@@ -431,6 +435,10 @@ Use "` + constants.CLIExtensionPrefix + ` help all" to show help for all command
431435
// Add flags to remove command
432436
removeCmd.Flags().Bool("keep-orphans", false, "Skip removal of orphaned include files that are no longer referenced by any workflow")
433437

438+
// Add flags to enable/disable commands
439+
enableCmd.Flags().StringP("repo", "r", "", "Repository to enable workflows in (owner/repo format)")
440+
disableCmd.Flags().StringP("repo", "r", "", "Repository to disable workflows in (owner/repo format)")
441+
434442
// Add flags to run command
435443
runCmd.Flags().Int("repeat", 0, "Number of times to repeat running workflows (0 = run once)")
436444
runCmd.Flags().Bool("enable-if-needed", false, "Enable the workflow before running if needed, and restore state afterward")

pkg/cli/enable.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import (
1414
)
1515

1616
// EnableWorkflowsByNames enables workflows by specific names, or all if no names provided
17-
func EnableWorkflowsByNames(workflowNames []string) error {
18-
return toggleWorkflowsByNames(workflowNames, true)
17+
func EnableWorkflowsByNames(workflowNames []string, repoOverride string) error {
18+
return toggleWorkflowsByNames(workflowNames, true, repoOverride)
1919
}
2020

2121
// DisableWorkflowsByNames disables workflows by specific names, or all if no names provided
22-
func DisableWorkflowsByNames(workflowNames []string) error {
23-
return toggleWorkflowsByNames(workflowNames, false)
22+
func DisableWorkflowsByNames(workflowNames []string, repoOverride string) error {
23+
return toggleWorkflowsByNames(workflowNames, false, repoOverride)
2424
}
2525

2626
// Deprecated: Use EnableWorkflowsByNames with specific workflow names instead
@@ -40,7 +40,7 @@ func DisableWorkflows(pattern string) error {
4040
}
4141

4242
// toggleWorkflowsByNames toggles workflows by specific names, or all if no names provided
43-
func toggleWorkflowsByNames(workflowNames []string, enable bool) error {
43+
func toggleWorkflowsByNames(workflowNames []string, enable bool, repoOverride string) error {
4444
action := "enable"
4545
if !enable {
4646
action = "disable"
@@ -68,7 +68,7 @@ func toggleWorkflowsByNames(workflowNames []string, enable bool) error {
6868
}
6969

7070
// Recursively call with all workflow names
71-
return toggleWorkflowsByNames(allWorkflowNames, enable)
71+
return toggleWorkflowsByNames(allWorkflowNames, enable, repoOverride)
7272
}
7373

7474
// Check if gh CLI is available
@@ -202,9 +202,17 @@ func toggleWorkflowsByNames(workflowNames []string, enable bool) error {
202202
if enable {
203203
// Prefer enabling by ID, otherwise fall back to lock file name
204204
if t.ID != 0 {
205-
cmd = exec.Command("gh", "workflow", "enable", strconv.FormatInt(t.ID, 10))
205+
args := []string{"workflow", "enable", strconv.FormatInt(t.ID, 10)}
206+
if repoOverride != "" {
207+
args = append(args, "--repo", repoOverride)
208+
}
209+
cmd = exec.Command("gh", args...)
206210
} else {
207-
cmd = exec.Command("gh", "workflow", "enable", t.LockFileBase)
211+
args := []string{"workflow", "enable", t.LockFileBase}
212+
if repoOverride != "" {
213+
args = append(args, "--repo", repoOverride)
214+
}
215+
cmd = exec.Command("gh", args...)
208216
}
209217
} else {
210218
// First cancel any running workflows (by ID when available, else by lock file name)
@@ -213,12 +221,20 @@ func toggleWorkflowsByNames(workflowNames []string, enable bool) error {
213221
fmt.Fprintf(os.Stderr, "Warning: Failed to cancel runs for workflow %s: %v\n", t.Name, err)
214222
}
215223
// Prefer disabling by lock file name for reliability
216-
cmd = exec.Command("gh", "workflow", "disable", t.LockFileBase)
224+
args := []string{"workflow", "disable", t.LockFileBase}
225+
if repoOverride != "" {
226+
args = append(args, "--repo", repoOverride)
227+
}
228+
cmd = exec.Command("gh", args...)
217229
} else {
218230
if err := cancelWorkflowRunsByLockFile(t.LockFileBase); err != nil {
219231
fmt.Fprintf(os.Stderr, "Warning: Failed to cancel runs for workflow %s: %v\n", t.Name, err)
220232
}
221-
cmd = exec.Command("gh", "workflow", "disable", t.LockFileBase)
233+
args := []string{"workflow", "disable", t.LockFileBase}
234+
if repoOverride != "" {
235+
args = append(args, "--repo", repoOverride)
236+
}
237+
cmd = exec.Command("gh", args...)
222238
}
223239
}
224240

pkg/cli/logs.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ Examples:
339339
` + constants.CLIExtensionPrefix + ` logs --tool-graph # Generate Mermaid tool sequence graph
340340
` + constants.CLIExtensionPrefix + ` logs --parse # Parse logs and generate markdown reports
341341
` + constants.CLIExtensionPrefix + ` logs --json # Output metrics in JSON format
342-
` + constants.CLIExtensionPrefix + ` logs --parse --json # Generate both markdown and JSON`,
342+
` + constants.CLIExtensionPrefix + ` logs --parse --json # Generate both markdown and JSON
343+
` + constants.CLIExtensionPrefix + ` logs weekly-research --repo owner/repo # Download logs from specific repository`,
343344
Run: func(cmd *cobra.Command, args []string) {
344345
var workflowName string
345346
if len(args) > 0 && args[0] != "" {
@@ -387,6 +388,7 @@ Examples:
387388
parse, _ := cmd.Flags().GetBool("parse")
388389
jsonOutput, _ := cmd.Flags().GetBool("json")
389390
timeout, _ := cmd.Flags().GetInt("timeout")
391+
repoOverride, _ := cmd.Flags().GetString("repo")
390392

391393
// Resolve relative dates to absolute dates for GitHub CLI
392394
now := time.Now()
@@ -435,7 +437,7 @@ Examples:
435437
os.Exit(1)
436438
}
437439

438-
if err := DownloadWorkflowLogs(workflowName, count, startDate, endDate, outputDir, engine, branch, beforeRunID, afterRunID, verbose, toolGraph, noStaged, firewallOnly, noFirewall, parse, jsonOutput, timeout); err != nil {
440+
if err := DownloadWorkflowLogs(workflowName, count, startDate, endDate, outputDir, engine, branch, beforeRunID, afterRunID, repoOverride, verbose, toolGraph, noStaged, firewallOnly, noFirewall, parse, jsonOutput, timeout); err != nil {
439441
fmt.Fprintln(os.Stderr, console.FormatError(console.CompilerError{
440442
Type: "error",
441443
Message: err.Error(),
@@ -454,6 +456,7 @@ Examples:
454456
logsCmd.Flags().String("branch", "", "Filter runs by branch name (e.g., main, feature-branch)")
455457
logsCmd.Flags().Int64("before-run-id", 0, "Filter runs with database ID before this value (exclusive)")
456458
logsCmd.Flags().Int64("after-run-id", 0, "Filter runs with database ID after this value (exclusive)")
459+
logsCmd.Flags().StringP("repo", "r", "", "Repository to download logs from (owner/repo format)")
457460
logsCmd.Flags().Bool("tool-graph", false, "Generate Mermaid tool sequence graph from agent logs")
458461
logsCmd.Flags().Bool("no-staged", false, "Filter out staged workflow runs (exclude runs with staged: true in aw_info.json)")
459462
logsCmd.Flags().Bool("firewall", false, "Filter to only runs with firewall enabled")
@@ -466,7 +469,7 @@ Examples:
466469
}
467470

468471
// DownloadWorkflowLogs downloads and analyzes workflow logs with metrics
469-
func DownloadWorkflowLogs(workflowName string, count int, startDate, endDate, outputDir, engine, branch string, beforeRunID, afterRunID int64, verbose bool, toolGraph bool, noStaged bool, firewallOnly bool, noFirewall bool, parse bool, jsonOutput bool, timeout int) error {
472+
func DownloadWorkflowLogs(workflowName string, count int, startDate, endDate, outputDir, engine, branch string, beforeRunID, afterRunID int64, repoOverride string, verbose bool, toolGraph bool, noStaged bool, firewallOnly bool, noFirewall bool, parse bool, jsonOutput bool, timeout int) error {
470473
logsLog.Printf("Starting workflow log download: workflow=%s, count=%d, startDate=%s, endDate=%s, outputDir=%s", workflowName, count, startDate, endDate, outputDir)
471474
if verbose {
472475
fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Fetching workflow runs from GitHub Actions..."))
@@ -543,7 +546,7 @@ func DownloadWorkflowLogs(workflowName string, count int, startDate, endDate, ou
543546
}
544547
}
545548

546-
runs, totalFetched, err := listWorkflowRunsWithPagination(workflowName, batchSize, startDate, endDate, beforeDate, branch, beforeRunID, afterRunID, len(processedRuns), count, verbose)
549+
runs, totalFetched, err := listWorkflowRunsWithPagination(workflowName, batchSize, startDate, endDate, beforeDate, branch, beforeRunID, afterRunID, repoOverride, len(processedRuns), count, verbose)
547550
if err != nil {
548551
return err
549552
}
@@ -1032,7 +1035,7 @@ func downloadRunArtifactsConcurrent(runs []WorkflowRun, outputDir string, verbos
10321035
// not the total number of matching runs the user wants to find.
10331036
//
10341037
// The processedCount and targetCount parameters are used to display progress in the spinner message.
1035-
func listWorkflowRunsWithPagination(workflowName string, limit int, startDate, endDate, beforeDate, branch string, beforeRunID, afterRunID int64, processedCount, targetCount int, verbose bool) ([]WorkflowRun, int, error) {
1038+
func listWorkflowRunsWithPagination(workflowName string, limit int, startDate, endDate, beforeDate, branch string, beforeRunID, afterRunID int64, repoOverride string, processedCount, targetCount int, verbose bool) ([]WorkflowRun, int, error) {
10361039
args := []string{"run", "list", "--json", "databaseId,number,url,status,conclusion,workflowName,createdAt,startedAt,updatedAt,event,headBranch,headSha,displayTitle"}
10371040

10381041
// Add filters
@@ -1056,6 +1059,10 @@ func listWorkflowRunsWithPagination(workflowName string, limit int, startDate, e
10561059
if branch != "" {
10571060
args = append(args, "--branch", branch)
10581061
}
1062+
// Add repo filter
1063+
if repoOverride != "" {
1064+
args = append(args, "--repo", repoOverride)
1065+
}
10591066

10601067
if verbose {
10611068
fmt.Println(console.FormatInfoMessage(fmt.Sprintf("Executing: gh %s", strings.Join(args, " "))))

pkg/cli/logs_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestDownloadWorkflowLogs(t *testing.T) {
1919
// Test the DownloadWorkflowLogs function
2020
// This should either fail with auth error (if not authenticated)
2121
// or succeed with no results (if authenticated but no workflows match)
22-
err := DownloadWorkflowLogs("", 1, "", "", "./test-logs", "", "", 0, 0, false, false, false, false, false, false, false, 0)
22+
err := DownloadWorkflowLogs("", 1, "", "", "./test-logs", "", "", 0, 0, "", false, false, false, false, false, false, false, 0)
2323

2424
// If GitHub CLI is authenticated, the function may succeed but find no results
2525
// If not authenticated, it should return an auth error
@@ -342,7 +342,7 @@ func TestListWorkflowRunsWithPagination(t *testing.T) {
342342

343343
// This should fail with authentication error (if not authenticated)
344344
// or succeed with empty results (if authenticated but no workflows match)
345-
runs, _, err := listWorkflowRunsWithPagination("nonexistent-workflow", 5, "", "", "2024-01-01T00:00:00Z", "", 0, 0, 0, 5, false)
345+
runs, _, err := listWorkflowRunsWithPagination("nonexistent-workflow", 5, "", "", "2024-01-01T00:00:00Z", "", 0, 0, "", 0, 5, false)
346346

347347
if err != nil {
348348
// If there's an error, it should be an authentication error or workflow not found
@@ -915,7 +915,7 @@ func TestDownloadWorkflowLogsWithEngineFilter(t *testing.T) {
915915
if !tt.expectError {
916916
// For valid engines, test that the function can be called without panic
917917
// It may still fail with auth errors, which is expected
918-
err := DownloadWorkflowLogs("", 1, "", "", "./test-logs", tt.engine, "", 0, 0, false, false, false, false, false, false, false, 0)
918+
err := DownloadWorkflowLogs("", 1, "", "", "./test-logs", tt.engine, "", 0, 0, "", false, false, false, false, false, false, false, 0)
919919

920920
// Clean up any created directories
921921
os.RemoveAll("./test-logs")

0 commit comments

Comments
 (0)