-
Notifications
You must be signed in to change notification settings - Fork 7
236 lines (206 loc) · 8.27 KB
/
Copy pathjules-session-manager.yml
File metadata and controls
236 lines (206 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# .github/workflows/jules-session-manager.yml
name: Jules Session Manager
on:
issue_comment:
types: [created]
permissions:
contents: read
pull-requests: write
issues: write
jobs:
jules-command:
if: |
github.event.issue.pull_request &&
contains(fromJson('["OWNER", "MEMBER", "COLLABORATOR"]'), github.event.comment.author_association) &&
(startsWith(github.event.comment.body, '@jules-delete') || startsWith(github.event.comment.body, '@jules-new'))
runs-on: self-hosted
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.issue.pull_request && format('refs/pull/{0}/head', github.event.issue.number) || github.sha }}
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v7
with:
python-version: '3.10'
- name: 'Parse Command'
id: parse_command
run: |
COMMENT_BODY="${{ github.event.comment.body }}"
if [[ "$COMMENT_BODY" == "@jules-delete"* ]]; then
echo "COMMAND=delete" >> $GITHUB_ENV
elif [[ "$COMMENT_BODY" == "@jules-new"* ]]; then
echo "COMMAND=new" >> $GITHUB_ENV
fi
- name: 'Get PR Context'
id: pr_context
if: env.COMMAND == 'new'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
run: |
# Fetch basic PR data
PR_DATA=$(gh pr view "$PR_NUMBER" --json headRefName,baseRefName,body,title,comments,reviews)
BRANCH=$(echo "$PR_DATA" | jq -r .headRefName)
BASE_REF=$(echo "$PR_DATA" | jq -r .baseRefName)
BODY=$(echo "$PR_DATA" | jq -r .body)
TITLE=$(echo "$PR_DATA" | jq -r .title)
# Fetch and format comments
COMMENTS=$(echo "$PR_DATA" | jq -r '.comments | .[] | "- \(.body)"' | sed 's/^/ /')
REVIEWS=$(echo "$PR_DATA" | jq -r '.reviews | .[] | select(.body != "") | "- \(.body)"' | sed 's/^/ /')
# Fetch git logs
# We assume the PR is up-to-date with the base branch
# This gets the logs from the point the branch was created
MERGE_BASE=$(git merge-base "origin/$BASE_REF" "HEAD")
LOGS=$(git log -n 50 --pretty=format:"- %s" "$MERGE_BASE..HEAD")
# Construct the full prompt
FULL_PROMPT=$(cat <<EOM
**PR Title:** $TITLE
**PR Body:**
$BODY
**Review Comments:**
$REVIEWS
**Other Comments:**
$COMMENTS
**Git Logs:**
$LOGS
EOM
)
echo "BRANCH=$BRANCH" >> $GITHUB_ENV
echo "PROMPT<<EOF" >> $GITHUB_ENV
echo "$FULL_PROMPT" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "TITLE=$TITLE" >> $GITHUB_ENV
- name: 'Find Jules Session ID'
id: find_session_id
if: env.COMMAND == 'delete'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
run: |
SESSION_ID=$(gh pr view "$PR_NUMBER" --json comments --jq '.comments | .[] | .body' | grep -oP '(?<=<!-- JULES_SESSION_ID=)\S+(?= *-->)' | tail -n 1)
if [ -z "$SESSION_ID" ]; then
echo "::error::Could not find Jules session ID in PR comments. Was a session previously created using '@jules-new' and completed successfully?"
exit 1
fi
echo "JULES_SESSION_ID=$SESSION_ID" >> $GITHUB_ENV
- name: 'Validate Secrets and Install Dependencies'
env:
JULES_API_URL: ${{ secrets.JULES_API_URL }}
JULES_API_KEY: ${{ secrets.JULES_API_KEY }}
run: |
set +e
missing_vars=""
if [ -z "$JULES_API_URL" ]; then
missing_vars="JULES_API_URL"
fi
if [ -z "$JULES_API_KEY" ]; then
if [ -n "$missing_vars" ]; then
missing_vars="$missing_vars, JULES_API_KEY"
else
missing_vars="JULES_API_KEY"
fi
fi
if [ -n "$missing_vars" ]; then
echo "::error::The following required secrets are not set: $missing_vars. Please configure them in your repository secrets."
exit 1
fi
echo "✅ All required Jules secrets are configured"
pip install -r .github/scripts/requirements.txt
- name: 'Run Jules Operation: New'
if: env.COMMAND == 'new'
id: jules_op_new
env:
JULES_API_KEY: ${{ secrets.JULES_API_KEY }}
BRANCH: ${{ env.BRANCH }}
TITLE: ${{ env.TITLE }}
PROMPT: ${{ env.PROMPT }}
REPO_OWNER: ${{ github.repository_owner }}
REPO_NAME: ${{ github.event.repository.name }}
run: |
set +e
OUTPUT=$(python3 .github/scripts/jules_ops.py \
--command "new" \
--prompt "$PROMPT" \
--branch "$BRANCH" \
--title "$TITLE" \
--owner "$REPO_OWNER" \
--repo-name "$REPO_NAME" \
--jules-api-url "${{ secrets.JULES_API_URL }}" 2>&1)
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo "error_message=$OUTPUT" >> $GITHUB_OUTPUT
exit $EXIT_CODE
fi
echo "session_id=$OUTPUT" >> $GITHUB_OUTPUT
- name: 'Run Jules Operation: Delete'
if: env.COMMAND == 'delete'
id: jules_op_delete
env:
JULES_API_KEY: ${{ secrets.JULES_API_KEY }}
JULES_SESSION_ID: ${{ env.JULES_SESSION_ID }}
run: |
set +e
OUTPUT=$(python3 .github/scripts/jules_ops.py \
--command "delete" \
--session-id "$JULES_SESSION_ID" \
--jules-api-url "${{ secrets.JULES_API_URL }}" 2>&1)
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo "error_message=$OUTPUT" >> $GITHUB_OUTPUT
exit $EXIT_CODE
fi
- name: 'Post Session ID Comment'
if: success() && env.COMMAND == 'new'
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const sessionId = "${{ steps.jules_op_new.outputs.session_id }}";
if (sessionId) {
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ github.event.issue.number }},
body: `<!-- JULES_SESSION_ID=${sessionId} -->`
});
}
- name: 'Add Reaction to Comment'
if: success()
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ github.event.comment.id }},
content: '+1'
})
- name: 'Handle Failure'
if: failure()
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const errorMessageNew = `${{ steps.jules_op_new.outputs.error_message || '' }}`;
const errorMessageDelete = `${{ steps.jules_op_delete.outputs.error_message || '' }}`;
const errorMessage = errorMessageNew || errorMessageDelete || 'Unknown network or resolution error';
const body = `### ❌ Jules Operation Failed\n\n**Error Details:**\n\`\`\`\n${errorMessage}\n\`\`\`\n\nPlease check the [Action Logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.`;
try {
github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ github.event.comment.id }},
content: '-1'
}).catch(() => {}); // Ignore if reaction fails
} catch (e) {
console.log('Could not add reaction:', e);
}
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ github.event.issue.number }},
body: body
})