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

Use Hono env adapter instead of the Deno.env() #64

Merged
merged 2 commits into from
Sep 27, 2024
Merged

Use Hono env adapter instead of the Deno.env() #64

merged 2 commits into from
Sep 27, 2024

Conversation

5ouma
Copy link
Owner

@5ouma 5ouma commented Sep 27, 2024

⚠️ Issue

close #


🔄 Type of the Change

  • 🎉 New Feature
  • 🧰 Bug
  • 🛡️ Security
  • 📖 Documentation
  • 🏎️ Performance
  • 🧹 Refactoring
  • 🧪 Testing
  • 🔧 Maintenance
  • 🎽 CI
  • ⛓️ Dependencies
  • 🧠 Meta

✏️ Description

Use the Hono env adapter to avoid Deno-dedicated API.


Make Reproxy workable on any runtime.
@prlabeler prlabeler bot added the 🎉 New Feature Introduce a new feature label Sep 27, 2024
Copy link

coderabbitai bot commented Sep 27, 2024

Walkthrough

The changes involve significant modifications to the repository handling and environment variable management within the application. Key alterations include the removal of specific environment variable permissions in the deno.json file, updates to function parameters in various test and implementation files to directly accept repository objects, and the deletion of previously existing functions that relied on environment variables. Additionally, the codebase has been streamlined by eliminating intermediary functions, enhancing clarity and directness in function calls.

Changes

Files Change Summary
deno.json Removed specific environment variable permissions from run and test tasks, simplifying the --allow-env flag.
src/libs/content.test.ts Modified test cases to directly pass repository parameters to the getContent function, removing the exportRepo function calls.
src/libs/content.ts Updated getContent function to require a repository parameter and an optional token parameter, replacing previous implicit retrieval methods. Documentation examples were also updated.
src/libs/env.test.ts Deleted unit tests for the getRepository function, which verified repository details from environment variables.
src/libs/env.ts Removed the getRepository function and the githubToken variable, which retrieved repository details and token from environment variables.
src/libs/mod.ts Added export for the type Repository from ./types.ts.
src/libs/redirect.test.ts Updated test cases for checkRedirect to directly pass repository parameters, removing exportRepo function calls.
src/libs/redirect.ts Modified checkRedirect function to require a Repository parameter, updating its internal logic and usage examples accordingly.
src/libs/test_utils.ts Removed the clearRepo function responsible for clearing specific repository details from environment variables.
src/server.ts Introduced new environment variable handling, creating a repository object from extracted variables, and updated function calls to include this object and the GITHUB_TOKEN.

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 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.

Copy link

codecov bot commented Sep 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (87188e5) to head (3cf54d7).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #64   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         6    -1     
  Lines          124       109   -15     
  Branches        11         6    -5     
=========================================
- Hits           124       109   -15     

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

Copy link

@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 and nitpick comments (9)
src/libs/redirect.test.ts (1)

9-9: Function calls updated correctly, consider adding comments for clarity

The checkRedirect function calls have been properly updated to include the new parameters (testRepo and testRef) as per the changes in the function signature. This makes the function calls more explicit and aligns with the goal of making the code workable on any runtime.

Consider adding brief comments above each test step to explain the purpose of each case, e.g.:

// Test redirect without a specific ref
await t.step("normal", () => {
  // ...
});

// Test redirect with a specific ref
await t.step("with ref", () => {
  // ...
});

This would enhance the readability and maintainability of the test cases.

Also applies to: 15-15

src/libs/content.test.ts (2)

9-9: LGTM: Test case simplified.

The test case has been simplified by directly passing testRepo to getContent, which aligns with the PR objective of making the code more runtime-agnostic. This change improves readability and directness.

Consider destructuring the return value for improved clarity:

-    const [data, status] = await getContent(testRepo);
+    const [content, status] = await getContent(testRepo);

This makes it clearer that the first returned value represents the content.


16-16: LGTM: Test cases consistently updated.

The "with ref" and "not found" test cases have been consistently updated to directly pass the repository objects to getContent. This change simplifies the tests and aligns with the PR objective.

For consistency with the previous suggestion, consider applying the same variable naming improvement to these test cases:

-    const [data, status] = await getContent(testRepo, testRef);
+    const [content, status] = await getContent(testRepo, testRef);

-    const [data, status] = await getContent(unknownRepo);
+    const [content, status] = await getContent(unknownRepo);

This will make the variable names consistent across all test cases and improve overall readability.

Also applies to: 23-23

src/server.ts (3)

28-41: LGTM: Improved environment variable handling and repository object creation

The use of the env function from the Hono adapter and the creation of a centralized repository object are excellent improvements. They enhance type safety and align with the goal of making the code runtime-agnostic.

Consider adding error handling for cases where required environment variables are missing. For example:

