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

Updating the setup script to handle sample data import for Docker #2691

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

Suyash878
Copy link

@Suyash878 Suyash878 commented Nov 18, 2024

What kind of change does this PR introduce?

Bug Fix

Issue Number:

Fixes #2270

Did you add tests for your changes?

No

Snapshots/Videos:

image

If relevant, did you update the documentation?

Not sure

Summary

The setup script will now prompt the user when it starts the process for sample data import provided the user is using Docker.

Does this PR introduce a breaking change?

No

Other information

This PR was supposed to be made against the develop-postgres branch but since it is currently under work, and unavailability of the setup script there I am currently raising the PR against the develop branch.
My changes may get introduced to the develop-postgres branch as a seperate issue later on.

Have you read the contributing guide?

Yes

Summary by CodeRabbit

  • New Features

    • Introduced a new shell script (entrypoint.sh) for importing sample data.
    • Enhanced Docker setup process to automatically create and execute the entrypoint.sh script for importing sample data, with improved error handling.
  • Bug Fixes

    • Ensured existing MongoDB and Redis configurations remain unaffected during the setup process.

Copy link

coderabbitai bot commented Nov 18, 2024

Walkthrough

A new shell script file entrypoint.sh has been added to facilitate the importation of sample data into the application. This script is created and executed within the setup.ts file when the setup process detects a Docker environment. The integration of this functionality enhances the setup process for Docker installations, ensuring that sample data can be imported automatically.

Changes

File Change Summary
entrypoint.sh New file created to execute npm run import:sample-data.
setup.ts Enhanced setup process for Docker; added logic to create and execute entrypoint.sh for data import.

Assessment against linked issues

