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

Fix double-counting of results and remove unnecessary try-catch in getData function #3765

Closed
coderabbitai bot opened this issue Mar 1, 2025 · 0 comments
Assignees

Comments

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 1, 2025

Issue

In the file, there are two issues that need to be fixed:

  1. There's a logical error where items from the first page are being counted twice
  2. There's an unnecessary try-catch block that just rethrows the error

Suggested fix

- // eslint-disable-next-line no-useless-catch
- try {
    if (!process.env.GITHUB_TOKEN) {
      throw new Error('GITHUB_TOKEN environment variable is required');
    }
    const allItems = [];
    let page = 1;

    const maxPerPage = 50;
    const getReqUrl = (PerPage: number, pageNo: number) =>
      `https://api.github.com/search/code?q=filename:.asyncapi-tool&per_page=${PerPage}&page=${pageNo}`;
    const headers = {
      accept: 'application/vnd.github.text-match+json',
      authorization: `token ${process.env.GITHUB_TOKEN}`
    };
    const result = await axios.get(getReqUrl(maxPerPage, page), {
      headers
    });
    const totalResults = result.data.total_count;

    allItems.push(...result.data.items);

    while (allItems.length < totalResults) {
      page++;

      logger.info(`Fetching page: ${page}`);
      // pause for 1 second to avoid rate limiting
      await pause(1000);
      const nextPageData = await axios.get(getReqUrl(maxPerPage, page), {
        headers
      });

      const { data } = nextPageData;

      allItems.push(...data.items);
    }

-   result.data.items.push(...allItems);
+   // Items from the first page are already in allItems, no need to append them again

    return result.data;
- } catch (err) {
-   throw err;
- }

This issue is related to the TypeScript migration in PR #3761.

Originally identified in: #3761 (comment)

Adding this as a task for issue #3764 as requested by @akshatnema.

@akshatnema akshatnema closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 2025
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

No branches or pull requests

1 participant