const { REPOSITORY_OWNER, REPOSITORY_NAME, REPOSITORY_PATH, GITHUB_TOKEN } =
  env<{
    REPOSITORY_OWNER: string;
    REPOSITORY_NAME: string;
    REPOSITORY_PATH: string;
    GITHUB_TOKEN: string;
  }>(ctx);

if (!REPOSITORY_OWNER || !REPOSITORY_NAME || !REPOSITORY_PATH || !GITHUB_TOKEN) {
  throw new Error("Missing required environment variables");
}

const repository: Repository = {
  owner: REPOSITORY_OWNER,
  name: REPOSITORY_NAME,
  path: REPOSITORY_PATH,
};

This addition would make the code more robust by explicitly handling potential configuration errors.


52-52: LGTM: Updated getContent function call with improved parameter passing

The modification to pass the repository object and GITHUB_TOKEN to the getContent function is a good improvement. It's consistent with the new approach and separates the token for better security practices.

For improved clarity, consider destructuring the return value of getContent. This would make it more explicit what values are being passed to ctx.text(). For example:

const [content, contentType] = await getContent(repository, ref, GITHUB_TOKEN);
return ctx.text(content, contentType);

This change would make the code more self-documenting and easier to understand at a glance.


Line range hint 1-60: Overall assessment: Significant improvements in runtime compatibility and code organization

The changes in this file successfully implement the PR's objective of making the code workable on any runtime. Key improvements include:

  1. Use of the Hono environment adapter for runtime-agnostic environment variable access.
  2. Centralization of repository information in a repository object.
  3. Simplified function calls with improved parameter passing.

These changes enhance the code's flexibility, maintainability, and type safety. The modifications align well with modern TypeScript practices and should make the codebase more adaptable to different environments.

Consider documenting these changes in the project's README or documentation, especially highlighting the new environment variable requirements and the improved runtime compatibility. This will help future contributors and users understand the recent architectural improvements.

src/libs/redirect.ts (2)

17-22: LGTM: Updated usage example for the default branch.

The new example correctly demonstrates how to create and use a Repository object, which is consistent with the updated function signature.

Consider adding a comment to explain that "master" is the default branch, as it's not explicitly mentioned in the function call:

 * const url: URL | null = checkRedirect(userAgent, repository); // Uses "master" as default branch

60-63: LGTM: Updated checkRedirect function signature and implementation.

The function signature and implementation have been correctly updated to include and use the repository: Repository parameter. This change aligns with the new design that avoids reliance on environment variables.

Consider updating the default value for ref from "master" to "main", as many repositories now use "main" as the default branch name:

export function checkRedirect(
  userAgent: UserAgent,
  repository: Repository,
  ref: string = "main",
): URL | null {
  const url = getGitHubUrl(repository, ref);
  // ... rest of the function
}

This change would make the function more aligned with current Git practices. However, ensure this doesn't break existing usage before implementing.

src/libs/content.ts (1)

53-59: LGTM with a suggestion: Updated getContent function signature and implementation

The changes to the getContent function are well-implemented and align with the PR objectives. The addition of the repository parameter and the optional token parameter improves flexibility and authentication management.

Consider adding a type annotation for the octokit constant to improve code readability:

- const octokit = new Octokit({ auth: token });
+ const octokit: Octokit = new Octokit({ auth: token });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 87188e5 and 3cf54d7.

📒 Files selected for processing (10)
  • deno.json (1 hunks)
  • src/libs/content.test.ts (1 hunks)
  • src/libs/content.ts (2 hunks)
  • src/libs/env.test.ts (0 hunks)
  • src/libs/env.ts (0 hunks)
  • src/libs/mod.ts (1 hunks)
  • src/libs/redirect.test.ts (1 hunks)
  • src/libs/redirect.ts (2 hunks)
  • src/libs/test_utils.ts (0 hunks)
  • src/server.ts (2 hunks)
💤 Files not reviewed due to no reviewable changes (3)
  • src/libs/env.test.ts
  • src/libs/env.ts
  • src/libs/test_utils.ts
🔇 Additional comments (18)
src/libs/mod.ts (1)

3-3: LGTM! Verify usage of the newly exported type.

The addition of this export statement for the Repository type aligns with the PR's objective of making the project workable on any runtime. It's correctly formatted and allows other modules to use this type directly.

To ensure this change is properly utilized, let's verify its usage across the codebase:

src/libs/redirect.test.ts (2)

4-4: LGTM: Import statement updated correctly

The import statement has been appropriately updated to include testRepo, which is now used in the test cases. This change aligns well with the modifications in the function calls.


Line range hint 1-19: Overall changes improve clarity and align with PR objectives

The modifications to this test file successfully align with the PR's goal of making the code workable on any runtime. By explicitly passing the repository information to the checkRedirect function, the tests become more self-contained and potentially more portable across different runtime environments.

These changes improve the clarity and directness of the tests without apparent loss of functionality or test coverage. They also reflect the removal of the intermediate exportRepo function, as mentioned in the AI-generated summary.

To ensure that these changes haven't inadvertently affected other parts of the codebase, please run the following verification script:

