|
| 1 | +name: Auto Optimization Draft PR |
| 2 | + |
| 3 | +"on": |
| 4 | + issues: |
| 5 | + types: [opened, edited, reopened, labeled] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + issue_number: |
| 9 | + description: "Monthly optimization task issue number" |
| 10 | + required: true |
| 11 | + |
| 12 | +jobs: |
| 13 | + auto-pr: |
| 14 | + if: contains(github.event.issue.labels.*.name, 'monthly-optimization-task') || inputs.issue_number != '' |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + actions: write |
| 18 | + contents: write |
| 19 | + issues: write |
| 20 | + pull-requests: write |
| 21 | + |
| 22 | + env: |
| 23 | + FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true" |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout |
| 27 | + uses: actions/checkout@v6 |
| 28 | + with: |
| 29 | + fetch-depth: 0 |
| 30 | + |
| 31 | + - name: Check automation prerequisites |
| 32 | + id: prereqs |
| 33 | + env: |
| 34 | + ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} |
| 35 | + run: | |
| 36 | + mkdir -p data/output/auto_optimization |
| 37 | + if [ -z "${ANTHROPIC_API_KEY}" ]; then |
| 38 | + echo "has_anthropic_key=false" >> "$GITHUB_OUTPUT" |
| 39 | + echo "Claude automation skipped: ANTHROPIC_API_KEY is not configured for this repo." > data/output/auto_optimization/skip_reason.txt |
| 40 | + else |
| 41 | + echo "has_anthropic_key=true" >> "$GITHUB_OUTPUT" |
| 42 | + fi |
| 43 | +
|
| 44 | + - name: Load issue context |
| 45 | + id: issue_context |
| 46 | + run: | |
| 47 | + python3 - <<'PY' |
| 48 | + import json |
| 49 | + import os |
| 50 | + import urllib.request |
| 51 | + from pathlib import Path |
| 52 | +
|
| 53 | + repo = os.environ["GITHUB_REPOSITORY"] |
| 54 | + issue_number = os.environ["ISSUE_NUMBER"] |
| 55 | + token = os.environ["GITHUB_TOKEN"] |
| 56 | + api_url = f"https://api.github.com/repos/{repo}/issues/{issue_number}" |
| 57 | + request = urllib.request.Request( |
| 58 | + api_url, |
| 59 | + headers={ |
| 60 | + "Accept": "application/vnd.github+json", |
| 61 | + "Authorization": f"Bearer {token}", |
| 62 | + "X-GitHub-Api-Version": "2022-11-28", |
| 63 | + "User-Agent": "auto-optimization-pr", |
| 64 | + }, |
| 65 | + ) |
| 66 | + with urllib.request.urlopen(request) as response: |
| 67 | + issue = json.load(response) |
| 68 | +
|
| 69 | + issue_context = { |
| 70 | + "number": issue["number"], |
| 71 | + "title": issue["title"], |
| 72 | + "body": issue.get("body", ""), |
| 73 | + } |
| 74 | + output_dir = Path("data/output/auto_optimization") |
| 75 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 76 | + (output_dir / "issue_context.json").write_text( |
| 77 | + json.dumps(issue_context, ensure_ascii=False, indent=2) + "\n", |
| 78 | + encoding="utf-8", |
| 79 | + ) |
| 80 | +
|
| 81 | + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output: |
| 82 | + print("issue_title<<EOF", file=output) |
| 83 | + print(issue_context["title"], file=output) |
| 84 | + print("EOF", file=output) |
| 85 | + print("issue_body<<EOF", file=output) |
| 86 | + print(issue_context["body"], file=output) |
| 87 | + print("EOF", file=output) |
| 88 | + PY |
| 89 | + env: |
| 90 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 91 | + ISSUE_NUMBER: ${{ inputs.issue_number || github.event.issue.number }} |
| 92 | + |
| 93 | + - name: Prepare auto optimization payload |
| 94 | + id: auto_payload |
| 95 | + run: | |
| 96 | + python3 scripts/prepare_auto_optimization_pr.py \ |
| 97 | + --issue-context-file data/output/auto_optimization/issue_context.json \ |
| 98 | + --output-dir data/output/auto_optimization >> "$GITHUB_OUTPUT" |
| 99 | +
|
| 100 | + - name: Append task summary |
| 101 | + run: cat data/output/auto_optimization/task_summary.md >> "$GITHUB_STEP_SUMMARY" |
| 102 | + |
| 103 | + - name: Append skip reason |
| 104 | + if: steps.prereqs.outputs.has_anthropic_key != 'true' || steps.auto_payload.outputs.should_run != 'true' |
| 105 | + run: | |
| 106 | + if [ -f data/output/auto_optimization/skip_reason.txt ]; then |
| 107 | + cat data/output/auto_optimization/skip_reason.txt >> "$GITHUB_STEP_SUMMARY" |
| 108 | + else |
| 109 | + echo "No eligible low-risk auto-pr-safe tasks were found; skipping draft PR generation." >> "$GITHUB_STEP_SUMMARY" |
| 110 | + fi |
| 111 | +
|
| 112 | + - name: Prepare automation branch |
| 113 | + if: steps.prereqs.outputs.has_anthropic_key == 'true' && steps.auto_payload.outputs.should_run == 'true' |
| 114 | + run: | |
| 115 | + git config user.name "github-actions[bot]" |
| 116 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 117 | + git checkout -B "${{ steps.auto_payload.outputs.branch_name }}" |
| 118 | +
|
| 119 | + - name: Run Claude auto optimization |
| 120 | + if: steps.prereqs.outputs.has_anthropic_key == 'true' && steps.auto_payload.outputs.should_run == 'true' |
| 121 | + uses: anthropics/claude-code-action@v1 |
| 122 | + with: |
| 123 | + anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} |
| 124 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 125 | + use_bedrock: false |
| 126 | + use_vertex: false |
| 127 | + prompt: | |
| 128 | + Do not ask for additional approval. |
| 129 | + Do not create a pull request yourself. The workflow will handle git, draft PR creation, and CI dispatch. |
| 130 | + Only implement the low-risk tasks explicitly marked `[auto-pr-safe]`. |
| 131 | + Ignore any medium-risk or high-risk tasks. |
| 132 | + You are working inside CryptoStrategies, the shared strategy-logic repository. |
| 133 | + Prefer minimal changes in shared helpers, documentation, and tests. |
| 134 | + Avoid changing shared production strategy behavior unless the task remains clearly low-risk and local. |
| 135 | + If the selected low-risk tasks do not map cleanly to this repository, leave the working tree unchanged. |
| 136 | + Run the most relevant tests or ruff checks when you make a change. |
| 137 | +
|
| 138 | + ## Issue Title |
| 139 | + ${{ steps.issue_context.outputs.issue_title }} |
| 140 | +
|
| 141 | + ## Issue Body |
| 142 | + ${{ steps.issue_context.outputs.issue_body }} |
| 143 | +
|
| 144 | + - name: Detect changes |
| 145 | + id: changes |
| 146 | + if: steps.prereqs.outputs.has_anthropic_key == 'true' && steps.auto_payload.outputs.should_run == 'true' |
| 147 | + run: | |
| 148 | + if git diff --quiet; then |
| 149 | + echo "has_changes=false" >> "$GITHUB_OUTPUT" |
| 150 | + else |
| 151 | + echo "has_changes=true" >> "$GITHUB_OUTPUT" |
| 152 | + fi |
| 153 | +
|
| 154 | + - name: Commit and push automation branch |
| 155 | + if: steps.changes.outputs.has_changes == 'true' |
| 156 | + run: | |
| 157 | + git add -A |
| 158 | + git commit -m "${{ steps.auto_payload.outputs.commit_message }}" |
| 159 | + git push --force-with-lease origin "${{ steps.auto_payload.outputs.branch_name }}" |
| 160 | +
|
| 161 | + - name: Create or update draft PR |
| 162 | + id: draft_pr |
| 163 | + if: steps.changes.outputs.has_changes == 'true' |
| 164 | + env: |
| 165 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 166 | + run: | |
| 167 | + BRANCH_NAME="${{ steps.auto_payload.outputs.branch_name }}" |
| 168 | + PR_TITLE="${{ steps.auto_payload.outputs.pr_title }}" |
| 169 | + PR_BODY_FILE="${{ steps.auto_payload.outputs.pr_body_file }}" |
| 170 | + EXISTING_PR_NUMBER=$(gh pr list --state open --head "${BRANCH_NAME}" --json number --jq '.[0].number // empty') |
| 171 | + if [ -n "${EXISTING_PR_NUMBER}" ]; then |
| 172 | + gh pr edit "${EXISTING_PR_NUMBER}" --title "${PR_TITLE}" --body-file "${PR_BODY_FILE}" |
| 173 | + PR_URL=$(gh pr view "${EXISTING_PR_NUMBER}" --json url --jq '.url') |
| 174 | + echo "pr_action=updated" >> "$GITHUB_OUTPUT" |
| 175 | + else |
| 176 | + PR_URL=$(gh pr create --draft --base main --head "${BRANCH_NAME}" --title "${PR_TITLE}" --body-file "${PR_BODY_FILE}") |
| 177 | + echo "pr_action=created" >> "$GITHUB_OUTPUT" |
| 178 | + fi |
| 179 | + echo "pr_url=${PR_URL}" >> "$GITHUB_OUTPUT" |
| 180 | +
|
| 181 | + - name: Dispatch CI workflow on automation branch |
| 182 | + if: steps.changes.outputs.has_changes == 'true' |
| 183 | + env: |
| 184 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 185 | + run: gh workflow run ci.yml --ref "${{ steps.auto_payload.outputs.branch_name }}" |
| 186 | + |
| 187 | + - name: Append automation result |
| 188 | + if: steps.prereqs.outputs.has_anthropic_key == 'true' && steps.auto_payload.outputs.should_run == 'true' |
| 189 | + run: | |
| 190 | + if [ "${{ steps.changes.outputs.has_changes }}" = "true" ]; then |
| 191 | + { |
| 192 | + echo "" |
| 193 | + echo "## Draft PR Result" |
| 194 | + echo "- Draft PR ${{ steps.draft_pr.outputs.pr_action }}: ${{ steps.draft_pr.outputs.pr_url }}" |
| 195 | + echo "- CI workflow dispatched on branch: `${{ steps.auto_payload.outputs.branch_name }}`" |
| 196 | + } >> "$GITHUB_STEP_SUMMARY" |
| 197 | + else |
| 198 | + echo "No code changes were produced for the selected low-risk tasks." >> "$GITHUB_STEP_SUMMARY" |
| 199 | + fi |
0 commit comments