Skip to content

Commit 89d975b

Browse files
Copilotpelikhan
andcommitted
Move helper function and prompt text to proper locations
- Move renderConditionAsIf to expressions.go as RenderConditionAsIf - Extract PR context prompt to sh/pr_context_prompt.md - Embed prompt text as resource in sh.go - Add WritePromptTextToYAML helper for writing prompt text - Update pr.go to use new helpers and embedded resources - Improve code organization and maintainability Co-authored-by: pelikhan <[email protected]>
1 parent b068f99 commit 89d975b

5 files changed

Lines changed: 58 additions & 45 deletions

File tree

‎.github/workflows/tidy.lock.yml‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/workflow/expressions.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,18 @@ func BuildPRCommentCondition() ConditionNode {
437437
}
438438
}
439439

440+
// RenderConditionAsIf renders a ConditionNode as an 'if' condition with proper YAML indentation
441+
func RenderConditionAsIf(yaml *strings.Builder, condition ConditionNode, indent string) {
442+
yaml.WriteString(" if: |\n")
443+
conditionStr := condition.Render()
444+
445+
// Format the condition with proper indentation
446+
lines := strings.Split(conditionStr, "\n")
447+
for _, line := range lines {
448+
yaml.WriteString(indent + line + "\n")
449+
}
450+
}
451+
440452
// ExpressionParser handles parsing of expression strings into ConditionNode trees
441453
type ExpressionParser struct {
442454
tokens []token

‎pkg/workflow/pr.go‎

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@ package workflow
22

33
import "strings"
44

5-
// renderConditionAsIf renders a ConditionNode as an 'if' condition with proper YAML indentation
6-
func renderConditionAsIf(yaml *strings.Builder, condition ConditionNode, indent string) {
7-
yaml.WriteString(" if: |\n")
8-
conditionStr := condition.Render()
9-
10-
// Format the condition with proper indentation
11-
lines := strings.Split(conditionStr, "\n")
12-
for _, line := range lines {
13-
yaml.WriteString(indent + line + "\n")
14-
}
15-
}
16-
175
// generatePRBranchCheckout generates a step to checkout the PR branch if the event is a comment on a PR
186
func (c *Compiler) generatePRBranchCheckout(yaml *strings.Builder, data *WorkflowData) {
197
// Check if any of the workflow's event triggers are comment-related events
@@ -34,7 +22,7 @@ func (c *Compiler) generatePRBranchCheckout(yaml *strings.Builder, data *Workflo
3422

3523
// Use the helper function to render the condition
3624
condition := BuildPRCommentCondition()
37-
renderConditionAsIf(yaml, condition, " ")
25+
RenderConditionAsIf(yaml, condition, " ")
3826

3927
yaml.WriteString(" run: |\n")
4028
WriteShellScriptToYAML(yaml, checkoutPRScript, " ")
@@ -67,43 +55,13 @@ func (c *Compiler) generatePRContextPromptStep(yaml *strings.Builder, data *Work
6755

6856
// Use the helper function to render the condition
6957
condition := BuildPRCommentCondition()
70-
renderConditionAsIf(yaml, condition, " ")
58+
RenderConditionAsIf(yaml, condition, " ")
7159

7260
yaml.WriteString(" env:\n")
7361
yaml.WriteString(" GITHUB_AW_PROMPT: /tmp/aw-prompts/prompt.txt\n")
7462
yaml.WriteString(" run: |\n")
7563
yaml.WriteString(" cat >> $GITHUB_AW_PROMPT << 'EOF'\n")
76-
yaml.WriteString(" \n")
77-
yaml.WriteString(" ---\n")
78-
yaml.WriteString(" \n")
79-
yaml.WriteString(" ## Current Branch Context\n")
80-
yaml.WriteString(" \n")
81-
yaml.WriteString(" **IMPORTANT**: This workflow was triggered by a comment on a pull request. The repository has been automatically checked out to the PR's branch, not the default branch.\n")
82-
yaml.WriteString(" \n")
83-
yaml.WriteString(" ### What This Means\n")
84-
yaml.WriteString(" \n")
85-
yaml.WriteString(" - The current working directory contains the code from the pull request branch\n")
86-
yaml.WriteString(" - Any file operations you perform will be on the PR branch code\n")
87-
yaml.WriteString(" - You can inspect, analyze, and work with the PR changes directly\n")
88-
yaml.WriteString(" - The PR branch has been checked out using `gh pr checkout`\n")
89-
yaml.WriteString(" \n")
90-
yaml.WriteString(" ### Available Actions\n")
91-
yaml.WriteString(" \n")
92-
yaml.WriteString(" You can:\n")
93-
yaml.WriteString(" - Review the changes in the PR by examining files\n")
94-
yaml.WriteString(" - Run tests or linters on the PR code\n")
95-
yaml.WriteString(" - Make additional changes to the PR branch if needed\n")
96-
yaml.WriteString(" - Create commits on the PR branch (they will appear in the PR)\n")
97-
yaml.WriteString(" - Switch to other branches using `git checkout` if needed\n")
98-
yaml.WriteString(" \n")
99-
yaml.WriteString(" ### Current Branch Information\n")
100-
yaml.WriteString(" \n")
101-
yaml.WriteString(" To see which branch you're currently on, you can run:\n")
102-
yaml.WriteString(" ```bash\n")
103-
yaml.WriteString(" git branch --show-current\n")
104-
yaml.WriteString(" git log -1 --oneline\n")
105-
yaml.WriteString(" ```\n")
106-
yaml.WriteString(" \n")
64+
WritePromptTextToYAML(yaml, prContextPromptText, " ")
10765
yaml.WriteString(" EOF\n")
10866
}
10967

‎pkg/workflow/sh.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
//go:embed sh/checkout_pr.sh
1010
var checkoutPRScript string
1111

12+
//go:embed sh/pr_context_prompt.md
13+
var prContextPromptText string
14+
1215
// WriteShellScriptToYAML writes a shell script with proper indentation to a strings.Builder
1316
func WriteShellScriptToYAML(yaml *strings.Builder, script string, indent string) {
1417
scriptLines := strings.Split(script, "\n")
@@ -19,3 +22,11 @@ func WriteShellScriptToYAML(yaml *strings.Builder, script string, indent string)
1922
}
2023
}
2124
}
25+
26+
// WritePromptTextToYAML writes prompt text with proper indentation to a strings.Builder
27+
func WritePromptTextToYAML(yaml *strings.Builder, text string, indent string) {
28+
textLines := strings.Split(text, "\n")
29+
for _, line := range textLines {
30+
fmt.Fprintf(yaml, "%s%s\n", indent, line)
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
---
3+
4+
## Current Branch Context
5+
6+
**IMPORTANT**: This workflow was triggered by a comment on a pull request. The repository has been automatically checked out to the PR's branch, not the default branch.
7+
8+
### What This Means
9+
10+
- The current working directory contains the code from the pull request branch
11+
- Any file operations you perform will be on the PR branch code
12+
- You can inspect, analyze, and work with the PR changes directly
13+
- The PR branch has been checked out using `gh pr checkout`
14+
15+
### Available Actions
16+
17+
You can:
18+
- Review the changes in the PR by examining files
19+
- Run tests or linters on the PR code
20+
- Make additional changes to the PR branch if needed
21+
- Create commits on the PR branch (they will appear in the PR)
22+
- Switch to other branches using `git checkout` if needed
23+
24+
### Current Branch Information
25+
26+
To see which branch you're currently on, you can run:
27+
```bash
28+
git branch --show-current
29+
git log -1 --oneline
30+
```
31+

0 commit comments

Comments
 (0)