From 8f9b4ccf8d9150b73b33afec01d7466775b99d74 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Thu, 14 Oct 2021 00:20:38 +0200 Subject: [PATCH] fix: fixes while exit condition --- src/helpers.ts | 16 +++++++++------- test/helpers.spec.ts | 35 ++++------------------------------- 2 files changed, 13 insertions(+), 38 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 839fd15..0f7affe 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -33,11 +33,15 @@ export async function renderer( } export function getNextPage(links: PaginationLink[]): string | null { - const link = links.find((l) => l.rel === 'next'); - if (!link) return null; - const match = link.uri.match(/page=([0-9]*)/); - if (!match) return null; - return match[1]; + const next = links.find((l) => l.rel === 'next'); + const last = links.find((l) => l.rel === 'last'); + if (!next || !last) return null; + const matchNext = next.uri.match(/page=([0-9]*)/); + const matchLast = next.uri.match(/page=([0-9]*)/); + + if (!matchNext || !matchLast) return null; + if (matchNext[1] === matchLast[1]) return null; + return matchNext[1]; } async function* paginateStars(url: string): AsyncGenerator { @@ -49,10 +53,8 @@ async function* paginateStars(url: string): AsyncGenerator { page: nextPage, }, }); - console.log('body, typeof body :>> ', body, typeof body); yield (body as unknown) as Stars; nextPage = getNextPage(link.parse(headers.link).refs); - console.log('sleeping 1second to avoid rate-limit'); await wait(1000); // avoid limits } catch (e) { console.error(e); diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index d1b35de..a91bbaf 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -7,7 +7,7 @@ sinon.replace(core, 'getInput', sinon.fake()); import GithubApi from '../src/api'; const GithubApiFake = sinon.fake((rul) => ({ - body: 'data', + body: [], headers: { link: '; rel="next", ; rel="last"', @@ -32,11 +32,9 @@ const writeFile = sinon.fake(); sinon.replace(fsp, 'writeFile', writeFile); import { - isLastPage, wait, renderer, apiGetStar, - paginate, generateMd, pushNewFiles, } from '../src/helpers'; @@ -46,40 +44,15 @@ test('wait should wait', async (t) => { t.pass(); }); -test('isLastPage', (t) => { - t.true( - isLastPage({ - next: 'last', - last: 'last', - }) - ); - t.false( - isLastPage({ - next: 'last', - last: undefined, - }) - ); -}); - test('renderer should render', async (t) => { const output = await renderer({ variable: 123 }, 'Test: <%= variable %>'); t.is(output, 'Test: 123'); }); test('apiGetStar', async (t) => { - let { data, links } = await apiGetStar('url'); + let stars = await apiGetStar('url'); t.true(GithubApiFake.called); - t.is(data, 'data'); - t.deepEqual(links, { - last: 'https://api.github.com/user/5617452/starred?page=2', - next: 'https://api.github.com/user/5617452/starred?page=2', - }); -}); - -test('paginate', async (t) => { - await paginate(); - const res = await paginate(); - t.is(res, null); + t.true(Array.isArray(stars)); }); test('generateMd should create TOC', async (t) => { @@ -97,7 +70,7 @@ test('generateMd should create TOC', async (t) => { }); test('should push', async (t) => { - await pushNewFiles([{filename: "README.md", data: '# title'}]); + await pushNewFiles([{ filename: 'README.md', data: '# title' }]); t.true(writeFile.calledWith('README.md', '# title')); t.true(pull.called); t.true(add.calledWith('README.md'));