-
Notifications
You must be signed in to change notification settings - Fork 0
piech.dev badges #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
piech.dev badges #33
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import fs from 'node:fs/promises'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| type CoverageMetric = { | ||
| covered: number; | ||
| total: number; | ||
| }; | ||
|
|
||
| type CoverageSummary = { | ||
| total?: { | ||
| lines?: CoverageMetric; | ||
| }; | ||
| }; | ||
|
|
||
| type BadgePayload = { | ||
| color: string; | ||
| label: string; | ||
| message: string; | ||
| schemaVersion: 1; | ||
| }; | ||
|
|
||
| const currentDirectory = path.dirname(fileURLToPath(import.meta.url)); | ||
| const repoRoot = path.resolve(currentDirectory, '..', '..'); | ||
| const badgeDataDirectory = path.resolve(repoRoot, '.github', 'badge-data'); | ||
| const coverageSummaryPath = path.resolve( | ||
| repoRoot, | ||
| 'coverage', | ||
| 'coverage-summary.json', | ||
| ); | ||
|
|
||
| const readJsonFile = async <T,>(filePath: string): Promise<T> => | ||
| JSON.parse(await fs.readFile(filePath, 'utf8')) as T; | ||
|
|
||
| const createBadgePayload = ( | ||
| label: string, | ||
| message: string, | ||
| color: string, | ||
| ): BadgePayload => ({ | ||
| color, | ||
| label, | ||
| message, | ||
| schemaVersion: 1, | ||
| }); | ||
|
|
||
| const resolveCoverageColor = (coveragePercent: number): string => { | ||
| if (coveragePercent >= 90) { | ||
| return 'brightgreen'; | ||
| } | ||
|
|
||
| if (coveragePercent >= 80) { | ||
| return 'green'; | ||
| } | ||
|
|
||
| if (coveragePercent >= 70) { | ||
| return 'yellowgreen'; | ||
| } | ||
|
|
||
| if (coveragePercent >= 60) { | ||
| return 'yellow'; | ||
| } | ||
|
|
||
| if (coveragePercent >= 50) { | ||
| return 'orange'; | ||
| } | ||
|
|
||
| return 'red'; | ||
| }; | ||
|
|
||
| const formatCoveragePercent = (coveragePercent: number): string => { | ||
| const roundedPercent = Math.round(coveragePercent * 10) / 10; | ||
|
|
||
| return `${roundedPercent.toFixed(1).replace(/\.0$/, '')}%`; | ||
| }; | ||
|
|
||
| const readCoveragePercent = async (): Promise<number> => { | ||
| const coverageSummary = | ||
| await readJsonFile<CoverageSummary>(coverageSummaryPath); | ||
| const lines = coverageSummary.total?.lines; | ||
|
|
||
| if (!lines) { | ||
| throw new Error( | ||
| `Coverage summary at ${coverageSummaryPath} is missing total line metrics.`, | ||
| ); | ||
| } | ||
|
|
||
| if (lines.total === 0) { | ||
| throw new Error('Coverage total is zero.'); | ||
| } | ||
|
|
||
| return (lines.covered / lines.total) * 100; | ||
| }; | ||
|
|
||
| const readNodeVersion = async (): Promise<string> => { | ||
| const nvmrcPath = path.resolve(repoRoot, '.nvmrc'); | ||
| const nodeVersion = (await fs.readFile(nvmrcPath, 'utf8')).trim(); | ||
|
|
||
| if (!nodeVersion) { | ||
| throw new Error('.nvmrc is empty.'); | ||
| } | ||
|
|
||
| return nodeVersion.replace(/^v/i, ''); | ||
| }; | ||
|
|
||
| const writeBadgeFile = async ( | ||
| fileName: string, | ||
| payload: BadgePayload, | ||
| ): Promise<void> => { | ||
| await fs.writeFile( | ||
| path.resolve(badgeDataDirectory, fileName), | ||
| `${JSON.stringify(payload, null, 4)}\n`, | ||
| 'utf8', | ||
| ); | ||
| }; | ||
|
|
||
| const main = async (): Promise<void> => { | ||
| await fs.mkdir(badgeDataDirectory, { | ||
| recursive: true, | ||
| }); | ||
|
|
||
| const coveragePercent = await readCoveragePercent(); | ||
| const nodeVersion = await readNodeVersion(); | ||
|
|
||
| await Promise.all([ | ||
| writeBadgeFile( | ||
| 'coverage.json', | ||
| createBadgePayload( | ||
| 'coverage', | ||
| formatCoveragePercent(coveragePercent), | ||
| resolveCoverageColor(coveragePercent), | ||
| ), | ||
| ), | ||
| writeBadgeFile( | ||
| 'node.json', | ||
| createBadgePayload('node', nodeVersion, '5FA04E'), | ||
| ), | ||
| ]); | ||
| }; | ||
|
|
||
| void main(); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| name: readme badges | ||
|
|
||
| on: | ||
| push: | ||
| branches: [master, main] | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: write | ||
|
|
||
| jobs: | ||
| publish-badge-data: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - uses: actions/setup-node@v5 | ||
| with: | ||
| node-version-file: '.nvmrc' | ||
| cache: 'npm' | ||
|
|
||
| - name: install dependencies | ||
| run: npm ci | ||
|
|
||
| - name: generate badge data | ||
| run: npm run coverage:ci | ||
|
|
||
| - name: upload badge data | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: badge-data | ||
| path: | | ||
| coverage/coverage-summary.json | ||
| .github/badge-data | ||
| if-no-files-found: error | ||
|
|
||
| - name: publish badge data branch | ||
| env: | ||
| GITHUB_TOKEN: ${{ github.token }} | ||
| run: | | ||
| rm -rf badge-data-publish | ||
| mkdir -p badge-data-publish | ||
| cp -r .github/badge-data/. badge-data-publish/ | ||
| cd badge-data-publish | ||
| git init | ||
| git checkout -b badge-data | ||
| git config user.name "github-actions[bot]" | ||
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | ||
| git add --all | ||
| git commit -m "Update readme badge data" | ||
| git remote add origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" | ||
| git push --force origin badge-data |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ test-results/ | |
| *concatenated.txt | ||
| .react-router/ | ||
| .claude/ | ||
| .github/badge-data/ | ||
|
|
||
| # env | ||
| .env | ||
|
|
||
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
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.badgeImagewon’t override the existing.markdownContainer imgstyles formarginandborder-radiusbecause.markdownContainer imghas higher selector specificity (class + element) than.badgeImage(class only). As a result, badge images will still inherit the 0.5rem top margin and 4px border radius from the container rule. Consider scoping the generic image rule to.markdownImageinstead, or increase specificity for the badge rule (e.g.,.markdownContainer img.badgeImage/.markdownContainer .badgeImage) so the badge overrides actually take effect.