Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/patch-add-multiple-workflows.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion pkg/cli/add_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
disableSecurityScanner, _ := cmd.Flags().GetBool("disable-security-scanner")
if err := validateEngine(engineOverride); err != nil {
return err
}

Check failure on line 106 in pkg/cli/add_command.go

View workflow job for this annotation

GitHub Actions / copilot

error-format: fmt.Errorf can be replaced with errors.New (perfsprint)

opts := AddOptions{
Verbose: verbose,
Expand Down Expand Up @@ -273,7 +273,13 @@
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 // copy so that clearing Name does not affect subsequent iterations
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)
}
}
Expand Down
79 changes: 79 additions & 0 deletions pkg/cli/add_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package cli

import (
"context"
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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)")
}
Loading