Skip to content

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

@coderabbitai

Description

@coderabbitai

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions