|
| 1 | +name: Automated Code Optimization and Fix Suggestions |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run weekly on Mondays at 5 AM UTC |
| 6 | + - cron: '0 5 * * 1' |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + create_pr: |
| 10 | + description: 'Create a PR with auto-fixes' |
| 11 | + required: false |
| 12 | + default: 'false' |
| 13 | + type: choice |
| 14 | + options: |
| 15 | + - 'true' |
| 16 | + - 'false' |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: write |
| 20 | + pull-requests: write |
| 21 | + |
| 22 | +jobs: |
| 23 | + auto-fix-python: |
| 24 | + name: Auto-fix Python Code Issues |
| 25 | + runs-on: ubuntu-latest |
| 26 | + strategy: |
| 27 | + fail-fast: false |
| 28 | + matrix: |
| 29 | + sub-package: [backend, autogpt_libs] |
| 30 | + |
| 31 | + steps: |
| 32 | + - name: Checkout repository |
| 33 | + uses: actions/checkout@v4 |
| 34 | + with: |
| 35 | + ref: ${{ github.head_ref || github.ref }} |
| 36 | + |
| 37 | + - name: Set up Python |
| 38 | + uses: actions/setup-python@v5 |
| 39 | + with: |
| 40 | + python-version: '3.10' |
| 41 | + |
| 42 | + - name: Install Poetry |
| 43 | + run: curl -sSL https://install.python-poetry.org | python3 - |
| 44 | + |
| 45 | + - name: Install dependencies |
| 46 | + run: | |
| 47 | + cd autogpt_platform/${{ matrix.sub-package }} |
| 48 | + poetry install |
| 49 | +
|
| 50 | + - name: Install auto-fix tools |
| 51 | + run: | |
| 52 | + cd autogpt_platform/${{ matrix.sub-package }} |
| 53 | + poetry add --group dev autopep8 autoflake |
| 54 | +
|
| 55 | + - name: Run auto-fixes |
| 56 | + run: | |
| 57 | + cd autogpt_platform/${{ matrix.sub-package }} |
| 58 | + |
| 59 | + # Remove unused imports |
| 60 | + poetry run autoflake --in-place --remove-all-unused-imports --remove-unused-variables -r . || true |
| 61 | + |
| 62 | + # Auto-fix PEP8 issues |
| 63 | + poetry run autopep8 --in-place --aggressive --aggressive -r . || true |
| 64 | + |
| 65 | + # Run existing formatters |
| 66 | + poetry run isort . || true |
| 67 | + poetry run black . || true |
| 68 | +
|
| 69 | + - name: Check for changes |
| 70 | + id: changes |
| 71 | + run: | |
| 72 | + if [[ -n $(git status -s) ]]; then |
| 73 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 74 | + else |
| 75 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 76 | + fi |
| 77 | +
|
| 78 | + - name: Create fix summary |
| 79 | + if: steps.changes.outputs.has_changes == 'true' |
| 80 | + run: | |
| 81 | + mkdir -p fix-reports |
| 82 | + git diff --stat > fix-reports/changes-${{ matrix.sub-package }}.txt |
| 83 | + |
| 84 | + echo "# Auto-fix Summary for ${{ matrix.sub-package }}" > fix-reports/summary-${{ matrix.sub-package }}.md |
| 85 | + echo "" >> fix-reports/summary-${{ matrix.sub-package }}.md |
| 86 | + echo "## Changes Made" >> fix-reports/summary-${{ matrix.sub-package }}.md |
| 87 | + echo "" >> fix-reports/summary-${{ matrix.sub-package }}.md |
| 88 | + echo "\`\`\`" >> fix-reports/summary-${{ matrix.sub-package }}.md |
| 89 | + git diff --stat >> fix-reports/summary-${{ matrix.sub-package }}.md |
| 90 | + echo "\`\`\`" >> fix-reports/summary-${{ matrix.sub-package }}.md |
| 91 | + echo "" >> fix-reports/summary-${{ matrix.sub-package }}.md |
| 92 | + echo "## Auto-fixes Applied" >> fix-reports/summary-${{ matrix.sub-package }}.md |
| 93 | + echo "- Removed unused imports and variables (autoflake)" >> fix-reports/summary-${{ matrix.sub-package }}.md |
| 94 | + echo "- Fixed PEP8 violations (autopep8)" >> fix-reports/summary-${{ matrix.sub-package }}.md |
| 95 | + echo "- Sorted imports (isort)" >> fix-reports/summary-${{ matrix.sub-package }}.md |
| 96 | + echo "- Applied code formatting (black)" >> fix-reports/summary-${{ matrix.sub-package }}.md |
| 97 | +
|
| 98 | + - name: Upload fix reports |
| 99 | + if: steps.changes.outputs.has_changes == 'true' |
| 100 | + uses: actions/upload-artifact@v4 |
| 101 | + with: |
| 102 | + name: fix-report-${{ matrix.sub-package }} |
| 103 | + path: fix-reports/ |
| 104 | + retention-days: 30 |
| 105 | + |
| 106 | + - name: Commit changes |
| 107 | + if: steps.changes.outputs.has_changes == 'true' && github.event.inputs.create_pr == 'true' |
| 108 | + run: | |
| 109 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 110 | + git config --local user.name "github-actions[bot]" |
| 111 | + git add . |
| 112 | + git commit -m "chore: auto-fix code issues in ${{ matrix.sub-package }}" |
| 113 | +
|
| 114 | + auto-fix-frontend: |
| 115 | + name: Auto-fix Frontend Code Issues |
| 116 | + runs-on: ubuntu-latest |
| 117 | + |
| 118 | + steps: |
| 119 | + - name: Checkout repository |
| 120 | + uses: actions/checkout@v4 |
| 121 | + with: |
| 122 | + ref: ${{ github.head_ref || github.ref }} |
| 123 | + |
| 124 | + - name: Set up Node.js |
| 125 | + uses: actions/setup-node@v4 |
| 126 | + with: |
| 127 | + node-version: '21' |
| 128 | + |
| 129 | + - name: Install dependencies |
| 130 | + working-directory: autogpt_platform/frontend |
| 131 | + run: yarn install --frozen-lockfile |
| 132 | + |
| 133 | + - name: Run ESLint auto-fix |
| 134 | + working-directory: autogpt_platform/frontend |
| 135 | + run: | |
| 136 | + yarn lint --fix || true |
| 137 | + yarn format || true |
| 138 | +
|
| 139 | + - name: Check for changes |
| 140 | + id: changes |
| 141 | + run: | |
| 142 | + if [[ -n $(git status -s) ]]; then |
| 143 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 144 | + else |
| 145 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 146 | + fi |
| 147 | +
|
| 148 | + - name: Create fix summary |
| 149 | + if: steps.changes.outputs.has_changes == 'true' |
| 150 | + run: | |
| 151 | + mkdir -p fix-reports |
| 152 | + git diff --stat > fix-reports/changes-frontend.txt |
| 153 | + |
| 154 | + echo "# Auto-fix Summary for Frontend" > fix-reports/summary-frontend.md |
| 155 | + echo "" >> fix-reports/summary-frontend.md |
| 156 | + echo "## Changes Made" >> fix-reports/summary-frontend.md |
| 157 | + echo "" >> fix-reports/summary-frontend.md |
| 158 | + echo "\`\`\`" >> fix-reports/summary-frontend.md |
| 159 | + git diff --stat >> fix-reports/summary-frontend.md |
| 160 | + echo "\`\`\`" >> fix-reports/summary-frontend.md |
| 161 | + echo "" >> fix-reports/summary-frontend.md |
| 162 | + echo "## Auto-fixes Applied" >> fix-reports/summary-frontend.md |
| 163 | + echo "- Fixed ESLint violations" >> fix-reports/summary-frontend.md |
| 164 | + echo "- Applied Prettier formatting" >> fix-reports/summary-frontend.md |
| 165 | +
|
| 166 | + - name: Upload fix reports |
| 167 | + if: steps.changes.outputs.has_changes == 'true' |
| 168 | + uses: actions/upload-artifact@v4 |
| 169 | + with: |
| 170 | + name: fix-report-frontend |
| 171 | + path: fix-reports/ |
| 172 | + retention-days: 30 |
| 173 | + |
| 174 | + - name: Commit changes |
| 175 | + if: steps.changes.outputs.has_changes == 'true' && github.event.inputs.create_pr == 'true' |
| 176 | + run: | |
| 177 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 178 | + git config --local user.name "github-actions[bot]" |
| 179 | + git add . |
| 180 | + git commit -m "chore: auto-fix frontend code issues" |
| 181 | +
|
| 182 | + create-pr: |
| 183 | + name: Create Pull Request with Auto-fixes |
| 184 | + needs: [auto-fix-python, auto-fix-frontend] |
| 185 | + runs-on: ubuntu-latest |
| 186 | + if: github.event.inputs.create_pr == 'true' |
| 187 | + |
| 188 | + steps: |
| 189 | + - name: Checkout repository |
| 190 | + uses: actions/checkout@v4 |
| 191 | + with: |
| 192 | + ref: ${{ github.head_ref || github.ref }} |
| 193 | + fetch-depth: 0 |
| 194 | + |
| 195 | + - name: Pull latest changes |
| 196 | + run: | |
| 197 | + git pull origin ${{ github.ref_name }} |
| 198 | +
|
| 199 | + - name: Create Pull Request |
| 200 | + uses: peter-evans/create-pull-request@v6 |
| 201 | + with: |
| 202 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 203 | + commit-message: 'chore: automated code quality fixes' |
| 204 | + title: '[Automated] Code Quality Improvements' |
| 205 | + body: | |
| 206 | + ## Automated Code Quality Improvements |
| 207 | + |
| 208 | + This PR contains automated fixes for code quality issues detected by our CI/CD pipeline. |
| 209 | + |
| 210 | + ### Changes Include: |
| 211 | + - ✅ Removed unused imports and variables |
| 212 | + - ✅ Fixed PEP8 violations in Python code |
| 213 | + - ✅ Applied consistent code formatting |
| 214 | + - ✅ Fixed ESLint violations in frontend code |
| 215 | + - ✅ Applied Prettier formatting to frontend |
| 216 | + |
| 217 | + ### Review Checklist: |
| 218 | + - [ ] Verify all auto-fixes are correct |
| 219 | + - [ ] Run tests to ensure no functionality is broken |
| 220 | + - [ ] Check that formatting improvements don't affect logic |
| 221 | + |
| 222 | + **This PR was automatically generated by the Automated Code Optimization workflow.** |
| 223 | + branch: automated-fixes-${{ github.run_number }} |
| 224 | + delete-branch: true |
| 225 | + labels: | |
| 226 | + automated |
| 227 | + code-quality |
| 228 | + chore |
| 229 | +
|
| 230 | + summarize-fixes: |
| 231 | + name: Summarize All Auto-fixes |
| 232 | + needs: [auto-fix-python, auto-fix-frontend] |
| 233 | + runs-on: ubuntu-latest |
| 234 | + if: always() |
| 235 | + |
| 236 | + steps: |
| 237 | + - name: Download all fix reports |
| 238 | + uses: actions/download-artifact@v4 |
| 239 | + with: |
| 240 | + path: all-fix-reports |
| 241 | + |
| 242 | + - name: Create comprehensive summary |
| 243 | + run: | |
| 244 | + echo "# Automated Code Optimization Summary" > optimization-summary.md |
| 245 | + echo "" >> optimization-summary.md |
| 246 | + echo "## Run Information" >> optimization-summary.md |
| 247 | + echo "- **Date**: $(date)" >> optimization-summary.md |
| 248 | + echo "- **Branch**: ${{ github.ref_name }}" >> optimization-summary.md |
| 249 | + echo "- **Workflow**: Automated Code Optimization" >> optimization-summary.md |
| 250 | + echo "" >> optimization-summary.md |
| 251 | + echo "## Auto-fixes Applied" >> optimization-summary.md |
| 252 | + echo "" >> optimization-summary.md |
| 253 | + echo "### Python Backend" >> optimization-summary.md |
| 254 | + echo "- Removed unused imports and variables" >> optimization-summary.md |
| 255 | + echo "- Fixed PEP8 violations" >> optimization-summary.md |
| 256 | + echo "- Applied isort and black formatting" >> optimization-summary.md |
| 257 | + echo "" >> optimization-summary.md |
| 258 | + echo "### Frontend" >> optimization-summary.md |
| 259 | + echo "- Fixed ESLint violations" >> optimization-summary.md |
| 260 | + echo "- Applied Prettier formatting" >> optimization-summary.md |
| 261 | + echo "" >> optimization-summary.md |
| 262 | + echo "## Next Steps" >> optimization-summary.md |
| 263 | + echo "" >> optimization-summary.md |
| 264 | + if [ "${{ github.event.inputs.create_pr }}" = "true" ]; then |
| 265 | + echo "✅ A pull request has been created with all auto-fixes" >> optimization-summary.md |
| 266 | + echo "" >> optimization-summary.md |
| 267 | + echo "Please review and merge the PR to apply these improvements." >> optimization-summary.md |
| 268 | + else |
| 269 | + echo "ℹ️ Auto-fixes have been identified but not applied" >> optimization-summary.md |
| 270 | + echo "" >> optimization-summary.md |
| 271 | + echo "To create a PR with auto-fixes, run this workflow manually with 'create_pr' set to 'true'." >> optimization-summary.md |
| 272 | + fi |
| 273 | + |
| 274 | + cat optimization-summary.md |
| 275 | +
|
| 276 | + - name: Upload optimization summary |
| 277 | + uses: actions/upload-artifact@v4 |
| 278 | + with: |
| 279 | + name: optimization-summary |
| 280 | + path: optimization-summary.md |
| 281 | + retention-days: 90 |
0 commit comments