Merge pull request #183 from Atharva0506/feature/settings-page #3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Test and Build | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - 'frontend/**' | |
| pull_request: | |
| paths: | |
| - 'frontend/**' | |
| permissions: | |
| pull-requests: write | |
| contents: read | |
| jobs: | |
| test-and-build: | |
| runs-on: ubuntu-latest | |
| defaults: | |
| run: | |
| working-directory: ./frontend | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| cache: 'npm' | |
| cache-dependency-path: './frontend/package.json' | |
| - name: Install dependencies | |
| run: npm install | |
| - name: Run lint | |
| run: npm run lint | |
| - name: Run tests with coverage | |
| id: test | |
| continue-on-error: true | |
| run: | | |
| npm run test:ci > test_output.log 2>&1 || echo "TESTS_FAILED=true" >> $GITHUB_ENV | |
| cat test_output.log | |
| - name: Handle Test Failure | |
| if: env.TESTS_FAILED == 'true' && github.event_name == 'pull_request' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| let testOutput = ''; | |
| try { | |
| testOutput = fs.readFileSync('./frontend/test_output.log', 'utf8'); | |
| if (testOutput.length > 60000) { | |
| testOutput = testOutput.substring(testOutput.length - 60000); | |
| } | |
| } catch (e) { | |
| testOutput = 'Could not read test output.'; | |
| } | |
| await github.rest.issues.addLabels({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| labels: ['test case failing'] | |
| }); | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body: `⚠️ **Tests failed!**\n\n<details><summary>Test Output</summary>\n\n\`\`\`\n${testOutput}\n\`\`\`\n</details>` | |
| }); | |
| - name: Fail if tests failed | |
| if: env.TESTS_FAILED == 'true' | |
| run: exit 1 | |
| - name: Remove test failure label (if passed) | |
| if: env.TESTS_FAILED != 'true' && github.event_name == 'pull_request' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| try { | |
| await github.rest.issues.removeLabel({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| name: 'test case failing' | |
| }); | |
| } catch (e) { | |
| // Ignore if label doesn't exist | |
| } | |
| - name: Run build | |
| id: build | |
| continue-on-error: true | |
| run: | | |
| npm run build > build_output.log 2>&1 || echo "BUILD_FAILED=true" >> $GITHUB_ENV | |
| cat build_output.log | |
| - name: Handle Build Failure | |
| if: env.BUILD_FAILED == 'true' && github.event_name == 'pull_request' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| await github.rest.issues.addLabels({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| labels: ['build failed'] | |
| }); | |
| - name: Report Build Size | |
| if: env.BUILD_FAILED != 'true' && github.event_name == 'pull_request' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| let buildOutput = ''; | |
| try { | |
| buildOutput = fs.readFileSync('./frontend/build_output.log', 'utf8'); | |
| const lines = buildOutput.split('\n'); | |
| const metrics = lines.filter(line => line.includes('dist/assets/')).join('\n'); | |
| buildOutput = metrics || 'Metrics not found'; | |
| } catch (e) { | |
| buildOutput = 'Could not read build output.'; | |
| } | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body: `✅ **Build successful!**\n\n<details><summary>Build Size Metrics</summary>\n\n\`\`\`\n${buildOutput}\n\`\`\`\n</details>` | |
| }); | |
| - name: Fail if build failed | |
| if: env.BUILD_FAILED == 'true' | |
| run: exit 1 |