-
Notifications
You must be signed in to change notification settings - Fork 446
Document regular expressions in sync_back.ts
#3547
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
mbg
wants to merge
1
commit into
main
Choose a base branch
from
mbg/ts/sync-back/feedback
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.
+73
−18
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -27,13 +27,79 @@ const CHECKS_DIR = path.join(THIS_DIR, "checks"); | |||||
| const WORKFLOW_DIR = path.join(THIS_DIR, "..", ".github", "workflows"); | ||||||
| const SYNC_TS_PATH = path.join(THIS_DIR, "sync.ts"); | ||||||
|
|
||||||
| /** | ||||||
| * Used to find action references (including versions and comments) in a workflow file. | ||||||
| * | ||||||
| * This pattern captures `action_name` and `version_with_possible_comment` from | ||||||
| * `uses: action_name@version_with_possible_comment`. For example, if we have | ||||||
| * | ||||||
| * ``` | ||||||
| * uses: ruby/setup-ruby@09a7688d3b55cf0e976497ff046b70949eeaccfd # v1.288.0 | ||||||
| * ``` | ||||||
| * | ||||||
| * in a workflow file, this regular expression gets us: | ||||||
| * | ||||||
| * - `ruby/setup-ruby`; and | ||||||
| * - `09a7688d3b55cf0e976497ff046b70949eeaccfd # v1.288.0`. | ||||||
| */ | ||||||
| const EXTRACT_ACTION_REF_PATTERN: RegExp = | ||||||
| /uses:\s+([^/\s]+\/[^@\s]+)@([^@\n]+)/g; | ||||||
|
|
||||||
| /** | ||||||
| * Used to identify characters in `action_name` strings that need to | ||||||
| * be escaped before inserting them into TypeScript or YAML strings. | ||||||
| */ | ||||||
| const ESCAPE_PATTERN = /[.*+?^${}()|[\]\\]/g; | ||||||
|
|
||||||
| /** | ||||||
| * A `SyncBackPattern` is a function which constructs a regular expression for a specific `actionName`, | ||||||
| * which finds references to `actionName` and surrounding context in a particular file that we want | ||||||
| * to sync updated versions back to. | ||||||
| */ | ||||||
| type SyncBackPattern = (actionName: string) => RegExp; | ||||||
|
|
||||||
| /** | ||||||
| * Used to find lines containing action references in `sync.ts`. | ||||||
| * | ||||||
| * Matches `uses: "actionName@version_str"` in PR check specifications and groups `uses: "` | ||||||
| * and `"`, allowing `actionName@version_str` to be replaced with a new action reference. | ||||||
| */ | ||||||
| const TS_PATTERN: SyncBackPattern = (actionName: string) => | ||||||
| new RegExp(`(uses:\\s*")${actionName}@(?:[^"]+)(")`, "g"); | ||||||
mbg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| /** | ||||||
| * Used to find lines containing action references in a PR check specification. | ||||||
| * | ||||||
| * Matches `uses: actionName@rest_of_line` in PR check specifications and extracts `uses: actionName`, | ||||||
| * allowing `rest_of_line` to be replaced with a new version string. | ||||||
| */ | ||||||
| const YAML_PATTERN: SyncBackPattern = (actionName: string) => | ||||||
| new RegExp(`(uses:\\s+${actionName})@(?:[^@\n]+)`, "g"); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, we can probably expect this to be the start of the trimmed line.
Suggested change
|
||||||
|
|
||||||
| /** | ||||||
| * Constructs a regular expression using `patternFunction` for `actionName`, which is sanitised | ||||||
| * before `patternFunction` is called. | ||||||
| * | ||||||
| * @param patternFunction The pattern builder to use. | ||||||
| * @param actionName The action name, which will be sanitised. | ||||||
| * @returns The regular expression returned by `patternFunction`. | ||||||
| */ | ||||||
| function makeReplacementPattern( | ||||||
| patternFunction: SyncBackPattern, | ||||||
| actionName: string, | ||||||
| ): RegExp { | ||||||
| return patternFunction(actionName.replace(ESCAPE_PATTERN, "\\$&")); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Scan generated workflow files to extract the latest action versions. | ||||||
| * | ||||||
| * @param workflowDir - Path to .github/workflows directory | ||||||
| * @returns Map from action names to their latest versions (including comments) | ||||||
| */ | ||||||
| export function scanGeneratedWorkflows(workflowDir: string): Record<string, string> { | ||||||
| export function scanGeneratedWorkflows( | ||||||
| workflowDir: string, | ||||||
| ): Record<string, string> { | ||||||
| const actionVersions: Record<string, string> = {}; | ||||||
|
|
||||||
| const generatedFiles = fs | ||||||
|
|
@@ -43,13 +109,10 @@ export function scanGeneratedWorkflows(workflowDir: string): Record<string, stri | |||||
|
|
||||||
| for (const filePath of generatedFiles) { | ||||||
| const content = fs.readFileSync(filePath, "utf8"); | ||||||
|
|
||||||
| // Find all action uses in the file, including potential comments | ||||||
| // This pattern captures: action_name@version_with_possible_comment | ||||||
| const pattern = /uses:\s+([^/\s]+\/[^@\s]+)@([^@\n]+)/g; | ||||||
| let match: RegExpExecArray | null; | ||||||
|
|
||||||
| while ((match = pattern.exec(content)) !== null) { | ||||||
| EXTRACT_ACTION_REF_PATTERN.lastIndex = 0; | ||||||
| while ((match = EXTRACT_ACTION_REF_PATTERN.exec(content)) !== null) { | ||||||
| const actionName = match[1]; | ||||||
mbg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| const versionWithComment = match[2].trimEnd(); | ||||||
|
|
||||||
|
|
@@ -91,15 +154,11 @@ export function updateSyncTs( | |||||
| ? versionWithComment.split("#")[0].trim() | ||||||
| : versionWithComment.trim(); | ||||||
|
|
||||||
| // Look for patterns like uses: "actions/setup-node@v4" | ||||||
| // Update uses of `actionName` for `version`. | ||||||
| // Note that this will break if we store an Action uses reference in a | ||||||
| // variable - that's a risk we're happy to take since in that case the | ||||||
| // PR checks will just fail. | ||||||
| const escaped = actionName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||||||
| const pattern = new RegExp( | ||||||
| `(uses:\\s*")${escaped}@(?:[^"]+)(")`, | ||||||
| "g", | ||||||
| ); | ||||||
| const pattern = makeReplacementPattern(TS_PATTERN, actionName); | ||||||
| content = content.replace(pattern, `$1${actionName}@${version}$2`); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -139,12 +198,8 @@ export function updateTemplateFiles( | |||||
| for (const [actionName, versionWithComment] of Object.entries( | ||||||
| actionVersions, | ||||||
| )) { | ||||||
| // Look for patterns like 'uses: actions/setup-node@v4' or 'uses: actions/setup-node@sha # comment' | ||||||
| const escaped = actionName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||||||
| const pattern = new RegExp( | ||||||
| `(uses:\\s+${escaped})@(?:[^@\n]+)`, | ||||||
| "g", | ||||||
| ); | ||||||
| // Update uses of `actionName` for `versionWithComment`. | ||||||
| const pattern = makeReplacementPattern(YAML_PATTERN, actionName); | ||||||
| content = content.replace(pattern, `$1@${versionWithComment}`); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
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.
Should we maybe anchor it such that we expect it to be at the start of the line?
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.
Also, it's probably a minor thing, but in terms of YAML semantics, the user could have added quotes around the right-hand side. Meaning that the captured content will contain surrounding quotes.
I'm thinking we could improve robustness against this if we use the current regular expression to find interesting lines, and then reparse them as standalone YAML. We can then take the right-hand side value instead of the right-hand side syntactic definition.