Skip to content

Commit 115cbc6

Browse files
Copilotpelikhan
andauthored
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>
1 parent e795469 commit 115cbc6

2 files changed

Lines changed: 14 additions & 82 deletions

File tree

pkg/cli/add_command.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Workflow specifications:
7373
- Local wildcard: "./*.md" or "./dir/*.md" (adds all .md files matching pattern)
7474
- Version can be tag, branch, or SHA (for remote workflows)
7575
76-
The -n flag allows you to specify a custom name for the workflow file (only applies to the first workflow when adding multiple).
76+
The -n flag allows you to specify a custom name for the workflow file (not allowed when adding multiple workflows at once).
7777
The --dir flag allows you to specify the workflow directory (default: .github/workflows).
7878
The --create-pull-request flag creates a pull request with the workflow changes.
7979
The --force flag overwrites existing workflow files.
@@ -101,6 +101,11 @@ Note: For guided interactive setup, use the 'add-wizard' command instead.`,
101101
noStopAfter, _ := cmd.Flags().GetBool("no-stop-after")
102102
stopAfter, _ := cmd.Flags().GetString("stop-after")
103103
disableSecurityScanner, _ := cmd.Flags().GetBool("disable-security-scanner")
104+
105+
if nameFlag != "" && len(workflows) > 1 {
106+
return errors.New("--name flag cannot be used when adding multiple workflows at once")
107+
}
108+
104109
if err := validateEngine(engineOverride); err != nil {
105110
return err
106111
}
@@ -273,13 +278,7 @@ func addWorkflowsWithTracking(workflows []*ResolvedWorkflow, tracker *FileTracke
273278
fmt.Fprintln(os.Stderr, console.FormatProgressMessage(fmt.Sprintf("Adding workflow %d/%d: %s", i+1, len(workflows), resolved.Spec.WorkflowName)))
274279
}
275280

276-
// The --name flag only applies to the first workflow when adding multiple
277-
workflowOpts := opts // copy so that clearing Name does not affect subsequent iterations
278-
if i > 0 {
279-
workflowOpts.Name = ""
280-
}
281-
282-
if err := addWorkflowWithTracking(resolved, tracker, workflowOpts); err != nil {
281+
if err := addWorkflowWithTracking(resolved, tracker, opts); err != nil {
283282
return fmt.Errorf("failed to add workflow '%s': %w", resolved.Spec.String(), err)
284283
}
285284
}

pkg/cli/add_command_test.go

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ package cli
44

55
import (
66
"context"
7-
"os"
8-
"os/exec"
9-
"path/filepath"
107
"testing"
118

129
"github.com/spf13/cobra"
@@ -341,78 +338,14 @@ func TestAddCommandArgs(t *testing.T) {
341338
require.NoError(t, err, "Should not error with multiple arguments")
342339
}
343340

344-
// TestAddMultipleWorkflowsNameFlag verifies that when multiple workflows are added,
345-
// the --name flag applies only to the first workflow and subsequent workflows use their original names.
341+
// TestAddMultipleWorkflowsNameFlag verifies that --name is not allowed when multiple workflows are specified.
346342
func TestAddMultipleWorkflowsNameFlag(t *testing.T) {
347-
// Create a temporary directory for testing
348-
tmpDir, err := os.MkdirTemp("", "gh-aw-add-multiple-workflows-test")
349-
require.NoError(t, err, "Failed to create temp dir")
350-
defer os.RemoveAll(tmpDir)
351-
352-
// Change to temp directory
353-
originalDir, err := os.Getwd()
354-
require.NoError(t, err, "Failed to get current directory")
355-
defer func() {
356-
_ = os.Chdir(originalDir)
357-
}()
358-
359-
require.NoError(t, os.Chdir(tmpDir), "Failed to change to temp directory")
360-
361-
// Initialize a git repository
362-
if err := exec.Command("git", "init").Run(); err != nil {
363-
t.Skip("Skipping test - git not available")
364-
}
365-
_ = exec.Command("git", "config", "user.email", "test@example.com").Run()
366-
_ = exec.Command("git", "config", "user.name", "Test User").Run()
367-
368-
workflowsDir := filepath.Join(tmpDir, ".github", "workflows")
369-
require.NoError(t, os.MkdirAll(workflowsDir, 0755), "Failed to create workflows directory")
370-
371-
workflowContent := func(name string) []byte {
372-
return []byte("---\non: push\npermissions:\n contents: read\nengine: copilot\n---\n\n# " + name + "\n")
373-
}
374-
375-
resolved1 := &ResolvedWorkflow{
376-
Spec: &WorkflowSpec{
377-
RepoSpec: RepoSpec{RepoSlug: "test/repo"},
378-
WorkflowPath: "./workflow-alpha.md",
379-
WorkflowName: "workflow-alpha",
380-
},
381-
Content: workflowContent("Alpha"),
382-
SourceInfo: &FetchedWorkflow{
383-
Content: workflowContent("Alpha"),
384-
IsLocal: true,
385-
SourcePath: "./workflow-alpha.md",
386-
},
387-
}
388-
389-
resolved2 := &ResolvedWorkflow{
390-
Spec: &WorkflowSpec{
391-
RepoSpec: RepoSpec{RepoSlug: "test/repo"},
392-
WorkflowPath: "./workflow-beta.md",
393-
WorkflowName: "workflow-beta",
394-
},
395-
Content: workflowContent("Beta"),
396-
SourceInfo: &FetchedWorkflow{
397-
Content: workflowContent("Beta"),
398-
IsLocal: true,
399-
SourcePath: "./workflow-beta.md",
400-
},
401-
}
402-
403-
opts := AddOptions{
404-
Name: "custom-name",
405-
NoGitattributes: true,
406-
}
407-
408-
err = addWorkflows([]*ResolvedWorkflow{resolved1, resolved2}, opts)
409-
require.NoError(t, err, "addWorkflows should succeed")
343+
cmd := NewAddCommand(validateEngineStub)
410344

411-
// The first workflow should be saved as custom-name.md
412-
_, err = os.Stat(filepath.Join(workflowsDir, "custom-name.md"))
413-
require.NoError(t, err, "First workflow should be saved as custom-name.md")
345+
// Simulate calling the command with --name and multiple workflow arguments
346+
cmd.SetArgs([]string{"workflow1", "workflow2", "--name", "custom-name"})
414347

415-
// The second workflow should use its original name (workflow-beta.md), not custom-name.md
416-
_, err = os.Stat(filepath.Join(workflowsDir, "workflow-beta.md"))
417-
require.NoError(t, err, "Second workflow should be saved as workflow-beta.md (original name)")
348+
err := cmd.Execute()
349+
require.Error(t, err, "Should error when --name is used with multiple workflows")
350+
assert.Contains(t, err.Error(), "--name flag cannot be used when adding multiple workflows", "Error should mention --name restriction")
418351
}

0 commit comments

Comments
 (0)