-
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
Open
SteinGabriel
wants to merge
1
commit into
main
Choose a base branch
from
copyright-date-automation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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,172 @@ | ||
| #!/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'); | ||
| 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, (_, prefix, startYear, separator, endYear, suffix) => { | ||
| 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; | ||
|
|
||
| for (const line of lines) { | ||
| if (!MAYBE_PING_COPYRIGHT_LINE_REGEX.test(line) || !HEADER_COMMENT_LINE_REGEX.test(line)) { | ||
| continue; | ||
| } | ||
| present = true; | ||
| if (!VALID_PING_COPYRIGHT_LINE_REGEX.test(line)) { | ||
| invalid = true; | ||
| } | ||
| } | ||
|
|
||
| return { present, invalid }; | ||
| } | ||
|
|
||
| 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.
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.
This is almost 200 lines of code. It seems like we have separate logic for --check and --fix. We can combine those two and only apply --fix with
updateCopyrightYears. We have to perform the same validation whether we call the --check or --fix, the only difference is with --fix we also need to callupdateCopyrightYears.Some code optimizations:
Merge hasPingCopyrightHeader (171–179) into hasInvalidPingCopyrightHeader (155–168)
Both functions iterate lines and test MAYBE_PING_COPYRIGHT_LINE_REGEX + HEADER_COMMENT_LINE_REGEX. The only difference: one returns true when the header is present, the other returns true when it's present but invalid. One function returning { present, valid } replaces both.
Collapse three loops (22–35, 46–58, 60–69) into one
Right now run() builds stagedFileData in the first loop, then loops over it again for missing headers, then again for year updates. All three can run in a single for loop — read the file, check invalid format, check missing header, compute updated content — then branch once at the end based on checkOnly.
Inline isFile (112–118) and safeReadUtf8 (120–126)
Each is called exactly once. isFile is 3 lines of real logic, safeReadUtf8 is 3 lines. Not worth a named function.
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.
Good call! I applied the changes you suggested. Merged the two header-check helpers into one
inspectPingCopyrightHeaderthat returns{ present, invalid }and collapsed the three loops into one.Thanks for the suggestions!