Skip to content

Commit

Permalink
Handle pagination (#8)
Browse files Browse the repository at this point in the history
Added the ability to iterate over pages.

Resolves #4

The logic is quite simple. It fetches items from a page. If there are
more than 99 elements, then that page is full and it goes over to the
next page.

Upgraded version to `0.0.2` to have a new release
  • Loading branch information
Bullrich committed Apr 3, 2023
1 parent 26282e0 commit 464eb59
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ outputs:

runs:
using: 'docker'
image: 'docker://ghcr.io/paritytech/stale-issues-finder/action:0.0.1'
image: 'docker://ghcr.io/paritytech/stale-issues-finder/action:0.0.2'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stale-issues-finder",
"version": "0.0.1",
"version": "0.0.2",
"description": "Find what issues have been stale for a given time",
"main": "src/index.ts",
"engines": {
Expand Down
29 changes: 26 additions & 3 deletions src/github/issuesParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,35 @@ import { debug } from "@actions/core";
import { GitHub } from "@actions/github/lib/utils";
import moment from "moment";

const listForRepo = (octokit: InstanceType<typeof GitHub>, repo: Repo, per_page: number = 100, page: number = 1) => {
return octokit.rest.issues.listForRepo({ ...repo, per_page, state: "open", page });
}

//** Handles the problem of pagination */
const getAllIssues = async (octokit: InstanceType<typeof GitHub>, repo: Repo): Promise<IssueData[]> => {
const perPage = 100;
let currentPage = 1;
const { data } = await listForRepo(octokit, repo, perPage, currentPage);
let issues = data;
let fullPage = issues.length > 99;
while (fullPage) {
currentPage++;
debug(`Iterating on page ${currentPage} with ${issues.length} issues`);
const { data } = await listForRepo(octokit, repo, perPage, currentPage)
issues = issues.concat(data);
fullPage = data.length > 99;
}

debug(`Found a total of ${issues.length} issues`);
return issues;
}

export const fetchIssues = async (octokit: InstanceType<typeof GitHub>, repo: Repo): Promise<IssueData[]> => {
const issues = await octokit.rest.issues.listForRepo({ ...repo, per_page: 100, state: "open" });
debug(`Found elements ${issues.data.length}`);
const issues = await getAllIssues(octokit, repo);
debug(`Found elements ${issues.length}`);

// order them from stalest to most recent
const orderedDates = issues.data.sort((a, b) => {
const orderedDates = issues.sort((a, b) => {
return b.updated_at > a.updated_at ? -1 : b.updated_at < a.updated_at ? 1 : 0
});

Expand Down

0 comments on commit 464eb59

Please sign in to comment.