This script will help identify any calls to checkRedirect that might have been missed during the update, ensuring consistency across the codebase.

src/libs/content.test.ts (2)

5-5: LGTM: Import statement updated correctly.

The import statement has been appropriately updated to include testRef, testRepo, and unknownRepo. This change aligns with the modifications in the test cases where these objects are now used directly.


Line range hint 1-28: Overall assessment: Changes improve code clarity and align with PR objectives.

The modifications in this file consistently simplify the test cases by directly passing repository objects to getContent, removing the intermediate exportRepo function. These changes align well with the PR objective of making the code workable on any runtime and improve overall code clarity and maintainability.

Key points:

  1. Import statements have been correctly updated.
  2. All test cases have been consistently modified to use the new getContent function signature.
  3. The changes maintain the original test logic while simplifying the code structure.

The only suggestions made were minor improvements in variable naming for better clarity. Overall, these changes represent a positive step towards making the codebase more flexible and runtime-agnostic.

src/server.ts (2)

3-3: LGTM: Import changes align with PR objectives

The addition of the env import from "@hono/hono/adapter" and the modification of the Repository type import are in line with the PR's goal of making the code workable on any runtime. These changes contribute to better environment variable handling and type organization.

Also applies to: 8-8


43-47: LGTM: Simplified checkRedirect function call

The modification to pass the repository object to the checkRedirect function is a good improvement. It simplifies the function call and makes the code more maintainable by centralizing the repository information.

src/libs/redirect.ts (5)

4-4: LGTM: Import statement for Repository type.

The new import statement for the Repository type is correctly added and necessary for the updated function signature.


27-33: LGTM: Updated usage example for a specific branch.

The example has been correctly updated to demonstrate the creation of a Repository object and the use of a specific branch. It accurately reflects the new function signature while maintaining consistent comment style and formatting.


38-44: LGTM: Updated usage example for a specific tag.

The example has been appropriately updated to showcase the creation of a Repository object and the use of a specific tag. It correctly reflects the new function signature while maintaining consistent comment style and formatting.


49-55: LGTM: Updated usage example for a specific commit.

The example has been correctly updated to illustrate the creation of a Repository object and the use of a specific commit hash. It accurately reflects the new function signature while maintaining consistent comment style and formatting.


Line range hint 1-66: Overall assessment: Well-implemented changes to support runtime independence.

The modifications in this file successfully implement the PR objective of making the code workable on any runtime by avoiding reliance on environment variables. The introduction of the Repository type and its consistent use throughout the file, including in usage examples and the function implementation, demonstrates a thoughtful and thorough approach to this change.

The documentation has been kept up-to-date, which is commendable. The code is now more flexible and can potentially work across different runtime environments as intended.

Great job on maintaining code quality and consistency while implementing this significant change!

src/libs/content.ts (6)

5-5: LGTM: Import of Repository type

The addition of the Repository type import is consistent with the changes in the function signature and improves type safety and code readability.


15-20: LGTM: Updated example for default branch usage

The example has been correctly updated to demonstrate the creation and usage of a Repository object, which is consistent with the new function signature. This change improves the clarity of the documentation.


24-30: LGTM: Updated example for specific branch usage

The example has been correctly updated to demonstrate the creation and usage of a Repository object with a specific branch. This is consistent with the new function signature and improves the clarity of the documentation.


34-40: LGTM: Updated example for specific tag usage

The example has been correctly updated to demonstrate the creation and usage of a Repository object with a specific tag. This is consistent with the new function signature and improves the clarity of the documentation.


44-50: LGTM: Updated example for specific commit usage

The example has been correctly updated to demonstrate the creation and usage of a Repository object with a specific commit. This is consistent with the new function signature and improves the clarity of the documentation.


Line range hint 1-76: Overall assessment: Excellent improvements to flexibility and documentation

The changes in this file successfully achieve the PR's objective of making the code more runtime-agnostic. The introduction of the Repository type and the optional token parameter in the getContent function significantly improves the flexibility and reusability of the code. The thorough updates to the documentation, including multiple usage examples, greatly enhance the usability of this module.

These modifications align well with the goal of making the project workable on any runtime, as they remove implicit dependencies on specific environment variables or configurations. The code is now more adaptable to different usage scenarios and authentication methods.

deno.json Show resolved Hide resolved
deno.json Show resolved Hide resolved
@5ouma 5ouma merged commit 539fae1 into main Sep 27, 2024
13 checks passed
@5ouma 5ouma deleted the refactor-std branch September 27, 2024 06:54
@5ouma 5ouma changed the title Make workable on any runtime Use Hono env adapter instead of the Deno.env() Sep 29, 2024
This was referenced Oct 11, 2024
@coderabbitai coderabbitai bot mentioned this pull request Nov 7, 2024
11 tasks
@coderabbitai coderabbitai bot mentioned this pull request Dec 8, 2024
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎉 New Feature Introduce a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant