-
Notifications
You must be signed in to change notification settings - Fork 3
chore(copyright): add sync-header-years script and tests #639
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,201 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { execFileSync } from 'node:child_process'; | ||
| import { readFileSync, statSync, writeFileSync } from 'node:fs'; | ||
| import { resolve } from 'node:path'; | ||
| import { pathToFileURL } from 'node:url'; | ||
|
|
||
| function isCliExecution() { | ||
| return process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href; | ||
| } | ||
|
|
||
| function run() { | ||
| const args = new Set(process.argv.slice(2)); | ||
| const checkOnly = args.has('--check'); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| const fixMode = args.has('--fix'); | ||
|
|
||
| if (!checkOnly && !fixMode) { | ||
| console.error('Usage: sync-header-years.mjs --check | --fix'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const currentYear = new Date().getFullYear(); | ||
|
|
||
| const stagedFileData = []; | ||
| const invalidFiles = []; | ||
| const missingHeaderFiles = []; | ||
| const changedFiles = []; | ||
|
|
||
| for (const file of getStagedFiles()) { | ||
| if (isExcluded(file)) { | ||
| continue; | ||
| } | ||
|
|
||
| let original; | ||
| try { | ||
| if (!statSync(resolve(process.cwd(), file)).isFile()) { | ||
| continue; | ||
| } | ||
| original = readFileSync(resolve(process.cwd(), file), 'utf8'); | ||
| } catch { | ||
| continue; | ||
| } | ||
|
|
||
| const absolutePath = resolve(process.cwd(), file); | ||
| const { present, invalid } = inspectPingCopyrightHeader(original); | ||
|
|
||
| if (invalid) { | ||
| invalidFiles.push(file); | ||
| } | ||
| if (SOURCE_FILE_PATTERN.test(file) && !present) { | ||
| missingHeaderFiles.push(file); | ||
| } | ||
|
|
||
| const updated = updateCopyrightYears(original, currentYear); | ||
| if (updated !== original) { | ||
| changedFiles.push(file); | ||
| } | ||
| stagedFileData.push({ file, absolutePath, updated }); | ||
| } | ||
|
|
||
| if (invalidFiles.length > 0) { | ||
| console.error('Invalid Ping copyright header year format in staged files:'); | ||
| for (const file of invalidFiles) { | ||
| console.error(`- ${file}`); | ||
| } | ||
| process.exit(1); | ||
| } | ||
|
|
||
| if (checkOnly) { | ||
| if (missingHeaderFiles.length > 0) { | ||
| console.error('Missing Ping copyright header in staged files:'); | ||
| for (const file of missingHeaderFiles) { | ||
| console.error(`- ${file}`); | ||
| } | ||
| process.exit(1); | ||
| } | ||
| if (changedFiles.length > 0) { | ||
| console.error('Stale Ping copyright years found in staged files:'); | ||
| for (const file of changedFiles) { | ||
| console.error(`- ${file}`); | ||
| } | ||
| process.exit(1); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| for (const { file, absolutePath, updated } of stagedFileData) { | ||
| if (changedFiles.includes(file)) { | ||
| writeFileSync(absolutePath, updated, 'utf8'); | ||
| } | ||
| } | ||
|
|
||
| if (changedFiles.length > 0) { | ||
| execFileSync('git', ['add', '--', ...changedFiles], { stdio: 'inherit' }); | ||
| } | ||
| } | ||
|
|
||
| function getStagedFiles() { | ||
| const output = execFileSync('git', ['diff', '--cached', '--name-only', '--diff-filter=ACMR'], { | ||
| encoding: 'utf8', | ||
| }).trim(); | ||
|
|
||
| if (!output) { | ||
| return []; | ||
| } | ||
| return output.split('\n').filter(Boolean); | ||
| } | ||
|
|
||
| export function isExcluded(filePath) { | ||
| return EXCLUDE_PATTERNS.some((pattern) => pattern.test(filePath)); | ||
| } | ||
|
|
||
| const EXCLUDE_PATTERNS = [ | ||
| /(^|[/\\])dist[/\\]/, | ||
| /(^|[/\\])vendor[/\\]/, | ||
| /(^|[/\\])node_modules[/\\]/, | ||
| /(^|[/\\])tools[/\\]/, | ||
| /(^|[/\\])_polyfills[/\\]/, | ||
| /(^|[/\\])vite[^/\\]*\.config\.[cm]?[jt]sx?$/i, | ||
| /(^|[/\\])vitest\.setup\.[cm]?[jt]sx?$/i, | ||
| /(^|[/\\])playwright\.config\.[cm]?[jt]sx?$/i, | ||
| ]; | ||
|
|
||
| export function updateCopyrightYears(content, year) { | ||
| const regex = | ||
| /(^.*(?:©\s*|©\s*)?Copyright(?:\s*\(c\))?\s+)(\d{4})(?:([ \t]*-[ \t]*)(\d{4}))?(\s+Ping Identity(?: Corporation)?\b.*$)/gim; | ||
|
|
||
| return content.replace(regex, (match, prefix, startYear, separator, endYear, suffix) => { | ||
| if (!HEADER_COMMENT_LINE_REGEX.test(prefix)) { | ||
| return match; | ||
| } | ||
|
|
||
| const start = Number.parseInt(startYear, 10); | ||
| const end = endYear ? Number.parseInt(endYear, 10) : start; | ||
|
|
||
| if (Number.isNaN(start) || Number.isNaN(end)) { | ||
| return `${prefix}${startYear}${endYear ? `${separator}${endYear}` : ''}${suffix}`; | ||
| } | ||
|
|
||
| const resolvedEnd = end >= year ? end : year; | ||
|
|
||
| if (!endYear) { | ||
| // Single year already current — no range needed | ||
| if (resolvedEnd === start) { | ||
| return `${prefix}${startYear}${suffix}`; | ||
| } | ||
| return `${prefix}${startYear} - ${resolvedEnd}${suffix}`; | ||
| } | ||
|
|
||
| // Always normalize separator to ' - ' and bump end year when stale | ||
| return `${prefix}${startYear} - ${resolvedEnd}${suffix}`; | ||
| }); | ||
| } | ||
|
|
||
| export function inspectPingCopyrightHeader(content) { | ||
| const lines = content.split(/\r?\n/); | ||
| let present = false; | ||
| let invalid = false; | ||
| let inBlockComment = false; | ||
|
|
||
| for (const line of lines) { | ||
| const trimmed = line.trim(); | ||
| if (trimmed === '') { | ||
| continue; | ||
| } | ||
|
|
||
| const isHeaderLine = inBlockComment || HEADER_COMMENT_LINE_REGEX.test(line); | ||
| if (!isHeaderLine) { | ||
| break; | ||
| } | ||
|
|
||
| if (/^\/\*/.test(trimmed)) { | ||
| inBlockComment = true; | ||
| } | ||
| if (inBlockComment && /\*\//.test(trimmed)) { | ||
| inBlockComment = false; | ||
| } | ||
|
|
||
| if (!MAYBE_PING_COPYRIGHT_LINE_REGEX.test(line)) { | ||
| continue; | ||
| } | ||
| present = true; | ||
| if (!VALID_PING_COPYRIGHT_LINE_REGEX.test(line)) { | ||
| invalid = true; | ||
| } | ||
| } | ||
|
|
||
| return { present, invalid }; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const SOURCE_FILE_PATTERN = /\.[cm]?[jt]sx?$/i; | ||
|
|
||
| const MAYBE_PING_COPYRIGHT_LINE_REGEX = | ||
| /(?:©\s*|©\s*)?Copyright(?:\s*\(c\))?.*Ping Identity(?: Corporation)?/i; | ||
| const HEADER_COMMENT_LINE_REGEX = /^\s*(?:\/\*+|\*+|\/\/+|#+|<!--)\s*/; | ||
| const VALID_PING_COPYRIGHT_LINE_REGEX = | ||
| /^.*(?:©\s*|©\s*)?Copyright(?:\s*\(c\))?\s+\d{4}(?:[ \t]*-[ \t]*\d{4})?\s+Ping Identity(?: Corporation)?\b.*$/i; | ||
|
|
||
| if (isCliExecution()) { | ||
| run(); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.