Skip to content

Commit 69869b6

Browse files
committed
refactor: use Octokit paginate in getTranslationIssues and update unit tests
1 parent bae43fc commit 69869b6

2 files changed

Lines changed: 66 additions & 100 deletions

File tree

src/github/getTranslationIssues.ts

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,56 +24,30 @@ export const getTranslationIssues = async (
2424

2525
const issues: Issue[] = [];
2626

27-
let page = 1;
28-
29-
while (true) {
30-
log('I', `getTranslationIssues :: Fetching page ${page}`);
31-
32-
const { data } = await github.api.issues.listForRepo({
33-
...github.ownerAndRepo,
34-
state,
35-
per_page: 100,
36-
page,
37-
});
38-
39-
log(
40-
'I',
41-
`getTranslationIssues :: Page ${page} returned ${data.length} issues`,
42-
);
43-
44-
if (data.length === 0) {
45-
break;
46-
}
47-
48-
const openedIssuesWithoutHash = data.map<Omit<Issue, 'hash'>>(item => ({
49-
number: item.number,
50-
body: item.body ?? '',
51-
isoDate: item.created_at,
52-
labels: item.labels
53-
.map(convGithubIssueLabelToString)
54-
.filter(isNotEmpty)
55-
.sort(),
56-
}));
57-
58-
const openedYukiNoIssues = openedIssuesWithoutHash
59-
.filter(issue => isYukiNoIssue(github.configuredLabels, issue))
60-
.map(issue => ({ ...issue, hash: extractHashFromIssue(issue) }))
61-
.filter(hasHash);
62-
63-
log(
64-
'I',
65-
`getTranslationIssues :: Found ${openedYukiNoIssues.length} Yuki-no issues on page ${page}`,
66-
);
67-
68-
issues.push(...openedYukiNoIssues);
69-
70-
page++;
71-
72-
if (data.length < 100) {
73-
break;
74-
}
75-
}
76-
27+
const all = await github.api.paginate(github.api.issues.listForRepo, {
28+
...github.ownerAndRepo,
29+
state,
30+
per_page: 100,
31+
});
32+
33+
log('I', `getTranslationIssues :: Total issues fetched: ${all.length}`);
34+
35+
const openedIssuesWithoutHash = all.map<Omit<Issue, 'hash'>>(item => ({
36+
number: item.number,
37+
body: item.body ?? '',
38+
isoDate: item.created_at,
39+
labels: item.labels
40+
.map(convGithubIssueLabelToString)
41+
.filter(isNotEmpty)
42+
.sort(),
43+
}));
44+
45+
const openedYukiNoIssues = openedIssuesWithoutHash
46+
.filter(issue => isYukiNoIssue(github.configuredLabels, issue))
47+
.map(issue => ({ ...issue, hash: extractHashFromIssue(issue) }))
48+
.filter(hasHash);
49+
50+
issues.push(...openedYukiNoIssues);
7751
issues.sort((a, b) => (a.isoDate > b.isoDate ? 1 : -1));
7852

7953
log(

src/tests/github/getOpenedIssues.test.ts

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { beforeEach, expect, it, vi } from 'vitest';
55

66
vi.mock('../../github/core', () => ({
77
GitHub: vi.fn().mockImplementation(() => ({
8-
api: { issues: { listForRepo: vi.fn() } },
8+
api: { paginate: vi.fn(), issues: { listForRepo: vi.fn() } },
99
ownerAndRepo: { owner: 'test-owner', repo: 'test-repo' },
1010
configuredLabels: ['label1', 'label2'],
1111
})),
@@ -18,32 +18,28 @@ beforeEach(() => {
1818
});
1919

2020
it('Should return an empty array when there are no issues', async () => {
21-
(mockGitHub.api.issues.listForRepo as any).mockResolvedValue({
22-
data: [],
23-
});
21+
(mockGitHub.api.paginate as any).mockResolvedValue([]);
2422

2523
const result = await GetOpenedIssuesModule.getOpenedIssues(mockGitHub);
2624

2725
expect(result).toEqual([]);
2826
});
2927

3028
it('Issues with only different labels from the configuration or without body should be filtered out', async () => {
31-
(mockGitHub.api.issues.listForRepo as any).mockResolvedValue({
32-
data: [
33-
{
34-
number: 1,
35-
body: 'body',
36-
labels: [{ name: 'other-label' }],
37-
created_at: '2023-01-01T12:00:00Z',
38-
},
39-
{
40-
number: 2,
41-
body: undefined,
42-
labels: ['label1', 'label2'],
43-
created_at: '2023-01-01T12:00:00Z',
44-
},
45-
],
46-
});
29+
(mockGitHub.api.paginate as any).mockResolvedValue([
30+
{
31+
number: 1,
32+
body: 'body',
33+
labels: [{ name: 'other-label' }],
34+
created_at: '2023-01-01T12:00:00Z',
35+
},
36+
{
37+
number: 2,
38+
body: undefined,
39+
labels: ['label1', 'label2'],
40+
created_at: '2023-01-01T12:00:00Z',
41+
},
42+
]);
4743

4844
const result = await GetOpenedIssuesModule.getOpenedIssues(mockGitHub);
4945

@@ -53,21 +49,19 @@ it('Issues with only different labels from the configuration or without body sho
5349
it('Should return only issues that have all configured labels', async () => {
5450
const EXPECTED_HASH = 'abcd123';
5551

56-
(mockGitHub.api.issues.listForRepo as any).mockResolvedValue({
57-
data: [
58-
{
59-
number: 1,
60-
body: `https://github.com/org/name/commit/${EXPECTED_HASH}`,
61-
created_at: '2023-01-01T12:00:00Z',
62-
labels: [
63-
'label1',
64-
{ name: 'label2' },
65-
{ name: undefined },
66-
'extra-label',
67-
],
68-
},
52+
(mockGitHub.api.paginate as any).mockResolvedValue([
53+
{
54+
number: 1,
55+
body: `https://github.com/org/name/commit/${EXPECTED_HASH}`,
56+
created_at: '2023-01-01T12:00:00Z',
57+
labels: [
58+
'label1',
59+
{ name: 'label2' },
60+
{ name: undefined },
61+
'extra-label',
6962
],
70-
});
63+
},
64+
]);
7165

7266
const result = await GetOpenedIssuesModule.getOpenedIssues(mockGitHub);
7367

@@ -83,22 +77,20 @@ it('Should return only issues that have all configured labels', async () => {
8377
it('Issues without a hash should be filtered out', async () => {
8478
const EXPECTED_HASH = 'abcd123';
8579

86-
(mockGitHub.api.issues.listForRepo as any).mockResolvedValue({
87-
data: [
88-
{
89-
number: 1,
90-
body: `https://github.com/org/repo/commit/${EXPECTED_HASH}`,
91-
created_at: '2023-01-01T12:00:00Z',
92-
labels: [{ name: 'label1' }, { name: 'label2' }],
93-
},
94-
{
95-
number: 2,
96-
body: 'Issue body without hash',
97-
created_at: '2023-01-01T12:00:00Z',
98-
labels: [{ name: 'label1' }, { name: 'label2' }],
99-
},
100-
],
101-
});
80+
(mockGitHub.api.paginate as any).mockResolvedValue([
81+
{
82+
number: 1,
83+
body: `https://github.com/org/repo/commit/${EXPECTED_HASH}`,
84+
created_at: '2023-01-01T12:00:00Z',
85+
labels: [{ name: 'label1' }, { name: 'label2' }],
86+
},
87+
{
88+
number: 2,
89+
body: 'Issue body without hash',
90+
created_at: '2023-01-01T12:00:00Z',
91+
labels: [{ name: 'label1' }, { name: 'label2' }],
92+
},
93+
]);
10294

10395
const result = await GetOpenedIssuesModule.getOpenedIssues(mockGitHub);
10496

0 commit comments

Comments
 (0)