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

refactor: separate request responsibility #12

Merged
merged 1 commit into from
Dec 11, 2023

Conversation

gitcommitshow
Copy link
Owner

@gitcommitshow gitcommitshow commented Dec 11, 2023

A step before developing rate limit abilities - get all requests to be handled by single file network.js

Summary by CodeRabbit

  • New Features
    • Introduced a new function to retrieve all repositories.
  • Refactor
    • Improved the network request functionality with enhanced error handling and asynchronous support.
  • Tests
    • Adjusted test cases to align with the updated contributor count expectations.
    • Temporarily skipped two test cases pending further review.
  • Chores
    • Cleaned up code by removing unused imports and ensuring proper export declarations.
  • Documentation
    • Updated function export list to reflect the latest changes in the codebase.

Copy link
Contributor

coderabbitai bot commented Dec 11, 2023

Walkthrough

The codebase has undergone a refactoring to enhance modularity and error handling. A new function for repository retrieval was added, and existing functions were updated to use a centralized request handler, improving maintainability. Tests were adjusted to reflect these updates, including skipping some and updating expected values.

Changes

File(s) Summary
contributors.js Added semicolon; included getAllRepos export; replaced https with makeRequest from network.js.
network.js Updated makeRequest to return a Promise and handle errors; added https module import.
test/contributors.test.js Skipped two tests; updated expected contributor counts.
test/fixtures/contributors.fixture.js Renamed constants; added REPO_CONTRIBUTOR_COUNT_MIN.

🐇✨
In the burrow of the code, amidst the bits and bytes,
A rabbit hopped through functions, refactoring into the night.
With tests all snug and errors caught, the code did gleam,
"Hoppy" coding to all, and to all a good stream! 🌟🌙

(\__/) ||
(•ㅅ•) ||
/   づ

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 X ?


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

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.

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c6d25c6 and a9459eb.
Files selected for processing (4)
  • contributors.js (3 hunks)
  • network.js (1 hunks)
  • test/contributors.test.js (2 hunks)
  • test/fixtures/contributors.fixture.js (1 hunks)
Files skipped from review due to trivial changes (2)
  • test/contributors.test.js
  • test/fixtures/contributors.fixture.js
Additional comments: 6
contributors.js (5)
  • 6-6: Semicolon added for consistency in export statements.

  • 7-7: Addition of getAllRepos to the exports supports the centralization of network requests.

  • 13-13: Import of makeRequest from network.js replaces direct https usage, aligning with the centralization goal.

  • 45-61: Refactored getAllRepos to use makeRequest for network calls, which is a core part of the centralization effort.

  • 72-88: Refactored getRepoContributors to use makeRequest for network calls, which is consistent with the centralization goal.

network.js (1)
  • 1-51: The makeRequest function is well-implemented with proper error handling and usage of Promises for asynchronous operations. The documentation and example usage provided are clear and helpful. Ensure that the rest of the codebase is updated to use this new function for all network requests.

Comment on lines +47 to +59
console.log("Repo list request finished");
console.log('HTTP status: ', res.statusCode);
// console.log(data)
let dataJsonArray = JSON.parse(data);
if (dataJsonArray.length == 100) {
//It might have more data on the next page
pageNo++;
try {
let dataFromNextPage = await getAllRepos(owner, { pageNo: pageNo } );
dataJsonArray.push(...dataFromNextPage);
} catch (err) {
console.log("No more pagination needed")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider improving error handling beyond console logs to ensure robustness in production environments.

Also applies to: 75-86

Comment on lines +51 to +60
if (dataJsonArray.length == 100) {
//It might have more data on the next page
pageNo++;
try {
let dataFromNextPage = await getAllRepos(owner, { pageNo: pageNo } );
dataJsonArray.push(...dataFromNextPage);
} catch (err) {
console.log("No more pagination needed")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The pagination logic assumes that a full page of results indicates more data. It may be helpful to add a comment explaining this assumption for future maintainability.

Also applies to: 78-87

Comment on lines 42 to 43
if(options && options.GITHUB_PERSONAL_TOKEN){
GITHUB_REQUEST_OPTIONS.headers["Authorization"] = "token "+options.GITHUB_PERSONAL_TOKEN;
Copy link
Contributor

Choose a reason for hiding this comment

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

Direct modification of GITHUB_REQUEST_OPTIONS could lead to side effects. Consider creating a new options object for each request.

Comment on lines +55 to +58
let dataFromNextPage = await getAllRepos(owner, { pageNo: pageNo } );
dataJsonArray.push(...dataFromNextPage);
} catch (err) {
console.log("No more pagination needed")
Copy link
Contributor

Choose a reason for hiding this comment

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

Recursion for pagination could lead to stack overflow with many pages. An iterative approach may be more robust.

Also applies to: 82-85

@gitcommitshow gitcommitshow merged commit 1d16005 into main Dec 11, 2023
@gitcommitshow gitcommitshow added the enhancement New feature or request label Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant