Skip to content
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

feat: allow to set sourceRegistry by CNPMCORE_CONFIG_SOURCE_REGISTRY #753

Merged
merged 7 commits into from
Feb 9, 2025

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Feb 8, 2025

Improve the local development process based on docker-compose

Summary by CodeRabbit

  • New Features
    • Introduced a centralized configuration utility that validates environment variable types and provides fallback defaults.
  • Refactor
    • Standardized environment variable handling across configuration files, improving maintainability and consistency in system setup.

@fengmk2 fengmk2 requested a review from elrrrrrrr February 8, 2025 16:12
Copy link
Contributor

coderabbitai bot commented Feb 8, 2025

Walkthrough

This pull request centralizes and standardizes environment variable retrieval by introducing a new type and overloading the env() function in the EnvUtil module. The env() function now handles string, boolean, and number types with built-in error checking. In addition, configuration files (both the default and database configuration files) have been refactored to use this centralized utility, replacing direct process.env access. A minor type update has also been applied to clarify context usage in one configuration function.

Changes

Files Change Summary
app/.../EnvUtil.ts Introduces a new ValueType type; overloads the env() function for string, boolean, and number types; includes value processing with error handling for invalid inputs.
config/.../config.default.ts, config/.../database.ts Replaces direct environment variable access with calls to env(); updates configuration properties for type validation, default handling, and a clearer fallback mechanism; updates Context typing.
config/.../config.unittest.ts Updates database property logic to use logical OR operator for broader default value application.
test/.../TestUtil.ts Modifies getDatabaseConfig method to use logical OR operator for setting default database name.
.env.example Adds new environment variable entries for MySQL and PostgreSQL configurations.
docker-compose-postgres.yml Introduces new Docker Compose configuration for PostgreSQL setup with Redis and pgAdmin services.
docker-compose.yml Updates services and environment variables, including changes to MySQL service configuration and network settings.
prepare-database-mysql.sh Adds error handling with set -ex for better debugging in the script.
prepare-database-postgresql.sh Updates default values for database connection variables to improve usability.

Sequence Diagram(s)

sequenceDiagram
  participant C as Config/Database Module
  participant E as EnvUtil.env()
  participant OS as process.env

  C->>E: Request env variable with key, type, defaultValue
  E->>OS: Retrieve value using key
  alt Value exists and is valid
    E-->>C: Return parsed (string/boolean/number) value
  else Value missing or invalid
    E-->>C: Return default value or throw TypeError
  end
Loading

Possibly related PRs

  • feat: support PostgreSQL #733: The changes in the main PR, which introduce a new env function for handling environment variables with type validation, are related to the retrieved PR as both involve modifications to how environment variables are accessed and utilized, particularly in the context of database configurations.

Suggested labels

enhancement

Suggested reviewers

  • elrrrrrrr

Poem

🐰 In the realm of code, I hop with glee,
A new env function sets our settings free.
String, boolean, number all in one,
Parsing values under the coding sun.
With every bug banished and errors slight,
I celebrate these changes deep into the night!

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@fengmk2
Copy link
Member Author

fengmk2 commented Feb 8, 2025

将环境变量解析做了一下封装

Copy link
Member

@elrrrrrrr elrrrrrrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🔭 Outside diff range comments (1)
config/config.default.ts (1)

209-220: Add validation for Elasticsearch configuration.

Similar to OSS and S3 configurations, add assertions for required Elasticsearch environment variables.

   if (config.cnpmcore.enableElasticsearch) {
+    const node = env('CNPMCORE_CONFIG_ES_CLIENT_NODE', 'string', '');
+    const username = env('CNPMCORE_CONFIG_ES_CLIENT_AUTH_USERNAME', 'string', '');
+    const password = env('CNPMCORE_CONFIG_ES_CLIENT_AUTH_PASSWORD', 'string', '');
+    assert(node, 'require env CNPMCORE_CONFIG_ES_CLIENT_NODE');
+    assert(username, 'require env CNPMCORE_CONFIG_ES_CLIENT_AUTH_USERNAME');
+    assert(password, 'require env CNPMCORE_CONFIG_ES_CLIENT_AUTH_PASSWORD');
     config.elasticsearch = {
       client: {
-        node: env('CNPMCORE_CONFIG_ES_CLIENT_NODE', 'string', ''),
+        node,
         auth: {
-          username: env('CNPMCORE_CONFIG_ES_CLIENT_AUTH_USERNAME', 'string', ''),
-          password: env('CNPMCORE_CONFIG_ES_CLIENT_AUTH_PASSWORD', 'string', ''),
+          username,
+          password,
         },
       },
     };
   }
🧹 Nitpick comments (5)
app/common/EnvUtil.ts (2)

16-26: Consider enhancing boolean parsing flexibility.

