@@ -2,6 +2,18 @@ package workflow
22
33import "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
618func (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 " )
0 commit comments