Skip to content

Commit b988c4e

Browse files
Copilotpelikhan
andcommitted
Refactor PR checkout logic using helper functions
- Add BuildPRCommentCondition helper in expressions.go - Move PR-related functions to new pr.go file - Use helper function to generate conditional expressions - Simplify code organization and improve maintainability Co-authored-by: pelikhan <[email protected]>
1 parent 0f19dc9 commit b988c4e

4 files changed

Lines changed: 171 additions & 134 deletions

File tree

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

Lines changed: 2 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/workflow/compiler.go‎

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,69 +2083,6 @@ func (c *Compiler) generateSafeOutputsPromptStep(yaml *strings.Builder, safeOutp
20832083
yaml.WriteString(" EOF\n")
20842084
}
20852085

2086-
// generatePRContextPromptStep generates a separate step for PR context instructions
2087-
func (c *Compiler) generatePRContextPromptStep(yaml *strings.Builder, data *WorkflowData) {
2088-
// Check if any of the workflow's event triggers are comment-related events
2089-
hasCommentTriggers := c.hasCommentRelatedTriggers(data)
2090-
2091-
if !hasCommentTriggers {
2092-
return // No comment-related triggers, skip PR context instructions
2093-
}
2094-
2095-
// Also check if checkout step will be added - only show prompt if checkout happens
2096-
needsCheckout := c.shouldAddCheckoutStep(data)
2097-
if !needsCheckout {
2098-
return // No checkout, so no PR branch checkout will happen
2099-
}
2100-
2101-
yaml.WriteString(" - name: Append PR context instructions to prompt\n")
2102-
yaml.WriteString(" if: |\n")
2103-
yaml.WriteString(" (\n")
2104-
yaml.WriteString(" github.event_name == 'issue_comment' &&\n")
2105-
yaml.WriteString(" github.event.issue.pull_request != null\n")
2106-
yaml.WriteString(" ) || (\n")
2107-
yaml.WriteString(" github.event_name == 'pull_request_review_comment'\n")
2108-
yaml.WriteString(" ) || (\n")
2109-
yaml.WriteString(" github.event_name == 'pull_request_review'\n")
2110-
yaml.WriteString(" )\n")
2111-
yaml.WriteString(" env:\n")
2112-
yaml.WriteString(" GITHUB_AW_PROMPT: /tmp/aw-prompts/prompt.txt\n")
2113-
yaml.WriteString(" run: |\n")
2114-
yaml.WriteString(" cat >> $GITHUB_AW_PROMPT << 'EOF'\n")
2115-
yaml.WriteString(" \n")
2116-
yaml.WriteString(" ---\n")
2117-
yaml.WriteString(" \n")
2118-
yaml.WriteString(" ## Current Branch Context\n")
2119-
yaml.WriteString(" \n")
2120-
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")
2121-
yaml.WriteString(" \n")
2122-
yaml.WriteString(" ### What This Means\n")
2123-
yaml.WriteString(" \n")
2124-
yaml.WriteString(" - The current working directory contains the code from the pull request branch\n")
2125-
yaml.WriteString(" - Any file operations you perform will be on the PR branch code\n")
2126-
yaml.WriteString(" - You can inspect, analyze, and work with the PR changes directly\n")
2127-
yaml.WriteString(" - The PR branch has been checked out using `gh pr checkout`\n")
2128-
yaml.WriteString(" \n")
2129-
yaml.WriteString(" ### Available Actions\n")
2130-
yaml.WriteString(" \n")
2131-
yaml.WriteString(" You can:\n")
2132-
yaml.WriteString(" - Review the changes in the PR by examining files\n")
2133-
yaml.WriteString(" - Run tests or linters on the PR code\n")
2134-
yaml.WriteString(" - Make additional changes to the PR branch if needed\n")
2135-
yaml.WriteString(" - Create commits on the PR branch (they will appear in the PR)\n")
2136-
yaml.WriteString(" - Switch to other branches using `git checkout` if needed\n")
2137-
yaml.WriteString(" \n")
2138-
yaml.WriteString(" ### Current Branch Information\n")
2139-
yaml.WriteString(" \n")
2140-
yaml.WriteString(" To see which branch you're currently on, you can run:\n")
2141-
yaml.WriteString(" ```bash\n")
2142-
yaml.WriteString(" git branch --show-current\n")
2143-
yaml.WriteString(" git log -1 --oneline\n")
2144-
yaml.WriteString(" ```\n")
2145-
yaml.WriteString(" \n")
2146-
yaml.WriteString(" EOF\n")
2147-
}
2148-
21492086
// generatePostSteps generates the post-steps section that runs after AI execution
21502087
func (c *Compiler) generatePostSteps(yaml *strings.Builder, data *WorkflowData) {
21512088
if data.PostSteps != "" {
@@ -2250,61 +2187,6 @@ func (c *Compiler) shouldAddCheckoutStep(data *WorkflowData) bool {
22502187
return true // Add checkout because it's needed and not already present
22512188
}
22522189

2253-
// generatePRBranchCheckout generates a step to checkout the PR branch if the event is a comment on a PR
2254-
func (c *Compiler) generatePRBranchCheckout(yaml *strings.Builder, data *WorkflowData) {
2255-
// Check if any of the workflow's event triggers are comment-related events
2256-
hasCommentTriggers := c.hasCommentRelatedTriggers(data)
2257-
2258-
if !hasCommentTriggers {
2259-
return // No comment-related triggers, skip PR branch checkout
2260-
}
2261-
2262-
// Add a conditional step that checks out the PR branch if applicable
2263-
yaml.WriteString(" - name: Checkout PR branch if applicable\n")
2264-
yaml.WriteString(" if: |\n")
2265-
yaml.WriteString(" (\n")
2266-
yaml.WriteString(" github.event_name == 'issue_comment' &&\n")
2267-
yaml.WriteString(" github.event.issue.pull_request != null\n")
2268-
yaml.WriteString(" ) || (\n")
2269-
yaml.WriteString(" github.event_name == 'pull_request_review_comment'\n")
2270-
yaml.WriteString(" ) || (\n")
2271-
yaml.WriteString(" github.event_name == 'pull_request_review'\n")
2272-
yaml.WriteString(" )\n")
2273-
yaml.WriteString(" run: |\n")
2274-
yaml.WriteString(" set -e\n")
2275-
yaml.WriteString(" \n")
2276-
yaml.WriteString(" # Determine PR number based on event type\n")
2277-
yaml.WriteString(" if [ \"${{ github.event_name }}\" = \"issue_comment\" ]; then\n")
2278-
yaml.WriteString(" PR_NUMBER=\"${{ github.event.issue.number }}\"\n")
2279-
yaml.WriteString(" elif [ \"${{ github.event_name }}\" = \"pull_request_review_comment\" ]; then\n")
2280-
yaml.WriteString(" PR_NUMBER=\"${{ github.event.pull_request.number }}\"\n")
2281-
yaml.WriteString(" elif [ \"${{ github.event_name }}\" = \"pull_request_review\" ]; then\n")
2282-
yaml.WriteString(" PR_NUMBER=\"${{ github.event.pull_request.number }}\"\n")
2283-
yaml.WriteString(" fi\n")
2284-
yaml.WriteString(" \n")
2285-
yaml.WriteString(" echo \"Fetching PR #$PR_NUMBER...\"\n")
2286-
yaml.WriteString(" gh pr checkout \"$PR_NUMBER\"\n")
2287-
yaml.WriteString(" env:\n")
2288-
yaml.WriteString(" GH_TOKEN: ${{ github.token }}\n")
2289-
}
2290-
2291-
// hasCommentRelatedTriggers checks if the workflow has any comment-related event triggers
2292-
func (c *Compiler) hasCommentRelatedTriggers(data *WorkflowData) bool {
2293-
if data.On == "" {
2294-
return false
2295-
}
2296-
2297-
// Check for comment-related event types in the "on" configuration
2298-
commentEvents := []string{"issue_comment", "pull_request_review_comment", "pull_request_review"}
2299-
for _, event := range commentEvents {
2300-
if strings.Contains(data.On, event) {
2301-
return true
2302-
}
2303-
}
2304-
2305-
return false
2306-
}
2307-
23082190
// convertStepToYAML converts a step map to YAML string with proper indentation
23092191
func (c *Compiler) convertStepToYAML(stepMap map[string]any) (string, error) {
23102192
return ConvertStepToYAML(stepMap)

‎pkg/workflow/expressions.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,38 @@ func BuildMultilineDisjunction(terms ...ConditionNode) *DisjunctionNode {
405405
}
406406
}
407407

408+
// BuildPRCommentCondition creates a condition to check if the event is a comment on a pull request
409+
// This checks for:
410+
// - issue_comment on a PR (github.event.issue.pull_request != null)
411+
// - pull_request_review_comment
412+
// - pull_request_review
413+
func BuildPRCommentCondition() ConditionNode {
414+
// issue_comment event on a PR
415+
issueCommentOnPR := buildAnd(
416+
BuildEventTypeEquals("issue_comment"),
417+
BuildComparison(
418+
BuildPropertyAccess("github.event.issue.pull_request"),
419+
"!=",
420+
&ExpressionNode{Expression: "null"},
421+
),
422+
)
423+
424+
// pull_request_review_comment event
425+
prReviewComment := BuildEventTypeEquals("pull_request_review_comment")
426+
427+
// pull_request_review event
428+
prReview := BuildEventTypeEquals("pull_request_review")
429+
430+
// Combine all conditions with OR
431+
return &DisjunctionNode{
432+
Terms: []ConditionNode{
433+
issueCommentOnPR,
434+
prReviewComment,
435+
prReview,
436+
},
437+
}
438+
}
439+
408440
// ExpressionParser handles parsing of expression strings into ConditionNode trees
409441
type ExpressionParser struct {
410442
tokens []token

‎pkg/workflow/pr.go‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package workflow
2+
3+
import "strings"
4+
5+
// generatePRBranchCheckout generates a step to checkout the PR branch if the event is a comment on a PR
6+
func (c *Compiler) generatePRBranchCheckout(yaml *strings.Builder, data *WorkflowData) {
7+
// Check if any of the workflow's event triggers are comment-related events
8+
hasCommentTriggers := c.hasCommentRelatedTriggers(data)
9+
10+
if !hasCommentTriggers {
11+
return // No comment-related triggers, skip PR branch checkout
12+
}
13+
14+
// Add a conditional step that checks out the PR branch if applicable
15+
yaml.WriteString(" - name: Checkout PR branch if applicable\n")
16+
yaml.WriteString(" if: |\n")
17+
18+
// Use the helper function to generate the condition expression
19+
condition := BuildPRCommentCondition()
20+
conditionStr := condition.Render()
21+
22+
// Format the condition with proper indentation
23+
// The condition should be indented with " " (10 spaces)
24+
lines := strings.Split(conditionStr, "\n")
25+
for i, line := range lines {
26+
if i == 0 {
27+
yaml.WriteString(" " + line + "\n")
28+
} else {
29+
yaml.WriteString(" " + line + "\n")
30+
}
31+
}
32+
33+
yaml.WriteString(" run: |\n")
34+
yaml.WriteString(" set -e\n")
35+
yaml.WriteString(" \n")
36+
yaml.WriteString(" # Determine PR number based on event type\n")
37+
yaml.WriteString(" if [ \"${{ github.event_name }}\" = \"issue_comment\" ]; then\n")
38+
yaml.WriteString(" PR_NUMBER=\"${{ github.event.issue.number }}\"\n")
39+
yaml.WriteString(" elif [ \"${{ github.event_name }}\" = \"pull_request_review_comment\" ]; then\n")
40+
yaml.WriteString(" PR_NUMBER=\"${{ github.event.pull_request.number }}\"\n")
41+
yaml.WriteString(" elif [ \"${{ github.event_name }}\" = \"pull_request_review\" ]; then\n")
42+
yaml.WriteString(" PR_NUMBER=\"${{ github.event.pull_request.number }}\"\n")
43+
yaml.WriteString(" fi\n")
44+
yaml.WriteString(" \n")
45+
yaml.WriteString(" echo \"Fetching PR #$PR_NUMBER...\"\n")
46+
yaml.WriteString(" gh pr checkout \"$PR_NUMBER\"\n")
47+
yaml.WriteString(" env:\n")
48+
yaml.WriteString(" GH_TOKEN: ${{ github.token }}\n")
49+
}
50+
51+
// generatePRContextPromptStep generates a separate step for PR context instructions
52+
func (c *Compiler) generatePRContextPromptStep(yaml *strings.Builder, data *WorkflowData) {
53+
// Check if any of the workflow's event triggers are comment-related events
54+
hasCommentTriggers := c.hasCommentRelatedTriggers(data)
55+
56+
if !hasCommentTriggers {
57+
return // No comment-related triggers, skip PR context instructions
58+
}
59+
60+
// Also check if checkout step will be added - only show prompt if checkout happens
61+
needsCheckout := c.shouldAddCheckoutStep(data)
62+
if !needsCheckout {
63+
return // No checkout, so no PR branch checkout will happen
64+
}
65+
66+
yaml.WriteString(" - name: Append PR context instructions to prompt\n")
67+
yaml.WriteString(" if: |\n")
68+
69+
// Use the helper function to generate the condition expression
70+
condition := BuildPRCommentCondition()
71+
conditionStr := condition.Render()
72+
73+
// Format the condition with proper indentation
74+
// The condition should be indented with " " (10 spaces)
75+
lines := strings.Split(conditionStr, "\n")
76+
for i, line := range lines {
77+
if i == 0 {
78+
yaml.WriteString(" " + line + "\n")
79+
} else {
80+
yaml.WriteString(" " + line + "\n")
81+
}
82+
}
83+
84+
yaml.WriteString(" env:\n")
85+
yaml.WriteString(" GITHUB_AW_PROMPT: /tmp/aw-prompts/prompt.txt\n")
86+
yaml.WriteString(" run: |\n")
87+
yaml.WriteString(" cat >> $GITHUB_AW_PROMPT << 'EOF'\n")
88+
yaml.WriteString(" \n")
89+
yaml.WriteString(" ---\n")
90+
yaml.WriteString(" \n")
91+
yaml.WriteString(" ## Current Branch Context\n")
92+
yaml.WriteString(" \n")
93+
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")
94+
yaml.WriteString(" \n")
95+
yaml.WriteString(" ### What This Means\n")
96+
yaml.WriteString(" \n")
97+
yaml.WriteString(" - The current working directory contains the code from the pull request branch\n")
98+
yaml.WriteString(" - Any file operations you perform will be on the PR branch code\n")
99+
yaml.WriteString(" - You can inspect, analyze, and work with the PR changes directly\n")
100+
yaml.WriteString(" - The PR branch has been checked out using `gh pr checkout`\n")
101+
yaml.WriteString(" \n")
102+
yaml.WriteString(" ### Available Actions\n")
103+
yaml.WriteString(" \n")
104+
yaml.WriteString(" You can:\n")
105+
yaml.WriteString(" - Review the changes in the PR by examining files\n")
106+
yaml.WriteString(" - Run tests or linters on the PR code\n")
107+
yaml.WriteString(" - Make additional changes to the PR branch if needed\n")
108+
yaml.WriteString(" - Create commits on the PR branch (they will appear in the PR)\n")
109+
yaml.WriteString(" - Switch to other branches using `git checkout` if needed\n")
110+
yaml.WriteString(" \n")
111+
yaml.WriteString(" ### Current Branch Information\n")
112+
yaml.WriteString(" \n")
113+
yaml.WriteString(" To see which branch you're currently on, you can run:\n")
114+
yaml.WriteString(" ```bash\n")
115+
yaml.WriteString(" git branch --show-current\n")
116+
yaml.WriteString(" git log -1 --oneline\n")
117+
yaml.WriteString(" ```\n")
118+
yaml.WriteString(" \n")
119+
yaml.WriteString(" EOF\n")
120+
}
121+
122+
// hasCommentRelatedTriggers checks if the workflow has any comment-related event triggers
123+
func (c *Compiler) hasCommentRelatedTriggers(data *WorkflowData) bool {
124+
if data.On == "" {
125+
return false
126+
}
127+
128+
// Check for comment-related event types in the "on" configuration
129+
commentEvents := []string{"issue_comment", "pull_request_review_comment", "pull_request_review"}
130+
for _, event := range commentEvents {
131+
if strings.Contains(data.On, event) {
132+
return true
133+
}
134+
}
135+
136+
return false
137+
}

0 commit comments

Comments
 (0)