|
| 1 | +name: Code Generator CI |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +jobs: |
| 8 | + generate-and-check: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + steps: |
| 12 | + - name: Checkout codegen repo |
| 13 | + uses: actions/checkout@v4 |
| 14 | + with: |
| 15 | + fetch-depth: 0 |
| 16 | + |
| 17 | + - name: Run code generator |
| 18 | + run: | |
| 19 | + chmod +x codegen.sh |
| 20 | + ./codegen.sh |
| 21 | + |
| 22 | + - name: Check code format with clang-format |
| 23 | + id: format-check |
| 24 | + run: | |
| 25 | + # Find all generated C/C++ files (adjust pattern as needed) |
| 26 | + find . -type f \( -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) > files.txt |
| 27 | + |
| 28 | + # Run clang-format check |
| 29 | + if clang-format --dry-run --Werror $(cat files.txt) 2>&1 | tee format-output.txt; then |
| 30 | + echo "format_ok=true" >> $GITHUB_OUTPUT |
| 31 | + echo "✅ Code is properly formatted" |
| 32 | + else |
| 33 | + echo "format_ok=false" >> $GITHUB_OUTPUT |
| 34 | + echo "❌ Code needs formatting" |
| 35 | + |
| 36 | + # Generate the diff for needed changes |
| 37 | + clang-format -i $(cat files.txt) |
| 38 | + git diff > format-changes.patch |
| 39 | + fi |
| 40 | + |
| 41 | + - name: Commit to target repository |
| 42 | + if: steps.format-check.outputs.format_ok == 'true' |
| 43 | + env: |
| 44 | + TARGET_REPO: your-username/target-repo # Change this |
| 45 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Or use a PAT: secrets.TARGET_REPO_PAT |
| 46 | + run: | |
| 47 | + # Configure git |
| 48 | + git config --global user.name "github-actions[bot]" |
| 49 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 50 | + |
| 51 | + # Clone target repo |
| 52 | + git clone https://x-access-token:${GH_TOKEN}@github.com/${TARGET_REPO}.git target-repo |
| 53 | + cd target-repo |
| 54 | + |
| 55 | + # Create branch based on PR number |
| 56 | + BRANCH_NAME="codegen-pr-${{ github.event.pull_request.number }}" |
| 57 | + git checkout -b ${BRANCH_NAME} |
| 58 | + |
| 59 | + # Copy generated files (adjust paths as needed) |
| 60 | + cp -r ../generated-output/* . |
| 61 | + |
| 62 | + # Commit and push |
| 63 | + git add . |
| 64 | + git commit -m "Code generated from PR #${{ github.event.pull_request.number }}" \ |
| 65 | + -m "Source PR: ${{ github.event.pull_request.html_url }}" |
| 66 | + git push origin ${BRANCH_NAME} |
| 67 | + |
| 68 | + echo "✅ Pushed to ${TARGET_REPO} on branch ${BRANCH_NAME}" |
| 69 | + |
| 70 | + - name: Comment on PR with format issues |
| 71 | + if: steps.format-check.outputs.format_ok == 'false' |
| 72 | + uses: actions/github-script@v7 |
| 73 | + with: |
| 74 | + script: | |
| 75 | + const fs = require('fs'); |
| 76 | + const patch = fs.readFileSync('format-changes.patch', 'utf8'); |
| 77 | + |
| 78 | + const comment = `## ❌ Code Format Check Failed |
| 79 | + |
| 80 | + The generated code does not conform to clang-format rules. |
| 81 | + |
| 82 | + <details> |
| 83 | + <summary>Required formatting changes</summary> |
| 84 | + |
| 85 | + \`\`\`diff |
| 86 | + ${patch} |
| 87 | + \`\`\` |
| 88 | + |
| 89 | + </details> |
| 90 | + |
| 91 | + Please update your code generator to produce properly formatted code.`; |
| 92 | + |
| 93 | + github.rest.issues.createComment({ |
| 94 | + owner: context.repo.owner, |
| 95 | + repo: context.repo.repo, |
| 96 | + issue_number: context.issue.number, |
| 97 | + body: comment |
| 98 | + }); |
| 99 | + |
| 100 | + - name: Fail if format check failed |
| 101 | + if: steps.format-check.outputs.format_ok == 'false' |
| 102 | + run: exit 1 |
0 commit comments