From 4c176500e577332bf272bb7aa24e7a780cbe6044 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 24 Apr 2026 03:58:52 +0000 Subject: [PATCH 1/3] fix: apply --name flag only to first workflow when adding multiple Agent-Logs-Url: https://github.com/github/gh-aw/sessions/a25578bc-adfa-4662-9d96-202ed66c72ca Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> --- .changeset/patch-add-multiple-workflows.md | 5 ++ pkg/cli/add_command.go | 8 ++- pkg/cli/add_command_test.go | 79 ++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 .changeset/patch-add-multiple-workflows.md diff --git a/.changeset/patch-add-multiple-workflows.md b/.changeset/patch-add-multiple-workflows.md new file mode 100644 index 00000000000..82f7ae99245 --- /dev/null +++ b/.changeset/patch-add-multiple-workflows.md @@ -0,0 +1,5 @@ +--- +"gh-aw": patch +--- + +Fix `--name` flag to only apply to the first workflow when adding multiple workflows with `gh aw add workflow1 workflow2 ...`. Previously the name was applied to all workflows, causing each to overwrite the previous one. diff --git a/pkg/cli/add_command.go b/pkg/cli/add_command.go index 8a105dc8511..f9ce705c361 100644 --- a/pkg/cli/add_command.go +++ b/pkg/cli/add_command.go @@ -273,7 +273,13 @@ func addWorkflowsWithTracking(workflows []*ResolvedWorkflow, tracker *FileTracke fmt.Fprintln(os.Stderr, console.FormatProgressMessage(fmt.Sprintf("Adding workflow %d/%d: %s", i+1, len(workflows), resolved.Spec.WorkflowName))) } - if err := addWorkflowWithTracking(resolved, tracker, opts); err != nil { + // The --name flag only applies to the first workflow when adding multiple + workflowOpts := opts + if i > 0 { + workflowOpts.Name = "" + } + + if err := addWorkflowWithTracking(resolved, tracker, workflowOpts); err != nil { return fmt.Errorf("failed to add workflow '%s': %w", resolved.Spec.String(), err) } } diff --git a/pkg/cli/add_command_test.go b/pkg/cli/add_command_test.go index fb1ad142cdd..40e380505cd 100644 --- a/pkg/cli/add_command_test.go +++ b/pkg/cli/add_command_test.go @@ -4,6 +4,9 @@ package cli import ( "context" + "os" + "os/exec" + "path/filepath" "testing" "github.com/spf13/cobra" @@ -337,3 +340,79 @@ func TestAddCommandArgs(t *testing.T) { err = cmd.Args(cmd, []string{"workflow1", "workflow2"}) require.NoError(t, err, "Should not error with multiple arguments") } + +// TestAddMultipleWorkflowsNameFlag verifies that when multiple workflows are added, +// the --name flag applies only to the first workflow and subsequent workflows use their original names. +func TestAddMultipleWorkflowsNameFlag(t *testing.T) { + // Create a temporary directory for testing + tmpDir, err := os.MkdirTemp("", "gh-aw-add-multiple-workflows-test") + require.NoError(t, err, "Failed to create temp dir") + defer os.RemoveAll(tmpDir) + + // Change to temp directory + originalDir, err := os.Getwd() + require.NoError(t, err, "Failed to get current directory") + defer func() { + _ = os.Chdir(originalDir) + }() + + require.NoError(t, os.Chdir(tmpDir), "Failed to change to temp directory") + + // Initialize a git repository + if err := exec.Command("git", "init").Run(); err != nil { + t.Skip("Skipping test - git not available") + } + _ = exec.Command("git", "config", "user.email", "test@example.com").Run() + _ = exec.Command("git", "config", "user.name", "Test User").Run() + + workflowsDir := filepath.Join(tmpDir, ".github", "workflows") + require.NoError(t, os.MkdirAll(workflowsDir, 0755), "Failed to create workflows directory") + + workflowContent := func(name string) []byte { + return []byte("---\non: push\npermissions:\n contents: read\nengine: copilot\n---\n\n# " + name + "\n") + } + + resolved1 := &ResolvedWorkflow{ + Spec: &WorkflowSpec{ + RepoSpec: RepoSpec{RepoSlug: "test/repo"}, + WorkflowPath: "./workflow-alpha.md", + WorkflowName: "workflow-alpha", + }, + Content: workflowContent("Alpha"), + SourceInfo: &FetchedWorkflow{ + Content: workflowContent("Alpha"), + IsLocal: true, + SourcePath: "./workflow-alpha.md", + }, + } + + resolved2 := &ResolvedWorkflow{ + Spec: &WorkflowSpec{ + RepoSpec: RepoSpec{RepoSlug: "test/repo"}, + WorkflowPath: "./workflow-beta.md", + WorkflowName: "workflow-beta", + }, + Content: workflowContent("Beta"), + SourceInfo: &FetchedWorkflow{ + Content: workflowContent("Beta"), + IsLocal: true, + SourcePath: "./workflow-beta.md", + }, + } + + opts := AddOptions{ + Name: "custom-name", + NoGitattributes: true, + } + + err = addWorkflows([]*ResolvedWorkflow{resolved1, resolved2}, opts) + require.NoError(t, err, "addWorkflows should succeed") + + // The first workflow should be saved as custom-name.md + _, err = os.Stat(filepath.Join(workflowsDir, "custom-name.md")) + require.NoError(t, err, "First workflow should be saved as custom-name.md") + + // The second workflow should use its original name (workflow-beta.md), not custom-name.md + _, err = os.Stat(filepath.Join(workflowsDir, "workflow-beta.md")) + require.NoError(t, err, "Second workflow should be saved as workflow-beta.md (original name)") +} From e795469356da0ce0c06dc8fa42783ac2749660fb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 24 Apr 2026 04:00:23 +0000 Subject: [PATCH 2/3] fix: add comment clarifying struct copy intent for --name flag handling Agent-Logs-Url: https://github.com/github/gh-aw/sessions/a25578bc-adfa-4662-9d96-202ed66c72ca Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> --- pkg/cli/add_command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cli/add_command.go b/pkg/cli/add_command.go index f9ce705c361..7cc32234695 100644 --- a/pkg/cli/add_command.go +++ b/pkg/cli/add_command.go @@ -274,7 +274,7 @@ func addWorkflowsWithTracking(workflows []*ResolvedWorkflow, tracker *FileTracke } // The --name flag only applies to the first workflow when adding multiple - workflowOpts := opts + workflowOpts := opts // copy so that clearing Name does not affect subsequent iterations if i > 0 { workflowOpts.Name = "" } From 115cbc60c2f2edd33cfec434a3f511e6c69ad6b3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 24 Apr 2026 05:02:16 +0000 Subject: [PATCH 3/3] fix: error when --name is used with multiple workflow arguments Agent-Logs-Url: https://github.com/github/gh-aw/sessions/b7ed7694-7a0d-49f2-b0d5-15df8bd4a149 Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> --- pkg/cli/add_command.go | 15 ++++--- pkg/cli/add_command_test.go | 81 ++++--------------------------------- 2 files changed, 14 insertions(+), 82 deletions(-) diff --git a/pkg/cli/add_command.go b/pkg/cli/add_command.go index 7cc32234695..56e4348652a 100644 --- a/pkg/cli/add_command.go +++ b/pkg/cli/add_command.go @@ -73,7 +73,7 @@ Workflow specifications: - Local wildcard: "./*.md" or "./dir/*.md" (adds all .md files matching pattern) - Version can be tag, branch, or SHA (for remote workflows) -The -n flag allows you to specify a custom name for the workflow file (only applies to the first workflow when adding multiple). +The -n flag allows you to specify a custom name for the workflow file (not allowed when adding multiple workflows at once). The --dir flag allows you to specify the workflow directory (default: .github/workflows). The --create-pull-request flag creates a pull request with the workflow changes. The --force flag overwrites existing workflow files. @@ -101,6 +101,11 @@ Note: For guided interactive setup, use the 'add-wizard' command instead.`, noStopAfter, _ := cmd.Flags().GetBool("no-stop-after") stopAfter, _ := cmd.Flags().GetString("stop-after") disableSecurityScanner, _ := cmd.Flags().GetBool("disable-security-scanner") + + if nameFlag != "" && len(workflows) > 1 { + return errors.New("--name flag cannot be used when adding multiple workflows at once") + } + if err := validateEngine(engineOverride); err != nil { return err } @@ -273,13 +278,7 @@ func addWorkflowsWithTracking(workflows []*ResolvedWorkflow, tracker *FileTracke fmt.Fprintln(os.Stderr, console.FormatProgressMessage(fmt.Sprintf("Adding workflow %d/%d: %s", i+1, len(workflows), resolved.Spec.WorkflowName))) } - // The --name flag only applies to the first workflow when adding multiple - workflowOpts := opts // copy so that clearing Name does not affect subsequent iterations - if i > 0 { - workflowOpts.Name = "" - } - - if err := addWorkflowWithTracking(resolved, tracker, workflowOpts); err != nil { + if err := addWorkflowWithTracking(resolved, tracker, opts); err != nil { return fmt.Errorf("failed to add workflow '%s': %w", resolved.Spec.String(), err) } } diff --git a/pkg/cli/add_command_test.go b/pkg/cli/add_command_test.go index 40e380505cd..2428107d986 100644 --- a/pkg/cli/add_command_test.go +++ b/pkg/cli/add_command_test.go @@ -4,9 +4,6 @@ package cli import ( "context" - "os" - "os/exec" - "path/filepath" "testing" "github.com/spf13/cobra" @@ -341,78 +338,14 @@ func TestAddCommandArgs(t *testing.T) { require.NoError(t, err, "Should not error with multiple arguments") } -// TestAddMultipleWorkflowsNameFlag verifies that when multiple workflows are added, -// the --name flag applies only to the first workflow and subsequent workflows use their original names. +// TestAddMultipleWorkflowsNameFlag verifies that --name is not allowed when multiple workflows are specified. func TestAddMultipleWorkflowsNameFlag(t *testing.T) { - // Create a temporary directory for testing - tmpDir, err := os.MkdirTemp("", "gh-aw-add-multiple-workflows-test") - require.NoError(t, err, "Failed to create temp dir") - defer os.RemoveAll(tmpDir) - - // Change to temp directory - originalDir, err := os.Getwd() - require.NoError(t, err, "Failed to get current directory") - defer func() { - _ = os.Chdir(originalDir) - }() - - require.NoError(t, os.Chdir(tmpDir), "Failed to change to temp directory") - - // Initialize a git repository - if err := exec.Command("git", "init").Run(); err != nil { - t.Skip("Skipping test - git not available") - } - _ = exec.Command("git", "config", "user.email", "test@example.com").Run() - _ = exec.Command("git", "config", "user.name", "Test User").Run() - - workflowsDir := filepath.Join(tmpDir, ".github", "workflows") - require.NoError(t, os.MkdirAll(workflowsDir, 0755), "Failed to create workflows directory") - - workflowContent := func(name string) []byte { - return []byte("---\non: push\npermissions:\n contents: read\nengine: copilot\n---\n\n# " + name + "\n") - } - - resolved1 := &ResolvedWorkflow{ - Spec: &WorkflowSpec{ - RepoSpec: RepoSpec{RepoSlug: "test/repo"}, - WorkflowPath: "./workflow-alpha.md", - WorkflowName: "workflow-alpha", - }, - Content: workflowContent("Alpha"), - SourceInfo: &FetchedWorkflow{ - Content: workflowContent("Alpha"), - IsLocal: true, - SourcePath: "./workflow-alpha.md", - }, - } - - resolved2 := &ResolvedWorkflow{ - Spec: &WorkflowSpec{ - RepoSpec: RepoSpec{RepoSlug: "test/repo"}, - WorkflowPath: "./workflow-beta.md", - WorkflowName: "workflow-beta", - }, - Content: workflowContent("Beta"), - SourceInfo: &FetchedWorkflow{ - Content: workflowContent("Beta"), - IsLocal: true, - SourcePath: "./workflow-beta.md", - }, - } - - opts := AddOptions{ - Name: "custom-name", - NoGitattributes: true, - } - - err = addWorkflows([]*ResolvedWorkflow{resolved1, resolved2}, opts) - require.NoError(t, err, "addWorkflows should succeed") + cmd := NewAddCommand(validateEngineStub) - // The first workflow should be saved as custom-name.md - _, err = os.Stat(filepath.Join(workflowsDir, "custom-name.md")) - require.NoError(t, err, "First workflow should be saved as custom-name.md") + // Simulate calling the command with --name and multiple workflow arguments + cmd.SetArgs([]string{"workflow1", "workflow2", "--name", "custom-name"}) - // The second workflow should use its original name (workflow-beta.md), not custom-name.md - _, err = os.Stat(filepath.Join(workflowsDir, "workflow-beta.md")) - require.NoError(t, err, "Second workflow should be saved as workflow-beta.md (original name)") + err := cmd.Execute() + require.Error(t, err, "Should error when --name is used with multiple workflows") + assert.Contains(t, err.Error(), "--name flag cannot be used when adding multiple workflows", "Error should mention --name restriction") }