|
4 | 4 | - cron: '11 10 * * *' |
5 | 5 | workflow_dispatch: |
6 | 6 | permissions: |
7 | | - contents: read |
8 | | - issues: write |
| 7 | + contents: write |
| 8 | + issues: read |
9 | 9 | pull-requests: write |
| 10 | + actions: read |
10 | 11 | concurrency: |
11 | 12 | group: daily-ai-maintenance |
12 | 13 | cancel-in-progress: false |
13 | 14 | jobs: |
14 | | - triage: |
| 15 | + maintain: |
15 | 16 | runs-on: ubuntu-latest |
16 | | - timeout-minutes: 20 |
| 17 | + timeout-minutes: 55 |
17 | 18 | env: |
18 | 19 | GH_TOKEN: ${{ github.token }} |
19 | 20 | OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} |
20 | 21 | steps: |
21 | 22 | - uses: actions/checkout@v4 |
22 | 23 | with: |
23 | | - persist-credentials: false |
24 | | - - name: Audit PRs and safely merge eligible dependency fixes |
| 24 | + fetch-depth: 0 |
| 25 | + - uses: actions/setup-node@v4 |
| 26 | + with: |
| 27 | + node-version: '22' |
| 28 | + - uses: actions/setup-python@v5 |
| 29 | + with: |
| 30 | + python-version: '3.12' |
| 31 | + - name: Merge fully green low-risk dependency PRs |
25 | 32 | shell: bash |
26 | 33 | run: | |
27 | 34 | set -euo pipefail |
28 | | - echo "## Daily maintenance — $GITHUB_REPOSITORY" >> "$GITHUB_STEP_SUMMARY" |
29 | | - gh pr list --state open --limit 100 --json number,title,author,isDraft,mergeable,statusCheckRollup,labels > /tmp/prs.json |
| 35 | + echo '## Daily AI maintenance' >> "$GITHUB_STEP_SUMMARY" |
| 36 | + gh pr list --state open --limit 100 --json number,title,author,isDraft,mergeable,statusCheckRollup,labels,reviewDecision > /tmp/prs.json |
30 | 37 | jq -c '.[]' /tmp/prs.json | while IFS= read -r pr; do |
31 | | - number=$(jq -r '.number' <<< "$pr") |
32 | | - title=$(jq -r '.title' <<< "$pr") |
| 38 | + n=$(jq -r '.number' <<< "$pr") |
33 | 39 | author=$(jq -r '.author.login // ""' <<< "$pr") |
34 | | - failed=$(jq '[.statusCheckRollup[]? | select(.conclusion == "FAILURE" or .conclusion == "TIMED_OUT")] | length' <<< "$pr") |
35 | | - if [ "$failed" -gt 0 ]; then |
36 | | - echo "- PR #$number: failing checks" >> "$GITHUB_STEP_SUMMARY" |
37 | | - existing=$(gh issue list --state open --search "AI repair PR #$number in:title" --json number | jq 'length') |
38 | | - if [ "$existing" -eq 0 ]; then |
39 | | - gh issue create --title "AI repair PR #$number" --body "CI failure on PR #$number ($title). Review workflow logs, fix the failing check, and rerun tests. Do not merge without successful checks." |
40 | | - fi |
41 | | - continue |
42 | | - fi |
43 | | - # Never merge arbitrary contributions, major upgrades, or unchecked PRs. |
44 | | - if [[ "$author" != "renovate[bot]" && "$author" != "dependabot[bot]" ]]; then continue; fi |
45 | | - if jq -e '.isDraft or .mergeable != "MERGEABLE" or (.statusCheckRollup | length == 0) or any(.statusCheckRollup[]; .status != "COMPLETED" or .conclusion != "SUCCESS")' <<< "$pr" >/dev/null; then continue; fi |
46 | | - if grep -Eiq 'major|breaking|monorepo to v[0-9]+($|[^0-9])' <<< "$title"; then |
47 | | - echo "- PR #$number: manual review required for potentially breaking upgrade" >> "$GITHUB_STEP_SUMMARY" |
48 | | - continue |
49 | | - fi |
50 | | - # Respect branch protection and existing review requirements; do not force. |
51 | | - if gh pr merge "$number" --squash --auto 2>/tmp/merge-error; then |
52 | | - echo "- PR #$number: auto-merge requested (GitHub gates still apply)" >> "$GITHUB_STEP_SUMMARY" |
53 | | - else |
54 | | - echo "- PR #$number: merge deferred — $(head -c 150 /tmp/merge-error)" >> "$GITHUB_STEP_SUMMARY" |
55 | | - fi |
| 40 | + title=$(jq -r '.title' <<< "$pr") |
| 41 | + [[ "$author" == 'renovate[bot]' || "$author" == 'dependabot[bot]' ]] || continue |
| 42 | + if jq -e '.isDraft or .mergeable != "MERGEABLE" or (.reviewDecision == "CHANGES_REQUESTED") or (.reviewDecision == "REVIEW_REQUIRED") or (.statusCheckRollup | length == 0) or any(.statusCheckRollup[]; .status != "COMPLETED" or .conclusion != "SUCCESS") or any(.labels[]?; .name == "ai:review-required")' <<< "$pr" >/dev/null; then continue; fi |
| 43 | + if grep -Eiq '(major|breaking|\bv[0-9]+\b)' <<< "$title"; then continue; fi |
| 44 | + if gh pr merge "$n" --squash; then echo "Merged dependency PR #$n" >> "$GITHUB_STEP_SUMMARY"; fi |
56 | 45 | done |
57 | | - - name: AI repository improvement review |
| 46 | + - name: Install AI coding agent |
| 47 | + if: env.OPENAI_API_KEY != '' |
| 48 | + run: python -m pip install 'aider-chat==0.86.1' |
| 49 | + - name: Repair one failing dependency PR |
58 | 50 | if: env.OPENAI_API_KEY != '' |
| 51 | + continue-on-error: true |
59 | 52 | shell: bash |
60 | 53 | run: | |
61 | | - python - <<'PY' |
62 | | - import json, os, pathlib, subprocess, urllib.request |
63 | | - files = [] |
64 | | - for name in ('package.json', 'README.md', 'tsconfig.json', 'renovate.json'): |
65 | | - p = pathlib.Path(name) |
66 | | - if p.is_file(): |
67 | | - files.append((name, p.read_text(errors='replace')[:4500])) |
68 | | - if not files: |
69 | | - print('No root package metadata to review') |
70 | | - raise SystemExit(0) |
71 | | - prompt = ('Review repository metadata and identify ONE concrete, non-duplicative ' |
72 | | - 'quality, security-hardening, accessibility, or testing improvement. ' |
73 | | - 'Do not claim a vulnerability without evidence. Output a concise issue ' |
74 | | - 'with a title line and implementation/acceptance criteria. ' |
75 | | - 'Never include secrets or request workflow permission changes.\n\n' + |
76 | | - '\n\n'.join(n + ':\n' + v for n, v in files)) |
77 | | - data = json.dumps({'model':'gpt-4.1-mini','input':prompt,'max_output_tokens':350}).encode() |
78 | | - request = urllib.request.Request('https://api.openai.com/v1/responses',data=data, |
79 | | - headers={'Authorization':'Bearer '+os.environ['OPENAI_API_KEY'], |
80 | | - 'Content-Type':'application/json'}) |
81 | | - try: |
82 | | - with urllib.request.urlopen(request, timeout=40) as response: |
83 | | - result = json.load(response) |
84 | | - suggestion = '\n'.join(c.get('text','') for item in result.get('output',[]) |
85 | | - for c in item.get('content',[]) if c.get('type')=='output_text').strip() |
86 | | - if not suggestion: raise ValueError('Empty model response') |
87 | | - existing = subprocess.check_output(['gh','issue','list','--state','open', |
88 | | - '--search','Daily AI improvement in:title','--json','number'],text=True) |
89 | | - if json.loads(existing): raise SystemExit(0) |
90 | | - subprocess.run(['gh','issue','create','--title','Daily AI improvement review', |
91 | | - '--body',suggestion[:3500]+'\n\nAI suggestion; validate before implementing.'],check=True) |
92 | | - except Exception as exc: |
93 | | - print('Improvement review skipped:',str(exc)[:180]) |
94 | | - PY |
| 54 | + set -euo pipefail |
| 55 | + pr=$(jq -c '[.[] | select((.author.login == "renovate[bot]" or .author.login == "dependabot[bot]") and (.isDraft == false) and any(.statusCheckRollup[]?; .conclusion == "FAILURE" or .conclusion == "TIMED_OUT"))][0] // empty' /tmp/prs.json) |
| 56 | + [ -n "$pr" ] || exit 0 |
| 57 | + n=$(jq -r '.number' <<< "$pr") |
| 58 | + branch=$(gh pr view "$n" --json headRefName --jq .headRefName) |
| 59 | + owner=$(gh pr view "$n" --json headRepositoryOwner --jq .headRepositoryOwner.login) |
| 60 | + [ "$owner" = "${GITHUB_REPOSITORY%%/*}" ] || exit 0 |
| 61 | + gh pr checkout "$n" |
| 62 | + git config user.name 'github-actions[bot]' |
| 63 | + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' |
| 64 | + gh pr checks "$n" > /tmp/checks.txt 2>&1 || true |
| 65 | + run_id=$(gh run list --branch "$branch" --limit 10 --json databaseId,conclusion --jq '[.[] | select(.conclusion == "failure")][0].databaseId // empty') |
| 66 | + if [ -n "$run_id" ]; then gh run view "$run_id" --log-failed > /tmp/logs.txt 2>&1 || true; fi |
| 67 | + { echo "Repair failing checks for dependency PR #$n. Keep the dependency update. Do not change workflows, secrets, or public APIs."; head -c 16000 /tmp/logs.txt 2>/dev/null || cat /tmp/checks.txt; } > /tmp/repair.txt |
| 68 | + mapfile -t targets < <(git ls-files | grep -E '^(src/|test/|tests/|scripts/|package\.json$|tsconfig[^/]*\.json$)' | head -n 35) |
| 69 | + [ "${#targets[@]}" -gt 0 ] || exit 0 |
| 70 | + CI=1 aider --yes --no-check-update --no-show-release-notes --no-gitignore --no-auto-commits --no-pretty --no-stream --chat-history-file /tmp/repair-history.md --input-history-file /tmp/repair-input --model gpt-4.1 --message-file /tmp/repair.txt "${targets[@]}" |
| 71 | + git add -u |
| 72 | + git diff --cached --quiet && exit 0 |
| 73 | + if git diff --cached --name-only | grep -E '(^\.github/|^\.env|^AGENTS\.md$|^renovate\.json$|^package-lock\.json$)'; then exit 1; fi |
| 74 | + if [ -f package-lock.json ]; then npm ci --ignore-scripts; npm test --if-present -- --runInBand; fi |
| 75 | + git commit -m "fix: AI repair for PR #$n" |
| 76 | + git push origin HEAD:"$branch" |
| 77 | + echo "Pushed repair to PR #$n; awaiting CI" >> "$GITHUB_STEP_SUMMARY" |
| 78 | + - name: Create improvement PR requiring review |
| 79 | + if: env.OPENAI_API_KEY != '' |
| 80 | + shell: bash |
| 81 | + run: | |
| 82 | + set -euo pipefail |
| 83 | + git checkout "$GITHUB_REF_NAME" |
| 84 | + git reset --hard "origin/$GITHUB_REF_NAME" |
| 85 | + existing=$(gh pr list --state open --limit 100 --json labels --jq '[.[] | select(any(.labels[]?; .name == "ai:review-required"))] | length') |
| 86 | + [ "$existing" -eq 0 ] || exit 0 |
| 87 | + mapfile -t targets < <(git ls-files | grep -E '^(src/|test/|tests/|README\.md$)' | head -n 12) |
| 88 | + [ "${#targets[@]}" -gt 0 ] || exit 0 |
| 89 | + git config user.name 'github-actions[bot]' |
| 90 | + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' |
| 91 | + branch="ai/improvement-${GITHUB_RUN_ID}" |
| 92 | + git checkout -b "$branch" |
| 93 | + echo 'Implement ONE focused code-quality, test, documentation, or security-hardening improvement in the provided files. Do not invent vulnerabilities, alter public APIs, workflows, dependencies, secrets, or policies. If nothing useful can be improved, do not change files.' > /tmp/improvement.txt |
| 94 | + CI=1 aider --yes --no-check-update --no-show-release-notes --no-gitignore --no-auto-commits --no-pretty --no-stream --chat-history-file /tmp/improvement-history.md --input-history-file /tmp/improvement-input --model gpt-4.1 --message-file /tmp/improvement.txt "${targets[@]}" |
| 95 | + git add -- "${targets[@]}" |
| 96 | + git diff --cached --quiet && exit 0 |
| 97 | + unexpected=$(git status --porcelain | awk '{print substr($0,4)}' | grep -v -F -x -f <(printf '%s\n' "${targets[@]}") || true) |
| 98 | + [ -z "$unexpected" ] || { echo "Unexpected files: $unexpected"; exit 1; } |
| 99 | + if [ -f package-lock.json ]; then npm ci --ignore-scripts; npm test --if-present -- --runInBand; fi |
| 100 | + git commit -m 'ai: propose focused improvement' |
| 101 | + git push origin "$branch" |
| 102 | + gh label create 'ai:review-required' --color 'd4a72c' --description 'Maintainer review required' --force |
| 103 | + gh pr create --draft --base "$GITHUB_REF_NAME" --head "$branch" --title 'AI improvement proposal (review required)' --body 'AI-generated change. **Requires maintainer review and approval.** Never auto-merge.' |
| 104 | + gh pr edit "$branch" --add-label 'ai:review-required' |
| 105 | + echo 'Created improvement draft PR for review' >> "$GITHUB_STEP_SUMMARY" |
0 commit comments