Skip to content

Commit 21047ac

Browse files
fix: restore workflow_run trigger + use GH_PAT for PR creation
- Keep automatic trigger via workflow_run (runs after CI passes on PRs) - Keep manual trigger via workflow_dispatch - Replace GITHUB_TOKEN with GH_PAT to allow opening PRs from Actions - Use git add --force to correctly stage new test directories - Use find instead of git status to detect generated test files Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent cd01c3b commit 21047ac

1 file changed

Lines changed: 42 additions & 13 deletions

File tree

.github/workflows/generate-tests.yml

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
name: Auto Generate Cover+Test
22

3-
# Execução manual apenas — acesse Actions > Auto Generate Cover+Test > Run workflow.
4-
# Deixe override_files vazio para escanear todos os arquivos sem cobertura em craftd-core,
5-
# ou informe arquivos .kt específicos separados por espaço.
3+
# Dispara automaticamente após o CI (Build and Test for PRs) passar,
4+
# ou manualmente via Actions > Run workflow para testes pontuais.
65
on:
76
workflow_dispatch:
87
inputs:
@@ -11,6 +10,10 @@ on:
1110
required: false
1211
default: ''
1312

13+
workflow_run:
14+
workflows: ["Build and Test for PRs"]
15+
types: [completed]
16+
1417
permissions:
1518
contents: write
1619
pull-requests: write
@@ -20,15 +23,37 @@ jobs:
2023
name: Generate Unit Tests with Claude
2124
runs-on: ubuntu-latest
2225

26+
# workflow_dispatch: sempre roda
27+
# workflow_run: só roda se CI passou e não foi aberto pelo bot
28+
if: |
29+
github.event_name == 'workflow_dispatch' ||
30+
(
31+
github.event.workflow_run.conclusion == 'success' &&
32+
github.event.workflow_run.actor.login != 'github-actions[bot]'
33+
)
34+
2335
steps:
24-
# ── 1. Checkout ───────────────────────────────────────────────────────────
36+
# ── 1. Normaliza contexto para os dois gatilhos ───────────────────────────
37+
- name: Resolve trigger context
38+
id: ctx
39+
run: |
40+
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
41+
echo "head_sha=${{ github.sha }}" >> "$GITHUB_OUTPUT"
42+
echo "pr_number=manual" >> "$GITHUB_OUTPUT"
43+
else
44+
echo "head_sha=${{ github.event.workflow_run.head_sha }}" >> "$GITHUB_OUTPUT"
45+
echo "pr_number=${{ github.event.workflow_run.pull_requests[0].number }}" >> "$GITHUB_OUTPUT"
46+
fi
47+
48+
# ── 2. Checkout ───────────────────────────────────────────────────────────
2549
- name: Checkout
2650
uses: actions/checkout@v4
2751
with:
52+
ref: ${{ steps.ctx.outputs.head_sha }}
2853
fetch-depth: 0
2954
token: ${{ secrets.GH_PAT }}
3055

31-
# ── 2. Python ─────────────────────────────────────────────────────────────
56+
# ── 3. Python ─────────────────────────────────────────────────────────────
3257
- name: Set up Python
3358
uses: actions/setup-python@v5
3459
with:
@@ -37,7 +62,7 @@ jobs:
3762
- name: Install Python dependencies
3863
run: pip install anthropic
3964

40-
# ── 3. Escaneia craftd-core buscando arquivos sem cobertura ──────────────
65+
# ── 4. Escaneia craftd-core buscando arquivos sem cobertura ──────────────
4166
- name: Find uncovered Kotlin files in craftd-core
4267
id: changed
4368
run: |
@@ -73,15 +98,15 @@ jobs:
7398
printf "files<<EOF\n%s\nEOF\n" "$UNCOVERED" >> "$GITHUB_OUTPUT"
7499
fi
75100
76-
# ── 4. Chama Claude API para gerar os testes ─────────────────────────────
101+
# ── 5. Chama Claude API para gerar os testes ─────────────────────────────
77102
- name: Generate unit tests with Claude API
78103
if: steps.changed.outputs.has_changes == 'true'
79104
env:
80105
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
81106
CHANGED_FILES: ${{ steps.changed.outputs.files }}
82107
run: python .github/scripts/generate_tests.py
83108

84-
# ── 5. Verifica se arquivos foram gerados ─────────────────────────────────
109+
# ── 6. Verifica se arquivos foram gerados ─────────────────────────────────
85110
- name: Check generated files
86111
if: steps.changed.outputs.has_changes == 'true'
87112
id: check
@@ -101,12 +126,13 @@ jobs:
101126
echo "has_tests=false" >> "$GITHUB_OUTPUT"
102127
fi
103128
104-
# ── 6. Cria branch e commita os testes ────────────────────────────────────
129+
# ── 7. Cria branch e commita os testes ────────────────────────────────────
105130
- name: Commit generated tests
106131
if: steps.check.outputs.has_tests == 'true'
107132
id: commit
108133
run: |
109-
BRANCH="chore/add-tests-craftd-core-$(date +%Y%m%d-%H%M%S)"
134+
PR_NUMBER="${{ steps.ctx.outputs.pr_number }}"
135+
BRANCH="chore/add-tests-craftd-core-pr-${PR_NUMBER}"
110136
111137
git config user.name "github-actions[bot]"
112138
git config user.email "github-actions[bot]@users.noreply.github.com"
@@ -118,12 +144,13 @@ jobs:
118144
119145
echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"
120146
121-
# ── 7. Abre PR com os testes gerados ──────────────────────────────────────
147+
# ── 8. Abre PR com os testes gerados ──────────────────────────────────────
122148
- name: Open Pull Request with generated tests
123149
if: steps.check.outputs.has_tests == 'true'
124150
env:
125151
GH_TOKEN: ${{ secrets.GH_PAT }}
126152
run: |
153+
PR_NUMBER="${{ steps.ctx.outputs.pr_number }}"
127154
BRANCH="${{ steps.commit.outputs.branch }}"
128155
COVERED="${{ steps.check.outputs.covered_names }}"
129156
@@ -142,14 +169,16 @@ jobs:
142169
\`\`\`
143170
144171
### Como funciona
145-
1. Workflow executado manualmente via Actions
146-
2. Escaneou todos os arquivos sem cobertura em \`android_kmp/craftd-core\`
172+
1. Um PR tocou arquivos em \`android_kmp/craftd-core\`
173+
2. Após o CI passar, o workflow escaneou todos os arquivos sem cobertura
147174
3. Claude gerou testes **JUnit4 + MockK** para cada arquivo
148175
4. Este PR foi criado automaticamente com o resultado
149176
150177
### Checklist de revisão
151178
- [ ] Testes compilam sem erros (\`./gradlew testDebugUnitTest\`)
152179
- [ ] Testes cobrem os principais caminhos de lógica
153180
- [ ] Edge cases tratados (null, vazio, JSON inválido)
181+
182+
> Triggered by PR #${PR_NUMBER}
154183
EOF
155184
)"

0 commit comments

Comments
 (0)