-
Notifications
You must be signed in to change notification settings - Fork 435
feat: wildcard support for allowed-repos and standardize target-repo helpers #17280
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
| * Provides common repository parsing, validation, and resolution logic | ||
| */ | ||
|
|
||
| const { globPatternToRegex } = require("./glob_pattern_helpers.cjs"); | ||
|
|
||
| /** | ||
| * Parse the allowed repos from config value (array or comma-separated string) | ||
| * @param {string[]|string|undefined} allowedReposValue - Allowed repos from config (array or comma-separated string) | ||
|
|
@@ -47,14 +49,42 @@ function getDefaultTargetRepo(config) { | |
| return `${context.repo.owner}/${context.repo.repo}`; | ||
| } | ||
|
|
||
| /** | ||
| * Check if a qualified repo matches any allowed repo pattern. | ||
| * Supports exact matches and wildcard patterns using glob syntax: | ||
| * - "*" matches any repository | ||
| * - "github/*" matches any repository in the "github" org | ||
| * - "STAR/gh-aw" (where STAR is *) matches "gh-aw" in any org | ||
| * @param {string} qualifiedRepo - Fully qualified repo slug "owner/repo" | ||
| * @param {Set<string>} allowedRepos - Set of allowed repo patterns | ||
| * @returns {boolean} | ||
| */ | ||
| function isRepoAllowed(qualifiedRepo, allowedRepos) { | ||
| // Fast path: exact match | ||
| if (allowedRepos.has(qualifiedRepo)) { | ||
| return true; | ||
| } | ||
| // Check for wildcard patterns | ||
| for (const pattern of allowedRepos) { | ||
| if (pattern === "*") { | ||
| return true; | ||
| } | ||
| if (pattern.includes("*") && globPatternToRegex(pattern, { pathMode: true, caseSensitive: true }).test(qualifiedRepo)) { | ||
|
||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Validate that a repo is allowed for operations | ||
| * If repo is a bare name (no slash), it is automatically qualified with the | ||
| * default repo's organization (e.g., "gh-aw" becomes "github/gh-aw" if | ||
| * the default repo is "github/something"). | ||
| * Allowed repos support wildcard patterns (e.g., "github/*", "*"). | ||
| * @param {string} repo - Repository slug to validate (can be "owner/repo" or just "repo") | ||
| * @param {string} defaultRepo - Default target repository | ||
| * @param {Set<string>} allowedRepos - Set of explicitly allowed repos | ||
| * @param {Set<string>} allowedRepos - Set of explicitly allowed repo patterns | ||
| * @returns {{valid: boolean, error: string|null, qualifiedRepo: string}} | ||
| */ | ||
| function validateRepo(repo, defaultRepo, allowedRepos) { | ||
|
|
@@ -71,8 +101,8 @@ function validateRepo(repo, defaultRepo, allowedRepos) { | |
| if (qualifiedRepo === defaultRepo) { | ||
| return { valid: true, error: null, qualifiedRepo }; | ||
| } | ||
| // Check if it's in the allowed repos list | ||
| if (allowedRepos.has(qualifiedRepo)) { | ||
| // Check if it's in the allowed repos list (supports wildcards) | ||
| if (isRepoAllowed(qualifiedRepo, allowedRepos)) { | ||
| return { valid: true, error: null, qualifiedRepo }; | ||
| } | ||
| return { | ||
|
|
@@ -159,6 +189,7 @@ function resolveAndValidateRepo(item, defaultTargetRepo, allowedRepos, operation | |
| module.exports = { | ||
| parseAllowedRepos, | ||
| getDefaultTargetRepo, | ||
| isRepoAllowed, | ||
| validateRepo, | ||
| parseRepoSlug, | ||
| resolveTargetRepoConfig, | ||
|
|
||
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.
The documentation comment uses "STAR" as a placeholder instead of the actual asterisk character. This is unnecessarily confusing. The comment should directly show the pattern as "*/gh-aw" to match how the other patterns are documented on lines 55-56.