-
Notifications
You must be signed in to change notification settings - Fork 88
feat: Add PREEVY_HOST and PREEVY_ENV_ID environment variables #603
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
Draft
Copilot
wants to merge
2
commits into
main
Choose a base branch
from
copilot/fix-22c10194-ebbb-421e-8f63-0c274ef5a711
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.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { describe, it, expect } from '@jest/globals' | ||
| import { serviceLinkEnvVars } from './service-links.js' | ||
|
|
||
| describe('serviceLinkEnvVars', () => { | ||
| const mockServiceUrls = [ | ||
| { name: 'frontend', port: 3000, url: 'https://frontend-3000-env123-client456.livecycle.run/' }, | ||
| { name: 'api', port: 8080, url: 'https://api-8080-env123-client456.livecycle.run/' }, | ||
| { name: 'my-service', port: 9000, url: 'https://my-service-9000-env123-client456.livecycle.run/' }, | ||
| ] | ||
|
|
||
| describe('backward compatibility', () => { | ||
| it('should generate PREEVY_BASE_URI variables for each service and port', () => { | ||
| const result = serviceLinkEnvVars(mockServiceUrls) | ||
|
|
||
| expect(result).toMatchObject({ | ||
| 'PREEVY_BASE_URI_FRONTEND_3000': 'https://frontend-3000-env123-client456.livecycle.run/', | ||
| 'PREEVY_BASE_URI_API_8080': 'https://api-8080-env123-client456.livecycle.run/', | ||
| 'PREEVY_BASE_URI_MY_SERVICE_9000': 'https://my-service-9000-env123-client456.livecycle.run/', | ||
| }) | ||
| }) | ||
|
|
||
| it('should normalize service names with non-alphanumeric characters', () => { | ||
| const serviceUrls = [ | ||
| { name: 'my-service-name', port: 3000, url: 'https://example.com/' }, | ||
| { name: 'service.with.dots', port: 8080, url: 'https://example.com/' }, | ||
| ] | ||
|
|
||
| const result = serviceLinkEnvVars(serviceUrls) | ||
|
|
||
| expect(result).toMatchObject({ | ||
| 'PREEVY_BASE_URI_MY_SERVICE_NAME_3000': 'https://example.com/', | ||
| 'PREEVY_BASE_URI_SERVICE_WITH_DOTS_8080': 'https://example.com/', | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('new environment variables', () => { | ||
| it('should generate PREEVY_HOST variables with just the hostname', () => { | ||
| const result = serviceLinkEnvVars(mockServiceUrls) | ||
|
|
||
| expect(result).toMatchObject({ | ||
| 'PREEVY_HOST_FRONTEND_3000': 'frontend-3000-env123-client456.livecycle.run', | ||
| 'PREEVY_HOST_API_8080': 'api-8080-env123-client456.livecycle.run', | ||
| 'PREEVY_HOST_MY_SERVICE_9000': 'my-service-9000-env123-client456.livecycle.run', | ||
| }) | ||
| }) | ||
|
|
||
| it('should include PREEVY_ENV_ID when envId is provided', () => { | ||
| const result = serviceLinkEnvVars(mockServiceUrls, 'test-env-123') | ||
|
|
||
| expect(result).toMatchObject({ | ||
| 'PREEVY_ENV_ID': 'test-env-123', | ||
| }) | ||
| }) | ||
|
|
||
| it('should not include PREEVY_ENV_ID when envId is not provided', () => { | ||
| const result = serviceLinkEnvVars(mockServiceUrls) | ||
|
|
||
| expect(result).not.toHaveProperty('PREEVY_ENV_ID') | ||
| }) | ||
|
|
||
| it('should handle invalid URLs gracefully for host extraction', () => { | ||
| const serviceUrls = [ | ||
| { name: 'valid', port: 3000, url: 'https://valid.example.com/' }, | ||
| { name: 'invalid', port: 8080, url: 'not-a-url' }, | ||
| ] | ||
|
|
||
| const result = serviceLinkEnvVars(serviceUrls) | ||
|
|
||
| expect(result).toMatchObject({ | ||
| 'PREEVY_BASE_URI_VALID_3000': 'https://valid.example.com/', | ||
| 'PREEVY_BASE_URI_INVALID_8080': 'not-a-url', | ||
| 'PREEVY_HOST_VALID_3000': 'valid.example.com', | ||
| 'PREEVY_HOST_INVALID_8080': '', // Falls back to empty string for invalid URLs | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('complete environment variable generation', () => { | ||
| it('should generate all expected environment variables when envId is provided', () => { | ||
| const result = serviceLinkEnvVars(mockServiceUrls, 'test-env-123') | ||
|
|
||
| // Verify all PREEVY_BASE_URI variables | ||
| expect(result['PREEVY_BASE_URI_FRONTEND_3000']).toBe('https://frontend-3000-env123-client456.livecycle.run/') | ||
| expect(result['PREEVY_BASE_URI_API_8080']).toBe('https://api-8080-env123-client456.livecycle.run/') | ||
| expect(result['PREEVY_BASE_URI_MY_SERVICE_9000']).toBe('https://my-service-9000-env123-client456.livecycle.run/') | ||
|
|
||
| // Verify all PREEVY_HOST variables | ||
| expect(result['PREEVY_HOST_FRONTEND_3000']).toBe('frontend-3000-env123-client456.livecycle.run') | ||
| expect(result['PREEVY_HOST_API_8080']).toBe('api-8080-env123-client456.livecycle.run') | ||
| expect(result['PREEVY_HOST_MY_SERVICE_9000']).toBe('my-service-9000-env123-client456.livecycle.run') | ||
|
|
||
| // Verify PREEVY_ENV_ID | ||
| expect(result['PREEVY_ENV_ID']).toBe('test-env-123') | ||
|
|
||
| // Verify the total number of variables | ||
| expect(Object.keys(result)).toHaveLength(7) // 3 BASE_URI + 3 HOST + 1 ENV_ID | ||
| }) | ||
| }) | ||
| }) |
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 |
|---|---|---|
| @@ -1,6 +1,26 @@ | ||
| export const serviceLinkEnvVars = ( | ||
| expectedServiceUrls: { name: string; port: number; url: string }[], | ||
| ) => Object.fromEntries( | ||
| expectedServiceUrls | ||
| .map(({ name, port, url }) => [`PREEVY_BASE_URI_${name.replace(/[^a-zA-Z0-9_]/g, '_')}_${port}`.toUpperCase(), url]) | ||
| ) | ||
| envId?: string, | ||
| ): Record<string, string> => { | ||
| const baseUriVars = Object.fromEntries( | ||
| expectedServiceUrls | ||
| .map(({ name, port, url }) => [`PREEVY_BASE_URI_${name.replace(/[^a-zA-Z0-9_]/g, '_')}_${port}`.toUpperCase(), url]) | ||
| ) | ||
|
|
||
| const hostVars = Object.fromEntries( | ||
| expectedServiceUrls | ||
| .map(({ name, port, url }) => { | ||
| try { | ||
| const hostname = new URL(url).hostname | ||
| return [`PREEVY_HOST_${name.replace(/[^a-zA-Z0-9_]/g, '_')}_${port}`.toUpperCase(), hostname] | ||
| } catch (e) { | ||
| // If URL parsing fails, fallback to empty string | ||
| return [`PREEVY_HOST_${name.replace(/[^a-zA-Z0-9_]/g, '_')}_${port}`.toUpperCase(), ''] | ||
| } | ||
| }) | ||
| ) | ||
|
|
||
| const envIdVars: Record<string, string> = envId ? { PREEVY_ENV_ID: envId } : {} | ||
|
|
||
| return { ...baseUriVars, ...hostVars, ...envIdVars } | ||
| } |
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
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.
the regex replace can be extracted to format service name for url function