-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Enhance environment validation and configuration for Temporal worker #33
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
c47d37f
Remove workflowsPath from worker config and add env validation
anatolyshipitz d3968ac
Fix error message variable in environment validation utility
anatolyshipitz 7e4d067
Remove DEBUG variable from .env.test file to streamline environment c…
anatolyshipitz 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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| REDMINE_DB_HOST=localhost | ||
| REDMINE_DB_USER=testuser | ||
| REDMINE_DB_PASSWORD=testpassword | ||
| REDMINE_DB_NAME=testdb |
Large diffs are not rendered by default.
Oops, something went wrong.
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,58 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('../common/../configs', () => ({ | ||
| validationResult: { success: true }, | ||
| })); | ||
|
|
||
| import * as configs from '../common/../configs'; | ||
| import { validateEnv } from '../common/utils'; | ||
|
|
||
| type ValidationResult = { | ||
| success: boolean; | ||
| error?: { issues: { path: unknown[]; message: string }[] }; | ||
| }; | ||
| function setValidationResult(result: ValidationResult) { | ||
| (configs as { validationResult: ValidationResult }).validationResult = result; | ||
| } | ||
|
|
||
| describe('validateEnv', () => { | ||
| let errorSpy: ReturnType<typeof vi.spyOn>; | ||
| let exitSpy: ReturnType<typeof vi.spyOn>; | ||
|
|
||
| beforeEach(() => { | ||
| errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { | ||
| throw new Error('exit'); | ||
| }) as unknown as ReturnType<typeof vi.spyOn>; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| errorSpy.mockRestore(); | ||
| exitSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('does nothing if validationResult.success is true', () => { | ||
| setValidationResult({ success: true }); | ||
| expect(() => validateEnv()).not.toThrow(); | ||
| expect(errorSpy).not.toHaveBeenCalled(); | ||
| expect(exitSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('logs error and exits if validationResult.success is false', () => { | ||
| setValidationResult({ | ||
| success: false, | ||
| error: { | ||
| issues: [ | ||
| { path: ['FOO'], message: 'is required' }, | ||
| { path: [], message: 'unknown' }, | ||
| ], | ||
| }, | ||
| }); | ||
| expect(() => validateEnv()).toThrow('exit'); | ||
| expect(errorSpy).toHaveBeenCalledWith( | ||
| 'Missing or invalid environment variable: FOO (is required)\n' + | ||
| 'Missing or invalid environment variable: (unknown variable) (unknown)', | ||
| ); | ||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
| }); | ||
| }); |
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,15 @@ | ||
| import { validationResult } from '../configs'; | ||
|
|
||
| export function validateEnv() { | ||
| if (!validationResult.success) { | ||
| const errorMessage = validationResult.error.issues | ||
| .map( | ||
| ({ path, message }) => | ||
| `Missing or invalid environment variable: ${path.join('.') || '(unknown variable)'} (${message})`, | ||
| ) | ||
| .join('\n'); | ||
|
|
||
| console.error(errorMessage); | ||
| process.exit(1); | ||
| } | ||
| } |
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,8 @@ | ||
| import { redmineDatabaseSchema } from './redmineDatabase'; | ||
| import { temporalSchema } from './temporal'; | ||
| import { workerSchema } from './worker'; | ||
|
|
||
| export const validationResult = temporalSchema | ||
| .merge(workerSchema) | ||
| .merge(redmineDatabaseSchema) | ||
| .safeParse(process.env); |
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,15 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| export const redmineDatabaseConfig = { | ||
| host: process.env.REDMINE_DB_HOST, | ||
| user: process.env.REDMINE_DB_USER, | ||
| password: process.env.REDMINE_DB_PASSWORD, | ||
| database: process.env.REDMINE_DB_NAME, | ||
| }; | ||
|
|
||
| export const redmineDatabaseSchema = z.object({ | ||
| REDMINE_DB_HOST: z.string(), | ||
| REDMINE_DB_USER: z.string(), | ||
| REDMINE_DB_PASSWORD: z.string(), | ||
| REDMINE_DB_NAME: z.string(), | ||
| }); | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.