Objective Addressed Explanation
Prompt user for data import if Docker is running and store previous answers (#2270) The script does not prompt for data import.
Ensure sample data is imported correctly in Docker setup (#2270)
Provide error handling for non-Docker data import (#2270) No error handling for non-Docker scenarios.

Possibly related PRs

  • Fixed setup script #2512: The changes in setup.ts related to the creation and execution of the entrypoint.sh script for importing sample data directly connect to the main PR, which introduces this script.

Suggested reviewers

  • DMills27

Poem

In the land of code where rabbits play,
A script was born to save the day.
With entrypoint.sh, data flows,
In Docker's arms, our project grows.
Hop along, dear coder, with glee,
Sample data's here, as easy as can be! 🐇✨


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.

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

Our Pull Request Approval Process

We have these basic policies to make the approval process smoother for our volunteer team.

Testing Your Code

Please make sure your code passes all tests. Our test code coverage system will fail if these conditions occur:

  1. The overall code coverage drops below the target threshold of the repository
  2. Any file in the pull request has code coverage levels below the repository threshold
  3. Merge conflicts

The process helps maintain the overall reliability of the code base and is a prerequisite for getting your PR approved. Assigned reviewers regularly review the PR queue and tend to focus on PRs that are passing.

Reviewers

Do not assign reviewers. Our Queue Monitors will review your PR and assign them.
When your PR has been assigned reviewers contact them to get your code reviewed and approved via:

  1. comments in this PR or
  2. our slack channel

Reviewing Your Code

Your reviewer(s) will have the following roles:

  1. arbitrators of future discussions with other contributors about the validity of your changes
  2. point of contact for evaluating the validity of your work
  3. person who verifies matching issues by others that should be closed.
  4. person who gives general guidance in fixing your tests

CONTRIBUTING.md

Read our CONTRIBUTING.md file. Most importantly:

  1. PRs with issues not assigned to you will be closed by the reviewer
  2. Fix the first comment in the PR so that each issue listed automatically closes

Other

  1. 🎯 Please be considerate of our volunteers' time. Contacting the person who assigned the reviewers is not advised unless they ask for your input. Do not @ the person who did the assignment otherwise.
  2. Read the CONTRIBUTING.md file make

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: 1

🧹 Outside diff range and nitpick comments (1)
setup.ts (1)

1187-1213: Consider adding validation for npm command availability.

The script assumes npm run import:sample-data is available. Add validation to ensure the command exists.

Consider:

  1. Validating package.json for the script definition
  2. Checking if npm is available in PATH
  3. Adding fallback mechanisms if the command fails
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a6e7ac9 and 30adcd8.

📒 Files selected for processing (2)
  • entrypoint.sh (1 hunks)
  • setup.ts (1 hunks)
🧰 Additional context used
🪛 Shellcheck
entrypoint.sh

[error] 2-2: Remove leading spaces before the shebang.

(SC1114)


[error] 2-2: The shebang must be on the first line. Delete blanks and move comments.

(SC1128)

🔇 Additional comments (1)
setup.ts (1)

1187-1213: Verify Docker environment detection.

The Docker installation check relies solely on user input. Consider adding automated detection.

setup.ts Outdated Show resolved Hide resolved
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
setup.ts Fixed Show fixed Hide fixed
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 (1)
setup.ts (1)

1187-1223: Consider refactoring Docker sample data import into a separate function.

The implementation would be more maintainable if moved to a dedicated async function.

+async function importDockerSampleData(): Promise<void> {
+  console.log("Starting the sample data import for docker now...");
+
+  const entryPointScript = `#!/bin/bash
+npm run import:sample-data
+`;
+
+  const scriptPath = path.join(os.tmpdir(), `entrypoint-${Date.now()}.sh`);
+  
+  try {
+    // Create script with proper permissions
+    fs.writeFileSync(scriptPath, entryPointScript, { mode: 0o755 });
+    
+    await new Promise<void>((resolve, reject) => {
+      exec(scriptPath, { timeout: 60000 }, (error, stdout, stderr) => {
+        // Clean up script file
+        try {
+          fs.unlinkSync(scriptPath);
+        } catch (cleanupError) {
+          console.warn("Failed to cleanup script file:", cleanupError);
+        }
+        
+        if (error) {
+          console.error("Error importing sample data:");
+          console.error(`Exit code: ${error.code}`);
+          console.error(`Error message: ${error.message}`);
+          reject(error);
+          return;
+        }
+        
+        if (stderr) {
+          console.warn("Sample data import warnings:");
+          console.warn(stderr.trim());
+        }
+        
+        if (stdout) {
+          console.log("Sample data import output:");
+          console.log(stdout.trim());
+        }
+        console.log("Sample data import complete.");
+        resolve();
+      });
+    });
+  } catch (err) {
+    console.error("Failed to setup sample data import:", err);
+    if (fs.existsSync(scriptPath)) {
+      try {
+        fs.unlinkSync(scriptPath);
+      } catch (cleanupError) {
+        console.warn("Failed to cleanup script file:", cleanupError);
+      }
+    }
+    throw err;
+  }
+}

 if (isDockerInstallation) {
-  console.log("Starting the sample data import for docker now...");
-  ...
+  await importDockerSampleData();
 }
🧰 Tools
🪛 GitHub Check: CodeQL

[warning] 1201-1201: Shell command built from environment values
This shell command depends on an uncontrolled absolute path.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 30adcd8 and eab9d94.

📒 Files selected for processing (1)
  • setup.ts (1 hunks)
🧰 Additional context used
🪛 GitHub Check: CodeQL
setup.ts

[warning] 1201-1201: Shell command built from environment values
This shell command depends on an uncontrolled absolute path.

setup.ts Outdated Show resolved Hide resolved
setup.ts Outdated Show resolved Hide resolved
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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: 1

🧹 Outside diff range and nitpick comments (1)
setup.ts (1)

1184-1234: Implementation looks good with minor formatting suggestion.

The Docker sample data import implementation follows security best practices:

  • Uses secure temporary directory
  • Includes comprehensive error handling
  • Ensures proper cleanup of temporary files
  • Sets appropriate file permissions
  • Has execution timeout

Consider improving the script content formatting for better readability.

 const entryPointScript = `#!/bin/bash
-npm run import:sample-data
+
+# Import sample data for Docker environment
+npm run import:sample-data
+
`;
🧰 Tools
🪛 Biome

[error] 1197-1197: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1199-1199: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1199-1201: Illegal use of an import declaration not at the top level

move this declaration to the top level

(parse)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between eab9d94 and 41c9aac.

📒 Files selected for processing (1)
  • setup.ts (1 hunks)
🧰 Additional context used
🪛 Biome
setup.ts

[error] 1197-1197: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1199-1199: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1199-1201: Illegal use of an import declaration not at the top level

move this declaration to the top level

(parse)

setup.ts Outdated Show resolved Hide resolved
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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: 1

🧹 Outside diff range and nitpick comments (1)
setup.ts (1)

1184-1186: Improve code organization and remove redundant comment.

The success message should be moved after the Docker check to avoid confusion, and the redundant comment can be removed.

 console.log("\nCongratulations! Talawa API has been successfully setup! 🥂🎉");

-/* Performing the sample data import for docker */

 if (isDockerInstallation) {
+  console.log("\nCongratulations! Talawa API has been successfully setup! 🥂🎉");
   console.log("Starting the sample data import for docker now...");
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 41c9aac and 6144664.

📒 Files selected for processing (1)
  • setup.ts (1 hunks)
🧰 Additional context used
🪛 Biome
setup.ts

[error] 1215-1215: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1215-1215: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1215-1215: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1216-1216: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1216-1216: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1216-1216: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1215-1215: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 1216-1216: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 1215-1216: This block statement doesn't serve any purpose and can be safely removed.

Standalone block statements without any block-level declarations are redundant in JavaScript and can be removed to simplify the code.
Safe fix: Remove redundant block.

(lint/complexity/noUselessLoneBlockStatements)


[error] 1216-1217: This block statement doesn't serve any purpose and can be safely removed.

Standalone block statements without any block-level declarations are redundant in JavaScript and can be removed to simplify the code.
Safe fix: Remove redundant block.

(lint/complexity/noUselessLoneBlockStatements)

🔇 Additional comments (1)
setup.ts (1)

1187-1237: LGTM! Implementation follows security best practices.

The Docker sample data import implementation demonstrates several good practices:

  • Uses secure temporary directory with os.tmpdir()
  • Includes comprehensive error handling
  • Ensures cleanup of temporary files
  • Sets appropriate file permissions (0o755)
  • Has execution timeout (60s)
🧰 Tools
🪛 Biome

[error] 1215-1215: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1215-1215: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1215-1215: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1216-1216: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1216-1216: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1216-1216: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 1215-1215: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 1216-1216: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 1215-1216: This block statement doesn't serve any purpose and can be safely removed.

Standalone block statements without any block-level declarations are redundant in JavaScript and can be removed to simplify the code.
Safe fix: Remove redundant block.

(lint/complexity/noUselessLoneBlockStatements)


[error] 1216-1217: This block statement doesn't serve any purpose and can be safely removed.

Standalone block statements without any block-level declarations are redundant in JavaScript and can be removed to simplify the code.
Safe fix: Remove redundant block.

(lint/complexity/noUselessLoneBlockStatements)

setup.ts Outdated Show resolved Hide resolved
@varshith257
Copy link
Member

varshith257 commented Nov 18, 2024

@Suyash878 Isn't sample data is imported into docker mango container?

@Suyash878
Copy link
Author

Yes, sample data would be imported to the docker mongo container, totally depends on the mongodb url in the environment variables.

@palisadoes
Copy link
Contributor

Please fix the failing tests

@Suyash878
Copy link
Author

Suyash878 commented Nov 21, 2024

The base branch check is failing since it is not targeted to the postgres branch.
I cannot target the PR to postgres since postres does not have a setup script yet, And the other "sensitive file have been changed" is failing since I made a change to the setup script.
The testing application check is something I think others are also facing and is something that needs fixing, Although I could be wrong I posted on slack as well.

Copy link

codecov bot commented Nov 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.74%. Comparing base (37cfbde) to head (49b4d2f).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #2691   +/-   ##
========================================
  Coverage    97.74%   97.74%           
========================================
  Files          358      358           
  Lines        18114    18114           
  Branches      2599     2599           
========================================
  Hits         17706    17706           
  Misses         404      404           
  Partials         4        4           

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


🚨 Try these New Features:

@palisadoes palisadoes added the ignore-sensitive-files-pr The contributor has a legitimate reason for editiing protected files label Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ignore-sensitive-files-pr The contributor has a legitimate reason for editiing protected files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants