|
| 1 | +name: ClaudeBox |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + prompt: |
| 9 | + description: 'Prompt / instructions for Claude' |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + link: |
| 13 | + description: 'Context link (e.g., PR URL, issue URL, external reference)' |
| 14 | + required: false |
| 15 | + type: string |
| 16 | + |
| 17 | +jobs: |
| 18 | + claudebox: |
| 19 | + if: >- |
| 20 | + github.event_name == 'workflow_dispatch' || |
| 21 | + startsWith(github.event.comment.body, '/claudebox') |
| 22 | + runs-on: ubuntu-latest |
| 23 | + permissions: |
| 24 | + contents: read |
| 25 | + steps: |
| 26 | + - name: Check write access |
| 27 | + if: github.event_name == 'issue_comment' |
| 28 | + run: | |
| 29 | + ASSOCIATION="${{ github.event.comment.author_association }}" |
| 30 | + echo "Author association: $ASSOCIATION" |
| 31 | + if [[ "$ASSOCIATION" != "OWNER" && "$ASSOCIATION" != "MEMBER" && "$ASSOCIATION" != "COLLABORATOR" ]]; then |
| 32 | + echo "ERROR: User does not have write access (association: $ASSOCIATION)" |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | + echo "Access granted." |
| 36 | +
|
| 37 | + - name: Add reaction |
| 38 | + if: github.event_name == 'issue_comment' |
| 39 | + env: |
| 40 | + GH_TOKEN: ${{ secrets.AZTEC_BOT_GITHUB_TOKEN }} |
| 41 | + run: | |
| 42 | + gh api \ |
| 43 | + repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions \ |
| 44 | + -f content='eyes' || true |
| 45 | +
|
| 46 | + - name: Parse command |
| 47 | + id: parse |
| 48 | + env: |
| 49 | + COMMENT_BODY: ${{ github.event.comment.body || '' }} |
| 50 | + INPUT_PROMPT: ${{ inputs.prompt || '' }} |
| 51 | + INPUT_LINK: ${{ inputs.link || '' }} |
| 52 | + run: | |
| 53 | + if [ -n "$INPUT_PROMPT" ]; then |
| 54 | + PROMPT="$INPUT_PROMPT" |
| 55 | + LINK="$INPUT_LINK" |
| 56 | + else |
| 57 | + PROMPT=$(printf '%s' "$COMMENT_BODY" | sed 's|^/claudebox[[:space:]]*||') |
| 58 | + LINK="" |
| 59 | + fi |
| 60 | +
|
| 61 | + echo "link=$LINK" >> "$GITHUB_OUTPUT" |
| 62 | + { |
| 63 | + echo "prompt<<PROMPT_EOF" |
| 64 | + echo "$PROMPT" |
| 65 | + echo "PROMPT_EOF" |
| 66 | + } >> "$GITHUB_OUTPUT" |
| 67 | + echo "Parsed: prompt=${PROMPT:0:120}" |
| 68 | +
|
| 69 | + - name: Post status comment |
| 70 | + id: status_comment |
| 71 | + if: github.event_name == 'issue_comment' |
| 72 | + env: |
| 73 | + GH_TOKEN: ${{ secrets.AZTEC_BOT_GITHUB_TOKEN }} |
| 74 | + PROMPT_TEXT: ${{ steps.parse.outputs.prompt }} |
| 75 | + run: | |
| 76 | + ISSUE_NUM="${{ github.event.issue.number }}" |
| 77 | + RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 78 | + SHORT_PROMPT=$(printf '%.120s' "$PROMPT_TEXT") |
| 79 | + BODY="**ClaudeBox**: _${SHORT_PROMPT}_ ... [workflow run]($RUN_URL)" |
| 80 | +
|
| 81 | + COMMENT_ID=$(gh api \ |
| 82 | + repos/${{ github.repository }}/issues/$ISSUE_NUM/comments \ |
| 83 | + -f body="$BODY" \ |
| 84 | + --jq '.id') |
| 85 | + echo "run_comment_id=$COMMENT_ID" >> "$GITHUB_OUTPUT" |
| 86 | + echo "Posted status comment: $COMMENT_ID" |
| 87 | +
|
| 88 | + - name: Run ClaudeBox |
| 89 | + timeout-minutes: 5 |
| 90 | + env: |
| 91 | + CLAUDEBOX_URL: ${{ vars.CLAUDEBOX_URL }} |
| 92 | + CLAUDEBOX_API_SECRET: ${{ secrets.CLAUDEBOX_API_SECRET }} |
| 93 | + CLAUDEBOX_PROMPT: ${{ steps.parse.outputs.prompt }} |
| 94 | + CLAUDEBOX_LINK: ${{ steps.parse.outputs.link }} |
| 95 | + COMMENT_ID: ${{ github.event.comment.id || '' }} |
| 96 | + RUN_COMMENT_ID: ${{ steps.status_comment.outputs.run_comment_id || '' }} |
| 97 | + REPO: ${{ github.repository }} |
| 98 | + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 99 | + AUTHOR: ${{ github.event.comment.user.login || github.actor }} |
| 100 | + run: | |
| 101 | + PAYLOAD=$(jq -n \ |
| 102 | + --arg prompt "$CLAUDEBOX_PROMPT" \ |
| 103 | + --arg user "$AUTHOR" \ |
| 104 | + --arg comment_id "$COMMENT_ID" \ |
| 105 | + --arg run_comment_id "$RUN_COMMENT_ID" \ |
| 106 | + --arg repo "$REPO" \ |
| 107 | + --arg run_url "$RUN_URL" \ |
| 108 | + --arg link "$CLAUDEBOX_LINK" \ |
| 109 | + '{prompt: $prompt, user: $user, comment_id: $comment_id, run_comment_id: $run_comment_id, repo: $repo, run_url: $run_url, link: $link}') |
| 110 | +
|
| 111 | + # POST to ClaudeBox — returns 202 immediately with log URL |
| 112 | + RESPONSE=$(curl -sS -w "\n%{http_code}" \ |
| 113 | + -H "Authorization: Bearer ${CLAUDEBOX_API_SECRET}" \ |
| 114 | + -H "Content-Type: application/json" \ |
| 115 | + -d "$PAYLOAD" \ |
| 116 | + "${CLAUDEBOX_URL}/run") |
| 117 | +
|
| 118 | + HTTP_CODE=$(echo "$RESPONSE" | tail -1) |
| 119 | + BODY=$(echo "$RESPONSE" | head -n -1) |
| 120 | +
|
| 121 | + if [ "$HTTP_CODE" -ge 400 ] 2>/dev/null; then |
| 122 | + echo "ClaudeBox returned HTTP $HTTP_CODE: $BODY" |
| 123 | + exit 1 |
| 124 | + fi |
| 125 | +
|
| 126 | + LOG_URL=$(echo "$BODY" | jq -r '.log_url // empty') |
| 127 | + echo "ClaudeBox session started: $LOG_URL" |
0 commit comments