The current boolean parsing is strict and might be too restrictive. Consider supporting additional common boolean representations.

-    if (value === 'true' || value === '1') {
+    const trueValues = ['true', '1', 'yes', 'y'];
+    const falseValues = ['false', '0', 'no', 'n'];
+    if (trueValues.includes(value.toLowerCase())) {
       booleanValue = true;
-    } else if (value === 'false' || value === '0') {
+    } else if (falseValues.includes(value.toLowerCase())) {
       booleanValue = false;
     } else {
-      throw new TypeError(`Invalid boolean value: ${value} on process.env.${key}`);
+      throw new TypeError(`Invalid boolean value: ${value} for ${key}. Expected one of: ${[...trueValues, ...falseValues].join(', ')}`);
     }

28-34: Enhance number validation.

Consider adding range validation for numeric values to prevent potential issues.

   if (valueType === 'number') {
     const numberValue = Number(value);
     if (isNaN(numberValue)) {
-      throw new TypeError(`Invalid number value: ${value} on process.env.${key}`);
+      throw new TypeError(`Invalid number value: ${value} for ${key}. Value must be a valid number.`);
     }
+    if (!Number.isFinite(numberValue)) {
+      throw new TypeError(`Invalid number value: ${value} for ${key}. Value must be finite.`);
+    }
     return numberValue;
   }
config/database.ts (1)

32-32: Track SQLite implementation.

The TODO comment for SQLite implementation should be tracked in an issue.

Would you like me to create an issue to track the SQLite implementation?

config/config.default.ts (2)

135-138: Improve assertion messages for OSS configuration.

The assertion messages should match the environment variable names they're checking.

-    assert(ossConfig.cdnBaseUrl, 'require env CNPMCORE_NFS_OSS_BUCKET');
-    assert(ossConfig.endpoint, 'require env CNPMCORE_NFS_OSS_ENDPOINT');
-    assert(ossConfig.accessKeyId, 'require env CNPMCORE_NFS_OSS_ID');
-    assert(ossConfig.accessKeySecret, 'require env CNPMCORE_NFS_OSS_SECRET');
+    assert(ossConfig.cdnBaseUrl, 'require env CNPMCORE_NFS_OSS_CDN');
+    assert(ossConfig.endpoint, 'require env CNPMCORE_NFS_OSS_ENDPOINT');
+    assert(ossConfig.accessKeyId, 'require env CNPMCORE_NFS_OSS_ID');
+    assert(ossConfig.accessKeySecret, 'require env CNPMCORE_NFS_OSS_SECRET');

156-159: Consider using dynamic import for S3 client.

The TODO comment suggests using import for ESM support. This can be implemented now.

-    // TODO(@fengmk2): should change to use import to support esm
-    // eslint-disable-next-line @typescript-eslint/no-var-requires
-    const S3Client = require('s3-cnpmcore');
-    config.nfs.client = new S3Client(s3Config);
+    const { default: S3Client } = await import('s3-cnpmcore');
+    config.nfs.client = new S3Client(s3Config);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between aba2b36 and 7e91ad3.

📒 Files selected for processing (3)
  • app/common/EnvUtil.ts (1 hunks)
  • config/config.default.ts (9 hunks)
  • config/database.ts (1 hunks)
🧰 Additional context used
🪛 ESLint
config/database.ts

[error] 1-1: Strings must use singlequote.

(quotes)

🪛 GitHub Check: test-mysql57-fs-nfs (22, ubuntu-latest)
config/database.ts

[failure] 1-1:
Strings must use singlequote

🪛 GitHub Check: test-postgresql-fs-nfs (22, ubuntu-latest)
config/database.ts

[failure] 1-1:
Strings must use singlequote

🪛 GitHub Check: test-mysql57-fs-nfs (20, ubuntu-latest)
config/database.ts

[failure] 1-1:
Strings must use singlequote

🪛 GitHub Check: test-postgresql-fs-nfs (20, ubuntu-latest)
config/database.ts

[failure] 1-1:
Strings must use singlequote

🪛 GitHub Check: test-mysql57-fs-nfs (18, ubuntu-latest)
config/database.ts

[failure] 1-1:
Strings must use singlequote

🪛 GitHub Check: test-postgresql-fs-nfs (18, ubuntu-latest)
config/database.ts

[failure] 1-1:
Strings must use singlequote

🪛 GitHub Check: test-mysql57-fs-nfs (18.20.0, ubuntu-latest)
config/database.ts

[failure] 1-1:
Strings must use singlequote

🪛 GitHub Check: test-postgresql-fs-nfs (18.20.0, ubuntu-latest)
config/database.ts

[failure] 1-1:
Strings must use singlequote

🪛 GitHub Actions: Node.js CI
config/database.ts

[error] 1-1: Strings must use singlequote quotes.

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Analyze (typescript)
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (1)
app/common/EnvUtil.ts (1)

1-1: LGTM! Type definition is clear and concise.

The ValueType type accurately represents the supported value types.

Copy link

codecov bot commented Feb 8, 2025

Codecov Report

Attention: Patch coverage is 67.82609% with 37 lines in your changes missing coverage. Please review.

Project coverage is 96.35%. Comparing base (f1fc249) to head (bf9dc36).
Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
config/config.default.ts 62.71% 22 Missing ⚠️
app/common/EnvUtil.ts 62.16% 14 Missing ⚠️
config/database.ts 94.44% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #753      +/-   ##
==========================================
- Coverage   96.47%   96.35%   -0.12%     
==========================================
  Files         191      192       +1     
  Lines       19232    19274      +42     
  Branches     2508     2513       +5     
==========================================
+ Hits        18554    18572      +18     
- Misses        678      702      +24     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
config/database.ts (1)

32-32: Track SQLite implementation.

The TODO comment indicates pending SQLite implementation.

Would you like me to help implement the SQLite configuration or create an issue to track this task?

prepare-database-mysql.sh (1)

3-3: Enable shell error handling and debugging.

Uncomment the set -ex line to enable immediate exit on errors and command tracing, which will help with debugging database preparation issues.

Apply this diff:

-# set -ex
+set -ex
prepare-database-postgresql.sh (1)

6-9: Update PostgreSQL Default Connection Values.
The script now provides sensible defaults for connecting to PostgreSQL:
• db_host defaults to 127.0.0.1,
• db_username and db_password default to "postgres".
Note: The inline comment on line 9 (“# default to empty password”) should be updated to reflect the new default value ("postgres").

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7e91ad3 and bf9dc36.

📒 Files selected for processing (9)
  • .env.example (1 hunks)
  • DEVELOPER.md (3 hunks)
  • config/config.unittest.ts (1 hunks)
  • config/database.ts (1 hunks)
  • docker-compose-postgres.yml (1 hunks)
  • docker-compose.yml (2 hunks)
  • prepare-database-mysql.sh (1 hunks)
  • prepare-database-postgresql.sh (1 hunks)
  • test/TestUtil.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .env.example
⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: test-mysql57-fs-nfs (22, ubuntu-latest)
  • GitHub Check: test-postgresql-fs-nfs (22, ubuntu-latest)
  • GitHub Check: test-mysql57-fs-nfs (20, ubuntu-latest)
  • GitHub Check: test-postgresql-fs-nfs (20, ubuntu-latest)
  • GitHub Check: test-mysql57-fs-nfs (18, ubuntu-latest)
  • GitHub Check: test-postgresql-fs-nfs (18, ubuntu-latest)
  • GitHub Check: Analyze (typescript)
  • GitHub Check: test-mysql57-fs-nfs (18.20.0, ubuntu-latest)
  • GitHub Check: Analyze (javascript)
  • GitHub Check: test-postgresql-fs-nfs (18.20.0, ubuntu-latest)
🔇 Additional comments (24)
config/config.unittest.ts (1)

13-13: Verify the impact of using logical OR operator.

The change from nullish coalescing (??) to logical OR (||) means the default value 'cnpmcore_unittest' will be used for any falsy database.name value (empty string, 0, false), not just null/undefined. Ensure this aligns with the expected test behavior.

config/database.ts (3)

1-1: Fix string quote style.

Use single quotes consistently throughout the file to comply with the project's style guide.


9-14: Consider more secure default values.

Empty strings and zero values as defaults for database credentials could lead to unintended connections.


29-30: ⚠️ Potential issue

Remove hard-coded PostgreSQL credentials.

Hard-coded credentials pose a security risk and should be replaced with environment variables.

Apply this diff to remove hard-coded credentials:

-  dbUser = dbUser || env('CNPMCORE_POSTGRES_USER', 'string', '') || env('POSTGRES_USER', 'string', '') || 'postgres';
-  dbPassword = dbPassword || env('CNPMCORE_POSTGRES_PASSWORD', 'string', '') || env('POSTGRES_PASSWORD', 'string', '') || 'postgres';
+  dbUser = dbUser || env('CNPMCORE_POSTGRES_USER', 'string', '') || env('POSTGRES_USER', 'string', '');
+  dbPassword = dbPassword || env('CNPMCORE_POSTGRES_PASSWORD', 'string', '') || env('POSTGRES_PASSWORD', 'string', '');

Likely invalid or redundant comment.

test/TestUtil.ts (1)

52-52: Verify consistency with test configuration.

The change from nullish coalescing (??) to logical OR (||) matches the change in config/config.unittest.ts. Ensure this behavior is consistent with test requirements.

docker-compose.yml (8)

1-1: Clear Environment Name Declaration.
Setting the service name to "cnpmcore_dev_services_mysql" clearly identifies the intended development environment.


5-6: Centralize Redis Configuration with env_file.
Adding the env_file directive for the redis service ensures that environment variables are maintained in one place (the .env file), which enhances consistency and simplifies configuration management.


15-15: Consistent Network Assignment for Redis.
The update to assign the redis service to the "cnpm-mysql" network maintains connectivity consistency across services.


17-20: MySQL Service: Use of env_file and Updated Image.
Including the env_file for the MySQL service and updating the image to "mysql:9" help centralize environment configuration and align with required database version preferences. Please verify that version 9 is fully compatible with your application’s needs.


23-27: MySQL Service: Dynamic Environment Variables.
Defining MYSQL_ROOT_PASSWORD and MYSQL_DATABASE with default value expressions (e.g., ${MYSQL_ROOT_PASSWORD:-} and ${MYSQL_DATABASE:-cnpmcore}) improves portability and makes it easier to override these settings as needed.


31-32: Network Update for MySQL Service.
Switching the network to "cnpm-mysql" ensures that the MySQL container connects to the correct network for this configuration.


36-46: phpMyAdmin Service: Environment and Comments Verification.
Adding an env_file along with dynamic environment variable references for phpMyAdmin (and commenting out MYSQL_USER/PASSWORD) supports consistency with the other services. The commented variables are acceptable if they’re not required in the current configuration.


58-61: Custom Network Definition Updated.
The networks section now defines "cnpm-mysql" with a clear name and bridge driver, providing a dedicated network for database-related services.

docker-compose-postgres.yml (6)

1-1: Distinct Service Name for PostgreSQL Development.
The name "cnpmcore_dev_services_postgres" clearly differentiates this setup from the MySQL-based environment, aiding in environment management.


4-6: Redis Service Configuration for PostgreSQL Environment.
The redis service is configured with an env_file and appropriate mappings, aligning with the overall centralized configuration approach.


17-20: Postgres Service: Centralized Environment and Image Selection.
Using an env_file along with explicit environment variable settings for POSTGRES_USER and POSTGRES_PASSWORD ensures that the container inherits the correct credentials. The selection of the "postgres:17" image should be verified for compatibility (as version 17 is less common and may be a recent release).


22-24: Dynamic Environment Variables for Postgres.
The configuration defaults for POSTGRES_USER and POSTGRES_PASSWORD are set with fallback values ("postgres"), which is in line with best practices for ensuring a secure and predictable environment.


32-43: pgAdmin Service: Comprehensive Environment Setup.
The pgAdmin service correctly sources its environment settings (including default credentials and configuration parameters) from the .env file. This promotes consistency and facilitates easier configuration updates.


53-62: Volumes and Network Definition for PostgreSQL Setup.
The volumes and the custom network ("cnpm-postgres") sections are clearly defined, which supports container isolation and persistence management.

DEVELOPER.md (5)

7-11: Documentation: Generating Local Environment Configuration.
The added instructions for generating a local .env configuration file (by copying .env.example) are clear and user friendly. This aids developers in quickly setting up their local development environment.


15-23: MySQL Environment Setup Instructions.
The documentation now provides step‐by‐step commands for starting (and stopping) MySQL + Redis services using docker-compose.yml. The code block is clear and instructive.


25-33: PostgreSQL Environment Setup Instructions.
The added section for setting up PostgreSQL + Redis using docker-compose-postgres.yml is well structured and mirrors the MySQL setup instructions. This consistency supports ease of use for developers preferring either database option.


57-63: Login and Package Publishing Procedures.
The newly added instructions for logging in as an administrator (using npm login) and testing package publishing directly to the local registry are very helpful. They support quick verification of environment configurations and developer workflows.


74-89: Expanded Developer Guidelines and Operational Instructions.
The additional sections covering how to run unit tests for both MySQL and PostgreSQL, as well as the extended documentation on project structure and service guidelines, enhance developer clarity. The comprehensive nature of these instructions minimizes onboarding friction and supports efficient development practices.

@fengmk2 fengmk2 merged commit 9f4f1f1 into master Feb 9, 2025
16 of 19 checks passed
@fengmk2 fengmk2 deleted the change-CNPMCORE_CONFIG_SOURCE_REGISTRY branch February 9, 2025 03:15
fengmk2 pushed a commit that referenced this pull request Feb 9, 2025
[skip ci]

## [3.74.0](v3.73.1...v3.74.0) (2025-02-09)

### Features

* allow to set sourceRegistry by CNPMCORE_CONFIG_SOURCE_REGISTRY ([#753](#753)) ([9f4f1f1](9f4f1f1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants