Skip to content

Auto Optimization Draft PR #5

Auto Optimization Draft PR

Auto Optimization Draft PR #5

name: Auto Optimization Draft PR
"on":
issues:
types: [opened, edited, reopened, labeled]
workflow_dispatch:
inputs:
issue_number:
description: "Monthly optimization task issue number"
required: true
jobs:
auto-pr:
if: contains(github.event.issue.labels.*.name, 'monthly-optimization-task') || inputs.issue_number != ''
runs-on: ubuntu-latest
permissions:
actions: write
contents: write
issues: write
pull-requests: write
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Check automation prerequisites
id: prereqs
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
mkdir -p data/output/auto_optimization
if [ -z "${ANTHROPIC_API_KEY}" ]; then
echo "has_anthropic_key=false" >> "$GITHUB_OUTPUT"
echo "Claude automation skipped: ANTHROPIC_API_KEY is not configured for this repo." > data/output/auto_optimization/skip_reason.txt
else
echo "has_anthropic_key=true" >> "$GITHUB_OUTPUT"
fi
- name: Load issue context
id: issue_context
run: |
python3 - <<'PY'
import json
import os
import urllib.request
from pathlib import Path
repo = os.environ["GITHUB_REPOSITORY"]
issue_number = os.environ["ISSUE_NUMBER"]
token = os.environ["GITHUB_TOKEN"]
api_url = f"https://api.github.com/repos/{repo}/issues/{issue_number}"
request = urllib.request.Request(
api_url,
headers={
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28",
"User-Agent": "auto-optimization-pr",
},
)
with urllib.request.urlopen(request) as response:
issue = json.load(response)
issue_context = {
"number": issue["number"],
"title": issue["title"],
"body": issue.get("body", ""),
}
output_dir = Path("data/output/auto_optimization")
output_dir.mkdir(parents=True, exist_ok=True)
(output_dir / "issue_context.json").write_text(
json.dumps(issue_context, ensure_ascii=False, indent=2) + "\n",
encoding="utf-8",
)
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output:
print("issue_title<<EOF", file=output)
print(issue_context["title"], file=output)
print("EOF", file=output)
print("issue_body<<EOF", file=output)
print(issue_context["body"], file=output)
print("EOF", file=output)
PY
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_NUMBER: ${{ inputs.issue_number || github.event.issue.number }}
- name: Prepare auto optimization payload
id: auto_payload
run: |
python3 scripts/prepare_auto_optimization_pr.py \
--issue-context-file data/output/auto_optimization/issue_context.json \
--output-dir data/output/auto_optimization >> "$GITHUB_OUTPUT"
- name: Append task summary
run: cat data/output/auto_optimization/task_summary.md >> "$GITHUB_STEP_SUMMARY"
- name: Export selected task summary
id: selected_tasks
run: |
{
echo "task_summary<<EOF"
cat data/output/auto_optimization/task_summary.md
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Append skip reason
if: steps.prereqs.outputs.has_anthropic_key != 'true' || steps.auto_payload.outputs.should_run != 'true'
run: |
if [ -f data/output/auto_optimization/skip_reason.txt ]; then
cat data/output/auto_optimization/skip_reason.txt >> "$GITHUB_STEP_SUMMARY"
else
echo "No eligible low-risk auto-pr-safe tasks were found; skipping draft PR generation." >> "$GITHUB_STEP_SUMMARY"
fi
- name: Prepare automation branch
if: steps.prereqs.outputs.has_anthropic_key == 'true' && steps.auto_payload.outputs.should_run == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git checkout -B "${{ steps.auto_payload.outputs.branch_name }}"
- name: Run Claude auto optimization
if: steps.prereqs.outputs.has_anthropic_key == 'true' && steps.auto_payload.outputs.should_run == 'true'
timeout-minutes: 15
uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
github_token: ${{ secrets.GITHUB_TOKEN }}
use_bedrock: false
use_vertex: false
claude_args: --max-turns 8
prompt: |
Do not ask for additional approval.
Do not create a pull request yourself. The workflow will handle git, draft PR creation, and CI dispatch.
Only implement the low-risk tasks explicitly marked `[auto-pr-safe]`.
Ignore any medium-risk or high-risk tasks.
You are working inside CryptoLeaderRotation, the upstream selector repository.
Prefer minimal changes in documentation, report wording, validation, shadow/challenger plumbing, instrumentation, and tests.
Do not change production selector logic or ranking behavior from this issue alone.
If an eligible task is marked `experiment-only`, keep the change non-production.
If the selected low-risk tasks already appear implemented on the current main branch, leave the working tree unchanged.
Do not use Bash in this workflow. Limit yourself to file edits and repository-local reasoning.
The workflow will run CI after the draft PR is created.
## Issue Title
${{ steps.issue_context.outputs.issue_title }}
## Selected Low-Risk Tasks
${{ steps.selected_tasks.outputs.task_summary }}
- name: Detect changes
id: changes
if: steps.prereqs.outputs.has_anthropic_key == 'true' && steps.auto_payload.outputs.should_run == 'true'
run: |
if git diff --quiet; then
echo "has_changes=false" >> "$GITHUB_OUTPUT"
else
echo "has_changes=true" >> "$GITHUB_OUTPUT"
fi
- name: Commit and push automation branch
if: steps.changes.outputs.has_changes == 'true'
run: |
git add -A
git commit -m "${{ steps.auto_payload.outputs.commit_message }}"
git push --force-with-lease origin "${{ steps.auto_payload.outputs.branch_name }}"
- name: Create or update draft PR
id: draft_pr
if: steps.changes.outputs.has_changes == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH_NAME="${{ steps.auto_payload.outputs.branch_name }}"
PR_TITLE="${{ steps.auto_payload.outputs.pr_title }}"
PR_BODY_FILE="${{ steps.auto_payload.outputs.pr_body_file }}"
EXISTING_PR_NUMBER=$(gh pr list --state open --head "${BRANCH_NAME}" --json number --jq '.[0].number // empty')
if [ -n "${EXISTING_PR_NUMBER}" ]; then
gh pr edit "${EXISTING_PR_NUMBER}" --title "${PR_TITLE}" --body-file "${PR_BODY_FILE}"
PR_URL=$(gh pr view "${EXISTING_PR_NUMBER}" --json url --jq '.url')
echo "pr_action=updated" >> "$GITHUB_OUTPUT"
else
PR_URL=$(gh pr create --draft --base main --head "${BRANCH_NAME}" --title "${PR_TITLE}" --body-file "${PR_BODY_FILE}")
echo "pr_action=created" >> "$GITHUB_OUTPUT"
fi
echo "pr_url=${PR_URL}" >> "$GITHUB_OUTPUT"
- name: Dispatch CI workflow on automation branch
if: steps.changes.outputs.has_changes == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh workflow run ci.yml --ref "${{ steps.auto_payload.outputs.branch_name }}"
- name: Append automation result
if: steps.prereqs.outputs.has_anthropic_key == 'true' && steps.auto_payload.outputs.should_run == 'true'
run: |
if [ "${{ steps.changes.outputs.has_changes }}" = "true" ]; then
{
echo ""
echo "## Draft PR Result"
echo "- Draft PR ${{ steps.draft_pr.outputs.pr_action }}: ${{ steps.draft_pr.outputs.pr_url }}"
echo "- CI workflow dispatched on branch: `${{ steps.auto_payload.outputs.branch_name }}`"
} >> "$GITHUB_STEP_SUMMARY"
else
echo "No code changes were produced for the selected low-risk tasks." >> "$GITHUB_STEP_SUMMARY"
fi