-
Notifications
You must be signed in to change notification settings - Fork 359
Fix built-in pre-steps insertion to avoid splitting Setup Scripts YAML blocks
#27750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
631c55b
d6fee1f
1496da4
5b17889
c1e4896
9eb46f4
ef61052
87bb11d
7877754
6f027a4
4533228
b360dcc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -873,6 +873,16 @@ func insertPreStepsAfterSetupBeforeCheckout(steps []string, preSteps []string) [ | |
| for i, step := range steps { | ||
| if firstCheckoutIdx == -1 && strings.Contains(step, "uses: actions/checkout@") { | ||
| firstCheckoutIdx = i | ||
| // Walk backward to the checkout step's list-item boundary ("- "). | ||
| // If no boundary is found, keep the current index so insertion still | ||
| // occurs before the checkout uses-line. | ||
| for j := i; j >= 0; j-- { | ||
| trimmed := strings.TrimLeft(steps[j], " ") | ||
| if strings.HasPrefix(trimmed, "- ") { | ||
| firstCheckoutIdx = j | ||
| break | ||
| } | ||
| } | ||
| } | ||
| if exactSetupStepIDPattern.MatchString(step) { | ||
| lastSetupIdx = i | ||
|
|
@@ -881,7 +891,22 @@ func insertPreStepsAfterSetupBeforeCheckout(steps []string, preSteps []string) [ | |
|
|
||
| insertIdx := len(steps) | ||
| if lastSetupIdx >= 0 { | ||
| insertIdx = lastSetupIdx + 1 | ||
| // Setup step may be emitted as multiple []string entries (one line per entry). | ||
| // Insert after the full setup step by finding the next step boundary. | ||
| // A step boundary is identified by the YAML list-item prefix ("- ") after | ||
| // indentation trimming, which marks the beginning of the next step block. | ||
| // If no boundary is found (e.g. setup is the final step), insertIdx stays len(steps) | ||
| // and pre-steps are appended by the slice insertion logic below. | ||
| for i := lastSetupIdx + 1; i < len(steps); i++ { | ||
| trimmed := strings.TrimLeft(steps[i], " ") | ||
| if strings.HasPrefix(trimmed, "- ") { | ||
| insertIdx = i | ||
| break | ||
| } | ||
| } | ||
| if insertIdx == len(steps) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The log message here is helpful for debugging. Consider also logging the |
||
| compilerJobsLog.Print("No step boundary found after setup step; appending pre-steps at end") | ||
| } | ||
| } else if firstCheckoutIdx >= 0 { | ||
| insertIdx = firstCheckoutIdx | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,7 @@ import ( | |||||||
|
|
||||||||
| "github.com/github/gh-aw/pkg/constants" | ||||||||
| "github.com/github/gh-aw/pkg/testutil" | ||||||||
| "github.com/goccy/go-yaml" | ||||||||
| ) | ||||||||
|
|
||||||||
| // ======================================== | ||||||||
|
|
@@ -859,6 +860,227 @@ jobs: | |||||||
| ) | ||||||||
| } | ||||||||
|
|
||||||||
| func TestPreStepsInsertAfterSetupBoundary(t *testing.T) { | ||||||||
| tmpDir := testutil.TempDir(t, "builtin-job-pre-steps-setup-boundary") | ||||||||
|
|
||||||||
| frontmatter := `--- | ||||||||
| on: push | ||||||||
| permissions: | ||||||||
| contents: read | ||||||||
| engine: copilot | ||||||||
| strict: false | ||||||||
| jobs: | ||||||||
| pre_activation: | ||||||||
| pre-steps: | ||||||||
| - name: Pre-activation uses pre-step | ||||||||
| uses: actions/setup-node@v4 | ||||||||
| with: | ||||||||
| node-version: "20" | ||||||||
| activation: | ||||||||
| pre-steps: | ||||||||
| - name: Activation run pre-step | ||||||||
| run: echo "activation prep" | ||||||||
| --- | ||||||||
|
|
||||||||
| # Test Workflow | ||||||||
| ` | ||||||||
|
|
||||||||
| testFile := filepath.Join(tmpDir, "test.md") | ||||||||
| if err := os.WriteFile(testFile, []byte(frontmatter), 0644); err != nil { | ||||||||
| t.Fatal(err) | ||||||||
| } | ||||||||
|
|
||||||||
| compiler := NewCompiler() | ||||||||
|
||||||||
| compiler := NewCompiler() | |
| compiler := NewCompiler() | |
| compiler.SetActionMode(ActionModeDev) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using
strings.TrimSpaceinstead ofstrings.TrimLeft(steps[i], " ")to handle tabs as well as spaces, since YAML indentation could theoretically use tabs (though in practice YAML prohibits tabs in indentation).