-
Notifications
You must be signed in to change notification settings - Fork 33
Npm plugin loading #242
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
base: main
Are you sure you want to change the base?
Npm plugin loading #242
Changes from all commits
671de5c
9a9e932
c451932
6aad92b
4488f96
59a5adc
fdafec3
454be60
4cadf62
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 |
|---|---|---|
|
|
@@ -3,7 +3,8 @@ import * as path from 'path' | |
| import {fileURLToPath} from 'url' | ||
| import * as core from '@actions/core' | ||
| import {loadPluginViaJsFile, loadPluginViaTsFile} from './pluginFileLoaders.js' | ||
| import type {Plugin, PluginDefaultParams} from './types.js' | ||
| import {loadPluginViaNpm} from './pluginNpmLoader.js' | ||
| import type {NpmPluginRequest, Plugin, PluginDefaultParams} from './types.js' | ||
|
|
||
| // Helper to get __dirname equivalent in ES Modules | ||
| const __filename = fileURLToPath(import.meta.url) | ||
|
|
@@ -20,12 +21,13 @@ export function getPlugins() { | |
| } | ||
| let pluginsLoaded = false | ||
|
|
||
| export async function loadPlugins() { | ||
| export async function loadPlugins(npmPlugins: NpmPluginRequest[] = []) { | ||
| try { | ||
| if (!pluginsLoaded) { | ||
| core.info('loading plugins') | ||
| await loadBuiltInPlugins() | ||
| await loadCustomPlugins() | ||
| await loadNpmPlugins(npmPlugins) | ||
| } | ||
| } catch { | ||
| plugins.length = 0 | ||
|
|
@@ -47,6 +49,16 @@ export function clearCache() { | |
| plugins.length = 0 | ||
| } | ||
|
|
||
| // True when the object is a usable plugin (exposes a name and default export). | ||
| function isValidPlugin(plugin: Plugin | undefined): plugin is Plugin { | ||
| return typeof plugin?.name === 'string' && typeof plugin.default === 'function' | ||
| } | ||
|
|
||
| // True when a plugin with the same name is already loaded. | ||
| function isDuplicatePlugin(plugin: Plugin): boolean { | ||
| return plugins.some(existing => existing.name === plugin.name) | ||
| } | ||
|
|
||
| // exported for mocking/testing. not for actual use | ||
| export async function loadBuiltInPlugins() { | ||
| core.info('Loading built-in plugins') | ||
|
|
@@ -55,7 +67,7 @@ export async function loadBuiltInPlugins() { | |
| await loadPluginsFromPath({pluginsPath}) | ||
| } | ||
|
|
||
| // exported for mocking/testing. not for actual use | ||
| // export to be used for mocking/testing. not for actual use | ||
|
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. OK, I see these "not for actual" use comments are increasing in number; are they still accurate?
Contributor
Author
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. It was an existing convention I noticed, for functions that needed to be exported since it was used in testing files, but the export wouldn't actually be used anywhere in production. Technically its accurate but it can be misleading. Do we need the label here or should I clean it up?
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.
Feel free to do this in a followup pull request, but yeah, I'm not sure these comments are particularly useful here since they're not programmatically enforced + nothing crazy should happen if, for some reason, you import and use one of these functions individually. But cc @abdulahmad307 in the event I'm missing something. If we just want to separate out exports which solely exist for testing, maybe we could move these function definitions into a separate file imported both here and in tests? 🤷♀️ |
||
| export async function loadCustomPlugins() { | ||
| core.info('Loading custom plugins') | ||
| const pluginsPath = path.join(process.cwd(), '.github/scanner-plugins/') | ||
|
|
@@ -75,6 +87,51 @@ export async function loadCustomPlugins() { | |
| await loadPluginsFromPath({pluginsPath, skipBuiltInPlugins: BUILT_IN_PLUGINS}) | ||
| } | ||
|
|
||
| // First-party packages allowed to be installed and loaded from NPM. | ||
| const FIRST_PARTY_NPM_PLUGINS = ['@github/accessibility-scanner-alt-text-plugin'] | ||
|
|
||
| // exported for mocking/testing. not for actual use | ||
| export async function loadNpmPlugins(npmPlugins: NpmPluginRequest[]) { | ||
| if (npmPlugins.length === 0) { | ||
| return | ||
| } | ||
| core.info('Loading NPM plugins') | ||
|
|
||
| for (const request of npmPlugins) { | ||
| // Only install first-party packages. | ||
| if (!FIRST_PARTY_NPM_PLUGINS.includes(request.package)) { | ||
| core.warning(`Skipping NPM plugin '${request.package}' because it is not a first-party package`) | ||
| continue | ||
| } | ||
|
|
||
| const plugin = await loadPluginViaNpm(request) | ||
| if (!plugin) { | ||
| continue | ||
| } | ||
|
|
||
| // Plugin doesn't expose a usable name/default export. | ||
| if (!isValidPlugin(plugin)) { | ||
| core.warning(`Skipping NPM plugin '${request.package}' because it does not export a valid plugin`) | ||
| continue | ||
| } | ||
| // Mismatch means the plugin would load but never run. | ||
| if (plugin.name !== request.name) { | ||
| core.warning( | ||
| `Skipping NPM plugin '${request.package}' because it exported name '${plugin.name}', which does not match requested name '${request.name}'`, | ||
| ) | ||
| continue | ||
| } | ||
| // Built-in and local plugins take precedence over NPM ones of the same name. | ||
| if (isDuplicatePlugin(plugin)) { | ||
| core.info(`Skipping NPM plugin '${plugin.name}' because a plugin with that name is already loaded`) | ||
| continue | ||
| } | ||
|
|
||
| core.info(`Found NPM plugin: ${plugin.name}`) | ||
| plugins.push(plugin) | ||
| } | ||
| } | ||
|
|
||
| // exported for mocking/testing. not for actual use | ||
| export async function loadPluginsFromPath({ | ||
| pluginsPath, | ||
|
|
@@ -105,6 +162,15 @@ export async function loadPluginsFromPath({ | |
| continue | ||
| } | ||
|
|
||
| if (!isValidPlugin(plugin)) { | ||
| core.warning(`Skipping plugin '${pluginFolder}' because it does not export a valid plugin`) | ||
| continue | ||
| } | ||
| if (isDuplicatePlugin(plugin)) { | ||
| core.warning(`Skipping plugin '${pluginFolder}' because a plugin named '${plugin.name}' is already loaded`) | ||
| continue | ||
| } | ||
|
|
||
| core.info(`Found plugin: ${plugin.name}`) | ||
| plugins.push(plugin) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import {execFileSync} from 'child_process' | ||
|
kzhou314 marked this conversation as resolved.
|
||
| import * as core from '@actions/core' | ||
| import type {NpmPluginRequest, Plugin} from './types.js' | ||
|
|
||
| // Install the package at runtime. | ||
| export function installNpmPackage(spec: string) { | ||
| execFileSync('npm', ['install', spec, '--no-save', '--no-package-lock', '--ignore-scripts'], {stdio: 'inherit'}) | ||
| } | ||
|
|
||
| // Install and import a single NPM-published plugin | ||
| export async function loadPluginViaNpm(request: NpmPluginRequest): Promise<Plugin | undefined> { | ||
| const spec = request.version ? `${request.package}@${request.version}` : request.package | ||
| try { | ||
| core.info(`Installing NPM plugin: ${spec}`) | ||
| installNpmPackage(spec) | ||
| // Import the bare package specifier as-is; pathToFileURL would mangle it. | ||
| const imported = await import(request.package) | ||
| return imported as Plugin | ||
| } catch (e) { | ||
| core.warning(`Failed to load NPM plugin '${spec}': ${e}`) | ||
| return undefined | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import {describe, it, expect, vi, beforeEach} from 'vitest' | ||
|
|
||
| import * as childProcess from 'child_process' | ||
| import * as core from '@actions/core' | ||
| import * as pluginManager from '../src/pluginManager/index.js' | ||
| import * as npmPluginLoader from '../src/pluginManager/pluginNpmLoader.js' | ||
| import type {Plugin} from '../src/pluginManager/types.js' | ||
|
|
||
| vi.mock('child_process', {spy: true}) | ||
| vi.mock('@actions/core', {spy: true}) | ||
| vi.mock('../src/pluginManager/pluginNpmLoader.js', {spy: true}) | ||
|
|
||
| const ALLOWED = '@github/accessibility-scanner-alt-text-plugin' | ||
|
|
||
| describe('npmPluginLoader', () => { | ||
| beforeEach(() => { | ||
| vi.restoreAllMocks() | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| describe('installNpmPackage', () => { | ||
| it('installs with --no-save, --no-package-lock and --ignore-scripts', () => { | ||
| const execSpy = vi.spyOn(childProcess, 'execFileSync').mockImplementation(() => Buffer.from('')) | ||
| npmPluginLoader.installNpmPackage('some-pkg@1.0.0') | ||
| expect(execSpy).toHaveBeenCalledWith( | ||
| 'npm', | ||
| ['install', 'some-pkg@1.0.0', '--no-save', '--no-package-lock', '--ignore-scripts'], | ||
| { | ||
| stdio: 'inherit', | ||
| }, | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('loadPluginViaNpm', () => { | ||
| it('pins the version in the install spec', async () => { | ||
| const execSpy = vi.spyOn(childProcess, 'execFileSync').mockImplementation(() => Buffer.from('')) | ||
| await npmPluginLoader.loadPluginViaNpm({name: 'p', package: 'nonexistent-pkg-xyz', version: '2.3.4'}) | ||
| expect(execSpy).toHaveBeenCalledWith( | ||
| 'npm', | ||
| ['install', 'nonexistent-pkg-xyz@2.3.4', '--no-save', '--no-package-lock', '--ignore-scripts'], | ||
| {stdio: 'inherit'}, | ||
| ) | ||
| }) | ||
|
|
||
| it('returns undefined and warns when loading fails', async () => { | ||
| vi.spyOn(childProcess, 'execFileSync').mockImplementation(() => Buffer.from('')) | ||
| const warnSpy = vi.spyOn(core, 'warning').mockImplementation(() => {}) | ||
| const plugin = await npmPluginLoader.loadPluginViaNpm({name: 'p', package: 'nonexistent-pkg-xyz'}) | ||
| expect(plugin).toBeUndefined() | ||
| expect(warnSpy).toHaveBeenCalled() | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('loadNpmPlugins', () => { | ||
| beforeEach(() => { | ||
| vi.restoreAllMocks() | ||
| vi.clearAllMocks() | ||
| pluginManager.clearCache() | ||
| }) | ||
|
|
||
| it('loads a plugin from a first-party package', async () => { | ||
| vi.spyOn(npmPluginLoader, 'loadPluginViaNpm').mockResolvedValue({name: 'alt-text-scan', default: vi.fn()}) | ||
| await pluginManager.loadNpmPlugins([{name: 'alt-text-scan', package: ALLOWED}]) | ||
| expect(pluginManager.getPlugins().map(plugin => plugin.name)).toContain('alt-text-scan') | ||
| }) | ||
|
|
||
| it('skips and warns when a package is not first-party', async () => { | ||
| const loadSpy = vi.spyOn(npmPluginLoader, 'loadPluginViaNpm').mockResolvedValue(undefined) | ||
| const warnSpy = vi.spyOn(core, 'warning').mockImplementation(() => {}) | ||
| await pluginManager.loadNpmPlugins([{name: 'evil', package: 'evil-pkg'}]) | ||
| expect(loadSpy).not.toHaveBeenCalled() | ||
| expect(warnSpy).toHaveBeenCalled() | ||
| expect(pluginManager.getPlugins().length).toBe(0) | ||
| }) | ||
|
|
||
| it('skips a package that does not export a valid plugin', async () => { | ||
| vi.spyOn(npmPluginLoader, 'loadPluginViaNpm').mockResolvedValue({name: 'bad'} as unknown as Plugin) | ||
| const warnSpy = vi.spyOn(core, 'warning').mockImplementation(() => {}) | ||
| await pluginManager.loadNpmPlugins([{name: 'bad', package: ALLOWED}]) | ||
| expect(warnSpy).toHaveBeenCalled() | ||
| expect(pluginManager.getPlugins().length).toBe(0) | ||
| }) | ||
|
|
||
| it('skips an NPM plugin whose exported name does not match the requested name', async () => { | ||
| vi.spyOn(npmPluginLoader, 'loadPluginViaNpm').mockResolvedValue({name: 'actual-name', default: vi.fn()}) | ||
| const warnSpy = vi.spyOn(core, 'warning').mockImplementation(() => {}) | ||
| await pluginManager.loadNpmPlugins([{name: 'requested-name', package: ALLOWED}]) | ||
| expect(warnSpy).toHaveBeenCalled() | ||
| expect(pluginManager.getPlugins().length).toBe(0) | ||
| }) | ||
|
|
||
| it('skips an NPM plugin whose name collides with an already-loaded plugin', async () => { | ||
| pluginManager.getPlugins().push({name: 'dup', default: vi.fn()}) | ||
| vi.spyOn(npmPluginLoader, 'loadPluginViaNpm').mockResolvedValue({name: 'dup', default: vi.fn()}) | ||
| await pluginManager.loadNpmPlugins([{name: 'dup', package: ALLOWED}]) | ||
| expect(pluginManager.getPlugins().filter(plugin => plugin.name === 'dup').length).toBe(1) | ||
| }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.