-
Notifications
You must be signed in to change notification settings - Fork 0
Feat ci/slug checker #15
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
Merged
Changes from all commits
Commits
Show all changes
2 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
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,25 @@ | ||
| name: Slug Check | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [main] | ||
|
|
||
| jobs: | ||
| slug-check: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup pnpm | ||
| uses: pnpm/action-setup@v4 | ||
| with: | ||
| version: 10 | ||
|
|
||
| - uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: '22' | ||
| cache: pnpm | ||
|
|
||
| - run: pnpm install --frozen-lockfile | ||
| - run: pnpm check:slugs | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #!/usr/bin/env node | ||
| import { promises as fs } from 'fs' | ||
| import path from 'path' | ||
| import matter from 'gray-matter' | ||
| import { SUPPORTED_LANGS, LANGUAGES } from '../src/config.mjs' | ||
|
|
||
| const RESERVED_SLUGS = { | ||
| tr: new Set(['articles', 'categories', 'tags', '404']), | ||
| en: new Set(['en', 'en/articles', 'en/categories', 'en/tags', 'en/404']), | ||
| } | ||
|
|
||
| let hasErrors = false | ||
|
|
||
| const __dirname = new URL('.', import.meta.url).pathname | ||
| const repoRoot = path.resolve(__dirname, '..') | ||
|
|
||
| ;(async () => { | ||
| for (const lang of SUPPORTED_LANGS) { | ||
| const contentDir = path.join(repoRoot, 'src/content/articles', lang) | ||
| console.log(`Checking slugs for language: ${lang}`) | ||
|
|
||
| let mdFiles | ||
| try { | ||
| mdFiles = (await fs.readdir(contentDir)).filter((f) => f.endsWith('.md') || f.endsWith('.mdx')) | ||
| } catch { | ||
| console.log(` No content directory for language '${lang}': ${contentDir}`) | ||
| console.log(` Skipping.`) | ||
| continue | ||
| } | ||
|
|
||
| const slugMap = new Map() | ||
| const reserved = RESERVED_SLUGS[lang] || new Set() | ||
| const langPrefix = LANGUAGES[lang]?.prefix || '' | ||
|
|
||
| for (const file of mdFiles) { | ||
| const filePath = path.join(contentDir, file) | ||
| const raw = await fs.readFile(filePath, 'utf8') | ||
| const { data } = matter(raw) | ||
|
|
||
| const slug = data.slug?.trim() | ||
| if (!slug) { | ||
| console.log(` ✗ ${file} — MISSING slug field in frontmatter`) | ||
| hasErrors = true | ||
| continue | ||
| } | ||
|
|
||
| if (reserved.has(slug) || (langPrefix && slug.startsWith(langPrefix.slice(1) + '/'))) { | ||
| console.log(` ✗ ${file} — RESERVED slug '${slug}' (conflicts with static route)`) | ||
| hasErrors = true | ||
| continue | ||
| } | ||
|
|
||
| if (slugMap.has(slug)) { | ||
| const original = slugMap.get(slug) | ||
| console.log(` ✗ ${file} — DUPLICATE slug '${slug}' (already used by ${original})`) | ||
| hasErrors = true | ||
| } else { | ||
| slugMap.set(slug, file) | ||
| console.log(` ✓ ${slug} — slug OK`) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (hasErrors) { | ||
| console.log('\nFound slug errors. Exiting with code 1.') | ||
| process.exit(1) | ||
| } else { | ||
| console.log('\nAll slugs are valid.') | ||
| process.exit(0) | ||
| } | ||
| })() |
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.
Check warning
Code scanning / CodeQL
Workflow does not contain permissions Medium
Copilot Autofix
AI 2 months ago
To fix the problem, we should explicitly restrict the
GITHUB_TOKENpermissions for this workflow to the minimum needed. The job only checks code (checkout, install, runpnpm check:slugs) and does not need to write to the repository or modify issues, PRs, or other resources. The minimal appropriate permission iscontents: read. We can declare this either at the workflow root (applies to all jobs) or within the specificslug-checkjob. Since there is only one job, either is fine; adding it at the workflow root is slightly clearer and follows the recommendation pattern.Concretely, in
.github/workflows/slug-check.yml, add apermissions:block withcontents: read(and nothing else) near the top of the file, at the root level, alongsidenameandon. This will ensure theGITHUB_TOKENused byactions/checkoutis limited to read-only access to repository contents, satisfying the CodeQL rule without changing any existing functionality of the workflow.