SDK Code Generation Agent Trigger #6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: SDK Code Generation Agent Trigger | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - "openapi.json" | |
| workflow_dispatch: {} | |
| permissions: {} | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.ref }} | |
| cancel-in-progress: true | |
| jobs: | |
| launch-sdk-agent: | |
| name: Launch SDK code generation agent | |
| permissions: | |
| contents: read | |
| runs-on: blacksmith-2vcpu-ubuntu-2404 | |
| environment: codegen | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 | |
| with: | |
| fetch-depth: 2 | |
| persist-credentials: false | |
| - name: Check for openapi.json changes | |
| id: check_changes | |
| env: | |
| GH_REF_NAME: ${{ github.ref_name }} | |
| run: | | |
| set -e | |
| if [ "$GH_REF_NAME" = "main" ]; then | |
| # On main (push event): compare with previous commit | |
| BASELINE="HEAD~1" | |
| else | |
| # On a branch (workflow_dispatch): compare with main | |
| git fetch origin main --depth=1 --quiet | |
| BASELINE="origin/main" | |
| fi | |
| if git show "${BASELINE}:openapi.json" > /tmp/previous_openapi.json 2>/dev/null; then | |
| if diff -q openapi.json /tmp/previous_openapi.json > /dev/null 2>&1; then | |
| echo "No changes detected in openapi.json compared to ${BASELINE}" | |
| echo "has_changes=false" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "Changes detected in openapi.json compared to ${BASELINE}" | |
| echo "has_changes=true" >> "$GITHUB_OUTPUT" | |
| echo "previous_spec=/tmp/previous_openapi.json" >> "$GITHUB_OUTPUT" | |
| fi | |
| else | |
| echo "No openapi.json found at ${BASELINE} (new file)" | |
| echo "has_changes=true" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Install uv | |
| if: steps.check_changes.outputs.has_changes == 'true' | |
| uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0 | |
| with: | |
| enable-cache: true | |
| python-version: "3.12" | |
| - name: Generate comparison summary | |
| if: steps.check_changes.outputs.has_changes == 'true' | |
| id: generate_summary | |
| env: | |
| PREVIOUS_SPEC: ${{ steps.check_changes.outputs.previous_spec }} | |
| run: | | |
| set -e | |
| if [ -n "$PREVIOUS_SPEC" ] && [ -f "$PREVIOUS_SPEC" ]; then | |
| echo "Comparing previous and current openapi.json..." | |
| SUMMARY=$(uv run python scripts/compare_openapi_specs.py "$PREVIOUS_SPEC" openapi.json 2>&1) | |
| else | |
| echo "No previous spec found, using default summary" | |
| SUMMARY="New OpenAPI spec file: openapi.json" | |
| fi | |
| echo "summary<<EOF" >> "$GITHUB_OUTPUT" | |
| echo "$SUMMARY" >> "$GITHUB_OUTPUT" | |
| echo "EOF" >> "$GITHUB_OUTPUT" | |
| - name: Render SDK agent prompt | |
| if: steps.check_changes.outputs.has_changes == 'true' | |
| id: render_prompt | |
| env: | |
| SUMMARY: ${{ steps.generate_summary.outputs.summary }} | |
| run: | | |
| set -e | |
| PROMPT=$(uv run python scripts/render_sdk_agent_prompt.py --summary "$SUMMARY") | |
| echo "prompt<<EOF" >> "$GITHUB_OUTPUT" | |
| echo "$PROMPT" >> "$GITHUB_OUTPUT" | |
| echo "EOF" >> "$GITHUB_OUTPUT" | |
| - name: Launch Cursor Agent | |
| if: steps.check_changes.outputs.has_changes == 'true' | |
| id: launch_agent | |
| env: | |
| CURSOR_API_KEY: ${{ secrets.CURSOR_API_KEY }} | |
| PROMPT_TEXT_RAW: ${{ steps.render_prompt.outputs.prompt }} | |
| run: | | |
| set -e | |
| BRANCH_NAME="cursor/update-api-$(date -u +%Y%m%d-%H%M%S)" | |
| PAYLOAD=$(jq -n \ | |
| --arg prompt_text "$PROMPT_TEXT_RAW" \ | |
| --arg branch_name "$BRANCH_NAME" \ | |
| '{ | |
| "prompt": { "text": $prompt_text }, | |
| "source": { | |
| "repository": "https://github.com/Giskard-AI/giskard-hub-python", | |
| "ref": "main" | |
| }, | |
| "target": { | |
| "autoCreatePr": true, | |
| "branchName": $branch_name, | |
| "openAsCursorGithubApp": true, | |
| "skipReviewerRequest": false | |
| } | |
| }') | |
| RESPONSE=$(curl -s -w "\n%{http_code}" \ | |
| --request POST \ | |
| --url https://api.cursor.com/v0/agents \ | |
| -u "${CURSOR_API_KEY}:" \ | |
| --header 'Content-Type: application/json' \ | |
| --data "$PAYLOAD") | |
| HTTP_CODE=$(echo "${RESPONSE}" | tail -n 1) | |
| BODY=$(echo "${RESPONSE}" | sed '$d') | |
| echo "http_code=$HTTP_CODE" >> "$GITHUB_OUTPUT" | |
| echo "body<<EOF" >> "$GITHUB_OUTPUT" | |
| echo "$BODY" >> "$GITHUB_OUTPUT" | |
| echo "EOF" >> "$GITHUB_OUTPUT" | |
| - name: Process agent response | |
| if: steps.check_changes.outputs.has_changes == 'true' | |
| env: | |
| HTTP_CODE: ${{ steps.launch_agent.outputs.http_code }} | |
| BODY: ${{ steps.launch_agent.outputs.body }} | |
| run: | | |
| set -e | |
| if [ "${HTTP_CODE}" -eq 200 ] || [ "${HTTP_CODE}" -eq 201 ]; then | |
| AGENT_ID=$(echo "${BODY}" | jq -r '.id') | |
| AGENT_URL=$(echo "${BODY}" | jq -r '.target.url') | |
| echo "Cursor Agent launched successfully!" | |
| echo "Agent ID: ${AGENT_ID}" | |
| echo "Agent URL: ${AGENT_URL}" | |
| else | |
| echo "Failed to launch Cursor Agent" | |
| echo "HTTP Status: ${HTTP_CODE}" | |
| echo "Response: ${BODY}" | |
| exit 1 | |
| fi |