Skip to content

Commit 8eb7f59

Browse files
fix: Ensure all label pages are traversed and remove per_page from API and tests (#91)
1 parent a4fafed commit 8eb7f59

File tree

3 files changed

+188
-319
lines changed

3 files changed

+188
-319
lines changed

index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ async function action() {
8787
core.debug(`fetch the labels for ${issue_number} using the API`);
8888
// We use the API rather than read event.json in case earlier steps
8989
// added a label
90-
const labels = (
91-
await octokit.rest.issues.listLabelsOnIssue({
90+
// Use octokit.paginate to fetch all pages of labels
91+
const labels = await octokit.paginate(
92+
octokit.rest.issues.listLabelsOnIssue,
93+
{
9294
...github.context.repo,
9395
issue_number,
94-
})
95-
).data;
96+
},
97+
);
9698

9799
const appliedLabels = labels.map((label) => label.name);
98100

index.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,40 @@ describe("Required Labels", () => {
125125

126126
await action();
127127
});
128+
129+
it("fetches all label pages from the API", async () => {
130+
restoreTest = mockPr({
131+
INPUT_LABELS: "enhancement",
132+
INPUT_MODE: "minimum",
133+
INPUT_COUNT: "1",
134+
GITHUB_TOKEN: "mock-token-here-abc",
135+
});
136+
137+
// First page of labels
138+
nock("https://api.github.com")
139+
.get("/repos/mheap/missing-repo/issues/28/labels")
140+
.reply(200, [{ name: "triage" }], {
141+
Link: '<https://api.github.com/repos/mheap/missing-repo/issues/28/labels?page=2>; rel="next"',
142+
});
143+
144+
// Second page of labels
145+
nock("https://api.github.com")
146+
.get("/repos/mheap/missing-repo/issues/28/labels")
147+
.query({ page: 2 })
148+
.reply(200, [{ name: "bug" }], {
149+
Link: '<https://api.github.com/repos/mheap/missing-repo/issues/28/labels?page=3>; rel="next"',
150+
});
151+
152+
// Third page of labels
153+
nock("https://api.github.com")
154+
.get("/repos/mheap/missing-repo/issues/28/labels")
155+
.query({ page: 3 })
156+
.reply(200, [{ name: "enhancement" }]);
157+
158+
await action();
159+
expect(core.setOutput).toBeCalledWith("labels", "enhancement");
160+
expect(core.setOutput).toBeCalledWith("status", "success");
161+
});
128162
});
129163

130164
describe("success", () => {

0 commit comments

Comments
 (0)