|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package cli |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | + |
| 12 | + goyaml "github.com/goccy/go-yaml" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestCompileCreatePullRequestRuntimeRoutingWorkflow(t *testing.T) { |
| 18 | + setup := setupIntegrationTest(t) |
| 19 | + defer setup.cleanup() |
| 20 | + |
| 21 | + srcPath := filepath.Join(projectRoot, "pkg/cli/workflows/test-copilot-create-pull-request-runtime-routing.md") |
| 22 | + dstPath := filepath.Join(setup.workflowsDir, "test-copilot-create-pull-request-runtime-routing.md") |
| 23 | + copyWorkflowFile(t, srcPath, dstPath) |
| 24 | + |
| 25 | + cmd := exec.Command(setup.binaryPath, "compile", dstPath) |
| 26 | + output, err := cmd.CombinedOutput() |
| 27 | + require.NoError(t, err, "compile should succeed:\n%s", string(output)) |
| 28 | + |
| 29 | + lockFilePath := filepath.Join(setup.workflowsDir, "test-copilot-create-pull-request-runtime-routing.lock.yml") |
| 30 | + lockContent, err := os.ReadFile(lockFilePath) |
| 31 | + require.NoError(t, err, "failed to read compiled lock file") |
| 32 | + |
| 33 | + lockContentStr := string(lockContent) |
| 34 | + assert.Contains(t, lockContentStr, "GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG", "lock file should include safe outputs handler config") |
| 35 | + assert.Contains(t, lockContentStr, `GH_AW_INPUT_REVIEW_TEAM: ${{ inputs.review-team }}`, "workflow_dispatch input should be preserved for runtime templating") |
| 36 | + |
| 37 | + var compiledWorkflow map[string]any |
| 38 | + require.NoError(t, goyaml.Unmarshal(lockContent, &compiledWorkflow), "lock file should be valid YAML") |
| 39 | + |
| 40 | + handlerConfigJSON := extractHandlerConfigJSON(t, compiledWorkflow) |
| 41 | + |
| 42 | + var handlerConfig map[string]map[string]any |
| 43 | + require.NoError(t, json.Unmarshal([]byte(handlerConfigJSON), &handlerConfig), "handler config should be valid JSON") |
| 44 | + |
| 45 | + createPullRequestConfig, ok := handlerConfig["create_pull_request"] |
| 46 | + require.True(t, ok, "handler config should include create_pull_request") |
| 47 | + assert.Equal(t, "${{ github.actor }}", createPullRequestConfig["reviewers"], "reviewers expression should remain a runtime string in handler config") |
| 48 | + // Handler config normalizes frontmatter keys like team-reviewers to team_reviewers |
| 49 | + // to match JSON property naming conventions. |
| 50 | + assert.Equal(t, "${{ inputs.review-team }}", createPullRequestConfig["team_reviewers"], "team_reviewers expression should remain a runtime string in handler config") |
| 51 | + assert.Equal(t, "${{ github.actor }}", createPullRequestConfig["assignees"], "assignees expression should remain a runtime string in handler config") |
| 52 | +} |
| 53 | + |
| 54 | +func extractHandlerConfigJSON(t *testing.T, compiledWorkflow map[string]any) string { |
| 55 | + t.Helper() |
| 56 | + |
| 57 | + jobs, ok := compiledWorkflow["jobs"].(map[string]any) |
| 58 | + require.True(t, ok, "compiled workflow should include jobs map") |
| 59 | + |
| 60 | + safeOutputsJob, ok := jobs["safe_outputs"].(map[string]any) |
| 61 | + require.True(t, ok, "compiled workflow should include safe_outputs job") |
| 62 | + |
| 63 | + steps, ok := safeOutputsJob["steps"].([]any) |
| 64 | + require.True(t, ok, "safe_outputs job should include steps") |
| 65 | + |
| 66 | + for _, step := range steps { |
| 67 | + stepMap, ok := step.(map[string]any) |
| 68 | + if !ok { |
| 69 | + continue |
| 70 | + } |
| 71 | + env, ok := stepMap["env"].(map[string]any) |
| 72 | + if !ok { |
| 73 | + continue |
| 74 | + } |
| 75 | + handlerConfig, ok := env["GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG"].(string) |
| 76 | + if ok { |
| 77 | + return handlerConfig |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + t.Fatal("failed to locate GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG in safe_outputs job steps") |
| 82 | + return "" |
| 83 | +} |
0 commit comments