From 3872b8f5df513752a9f6e09b53c22f74987b329e Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Wed, 11 Dec 2024 08:46:04 +0000 Subject: [PATCH] fix: update URL validation to handle GitHub URLs properly Co-Authored-By: Michael Latman --- src/scripts/pr-check.js | 7 +++--- src/scripts/pr-check.test.js | 42 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/scripts/pr-check.test.js diff --git a/src/scripts/pr-check.js b/src/scripts/pr-check.js index 677416b..7a8b145 100644 --- a/src/scripts/pr-check.js +++ b/src/scripts/pr-check.js @@ -99,10 +99,9 @@ export function validateRequiredFields(pkg) { console.log('Validating URLs...'); const urlFields = ['sourceUrl', 'homepage']; for (const field of urlFields) { - try { - new URL(pkg[field]); - } catch (error) { - throw new Error(`Package ${pkg.name} has invalid ${field} URL: ${pkg[field]}`); + const url = pkg[field]; + if (!url.startsWith('http://') && !url.startsWith('https://')) { + throw new Error(`Package ${pkg.name} has invalid ${field} URL: ${url} - must start with http:// or https://`); } } } diff --git a/src/scripts/pr-check.test.js b/src/scripts/pr-check.test.js new file mode 100644 index 0000000..1820136 --- /dev/null +++ b/src/scripts/pr-check.test.js @@ -0,0 +1,42 @@ +import { validateRequiredFields } from './pr-check.js'; + +describe('validateRequiredFields', () => { + test('accepts valid GitHub URLs', () => { + const pkg = { + name: 'test-package', + description: 'Test package', + vendor: 'Test Vendor', + sourceUrl: 'https://github.com/owner/repo', + homepage: 'https://github.com/owner/repo', + license: 'MIT', + runtime: 'node' + }; + expect(() => validateRequiredFields(pkg)).not.toThrow(); + }); + + test('rejects invalid URLs without protocol', () => { + const pkg = { + name: 'test-package', + description: 'Test package', + vendor: 'Test Vendor', + sourceUrl: 'github.com/owner/repo', + homepage: 'https://github.com/owner/repo', + license: 'MIT', + runtime: 'node' + }; + expect(() => validateRequiredFields(pkg)).toThrow(/invalid sourceUrl URL/); + }); + + test('accepts both http and https protocols', () => { + const pkg = { + name: 'test-package', + description: 'Test package', + vendor: 'Test Vendor', + sourceUrl: 'http://github.com/owner/repo', + homepage: 'https://github.com/owner/repo', + license: 'MIT', + runtime: 'node' + }; + expect(() => validateRequiredFields(pkg)).not.toThrow(); + }); +});