Skip to content

Commit b068f99

Browse files
Copilotpelikhan
andcommitted
Refactor PR checkout with helper functions and embedded scripts
- Add renderConditionAsIf helper for rendering conditions in YAML - Move bash script to pkg/workflow/sh/checkout_pr.sh - Create sh.go to embed and format shell scripts - Add permissions check for contents:read access - Use helpers in both generatePRBranchCheckout and generatePRContextPromptStep - Improve code organization and reusability Co-authored-by: pelikhan <[email protected]>
1 parent b988c4e commit b068f99

4 files changed

Lines changed: 65 additions & 43 deletions

File tree

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

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

‎pkg/workflow/pr.go‎

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ 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+
517
// generatePRBranchCheckout generates a step to checkout the PR branch if the event is a comment on a PR
618
func (c *Compiler) generatePRBranchCheckout(yaml *strings.Builder, data *WorkflowData) {
719
// Check if any of the workflow's event triggers are comment-related events
@@ -11,39 +23,21 @@ func (c *Compiler) generatePRBranchCheckout(yaml *strings.Builder, data *Workflo
1123
return // No comment-related triggers, skip PR branch checkout
1224
}
1325

26+
// Check that permissions allow contents read access
27+
permParser := NewPermissionsParser(data.Permissions)
28+
if !permParser.HasContentsReadAccess() {
29+
return // No contents read access, cannot checkout
30+
}
31+
1432
// Add a conditional step that checks out the PR branch if applicable
1533
yaml.WriteString(" - name: Checkout PR branch if applicable\n")
16-
yaml.WriteString(" if: |\n")
1734

18-
// Use the helper function to generate the condition expression
35+
// Use the helper function to render the condition
1936
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-
}
37+
renderConditionAsIf(yaml, condition, " ")
3238

3339
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")
40+
WriteShellScriptToYAML(yaml, checkoutPRScript, " ")
4741
yaml.WriteString(" env:\n")
4842
yaml.WriteString(" GH_TOKEN: ${{ github.token }}\n")
4943
}
@@ -63,23 +57,17 @@ func (c *Compiler) generatePRContextPromptStep(yaml *strings.Builder, data *Work
6357
return // No checkout, so no PR branch checkout will happen
6458
}
6559

60+
// Check that permissions allow contents read access
61+
permParser := NewPermissionsParser(data.Permissions)
62+
if !permParser.HasContentsReadAccess() {
63+
return // No contents read access, cannot checkout
64+
}
65+
6666
yaml.WriteString(" - name: Append PR context instructions to prompt\n")
67-
yaml.WriteString(" if: |\n")
6867

69-
// Use the helper function to generate the condition expression
68+
// Use the helper function to render the condition
7069
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-
}
70+
renderConditionAsIf(yaml, condition, " ")
8371

8472
yaml.WriteString(" env:\n")
8573
yaml.WriteString(" GITHUB_AW_PROMPT: /tmp/aw-prompts/prompt.txt\n")

‎pkg/workflow/sh.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package workflow
2+
3+
import (
4+
_ "embed"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
//go:embed sh/checkout_pr.sh
10+
var checkoutPRScript string
11+
12+
// WriteShellScriptToYAML writes a shell script with proper indentation to a strings.Builder
13+
func WriteShellScriptToYAML(yaml *strings.Builder, script string, indent string) {
14+
scriptLines := strings.Split(script, "\n")
15+
for _, line := range scriptLines {
16+
// Skip empty lines at the beginning or end
17+
if strings.TrimSpace(line) != "" {
18+
fmt.Fprintf(yaml, "%s%s\n", indent, line)
19+
}
20+
}
21+
}

‎pkg/workflow/sh/checkout_pr.sh‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Determine PR number based on event type
5+
if [ "${{ github.event_name }}" = "issue_comment" ]; then
6+
PR_NUMBER="${{ github.event.issue.number }}"
7+
elif [ "${{ github.event_name }}" = "pull_request_review_comment" ]; then
8+
PR_NUMBER="${{ github.event.pull_request.number }}"
9+
elif [ "${{ github.event_name }}" = "pull_request_review" ]; then
10+
PR_NUMBER="${{ github.event.pull_request.number }}"
11+
fi
12+
13+
echo "Fetching PR #$PR_NUMBER..."
14+
gh pr checkout "$PR_NUMBER"

0 commit comments

Comments
 (0)